object-oriented application frameworks cs 3331 sections 8.1 & 8.3 of [jia 03]

48
Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

Upload: cecilia-parker

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

3 What’s a Framework? A set of cooperating (abstract) classes and interface that:  Represent reusable designs of software systems in a particular application domain,  Provide semi-complete applications that can be specialized to produce custom applications, and  Defines conventions for extending the abstract classes, implementing the interfaces, and allowing their instances to interact with one another

TRANSCRIPT

Page 1: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

Object-Oriented Application Frameworks

CS 3331

Sections 8.1 & 8.3 of [Jia 03]

Page 2: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

2

Outline

Frameworks MVC Model GUI components (widgets) Layout managers Handling events

Page 3: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

3

What’s a Framework? A set of cooperating (abstract) classes and

interface that: Represent reusable designs of software systems in a

particular application domain, Provide semi-complete applications that can be

specialized to produce custom applications, and Defines conventions for extending the abstract

classes, implementing the interfaces, and allowing their instances to interact with one another

Page 4: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

4

Examples Small frameworks

Applets, AnimationApplet, DBAnimationApplet, etc. GUI frameworks

AWT and Swing MFC

Distributed programming frameworks RMI J2EE CORBA

Page 5: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

5

Why Frameworks? Main goal is:

To support reuse of design and implementations in particular application domains

Benefits Quick and easy application development Reliable application (i.e., high confidence) Standardized applications

Page 6: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

6

Characteristics

Extensibility Inversion of control Design patterns as building blocks

Page 7: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

7

Questions

How frameworks differ from conventional OO libraries?

How frameworks differ from design patterns?

Page 8: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

8

Frameworks To Be Discussed

GUI frameworks (AWT and Swing) Collection classes Input and output classes

Page 9: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

9

MVC Model A way of cleanly breaking an application into

three parts Model for maintaining data, View for displaying all or a portion of the data, and Controller for handling events that affect the model or

views.

Model

ControllerView

Page 10: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

10

Separating M from VC

Controller

changes model or view

get datafrom model

update viewwhen data changes

View

Model

Page 11: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

11

Due to Separation …

Multiple views and controllers for models

gui.ppt

change notificationrequest & modificationviews

model

Page 12: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

12

Due to Separation … (Cont.)

New views and controllers can be added for the model.

Changes in views are localized; such changes have minimal or no impact on the model.

Page 13: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

13

Outline

MVC Model GUI components (widgets) Layout managers Handling events

Page 14: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

14

Sample GUI Program Counter applet

Increment10Value:

Page 15: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

15

Java GUI Frameworks GUI frameworks

Part of Java Foundation Class (JFC) Abstract Window Toolkit (AWT) and Swing

Framework classes GUI components (or widgets) Layout managers Events and event listeners Graphics and imaging classes

Page 16: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

16

AWT vs. Swing

AWT Heavyweight components Associated with native components called peers Same behaviour, but platform-dependent look Package java.awt

Swing Lightweight components, i.e., no peer components Same look and feel across platforms Support pluggable look and feel Package javax.swing

Page 17: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

17

AWT Components

primitive container

Page 18: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

18

Swing ComponentsEvery widget is a container!

Page 19: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

19

Composite Design Pattern To allow clients to treat both single components and collections of

components identically To define recursive data structures such as trees

Clientuses

Leaf

operation()

Component

operation()

Composite

operation()add(Component)remove(Component)

*

Page 20: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

20

Exercise Design a class hierarchy to represent parse

trees for:Expression ::= Literal | Expression “+” Expression

Page 21: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

21

Outline

MVC Model GUI components (widgets) Layout managers Handling events

Page 22: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

22

Q: How to Layout Widgets? Three approaches

Manually specify absolute positions Manually specify relative positions Automate it

What’re pros and cons?

Page 23: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

23

Layout Managers Associated with containers Automate the layout of elements

When elements are added to the containerWhen the window is resized

automatically adjust the positions and sizes of the elements.

Page 24: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

24

Hierarchy of Layout Managers

Q: Can you identify the design pattern used here?

Page 25: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

25

Using Layout Managers

Method Description

setLayout(lm) Set lm as the layout manageradd(comp) Add a componentadd(comp, cst) Add a component with constraint

