object-oriented frameworks
DESCRIPTION
Object-Oriented Frameworks. Supplementary reading: Java text (e.g. Budd Ch. 21, Ch. 22). Frameworks. Framework classes. A framework is a set of classes that cooperate closely and collectively embody a reusable solution to a common problem - PowerPoint PPT PresentationTRANSCRIPT
Object-Oriented Frameworks
Supplementary reading:Java text (e.g. Budd Ch. 21, Ch. 22)
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 2
Frameworks
A framework is a set of classes that cooperate closely and collectively embody a reusable solution to a common problem
A framework is intended to be extended to meet the needs of a particular application Extension is carried out by
subclassing, overriding methods, and implementing interfaces
<< interface >>
Framework classes
Application classes
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 3
Reuse and specialisation
Window
setTitle( title : String ) : voidsetSize( width : int, height : int ) : voidmoveTo( x : int, y : int ) : voidaddMenu( menu : Menu ) : voidrepaint( ) : voidmouseDown( x : int, y : int ) : voidkeyPress( c: char ) : voidPaint( ) : void
SpecialWindow
}} Code reuse.
Methods called by class users and subclasses.
Concept reuse.Methods that are applicable to any kind of Window, but for which meaningful implementation is deferred to subclasses.
A windowing framework will expect any kind of Window object to understand, for example, a paint( ) message. However, the windowing framework won’t know the application-specific response. Such responses are implemented by subclassing and method overriding.
A windowing framework will expect any kind of Window object to understand, for example, a paint( ) message. However, the windowing framework won’t know the application-specific response. Such responses are implemented by subclassing and method overriding.
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 4
Inversion of control
Library code
Application code
Application calls an I/O library routine
Application calls a sort routine
The traditional case where the application code defines the flow of execution.
Control flow is dictated by the framework. The application programmer implements routines invoked by the framework, but doesn’t change the solution’s overall structure.
Time
Framework code
Application code
Framework generates a window resize event
Framework generates a button click event
Framework generates a window deiconification event
Time
We’ve already come across the idea of inverted control structures. Where?
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 5
Java applets: a simple framework
Applet framework (web browser)
Application code
init( )
start( )
paint( ) stop( )
destroy( )
Applet
init( )start( )paint( )stop( )destroy( )
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 6
Object
Component
getSize( ) : intsetSize( width : int, height : int ) : voidsetVisible( visible : boolean ) : voidrepaint( g : Graphics ) : voidpaint( g : Graphics ) : voidaddMouseListener( listener : MouseListener ) : voidaddKeyListener( listener : KeyListener ) : void
Container
setLayout( mgr : LayoutManager ) : voidadd( c : Component ) : void
Window
toFront( )toBack( )
Frame
setTitle( title : String ) : voidsetCursor( c : int ) : voidsetResizable( resizable : boolean ) : voidsetMenuBar( mb : MenuBar ) : void
Button Checkbox Choice Label List Scrollbar
TextComponent
TextArea TextField
Panel ScrollPanel
A Component is something that can be displayed on a GUI and with which a user can interact.
A Window is a special Container that can be stacked, and moved to the front/back.
A Frame is a special Window with a title, menubar, border and cursor.
repaint( ) schedules a Component instance for repainting. The AWT framework will subsequently call paint( ) on the Component object.
Interpret this diagram and write down at least 3 points of interest.
Java’s AWT framework
A Container is a special component that can nest other components, be they simple components (e.g. Buttons, Labels) or themselves Containers (e.g. Panels).
paint( ) is a hook method.
add( ) is a polymorphic method; it can take as actual argument any kind of Component.
Generalisation
Specialisation
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 7
FramePanel
LabelTextField
Label
Panel
Button
Button
Panel
Component
Container
add( c : Component ) : void
class Container extends Component { private List<Component> children; … public void add( Component c ) { children.add( c ); }}
Frame instance
TextField instance
Panel instance
Buttoninstance
Buttoninstance
Labelinstance
Panel instance
Panel instance
Labelinstance
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 8
Traversing the nested structure
Frame instance
TextField instance
Panel instance
Buttoninstance
Buttoninstance
Labelinstance
Panel instance
Panel instance
Labelinstance
class Container extends Component { private List<Component> children; … public void add( Component c ) { children.add( c ); }
public void paint( Graphics g ) { for( Component c : children ) c.paint( ); }}
1: paint( )
2: paint( )
3: paint( ) 4: paint( )
5
6
7
8
9
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 9
Layout management
Container
setLayout( mgr : LayoutManager ) : voiddoLayout( )add( c : Component ) : void
Component
<< interface >>LayoutManager
layoutContainer( c : Container )minimumLayoutSize( c : Container ) : DimensionpreferredLayoutSize( c : Container ) : Dimension
BorderLayout BoxLayout FlowLayout GridLayout
Why is this design for layout management so effective?
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 10
Layout management
Container
Panel
PanelWithBorderLayout PanelWithBoxLayout…
Alternative design: for each special kind of container, implement further subclasses, one for each kind of layout strategy.
Composition and interfaces Inheritance
This solution requires only one class for each special type of container, and one class for each layout strategy. Any kind of container instance can be configured with any kind of strategy object.
Subclass explosion! To support all container (c) and layout strategy (s) combinations, c * s classes would be required.
Composition offers run-time flexibility. A Frame can change how it lays out its children simply by swapping its current layout strategy (e.g. BorderLayout) for another (e.g. GridLayout).
Overly rigid structure. Once a PanelWithBorderLayout object, for example, has been created, the panel will always be laid out according to the border strategy.
Frame
…
FrameWithBorderLayout FrameWithBoxLayout…
Object-Oriented Frameworks
Layout management
COMPSCI 230 Software Design & Construction 11
public class Container extends Component { … private List< Component > children; private LayoutManager layoutManager;
public void setLayout( LayoutManager lm ) { layoutManager = lm; }
public void doLayout( ) { layoutManager.layout( this ); }}
Frame instance
TextField instance
Panel instance
Buttoninstance
Buttoninstance
Labelinstance
Panel instance
Panel instance
Labelinstance
1: doLayout( )
2: layoutContainer( this )
LayoutManager instance
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 12
Event handling
Components generate events in response to user interaction
To hook in application-specific responses to events, listeners are used A listener is an object
that is registered with a component; in handling user interaction the component informs any registered listeners of the event
Framework code
Application code
Framework generates button click event
Framework waits for next user interaction
Framework generates next event
Registered listener executes
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 13
Event handling
<< interface >>ActionListener
actionPerformed( e : ActionEvent ) : void
<< interface >>KeyListener
keyPressed( e : KeyEvent ) : voidkeyReleased( e : KeyEvent ) : voidkeyTyped( e : KeyEvent ) : void
<< interface >>MouseListener
mouseReleased( e : MouseEvent ) : voidmouseClicked( e : MouseEvent ) : voidmouseEntered( e : MouseEvent ) : voidmouseExited( e : MouseEvent ) : void
AWT frameworkButton
addActionListener( listener : ActionListener )
ApplicationListener
Application
Buttoninstance
ActionListener Instance
listener
(an instance of a class that implements the ActionListener interface)
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 14
public class PalindromeGUI extends Frame implements ActionListener {
private TextField text; private Button isPalindrome, quit; private Label output;
public PalindromeGUI() { super( "Palindrome detector" ); … isPalindrome.addActionListener( this ); quit.addActionListener( this ); }
public void actionPerformed( ActionEvent e ) { if( e.getSource( ) == isPalindrome ) { // isPalindrome pressed. String enteredText = text.getText( ); if( enteredText.length( ) > 0 ) { if( isPalindromic( enteredText ) ) { output.setText( enteredText + " is palindromic" ); } else { output.setText( enteredText + " is not palidromic" ); } } } else { // quit pressed. System.exit( 0 ); } }}
PalindromeGUI / ActionListener instance
Button instance
isPalindrome
quit
listenerlistener
addActionListener( this )
actionPerformed( )
Button instance
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 15
public class PalindromeGUI extends Frame { private TextField text; private Button isPalindrome, quit; private Label output;
public PalindromeGUI( ) { … isPalindrome.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { String enteredText = text.getText( );
if( enteredText.length( ) > 0 ) { if( isPalindromic( enteredText ) ) { output.setText( enteredText + " is palindromic" ); } } else { output.setText( enteredText + " is not palindromic" ); } } } );
quit.addActionListener( new ActionListener( ) { public void actionPerformed( ActionEvent e ) { System.exit( 0 ); } } ); …}
PalindromeGUI instance
Button instance
isPalindrome
quit
listenerlistener
actionPerformed( )
Button instance
ActionListener instance
ActionListener instance
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 16
MouseListenerpublic class MouseMain extends Frame {
public MouseMain() { super( “MouseListener" );
Panel mousePanel = new MousePositionPanel( ); add( mousePanel );
setBounds( 100, 100, 200, 200 ); setVisible( true ); }
public static void main( String[ ] args ) { MouseMain mouseMain = new MouseMain( ); }}
class MousePositionPanel extends Panel implements MouseListener { private int mouseX, mouseY;
public MousePositionPanel( ) { mouseX = 100; mouseY = 100;
addMouseListener( this ); }
public void paint( Graphics g ) { g.drawString( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY ); }
public void mousePressed( MouseEvent e ) { mouseX = e.getX( ); mouseY = e.getY( ); repaint( ); }
public void mouseReleased( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { }}
Object-Oriented Frameworks COMPSCI 230 Software Design & Construction 17
Custom painting in AWT
The MouseListener application is an example where custom painting is required The MousePositionPanel
component is painted by displaying the position where the mouse was clicked
Each time the mouse is clicked, the application updates its state and requests that its MousePositionPanel component be repainted
To carry out custom painting in a subclass of Component, you should override method paint( ).
Component subclasses should not override method repaint( ).
To carry out custom painting in a subclass of Component, you should override method paint( ).
Component subclasses should not override method repaint( ).
Container
Component
paint( g : Graphics ) : voidrepaint( ) : void
Panel
MousePositionPanel
<< interface >>MouseListener
Overrides paint( )
Object-Oriented Frameworks
Swing
Swing is a more complex and powerful replacement for the AWT
Swing includes: A richer set of GUI components, many of which are located in package
javax.swing By convention, Swing component names are prefixed by J (e.g. JButton,
JLabel etc.) and are subclasses of JComponent, which is a subclass of the AWT’s Component class
Components that have separate models; the Swing framework originates from the Model-View-Control (MVC) paradigm
Components that have a pluggable look-and-feel Cosmetic improvements – e.g. buttons and labels can show images
rather than just text, and components can be decorated with a variety of borders
Swing’s painting model improves the AWT’s but is slightly more complex
Swing uses the AWT’s layout management and event handling infrastructure
COMPSCI 230 Software Design & Construction 18
Object-Oriented Frameworks
Pluggable look and feel
COMPSCI 230 Software Design & Construction 19
With a Swing application, it is possible to change the look-and-feel at run-time.
What technique do you think has been used in implementing this feature?
JButton
setUI( delegate : ButtonUI ) : void
<< interface >>ButtonUI
paint( g : Graphics ) : void
Implemented by one class for each different look-and-feel, e.g. Windows, Metal, Motif.
Object-Oriented Frameworks
Custom painting in Swing
COMPSCI 230 Software Design & Construction 20
For custom painting in Swing, JComponent subclasses should override paintComponent( ) (and, in general, make a super.paintComponent( ) call).
JComponent subclasses should not override paint( ) or repaint( ).
For custom painting in Swing, JComponent subclasses should override paintComponent( ) (and, in general, make a super.paintComponent( ) call).
JComponent subclasses should not override paint( ) or repaint( ).
JComponent
paintComponent( g : Graphics )
Component
paint( g : Graphics ) : voidrepaint( ) : void
JPanel
MousePositionPanel
<< interface >>MouseListener
public void paint ( Graphics g ) { paintComponent( g ); // Code to paint border. // Code to paint children. …}
public void paintComponent( Graphics g ) { // Code to paint the background.}
public void paintComponent( Graphics g ) { // Make sure the background is painted. super.paintComponent( g );
// Now do custom painting. g.drawString ( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY );}
Object-Oriented Frameworks
Custom painting in Swing
COMPSCI 230 Software Design & Construction 21
import java.awt.*;import java.awt.event.*;import javax.swing.*;
public class SwingMouseMain extends JFrame {
public SwingMouseMain( ) { super( "Mouse Listener" );
JPanel mousePanel = new MousePositionPanel( ); add( mousePanel );
setBounds( 100, 100, 200, 200 ); setVisible( true ); }
public static void main( String[ ] args ) { SwingMouseMain mouseMain = new SwingMouseMain( ); }}
class MousePositionPanel extends JPanel { private int mouseX, mouseY;
public MousePositionPanel( ) { mouseX = 100; mouseY = 100;
addMouseListener( new MouseAdapter( ) { public void mousePressed( MouseEvent e ) { mouseX = e.getX( ); mouseY = e.getY( ); repaint( ); } } ); }
public void paintComponent( Graphics g ) { super.paintComponent( g ); g.drawString( "(" + mouseX + ", " + mouseY + ")", mouseX, mouseY ); }}