unit 22 command summary prepared by kirk scott 1

86
Unit 22 Command Summary prepared by Kirk Scott 1

Upload: oswald-sutton

Post on 01-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Unit 22 Command Summary prepared by Kirk Scott 1

1

Unit 22Command

Summary prepared by Kirk Scott

Page 2: Unit 22 Command Summary prepared by Kirk Scott 1

2

Design Patterns in JavaChapter 24Command

Summary prepared by Kirk Scott

Page 3: Unit 22 Command Summary prepared by Kirk Scott 1

3

The Introduction Before the Introduction

• The ordinary way for a client to cause a method to execute is to call it (on an object), in-line

• There may be other times when the client can’t specify exactly when or where a particular method should be called, but would like to be able to pass the method to another piece of code which will call it when needed

Page 4: Unit 22 Command Summary prepared by Kirk Scott 1

4

• This can be accomplished by writing a class which contains the desired method

• Then at run time an instance of that class can be constructed

• That object can have the method called on it• Passing the method can be accomplished by

passing the object to another piece of code as an explicit parameter

Page 5: Unit 22 Command Summary prepared by Kirk Scott 1

5

• Book definition:• The intent of the Command pattern is to

encapsulate a request in an object.• Comment mode on:• Notice that this is what has always happened when

writing a class definition and putting a public method in it.

• You learned to pass objects as parameters a long time ago, and within receiving code it is possible to call methods on the parameters

Page 6: Unit 22 Command Summary prepared by Kirk Scott 1

6

A Classic Example: Menu Commands

• Menus and listeners work together using the Command design pattern

• They illustrate the idea that the application writer can’t predict in advance when a user will select a menu item, but can write the code to deal with it when it happens

Page 7: Unit 22 Command Summary prepared by Kirk Scott 1

7

• In schematic form, these are the steps in creating a menu in code:

• JMenuBar myMenuBar = new JMenuBar();

• JMenu myMenu = new JMenu(“File”);

• myMenuBar.add(myMenu);

Page 8: Unit 22 Command Summary prepared by Kirk Scott 1

8

• These steps are followed by the steps for adding items to the menu:

• JMenuItem myExitMenuItem = new JMenuItem(“Exit”);• ExitListener myExitListener = new ExitListener();• myExitMenuItem.addActionListener(myExitListener);• myMenu.add(myExitMenuItem);

Page 9: Unit 22 Command Summary prepared by Kirk Scott 1

9

• The ExitListener is implemented as an inner class of the frame which has the menu

• This is what its code looks like:

• private class ExitListener implements ActionListener• {• public void actionPerformed(ActionEvent event)• {• System.exit(0);• }• }

Page 10: Unit 22 Command Summary prepared by Kirk Scott 1

10

• The ExitListener class has an actionPerformed() method in it

• The actionPerformed() method is executed when the Exit item is clicked in the menu

• In the constructor for the frame, where the menu is being created, it is impossible to predict when the Exit item will be clicked

• In other words, it is impossible to predict when the actionPerformed() method will be called

Page 11: Unit 22 Command Summary prepared by Kirk Scott 1

11

• That’s all right• The following line of code shows how

actionPerformed() is linked to the menu item• It is this line of code that illustrates the

Command design pattern• myExitMenuItem.addActionListener(myExitListener);

• The object myExitListener is passed to myExitItem as an explicit parameter in the call to addActionListener

Page 12: Unit 22 Command Summary prepared by Kirk Scott 1

12

• The actionPerformed() method can be called on myExitItem in the future in response to a mouse click on myExitItem

• This is mildly cryptic because programmer written code does not contain the call myExitListener.actionPerformed()

• The actual call is made by the system through Java’s event handling mechanism

• Even so, the Command design pattern is evident in the way the item and the listener are linked

Page 13: Unit 22 Command Summary prepared by Kirk Scott 1

13

• The book notes that once again, polymorphism is at work in the way menus function

• Each menu item can have a different listener which takes different actions

• Each listener has a method with the same name, actionPerformed()

Page 14: Unit 22 Command Summary prepared by Kirk Scott 1

14

