Design Case Study: GUI with Swing (2)

Similar documents
Design Case Study: GUI with Swing

Charlie Garrod Bogdan Vasilescu

Jonathan Aldrich Charlie Garrod

Chris9an Kästner Charlie Garrod

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

Introduction to concurrency and GUIs

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

UI Software Organization

Design Patterns Reid Holmes

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Design for Large-Scale Reuse: Libraries and Frameworks

CSE 331 Software Design & Implementation

Topics in Object-Oriented Design Patterns

Implementing Graphical User Interfaces

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

Charlie Garrod Michael Hilton

Lecture 19 GUI Events

Graphical User Interface (GUI)

Design for large-scale reuse: Libraries and frameworks

Systems Programming Graphical User Interfaces

Introduction to the JAVA UI classes Advanced HCI IAT351

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

Building Graphical User Interfaces. GUI Principles

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

CS410G: GUI Programming. The Model/View/Controller Pattern. Model. Controller. View. MVC is a popular architecture for building GUIs

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Java: Graphical User Interfaces (GUI)

Charlie Garrod Bogdan Vasilescu

GUI Software Architecture

Building Graphical User Interfaces. Overview

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CS11 Java. Fall Lecture 4

Charlie Garrod Michael Hilton

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CSE 70 Final Exam Fall 2009

Graphical User Interface (GUI)

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns and Frameworks Command

Overview. Building Graphical User Interfaces. GUI Principles. AWT and Swing. Constructing GUIs Interface components GUI layout Event handling

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Design Patterns Design patterns advantages:

Design Patterns Lecture 2

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Patterns Reid Holmes

Basics of programming 3. Java GUI and SWING

SDC Design patterns GoF

ITEC 120 4/14/11. Review. Need. Objectives. GUI basics JFrame JPanel JLabel, JButton, JTextField Layout managers. Lecture 38 GUI Interactivity

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Design Patterns IV Structural Design Patterns, 1

Layout. Dynamic layout, Swing and general layout strategies

MIT AITI Swing Event Model Lecture 17

SOFTWARE PATTERNS. Joseph Bonello

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

PIC 20A GUI with swing

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

EPITA Première Année Cycle Ingénieur. Atelier Java - J5

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Graphic User Interfaces. - GUI concepts - Swing - AWT

Software Construction

Uppsala University. Assignment 3. Separation into Model-View TableModel ListModel ( multiple inheritance or adapter) Renderer (delegation)

CSC207 Week 4. Larry Zhang

Graphical User Interfaces. Comp 152

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

CS 251 Intermediate Programming GUIs: Components and Layout

Part I: Learn Common Graphics Components

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

2/12/15. Template Method. Design Patterns. In Swing s JComponent. Template Method. Where do template methods come from? Making Template Methods

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

SELF-STUDY. Glossary

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Adapter & Facade. CS356 Object-Oriented Design and Programming November 19, 2014

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

MVC: Model View Controller

Adapter Pattern Structural

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

Class 16: The Swing Event Model

CS11 Java. Fall Lecture 3

JLayeredPane. Depth Constants in JLayeredPane

Object-Oriented Design

Example: Building a Java GUI

An Introduction to Patterns

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Example: Building a Java GUI

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

CS 349 / SE 382 Custom Components. Professor Michael Terry February 6, 2009

Transcription:

Principles of Software Construction: Objects, Design, and Concurrency (Part 3: Design Case Studies) Design Case Study: GUI with Swing (2) Christian Kästner Charlie Garrod School of Computer Science 1

Administrivia Homework 4a feedback tomorrow in recitation 2

3 3

Summary GUIs are full of design patterns Strategy pattern Template Method pattern Composite pattern Observer pattern Decorator pattern Façade pattern Adapter pattern Command pattern Model-View-Controller Separation of GUI and Core 4

SEPARATING GUI AND CORE 5 5

Decoupling with the Observer pattern Let the world tell all interested components about updates World GUI Warning register register step notify notify update update 6

An architectural pattern: Model-View- Controller (MVC) Manage inputs from user: mouse, keyboard, menu, etc. Manage display of information on the screen Manage data related to the application domain 7

Model-View-Controller (MVC) Passive model Active model http://msdn.microsoft.com/en-us/library/ff649643.aspx 8

Separating application core and GUI, a summary Reduce coupling: do not allow core to depend on UI Create and test the core without a GUI Use the Observer pattern to communicate information from the core (Model) to the GUI (View) GUI Tests GUI Core Tests Core 9

DESIGN OF LIST AND TABLE ENTRIES 10

JList and JTree Lists and trees highly flexible (reusable) Can change rendering of cells Can change source of data to display //simple use String [] items = { a, b, c ; JList list = new JList(items); 11