public class CounterApplet extends Applet { public CounterApplet () { setLayout(new FlowLayout()); add(new JButton(“Increment")); add(new JButton(“Decrement”)); }}

Page 26: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

26

Flow Layoutwidth=400 height=50

width=100 height=120

Page 27: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

27

Flow Layout (Cont.)public class Flow extends Applet { public Flow () { setLayout(new FlowLayout()); add(new JButton("Java")); add(new JButton("C++")); add(new JButton("Perl")); add(new JButton("Ada")); add(new JButton("Smalltalk")); add(new JButton("Eiffel")); }}

Page 28: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

28

Grid Layout

3x2 grid

1x0 grid

0x1 grid

Page 29: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

29

Grid Layout (Cont.)public class Grid extends Applet { public void init () { int row = 0; int col = 0; String att = getParameter("row"); if (att != null) { row = Integer.parseInt(att); } att = getParameter("col"); if (att != null) { col = Integer.parseInt(att); } if (row == 0 && col == 0) { row = 3; col = 2; }

setLayout(new GridLayout(row, col)); add(new JButton("Java")); add(new JButton("C++")); add(new JButton("Perl")); add(new JButton("Ada")); add(new JButton("Smalltalk")); add(new JButton("Eiffel")); }}

Page 30: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

30

Question Why compose the GUI in the init() method

not in the constructor?

Page 31: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

31

Border Layout

Page 32: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

32

Border Layout (Cont.)

public class Border extends Applet { public Border () { setLayout(new BorderLayout()); add(new JButton("North"), BorderLayout.NORTH); add(new JButton("South"), BorderLayout.SOUTH); add(new JButton("East"), BorderLayout.EAST); add(new JButton("West"), BorderLayout.WEST); add(new JButton("Center"), BorderLayout.CENTER); }}

Page 33: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

33

Exercise Write an applet of the following layout.

public class ClassRoom extends Applet { public ClassRoom () { setLayout(new BorderLayout()); // WRITE YOUR CODE HERE…

}}

Page 34: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

34

Outline

MVC Model GUI components (widgets) Layout managers Handling events

Page 35: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

35

Event Handling Mechanism to write control code Composed of:

EventEvent sourceEvent listener (or handler)

Page 36: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

36

Event Handling (Cont.) Event

A way for GUI components to communicate with the rest of application

Implemented as event classes (e.g., ActionEvent)

Event sourceComponents generating eventsExamples: buttons, check boxes, combo

boxes, etc.

Page 37: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

37

Event Handling (Cont.) Event listener (or handler)

Objects that receives and processes eventsMust implement an appropriate listener

interfaceMust inform the source its interest in handling

a certain type of events (by registering)May listen to several sources and different

types of events

Page 38: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

38

Example// create a buttonJButton button = new JButton(“Increment”);

// register an action listenerbutton.addActionListener(new ButtonActionListener());

// Action listener classclass ButtonActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { // handle the event e … System.out.println(“Increment button pressed!”); }}

Page 39: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

39

How Does It Work?

<<create>>

h: ActionListenerb: JButton

e: ActionEvent

addActionListener(h)

actionPerformed(e)

getSource()

Page 40: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

40

Naming Convention

For event XYZ …Event class: XYZEventListener interface: XYZListenerAdapter class: XYZAdapterRegistration method: addXYZListener()

Page 41: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

41

Events and ListenersEvent Listener Adapter ActionEvent ActionListenerComponentEvent ComponentListener ComponentAdapterFocusEvent FocusListener FocusAdapterKeyEvent KeyListener KeyAdapterMouseEvent MouseListener MouseAdapter MouseMotionListener MouseMotionAdapterWindowEvent WindowListener WindowAdapterItemEvent ItemListenerTextEvent TextListener…

ComponentEvent: component’s changes of size, location, and visibilityFocusEvent: component’s gain/loss of keyboard focusKeyEvent: keystrokesMouseEvent: mouse press, release, click, enter and exit; mouse motion (e.g., dragging and moving)WindowEvent: opened, closed, activated or deactivated, iconified or deiconified.ItemEvent: item selection, etc.

Page 42: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

42

Example: Resizing Component To prevent windows from being resized too small, use

ComponentEvent and ComponentListener

public class Main extends JFrame { public Main(String name) { super(name); // …. setResizable(true); addComponentListener(Util.createComponentListener(400, 300)); } // …}

Page 43: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

43

Example (Cont.)public class Util { public static ComponentListener createComponentListener(int width, int height) { return new MyComponentListener(width, height); }

private static class MyComponentListener extends ComponentAdapter { private int width, height; public MyComponentListener(int w, int h) { width = w; height = h; } public void componentResized(ComponentEvent e) { Component c = e.getComponent(); if (c.getWidth() < width || c.getHeight() < height) { c.setSize(Math.max(width, c.getWidth()), Math.max(height, c.getHeight())); } } } // MyComponentListener}

Page 44: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

44

Using Anonymous Class// same code with anonymous classpublic class Util {

public static ComponentListener createComponentListener( final int width, final int height) {

return new ComponentAdapter() { public void componentResized(ComponentEvent e) { Component c = e.getComponent(); if (c.getWidth() < width || c.getHeight() < height) { c.setSize(Math.max(width, c.getWidth()), Math.max(height, c.getHeight())); } } // componentResized }; // ComponentAdapter

} // createComponentListener}

Page 45: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

45

ExerciseWrite handler code by using anonymous class to print a goodbye message to System.out when the main window is closed.

Hint: The WindowListener interface defines, among others, the method “void windowClosing(WindowEvent)”.

public class Main extends JFrame { public Main(String name) { // …. // WRITE YOUR CODE HERE

} // …}

Page 46: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

46

Exercise: Mouse Motion EventExtend the counter applet to change its button color when the mouse enters the button.

Hints - The interface MouseListener declares, among others, void mouseEntered(MouseEvent) and void mouseExited(MouseEvent).- The method setBackground(Color) sets the background color of a widget.- The source (e.g., JButton) can be obtained from an event object by calling the method “Object getSource()”.

Page 47: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

47

Exercise (Cont.)public CounterApplet extends Applet { public CounterApplet() { // … JButton button = new JButton("Increment"); // WRITE YOUR CODE HERE!

// … } // …}

Page 48: Object-Oriented Application Frameworks CS 3331 Sections 8.1 & 8.3 of [Jia 03]

48

Exercise: JavaChatCreate UI of JavaChat dialog (sample screens below); dialog template available from course website.

Upon the Send button click when not connected (left) and connected (right)

UI components: JButton, JTextField, JTextArea, JScrollPane