• Which action is performed depends on which kind of listener the system calls actionPerformed() on

• The system only needs to know that in each case, actionPerformed() should be called, and polymorphism makes sure that the right action is taken for each different kind of listener

Page 15: Unit 22 Command Summary prepared by Kirk Scott 1

15

• In CS 202, for example, event handling is simply described as the system calling the actionPerformed() method

• It is important to note that the system calls the actionPerformed() method on the listener object

• The system has access to the objects that are declared as listeners in a program

Page 16: Unit 22 Command Summary prepared by Kirk Scott 1

16

Menu Commands and the Mediator Pattern

• Challenge 24.1• The mechanics of Java menus make it easy to

apply the Command pattern but do not require that you organize your code as commands.

• In fact, it is common to develop an application in which a single object listens to all the events in a GUI.

• What pattern does that follow?

Page 17: Unit 22 Command Summary prepared by Kirk Scott 1

17

• Comment mode on:• This may be an interesting question, but the

authors are spinning out into space now• This question is unrelated to understanding

the design pattern in question.

Page 18: Unit 22 Command Summary prepared by Kirk Scott 1

18

• Solution 24.1• Many Java Swing applications apply the

Mediator pattern, registering a single object to receive all GUI events.

• This object mediates the interaction of the components and translates user input into commands for business domain objects.

Page 19: Unit 22 Command Summary prepared by Kirk Scott 1

19

• Notice that the approach just described could be a mess to implement for menus and it could be equally messy for other situations

• If you had a single listener for all menu items, the contents of the actionPerformed() method would degenerate into a series of if/else statements

Page 20: Unit 22 Command Summary prepared by Kirk Scott 1

20

• These if/elses would have to determine which item had been clicked

• Their bodies would consist of blocks of code implementing the appropriate actions

• Notice how this is exactly the opposite of having many different listeners and relying on polymorphism and dynamic binding to take care of which action to take

Page 21: Unit 22 Command Summary prepared by Kirk Scott 1

21

The Command and Strategy Patterns

• If you think back to the Strategy design pattern, for example, that was the kind of mess you were hoping to avoid

• Instead of having if/else code, the solution was to have different classes for each strategy (in this case, each menu item)

• Each class would have a method with the same name but containing the implementation of a different strategy (in this case, a different action to be performed)

Page 22: Unit 22 Command Summary prepared by Kirk Scott 1

22

• In other words, the handling of menus in Java exhibits the characteristics of two design patterns

• The fact that there are different listener classes, each with an actionPerformed() method illustrates the Strategy design pattern

• The fact that listeners are added to items so that their actionPerformed() method can be called in the future when needed illustrates the Command design pattern

Page 23: Unit 22 Command Summary prepared by Kirk Scott 1

23

• Recall also, that with the Strategy design pattern you ended up with lots of little classes, each containing just a single method, and which you only needed one instance of

• The same thing happens with (standard) menus• There is a different kind of listener for each menu

item• Each listener just contains an implementation of

actionPerformed()• Only one instance of each listener is needed

Page 24: Unit 22 Command Summary prepared by Kirk Scott 1

24

Anonymous Classes

• The book observes that this is the kind of situation where anonymous classes can be used

• I still don’t like anonymous classes, but it is important to get used to them because other programmers use them

• Recall that in CS 202, it was also a listener which was used as an anonymous class example

Page 25: Unit 22 Command Summary prepared by Kirk Scott 1

25

• The book next presents code with the anonymous listeners missing

• It is not reproduced here because it is immediately followed by a challenge to fill in the missing listeners

• It’s easier to just go directly to the complete example with all of the pieces included

Page 26: Unit 22 Command Summary prepared by Kirk Scott 1

26

• Challenge 24.1• Fill in the code for the anonymous subclasses

of ActionListener, overriding the actionPerformed() method. Note that this method expects an ActionEvent argument.

Page 27: Unit 22 Command Summary prepared by Kirk Scott 1

27

• Solution 24.2• Your code should look something like this:• Comment mode on:• In the book, the solution code is followed by

some additional remarks• Instead of putting them at the end, they are

given up front here, followed by a comment on them, followed by the code

