Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object-Oriented Software Engineering PersonGui (Mark 2) Case Study."— Presentation transcript:

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

2 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

3 UniS PersonGui (Mark 2) http://www.computing.surrey.ac.uk/courses/cs288/Examples/PersonGui Mark 2 will be a fully functional GUI

4 UniS Use Cases Define AgeDefine Height user Define First Name Define Last Name Display Attributes in seperate JFrame Choose Image for Person > Choose Background Colour > Choose Font > Choose GUI Display Settings

5 UniS PersonGui Mark 2 PersonGui PersonMaker Person > ActionListener JPanel JFrame JScrollPane JTextField JToolBar PopupListener > MouseAdapter 1 1 1 1 1 1 * JTextArea 1 Depends on relation E.g. local variables of this class type

6 UniS The PopupListener Class In this example we choose to implement most of the functionality with a pop-up menu. This is triggered by the right mouse button. We will implement this as an internal class to PersonGui. PopupListener class instance

7 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 instance of the PopupListener and attaches the menu options to it.

8 UniS PersonGui Sequence Diagram :PersonGui :Event Dispatcherframe:JFrame createAndShowGUI > Note: It is not strictly correct to say the Event Dispatcher executes the method. Rather is creates a separate object that executes the code. For simplicity we will ignore this part. To reduce clutter not showing dashed return arrow for method calls

9 UniS createAndShowGUI ( ) (cntd) :Event Dispatcher newContentPane :PersonGui > frame:JFrame setContentPane(newContentPane) createPopupMenu( ) pack( ) setVisible(true)

10 UniS createPopupMenu( ) sequence diagram newContentPane :PersonGui popup :JPopupMenu > menuItem :JMenuItem > addActionListener(this) add(menuItem) loop This gives abstract description of adding menu items within a loop. In the implementation there is no loop each menu item has separate code.

11 UniS createPopupMenu( ) sequence diagram (ctd) newContentPane :PersonGui submenu :JMenu > menuItem :JMenuItem > addActionListener(this) add(menuItem) loop popup :JPopupMenu

12 UniS createPopupMenu( ) sequence diagram (ctd) newContentPane :PersonGui textArea :JTextArea popup :JPopupMenu add(submenu) addMouseListener(popupListener) popupListener :PopupListener >

13 UniS Menu Items on PopupListener menuItem used to create each of these menu items in turn, and then add them to PopupListener Then menuItem used to create each of these menu items in turn, and then add them to the submenu.

14 UniS Internal Class Definitions in Java Until now each class has been defined within a separate file. An internal class is one whose code is contained in the body of another class. This is convenient for a class that is useful only in the context of its containing class. An internal class is an easy way of packaging up some object characteristics that make more sense kept separate from 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 PersonGui class.

15 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 class PopupListener 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 }

16 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()); }

17 UniS MouseAdapter The MouseAdapter is an abstract class with empty methods. Hence, extending this class only requires that we implement those mouse event handling methods that we need. If MouseAdapter were an interface, we would have to implement every method it defines, even though many of them would be empty.

18 UniS When a Right Mouse Click Occurs textArea:JTextArea popupListener: e:Mouse Event > e.getComponent() notify(e ) right_mouse_click e.getX() e.getY() show popup: JPopupMenu

19 UniS...and then a Left Mouse Click Occurs on a Menu Item :JMenuItem:PersonGui e:Action Event > String cmd = e.getActionCommand(); notify(e ) left_mouse_click

20 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

21 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); } :PersonGuitextArea: :JColorChooser setBackground showDialog newColor

22 UniS

23 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 responsibility of the dude object to create a JFrame to display the person


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

Similar presentations


Ads by Google