The ListModel Allows list widget (view) to react to changes in //with the model a ListModel ListModel model = new DefaultListModel(); model.addelement( a ); JList list = new JList(model); interface ListModel<T> { int getsize(); T getelementat(int index); void addlistdatalistener(listdatalistener l); void removelistdatalistener(listdatalistener l); 12

The ListModel Allows list widget (view) to react to changes in //with the model a ListModel ListModel model = new DefaultListModel(); model.addelement( a ); JList list = new JList(model); interface ListModel<T> { int getsize(); interface T getelementat(int ListDataListener index); extends EventListener { void void addlistdatalistener(listdatalistener intervaladded( ); l); void void removelistdatalistener(listdatalistener intervalremoved( ); l); void contentschanged( ); 13

Scenario Assume we want to show all buses of the simulation in a list and update the items //design 1 class Simulation implements ListModel<String> { List<Bus> items int getsize() { return items.size(); String getelementat(int index) { items.get(index).getroute(); void addlistdatalistener(listdatalistener l) { protected void firelistupdated() { 14

Scenario Assume we want to show all buses of the simulation in a list and update the items //design 2 class World { DefaultListModel<Item> items public getlistmodel() { return items; public Iterable<Item> getitems() { return items.elements(); 15

Scenario Assume we want to show all buses of the simulation in a list and update the items //design 3 class SimulationAdapter implements ListModel<String> { private final Simulation simulation; public SimulationAdapter(Simulation s) {simulation = s; int getsize() { return count(simulation.getbuses()); String getelementat(int index) { find(simulation.getbuses(), index).getroute(); void addlistdatalistener(listdatalistener l) { 16

Comparing the three solutions 1 «interface» ListModel JList 2 Simulation +getitems() +getsize() +getelementat() 1 DefaultListModel JList Simulation -items +getitems() +getsize() +getelementat() 3 «interface» ListModel +getsize() +getelementat() +getsize() +getelementat() JList Simulation SimulationAdaptr -items +getitems() +getsize() +getelementat() 17

The Adapter Design Pattern 18

The Adapter Design Pattern Applicability You want to use an existing class, and its interface does not match the one you need You want to create a reusable class that cooperates with unrelated classes that don t necessarily have compatible interfaces You need to use several subclasses, but it s impractical to adapt their interface by subclassing each one Consequences Exposes the functionality of an object in another form Unifies the interfaces of multiple incompatible adaptee objects Lets a single adapter work with multiple adaptees in a hierarchy -> Low coupling, high cohesion 19

Façade vs. Adapter Motivation Façade: simplify the interface Adapter: match an existing interface Adapter: interface is given Not typically true in Façade Adapter: polymorphic Dispatch dynamically to multiple implementations Façade: typically choose the implementation statically 21

Design Goals Design to explicit interfaces Façade a new interface for a library Adapter design application to a common interface, adapt other libraries to that Favor composition over inheritance Façade library is composed within Façade Adapter adapter object interposed between client and implementation Design for change with information hiding Both Façade and Adapter shields variations in the implementation from the client Design for reuse Façade provides a simple reusable interface to subsystem Adapter allows to reuse objects without fitting interface 22

DESIGN OF A LAYOUT MECHANISM 24 24

Swing Layout Manager see http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html 25