Page 28: Unit 22 Command Summary prepared by Kirk Scott 1

28

• Book’s remarks on the solution code:• Although the actionPerformed() method requires

an ActionEvent argument, you can safely ignore it. • The menus() method registers a single instance of

an anonymous class with the Save menu item and a single instance of another anonymous class with the Load menu item.

• When these methods are called, there is no doubt about the source of the event.

Page 29: Unit 22 Command Summary prepared by Kirk Scott 1

29

• Comment mode on:• There are two things to say about this:• First of all, the book’s statement forewarns us

that the creation of the menu in the solution code occurs in a separate method in the application, named menus()

Page 30: Unit 22 Command Summary prepared by Kirk Scott 1

30

• Secondly, there is nothing new in the remarks about the ActionEvent parameter

• We have always ignored it when writing menu listeners• We’ve seen examples in CS 202 where the event

parameter is of interest• For example, it was necessary to get the (x, y)

coordinates of a mouse click to see if it occurred in a cup

• But quite often, there has been no need to do anything with the event parameter in a listener

Page 31: Unit 22 Command Summary prepared by Kirk Scott 1

31

• The book’s solution code is given on the following overheads

• Keep in mind the purpose of the challenge and the code shown

• It is to illustrate how menu listeners (in this case, anonymous ones) make use of the Command design pattern

Page 32: Unit 22 Command Summary prepared by Kirk Scott 1

32

• public class Visualization2 extends Visualization {• public static void main(String[] args) • {• Visualization2 panel = new Visualization2(UI.NORMAL);• JFrame frame = SwingFacade.launch(panel, "Operational Model");• frame.setJMenuBar(panel.menus());• frame.setVisible(true);• }

• public Visualization2(UI ui) • {• super(ui);• }

Page 33: Unit 22 Command Summary prepared by Kirk Scott 1

33

• public JMenuBar menus() • {• JMenuBar menuBar = new JMenuBar();

• JMenu menu = new JMenu("File");• menuBar.add(menu);

• JMenuItem menuItem = new JMenuItem("Save As...");• menuItem.addActionListener(new ActionListener() • {• public void actionPerformed(ActionEvent e) • {• save();• }• });• menu.add(menuItem);

• menuItem = new JMenuItem("Restore From...");• menuItem.addActionListener(new ActionListener() • {• public void actionPerformed(ActionEvent e) • {• restore();• }• });• menu.add(menuItem);

• return menuBar;• }

Page 34: Unit 22 Command Summary prepared by Kirk Scott 1

34

• public void save() • {• try • {• mediator.save(this);• } • catch (Exception ex) • {• System.out.println("Failed save: " + ex.getMessage());• }• }

• public void restore() • {• try • {• mediator.restore(this);• } • catch (Exception ex) • {• System.out.println("Failed restore: " + ex.getMessage());• }• }• }

Page 35: Unit 22 Command Summary prepared by Kirk Scott 1

35

Using Command to Supply a Service

• In the previous example, half the work was done by the Java API

• All that was necessary was to plug a command into an existing context

• In other words, the addActionListener() machinery is provided by the API

• It is also possible for a programmer to provide both the context and the command

Page 36: Unit 22 Command Summary prepared by Kirk Scott 1

36

The Next Example

• The next example shows how to time how long it takes to execute some method, methodA()

• There will be a timing method, methodB(), that takes as its input parameter an object which can have methodA() called on it

• Inside methodB() the call to methodA() on the input parameter will be sandwiched between timing code

Page 37: Unit 22 Command Summary prepared by Kirk Scott 1

37

Setting up the Command to be Passed

• Let there be an abstract class named Command which includes this abstract method declaration:

• public abstract void execute();• A concrete command class would extend

Command and implement execute()• The abstract method, execute(), defines the

interface for using a command object

Page 38: Unit 22 Command Summary prepared by Kirk Scott 1

38

Setting up the Code that will Receive the Command

• In addition to a concrete command class, the example includes a class named CommandTimer

• This class contains a static method named time()

• The time() method takes as an input parameter an instance of a command class

• execute() is called on that command class object in the body of the time() method

