Top Banner
Object-Oriented Software Engineering PersonGui (Mark 2) Case Study
23

Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

Object-Oriented Software Engineering

PersonGui (Mark 2)

Case Study

Page 2: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Contents

• PersonGui Mark 2 Case Study Example Overview

• Use Cases

• Class Diagram

• Popup Listeners

• Sequence Diagrams for Popup Listeners

• Internal Class Definitions

• Mouse Adaptors

• Dynamics of Mouse Events

Page 3: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

PersonGui (Mark 2)

http://www.computing.surrey.ac.uk/courses/cs288/Examples/PersonGui

Mark 2will be a fullyfunctional GUI

Page 4: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Use CasesDefine Age

Define Heightuser

Define First Name

Define Last Name

Display Attributes in seperate JFrame

Choose Imagefor Person

<<include>>

Choose BackgroundColour

<<include>>

Choose Font

<<include>>

Choose GUI Display Settings

Page 5: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

PersonGui Mark 2

PersonGui

PersonMaker

Person

<<interface>>

ActionListener

JPanel

JFrame

JScrollPane

JTextField

JToolBar

PopupListener

<<abstract>>

MouseAdapter

1

1

1

1

1

1

*

JTextArea1

Depends on relationE.g. local variables ofthis class type

Page 6: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

The PopupListener Class

In this example we choose to implement most of thefunctionality with a pop-up menu. This is triggered bythe right mouse button.

We will implement this as an internal class to PersonGui.

PopupListenerclass instance

Page 7: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Create and Show GUI

createAndShowGUI()

Recall that all our Java GUI-s appear on screen due to the execution of the

method.

This • creates a JFrame• creates a new content pane, an instance of

PersonGui• attaches the content pane to the JFrame

When PersonGui is created, it creates an instanceof the PopupListener and attaches the menu optionsto it.

Page 8: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

PersonGui Sequence Diagram

:PersonGui :Event Dispatcher

frame:JFrame

createAndShowGUI

<<creates>>

Note:It is not strictlycorrect to say theEvent Dispatcherexecutes the method.Rather is creates a separate object that executes the code.For simplicity we willignore this part.

To reduceclutter not showing dashedreturn arrow formethod calls

Page 9: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

createAndShowGUI ( ) (cntd)

:Event DispatchernewContentPane

:PersonGui

<<creates>>

frame:JFrame

setContentPane(newContentPane)

createPopupMenu( )pack( )

setVisible(true)

Page 10: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

createPopupMenu( ) sequence diagram

newContentPane:PersonGui

popup:JPopupMenu

<<creates>>

menuItem:JMenuItem

<<creates>>

addActionListener(this)

add(menuItem)

loop

This givesabstract descriptionof adding menu itemswithin a loop.In the implementationthere is no loop each menuitem has separate code.

Page 11: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

createPopupMenu( ) sequence diagram (ctd)

newContentPane:PersonGui

submenu:JMenu

<<creates>>

menuItem:JMenuItem

<<creates>>

addActionListener(this)

add(menuItem)

loop

popup:JPopupMenu

Page 12: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

createPopupMenu( ) sequence diagram (ctd)

newContentPane:PersonGui

textArea:JTextArea

popup:JPopupMenu

add(submenu)

addMouseListener(popupListener)

popupListener:PopupListener

<<creates>>

Page 13: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Menu Items on PopupListener

menuItemused to createeach of thesemenu items in turn,and then add themto PopupListener

Then menuItemused to createeach of thesemenu items in turn,and then add themto the submenu.

Page 14: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Internal Class Definitions in JavaUntil now each class has been defined within a separatefile.

An internal class is one whose code is contained in thebody of another class.

This is convenient for a class that is useful only in thecontext of its containing class.

An internal class is an easy way of packaging up someobject characteristics that make more sense kept separatefrom the rest of the containing object's fields.

In this case we can clearly see that a popup menu is a separate entity, but only useful within the PersonGuiclass.

Page 15: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Internal Class Definitions in Java

public class PersonGui extends JPanel implements ActionListener {

// methods defined here// can create instances of PopupListener // just as with any other classPopupListener foo_variable = new PopupListener ( );

// can access methods for foo_variable just as// with any other class foo_variable.someMethod(someParameters);

class PopupListener extends MouseAdapter {// methods defined here}

}

Page 16: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

class PopupListener extends MouseAdapter { JPopupMenu popup;

PopupListener(JPopupMenu popupMenu) { popup = popupMenu; }

public void mousePressed(MouseEvent e) { maybeShowPopup(e); }

public void mouseReleased(MouseEvent e) { maybeShowPopup(e); }

private void maybeShowPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(e.getComponent(), e.getX(), e.getY()); } } }

Page 17: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

MouseAdapter

The MouseAdapter is an abstract class with empty methods.Hence, extending this class only requires that we implementthose mouse event handling methods that we need.

If MouseAdapter were an interface, we would have to implementevery method it defines, even though many of them would beempty.

Page 18: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

When a Right Mouse Click Occurs

textArea:JTextArea popupListener:

e:MouseEvent

<<create>>

e.getComponent()

notify(e )

right_mouse_click

e.getX()e.getY()

show

popup:JPopupMenu

Page 19: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

...and then a Left Mouse Click Occurs on a Menu Item

:JMenuItem :PersonGui

e:ActionEvent

<<create>>

String cmd = e.getActionCommand();

notify(e )

left_mouse_click

Page 20: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

and finally PersonGui invokes actionPerformed(ActionEvent e)

:PersonGui

else if (AGE.equals(cmd)) { // second button clicked ageString = textField.getText();

dude.setAge(ageString); description = "The age of the person in: " + ageString + newline; textField.setText(""); }

textField: dude:Person

setAge(ageString);

setText("")

getText()

ageString

Same principle as for Mark 1

Page 21: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

actionPerformed(ActionEvent e): Choosing Colors

else if(POPUP_7.equals(cmd)) { //Bring up color chooser

Color newColor = JColorChooser.showDialog(

PersonGui.this, "Choose Background Color", textArea.getBackground());

textArea.setBackground(newColor);}

:PersonGui textArea: :JColorChooser

setBackground

showDialog

newColor

Page 22: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

Page 23: Object-Oriented Software Engineering PersonGui (Mark 2) Case Study.

UniS

actionPerformed(ActionEvent e): creating person object

PersonGui

if (PRINT_ATTRIBUTES.equals(cmd)) { //first button clicked String text = dude.Make_Person();

description = text + newline;

}

dude:Person

Make_Person( )

text

Note:It is the responsibilityof the dude objectto create a JFrameto display the person