A naïve Implementation Hard-code layout algorithms class JPanel { protected void dolayout() { switch(getlayouttype()) { case BOX_LAYOUT: adjustsizebox(); break; case BORDER_LAYOUT: adjustsizeborder(); break;... private adjustsizebox() { A new layout requires changing or overriding JPanel 26

Layout Manager A panel has a list of children Different layouts possible List of predefined layout strategies Own layouts possible Every widget has preferred size Delegate specific layout to a separate class implementing an explicit interface Use polymorphism for extensibility 27

Layout Managers panel.setlayout(new BorderLayout(0,0)); abstract class Container { // JPanel is a Container private LayoutManager layoutmgr; public dolayout() { LayoutManager m = this.layoutmgr; if (m!=null) m.layoutcontainer(this); public Component[] getcomponents() { interface LayoutManager { void layoutcontainer(container c); Dimension getminimumlayoutsize(container c); Dimension getpreferredlayoutsize(container c); 28

Behavioral: Strategy 29

Remember Design Discussion for Strategy Pattern Design to explicit interfaces Strategy: the algorithm interface Design for change and information hiding Find what varies and encapsulate it Allows adding alternative variations later Design for reuse Strategy class may be reused in different contexts Context class may be reused even if existing strategies don t fit Low coupling Decouple context class from strategy implementation internals 30

Template Method vs Strategy vs Observer Template method vs strategy pattern both support variations in larger common context Template method uses inheritance + abstract method Strategy uses interface and polymorphism (object composition) strategy objects reusable across multiple classes; multiple strategy objects per class possible Why is Layout in Swing using the Strategy pattern? Strategy vs observer pattern both use a callback mechanism Observer pattern supports multiple observers (0..n) Update method in observer triggers update; rarely returns result Strategy pattern supports exactly one strategy or an optional one if null is acceptable (0..1) Strategy method represents a computation; may return a result 31

The Strategy Pattern to Paint Borders contentpane.setborder(new EmptyBorder(5, 5, 5, 5)); Border interface has paintborder, getborderinsets and isborderopague methods //alternative design class JPanel { protected void paintborder(graphics g) { switch(getbordertype()) { case LINE_BORDER: paintlineborder(g); break; case ETCHED_BORDER: paintetchedborder(g); break; case TITLED_BORDER: painttitledborder(g); break;... 32

The Strategy Pattern to Paint Borders contentpane.setborder(new EmptyBorder(5, 5, 5, 5)); Border interface has paintborder, getborderinsets and isborderopague methods //alternative design class JPanel { protected void paintborder(graphics g) { switch(getbordertype()) { case LINE_BORDER: paintlineborder(g); break; case ETCHED_BORDER: paintetchedborder(g); break; case TITLED_BORDER: painttitledborder(g); break; //actual JComponent... implementation protected void paintborder(graphics g) { Border border = getborder(); if (border!= null) border.paintborder(this, g, 0, 0, getwidth(), getheight()); 33

Nesting Containers A JFrame contains a JPanel, which contains a JPanel (and/or other widgets), which contains a JPanel (and/or other widgets), which contains What design pattern fits this problem? 34

Composite Design Pattern Applicability You want to represent part-whole hierarchies of objects You want to be able to ignore the difference between compositions of objects and individual objects Consequences Makes the client simple, since it can treat objects and composites uniformly Makes it easy to add new kinds of components Can make the design overly general Operations may not make sense on every class Composites may contain only certain components 35

DESIGNING EVENT HANDLING FOR BUTTONS 36 36

Swing Button with ActionListener //static public void main JFrame window = JPanel panel = new JPanel(); window.setcontentpane(panel); JButton button = new JButton( Click me ); button.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { System.out.println( Button clicked ); ); panel.add(button); window.setvisible(true); 37

ActionListeners What design pattern is this? interface ActionListener { void actionperformed( ActionEvent e); class ActionEvent { int when; String actioncommand; int modifiers; Object source(); int id; class AbstractButton extends JComponent { private List<ActionListener> listeners; public void addactionlistener(actionlistener l) { listeners.add(l); protected void fireactionperformed(actionevent e) { for (ActionListener l: listeners) l.actionperformed(e); 38

The observer design pattern 39

Alternative Button class MyButton extends JButton { public MyButton() { super( Click me ); @Override protected void fireactionperformed(actionevent e) { super.fireactionperformed(e); System.out.println( Button clicked ); What design pattern is this? //static public void main JFrame window = JPanel panel = new JPanel(); window.setcontentpane(panel); panel.add(new MyButton()); window.setvisible(true); 40

The Template Method design pattern 41

Design Discussion Button implementation should be reusable but differ in button label and differ in event handling multiple independent clients might be interested in observing events basic button cannot know what to do Design goal: Decoupling action of button from button implementation itself for reuse Template method allows specialized buttons Observer pattern separates event handling multiple listeners possible multiple buttons can share same listener 43

JButton btnnewbutton = new JButton("New button"); btnnewbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { counter.inc(); ); btnnewbutton.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { JOptionPane.showMessageDialog(null, "button clicked", "alert", JOptionPane.ERROR_MESSAGE); ); 44

class MyButton extends JButton { Counter counter; public MyButton() { settitle protected void fireactionperformed(actionevent e) counter.inc(); JButton btnnewbutton = new MyButton(); panel.add(btnnewbutton); 45

public class ActionListenerPanel extends JPanel implements ActionListener { public ActionListenerPanel() { setup(); private void setup() { button1 = new JButton( a ); button1.addactionlistener(this); button2 = new JButton( b ); button2.addactionlistener(this); add(button1); add(button2); public void actionperformed(actionevent e) { if(e.getsource()==button1) display.settext( BUTTON_1 ); else if(if(e.getsource()==button1) 46

public class ActionListenerPanel extends JPanel implements ActionListener { public ActionListenerPanel() { setup(); private void setup() { button1 = new JButton( a ); button1.addactionlistener(this); button2 = new JButton( b ); button2.addactionlistener(this); add(button1); add(button2); public void actionperformed(actionevent e) { Cohesion? if(e.getsource()==button1) display.settext( BUTTON_1 ); Class responsibilities else include if(if(e.getsource()==button1) (1) building the display, (2) wiring buttons and listeners, (3) mapping events to proper response, (4) perform proper response Consider separating out event handling with different listeners 47

DESIGN OF DRAWING WIDGETS 48 48

JComponent 49

Template Method Design Pattern Applicability When an algorithm consists of varying and invariant parts that must be customized When common behavior in subclasses should be factored and localized to avoid code duplication To control subclass extensions to specific operations Consequences Code reuse Inverted Hollywood control: don t call us, we ll call you Ensures the invariant parts of the algorithm are not changed by subclasses 50

GUI design: Interfaces vs. inheritance Inherit from JPanel with custom drawing functionality Subclass is a special kind of Panel The subclass interacts closely with the JPanel e.g. the subclass calls back with super Accesses protected functionality otherwise not exposed The way you draw the subclass doesn t change as the program executes Implement the ActionListener interface, register with button The action to perform isn t really a special kind of button; it s just a way of reacting to the button. So it makes sense to be a separate object. The ActionListener is decoupled from the button. Once the listener is invoked, it doesn t call anything on the Button anymore. We may want to change the action performed on a button press so once again it makes sense for it to be a separate object See also Command pattern later 51 51

UNDOABLE ACTIONS 52

Actions in GUIs We want to make actions accessible in multiple places menu, context menu, keyboard shortcut, When disabling an action, all places should be disabled We may want to undo actions Separate action execution from presentation Delay, queue, or log actions eg macro recording, progress bars, executing remotely, transactions, wizards 53

Actions in Swing «interface» ActionListener +actionperformed() AbstractButton +getaction() +setaction() * 1 «interface» Action +getvalue(in key) +putvalue(in key, in value) +setenabled() +isenabled() +addpropertychangelistener() Keys: Name Icon Short description Long description Key combinations Selected JButton JMenuItem AbstractAction -enabled +...() 54

Action vs. ActionListener Action is self-describing (text, shortcut, icon, ) Can be used in many places e.g. log of executed commands, queue, undo list Action can have state (enabled, selected, ) Actions are synchronized in all cases where they are used with observer pattern: PropertyChangeListener 55

Implementing a Wizard Every step produces some execution Execution is delayed until user clicks finish Collect objects representing executions in a list Execute all at the end -> Design for change, division of labor, and reuse; low coupling interface ExecuteLater { void execute(); 56

Implementing a Wizard Every step produces some private execution final String name; Execution is delayed until user clicks finish Collect objects representing void execute() executions { in a list Execute all at the end -> Design for change, division of labor, and reuse; low coupling class SetCompanyName implements ExecuteLater { private final Registry registry; SetCompanyName(String n, Registry r) { name=n; registry = r; registry.writekey(, name); interface ExecuteLater { void execute(); 57

The Command Design Pattern 58

The Command Design Pattern Applicability Parameterize objects by an action to perform Specify, queue and execute requests at different times Support undo Support logging changes that can be reapplied after a crash Structure a system around high-level operations built out of primitives Consequences Decouples the object that invokes the operation from the one that performs it Since commands are objects they can be explicitly manipulated Can group commands into composite commands Easy to add new commands without changing existing code 59

Common Commands in Java javax.swing.action see above java.lang.runnable Used as an explicit operation that can be passed to threads, workers, timers and other objects or delayed or remote execution see FutureTask 60

notice the composite pattern Source: http://www.javaworld.com/article/2076698/core-java/add-an-undo-redo-function-to-your-java-appswith-swing.html 61

THE EVENT THREAD 62 62

The GUI Threading Architecture main() thread Create window Set up callbacks Show window (thread ends) GUI Thread Loop forever: Get system event Invoke callback Callback code: Compute fibonacci (UI is unresponsive) Show result 63 63

The GUI Threading Architecture main() thread Create window Set up callbacks Show window (thread ends) GUI thread Loop forever: Get system event Invoke callback Callback code: create SwingWorker start it executing Worker thread Worker thread execution: invoke doinbackground() compute fibonacci store result in SwingWorker signal to UI that we are done SwingWorker result : Long 64 64

The GUI Threading Architecture main() thread Create window Set up callbacks Show window (thread ends) GUI thread Loop forever: Get system event Invoke callback Worker thread Invoke SwingWorker.done() get() result from SwingWorker show result in the UI Worker thread execution: invoke doinbackground() compute fibonacci store result in SwingWorker signal to UI that we are done SwingWorker result : Long 65 65

Summary GUIs are full of design patterns Strategy pattern Template Method pattern Composite pattern Observer pattern Decorator pattern Façade pattern Adapter pattern Command pattern Model-View-Controller Separation of GUI and Core 66