Page 39: Unit 22 Command Summary prepared by Kirk Scott 1

39

• Calls to methods that make it possible to keep track of the passage of time can be placed before and after the call to execute() in the time() method

• Code for the CommandTimer class and its time() method are given on the next overhead

• Keep in mind that the method is static.

Page 40: Unit 22 Command Summary prepared by Kirk Scott 1

40

• public class CommandTimer• {• public static long time(Command command)• {• long t1 = System.currentTimeMillis();• command.execute();• long t2 = System.currentTimeMillis();• return t2 – t1;• }• }

Page 41: Unit 22 Command Summary prepared by Kirk Scott 1

41

• The book next presents code to test the setup, with the call that causes the execution missing

• The code with the missing step is not reproduced here because it is immediately followed by a challenge to fill in the missing line of code

• It’s easier to just go directly to the complete example with all of the pieces included

Page 42: Unit 22 Command Summary prepared by Kirk Scott 1

42

• The authors are trying to illustrate the use of a JUnit test framework

• This is mentioned in Appendix C, on the source code, and also at various points in the text

• You don’t have to worry about it• It just adds a little stuff at the end of the example

code which you can ignore• They also use the anonymous class syntax when

creating the command

Page 43: Unit 22 Command Summary prepared by Kirk Scott 1

43

• Challenge 24.3• Complete the assignment statement that sets

the value for actual in such a way that the doze command is timed.

Page 44: Unit 22 Command Summary prepared by Kirk Scott 1

44

• Solution 24.3• The testSleep() method passes the doze

command to the time() utility method.• Comment mode on:• The code creates an instance of Command,

named doze• It then calls the static time() method, passing

doze as a parameter• See the code on the next overhead.

Page 45: Unit 22 Command Summary prepared by Kirk Scott 1

45

• public class TestCommandTimer extends TestCase • {• public void testSleep() • {• Command doze = new Command() • {• public void execute() • {• try • {• Thread.sleep(2000 + Math.round(10 * Math.random()));• } • catch (InterruptedException ignored) • {• }• }• };

• long actual = CommandTimer.time(doze);

• long expected = 2000;• long delta = 5;• assertTrue("Should be " + expected + " +/- " + delta + " ms", • expected - delta <= actual• && actual <= expected + delta);• }• }

Page 46: Unit 22 Command Summary prepared by Kirk Scott 1

46

• The point of the foregoing example code was that it created an object of the Command class named doze

• This object contained an execute() method that caused it to sleep for a random amount of time

• The line of code shown below made use of the command machinery given previously to time how long it takes for doze.execute() to run

• long actual = CommandTimer.time(doze);

Page 47: Unit 22 Command Summary prepared by Kirk Scott 1

47

Command Hooks

• This section builds on material that was presented in the chapter on the Template Method

• You may recall that in that chapter I restricted myself to the discussion of sorting as implemented in the Java API

• I did not pursue the other examples• Therefore, it is not possible to pursue this

example in this chapter• You are not responsible for it

Page 48: Unit 22 Command Summary prepared by Kirk Scott 1

48

Command in Relation to Other Patterns

• The book states that command is similar to a pattern in which a client knows when an action is required but doesn’t know exactly which operation to call.

• This may be true, but this is not exactly what the examples in this chapter have illustrated.

Page 49: Unit 22 Command Summary prepared by Kirk Scott 1

49

• In the menu example, there was uncertainty about the timing of the call (sometime in the future) but which actionPerformed() method was linked to which item was well defined

• In the timing example there was also no doubt about what to do—just a need to encase a call to it in other predefined blocks of code

Page 50: Unit 22 Command Summary prepared by Kirk Scott 1

50

• Challenge 24.5• Which pattern addresses the situation in

which a client knows when to create an object but doesn’t know which class to instantiate?

Page 51: Unit 22 Command Summary prepared by Kirk Scott 1

51

• Comment mode on:• This challenge introduces yet another slight

difference between the thing they’re talking about and the examples they’re giving:

• Now we’re talking about the creation of an object.

• Since we don’t know what to create, this must mean calling a “creational” method rather than calling a constructor.

Page 52: Unit 22 Command Summary prepared by Kirk Scott 1

