View
217
Download
3
Embed Size (px)
Citation preview
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
UniS
PersonGui (Mark 2)
http://www.computing.surrey.ac.uk/courses/cs288/Examples/PersonGui
Mark 2will be a fullyfunctional GUI
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
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
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
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.
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
UniS
createAndShowGUI ( ) (cntd)
:Event DispatchernewContentPane
:PersonGui
<<creates>>
frame:JFrame
setContentPane(newContentPane)
createPopupMenu( )pack( )
setVisible(true)
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.
UniS
createPopupMenu( ) sequence diagram (ctd)
newContentPane:PersonGui
submenu:JMenu
<<creates>>
menuItem:JMenuItem
<<creates>>
addActionListener(this)
add(menuItem)
loop
popup:JPopupMenu
UniS
createPopupMenu( ) sequence diagram (ctd)
newContentPane:PersonGui
textArea:JTextArea
popup:JPopupMenu
add(submenu)
addMouseListener(popupListener)
popupListener:PopupListener
<<creates>>
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.
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.
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}
}
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()); } } }
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.
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
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
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
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
UniS
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