52

• Solution 24.5• In Factory Method, a client knows when to

create a new object but not what kind of object to create.

• Factory Method moves object creation to a method that isolates a client from knowing which class to instantiate.

• This principle also occurs in Abstract Factory.

Page 53: Unit 22 Command Summary prepared by Kirk Scott 1

53

• Comment mode on:• The foregoing observation is certainly true• After a while all of the patterns begin to look

similar• In particular, you observe a “meta pattern” that

there are some patterns that work with vanilla methods, and there are other analogous patterns that work with methods that create objects

Page 54: Unit 22 Command Summary prepared by Kirk Scott 1

54

An Example using Command with other Patterns

• The Command design pattern can be used with other design patterns

• The book gives code showing Command and Mediator being used together in a MVC design for their Visualization program

• MVC hasn’t been covered yet.• Still, the example can be covered as a

combination of mediator and command.

Page 55: Unit 22 Command Summary prepared by Kirk Scott 1

55

• In this example the mediator class is responsible for returning instances of different kinds of listeners, as needed, for the buttons created in the application

• The example code which will be given next shows part of the controller portion of an MVC design

• It’s the creation of a graphical button in a visualization with a listener attached to it

Page 56: Unit 22 Command Summary prepared by Kirk Scott 1

56

• The code contains a call like this: mediator.undoAction()

• This doesn’t “undo an action” on the mediator• It returns an “undo listener” which the

mediator is responsible for keeping track of

Page 57: Unit 22 Command Summary prepared by Kirk Scott 1

57

• The listener returned is used as an explicit parameter in this call: undoButton.addActionListener()

• This is the point where the command pattern is in evidence

• Attaching a listener to a button is analogous to attaching a listener to a menu item

• This example is analogous to the first example in the unit except that the listener comes from the mediator

Page 58: Unit 22 Command Summary prepared by Kirk Scott 1

58

• protected JButton undoButton() • {• if (undoButton == null) • {• undoButton = ui.createButtonCancel();• undoButton.setText("Undo");• undoButton.setEnabled(false);• undoButton.addActionListener(mediator.undoAction());• }• return undoButton;• }

Page 59: Unit 22 Command Summary prepared by Kirk Scott 1

59

Other Code for this Example

• If you go to the book’s Web site you can find the code for the VisMediator class

• Details of that class are not of interest at the moment

• All you need to know is that it contains implementations of methods like undoAction(), which return listeners

• Methods of this kind with different names return different kinds of listeners

Page 60: Unit 22 Command Summary prepared by Kirk Scott 1

60

actionPerformed() is the Command that is Packaged in a Listener

• When the listener is added to the button, the command that is being packaged up in the object is actionPerformed()

• Different versions of actionPerformed() contain calls to methods (commands) that do different things

• These commands are ultimately what is triggered by this use of the Command design pattern

Page 61: Unit 22 Command Summary prepared by Kirk Scott 1

61

An Extraneous Challenge

• Challenge 24.6• Which pattern provides for the storage and

restoration of an object’s state?• Comment mode on:• It’s not possible to answer the question

because I’m doing the patterns in an order different from the book’s.

• The answer is the memento pattern.

Page 62: Unit 22 Command Summary prepared by Kirk Scott 1

62

Another Example

• There is no need to go into the details of the other example

• It is mentioned just because it is built on the examples for the previous two patterns, factory method and abstract factory

Page 63: Unit 22 Command Summary prepared by Kirk Scott 1

63

Page 64: Unit 22 Command Summary prepared by Kirk Scott 1

64

UML for the Pattern

• A progression of UML diagrams for the pattern is shown on the following overheads

• The first diagram shows the simple roots of the pattern

• The second shows the direct use of a command• The third shows that in reality, a given context

could make similar use of many different commands

Page 65: Unit 22 Command Summary prepared by Kirk Scott 1

65

Page 66: Unit 22 Command Summary prepared by Kirk Scott 1

66

Page 67: Unit 22 Command Summary prepared by Kirk Scott 1

67

Page 68: Unit 22 Command Summary prepared by Kirk Scott 1

68

Lasater’s UML for the Pattern

• Lasater’s diagram attempts to show that a generic execute() command is really just a wrapper for a call to some action that is significant within the problem domain or context.

Page 69: Unit 22 Command Summary prepared by Kirk Scott 1

69

Page 70: Unit 22 Command Summary prepared by Kirk Scott 1

70

Summary

• The Command design pattern makes it possible to encapsulate a request for service in an object

• In other words, calls to methods can be managed by passing around objects which can receive the calls

• The actual calls can be made at the time needed or when necessary conditions have been met

Page 71: Unit 22 Command Summary prepared by Kirk Scott 1

71

• The Command design pattern is illustrated by the way menus are constructed in Java

• The listener for an item is an object which can have the actionPerformed() method called on it

• The listener is passed to the item as an explicit parameter in a method call

• actionPerformed() is actually executed only at the time that the item in the menu is selected

Page 72: Unit 22 Command Summary prepared by Kirk Scott 1

72

• The second example illustrated how the Command design pattern can be used to embed a call between blocks of code

• The third example in the book was not pursued, and you are not responsible for it

• That example happened to illustrate that the command pattern can be used to accomplish things that the Template Method design pattern can also be used to accomplish

Page 73: Unit 22 Command Summary prepared by Kirk Scott 1

73

• The idea of passing an object as a parameter and then calling methods on the object in the body of the receiving method is very basic

• You have written code that works this way many times without thinking about it or trying to classify it as some sort of pattern

Page 74: Unit 22 Command Summary prepared by Kirk Scott 1

74

• It takes on the meaning of a design pattern depending on the context in which it’s used

• In practice, the command pattern doesn’t necessarily appear in isolation

• It may appear in a more complex design in combination with the Mediator, Memento, and other design patterns

Page 75: Unit 22 Command Summary prepared by Kirk Scott 1

75

The End

Page 76: Unit 22 Command Summary prepared by Kirk Scott 1

76

• The UML diagram on the following overhead has to do with the section of the chapter that wasn’t covered.

• It is included here after the end for future reference in case it’s needed.

Page 77: Unit 22 Command Summary prepared by Kirk Scott 1

77

Page 78: Unit 22 Command Summary prepared by Kirk Scott 1

78

• Solution 24.6• The intent of the Memento pattern is to

provide storage and restoration of an object’s state. Typically, you can add a new memento to a stack with each execution of a command, popping and reapplying these mementos when a user needs to undo commands.

Page 79: Unit 22 Command Summary prepared by Kirk Scott 1

79

• public ActionListener addAction() • {• return new ActionListener() • {• public void actionPerformed(ActionEvent e) • {• VisMediator.this.add(e);• }• };• }• public ActionListener undoAction() • {• return new ActionListener() • {• public void actionPerformed(ActionEvent e) • {• VisMediator.this.undo(e);• }• };• }

Page 80: Unit 22 Command Summary prepared by Kirk Scott 1

80

• public MouseListener mouseDownAction() • {• return new MouseAdapter() • {• public void mousePressed(MouseEvent e) • {• VisMediator.this.mouseDown(e);• }

• public void mouseReleased(MouseEvent e) • {• VisMediator.this.mouseUp(e);• }• };• }• public MouseMotionListener mouseMotionAction() • {• return new MouseMotionListener() • {• public void mouseDragged(MouseEvent e) • {• VisMediator.this.mouseMove(e);• }

• public void mouseMoved(MouseEvent e) • {• }• };• }

Page 81: Unit 22 Command Summary prepared by Kirk Scott 1

81

• package com.oozinoz.visualization;

• /*• * Copyright (c) 2001, 2005. Steven J. Metsker.• * • * Steve Metsker makes no representations or warranties about• * the fitness of this software for any particular purpose, • * including the implied warranty of merchantability.• *• * Please use this software as you wish with the sole• * restriction that you may not claim that you wrote it.• */

• import java.awt.Component;• import java.awt.Cursor;• import java.awt.Point;• import java.awt.event.ActionEvent;• import java.awt.event.ActionListener;• import java.awt.event.MouseAdapter;• import java.awt.event.MouseEvent;• import java.awt.event.MouseListener;• import java.awt.event.MouseMotionListener;• import java.io.FileInputStream;• import java.io.FileOutputStream;• import java.io.ObjectInputStream;• import java.io.ObjectOutputStream;• import java.util.ArrayList;

• import javax.swing.JFileChooser;

Page 82: Unit 22 Command Summary prepared by Kirk Scott 1

82

• /** • * This class handles the UI events for the Visualization class• */• public class VisMediator {• public static final Point DEFAULT_LOCATION = new Point(10, 10);

• protected int initX;

• protected int initY;

• protected Point initLocation;

• Cursor originalCursor;

• protected boolean isMouseDown = false;

• protected FactoryModel factoryModel;

• public VisMediator(FactoryModel m) {• factoryModel = m;• }

• public ActionListener addAction() {• return new ActionListener() {• public void actionPerformed(ActionEvent e) {• VisMediator.this.add(e);• }• };• }

Page 83: Unit 22 Command Summary prepared by Kirk Scott 1

83

• public ActionListener undoAction() {• return new ActionListener() {• public void actionPerformed(ActionEvent e) {• VisMediator.this.undo(e);• }• };• }

• public MouseListener mouseDownAction() {• return new MouseAdapter() {• public void mousePressed(MouseEvent e) {• VisMediator.this.mouseDown(e);• }

• public void mouseReleased(MouseEvent e) {• VisMediator.this.mouseUp(e);• }• };• }

• public MouseMotionListener mouseMotionAction() {• return new MouseMotionListener() {• public void mouseDragged(MouseEvent e) {• VisMediator.this.mouseMove(e);• }

• public void mouseMoved(MouseEvent e) {• }• };• }

Page 84: Unit 22 Command Summary prepared by Kirk Scott 1

84

• private void add(ActionEvent e) {• factoryModel.add(DEFAULT_LOCATION);• }

• private void undo(ActionEvent e) {• factoryModel.undo();• }

• // A click on a picture box• private void mouseDown(MouseEvent e) {• if (e.getButton() != MouseEvent.BUTTON1)• return;

• Component source = (Component) e.getSource();

• Component parent = source.getParent();• originalCursor = parent.getCursor();• parent.setCursor(new Cursor(Cursor.MOVE_CURSOR));

• initLocation = source.getLocation();• initX = e.getX();• initY = e.getY();

• isMouseDown = true;• }

• // A drag while a picture box is clicked• private void mouseMove(MouseEvent e) {• if (!isMouseDown)• return;• }

Page 85: Unit 22 Command Summary prepared by Kirk Scott 1

85

• // Release picture box. Let the factory model know about this change.• private void mouseUp(MouseEvent e) {• if (e.getButton() != MouseEvent.BUTTON1)• return;

• Component parent = ((Component) e.getSource()).getParent();• parent.setCursor(originalCursor);

• isMouseDown = false;• factoryModel.drag(initLocation, new Point(initLocation.x + e.getX()• - initX, initLocation.y + e.getY() - initY));• }

• // User clicked "Save As..." menu item• public void save(Component source) throws Exception {• JFileChooser dialog = new JFileChooser();• dialog.showSaveDialog(source);

• if (dialog.getSelectedFile() == null)• return;

• FileOutputStream out = null;• ObjectOutputStream s = null;• try {• out = new FileOutputStream(dialog.getSelectedFile());• s = new ObjectOutputStream(out);• s.writeObject(factoryModel.getLocations());• } finally {• if (s != null)• s.close();• }• }

Page 86: Unit 22 Command Summary prepared by Kirk Scott 1

86

• // User clicked "Restore from..." menu item• public void restore(Component source) throws Exception {• JFileChooser dialog = new JFileChooser();• dialog.showOpenDialog(source);

• if (dialog.getSelectedFile() == null)• return;

• FileInputStream out = null;• ObjectInputStream s = null;• try {• out = new FileInputStream(dialog.getSelectedFile());• s = new ObjectInputStream(out);• ArrayList list = (ArrayList) s.readObject();• factoryModel.setLocations(list);• } finally {• if (s != null)• s.close();• }• }• }