Object-Oriented Software Engineering PersonGui (Mark 2) Case Study (cntd)
Contents PersonGui (Mark 2) Class Daigram PersonMaker Class Handling user input with dialogs Sequence Diagram for Dailogs Show Person Attributes Sequence Diagram Person Class Diagram Person Constructor Sequence Diagram JFileChooser
Class Diagram JPanel JTextArea ActionListener JToolBar JScrollPane <<interface>> ActionListener JTextArea 1 JToolBar 1 JScrollPane PersonGui 1 JTextField 1 JFrame 1 <<abstract>> MouseAdapter PersonMaker 1 1 * PopupListener Person
<<interface>> The PersonMaker Class JPanel <<interface>> ActionListener JToolBar PersonGui PersonMaker Basic classes defining application Person
PersonMaker Class PersonGui Mark 1 Could only display one set of attributes. Only needed one instance of Person class.
PersonMaker Class PersonGui Mark 2 Creates many instances of Person. PersonMaker manages this functionality on behalf of the PersonGui class
PersonMaker Use Case user Define Age :PersonGui dude:PersonMaker setAge(ageString);
PersonMaker setAge method public void setAge (String stringAge) { string_age = stringAge; try { age = new Double(stringAge); } catch (NumberFormatException exp1) { string_age = handleTextInput("Age"); //Why could this result in an error? age = new Double(string_age); } instance field, stores original string so that if numeric value is flawed can still refer to original during lifetime of object Attempt to convert string to numeric value of type Double. Exception causes dialog box via handleTextInput. See Lecture 5 for exception handling. Full code available on web site
PersonMaker handleTextInput public String handleTextInput (String dis_text) { String s = (String)JOptionPane.showInputDialog( null, dis_text, "PersonMaker Class", JOptionPane.PLAIN_MESSAGE, "Fluffy"); return s; }
PersonMaker handleTextInput JOptionPane.showInputDialog causes this dialog to appear
handleTextInput Sequence Diagram Option Dialog has anonymous buttons Static method, invoked on class not object dude: PersonMaker :JButton JOptionPane showInputDialog( ) left_mouse_click e:Action Event <<create>> notify(e ) query event String s Represents approximately events that occur in handleTextInput
<<include>> PersonMaker Use Case PersonGui dude:Person Make_Person( ) user text Display Attributes in seperate JFrame Choose Image for Person <<include>>
containing attributes public String Make_Person ( ) { Show_Attributes_InFrame ( ); return "This Person has these attributes: \n " + "\n\tFirst Name \t= " + getFirstName( ) + "\n\tLast Name \t= " + getLastName( ) + "\n\tHeight \t= " + getPersonHeight( ) + "\n\tAge \t= " + getPersonAge( ) + "\n"; } Calls internal method to construct JFrame containing attributes Returns composite string of attributes to be displayed in JTextArea
Show_Attributes_InFrame newContentPane :Person dude:Person frame:JFrame <<create>> Content Pane of JFrame is instance of Person Class. (This class is responsible for choice of image.) <<create>> setContentPane (newContentPane) pack( ) setVisible(true) Three classes used to create JFrame and its contents. Shows contents of JFrame can be completely separate from its container
<<interface>> Person Class Diagram <<interface>> ActionListener JPanel Person GridBagLayout File JFileChooser Depends on these classes. I.e. has local fields that are instances of these classes. See lecture 6 for more details.
Person Constructor Sequence Diagram file:File <<create>> fc:JFileChooser <<create>> getSelectedFile( ) file label_drag: JLabel <<create>> from dragon dragon: ImageIcon <<create>> from imgURL imgURL: java.net.URL <<create>> from icon_file_name getCanonicalPath( ) icon_file_name Finally add new JLabel to GridBagLayout. Note this is only approximate description.
Creating an ImageIcon When creating ImageIcon from a string value, need to use different code than that of the createImageIcon method try { java.net.URL imgURL = new URL("file:///"+icon_file_name); dragon = new ImageIcon(imgURL, "new icon image"); label_drag = new JLabel(dragon); } catch (java.net.MalformedURLException exp1) { System.out.println("MalformedURLException : \n" + icon_file_name + "\n====\n"); } Section of code in Person constructor that makes an ImageIcon from the string given by the JFileChooser
The JFileChooser Class int returnVal = fc.showOpenDialog(Person.this); Returns an integer. Also uses static fields to store data about choice of file for later use.
JFileChooser Class Diagram JComponent JFileChooser public static int APPROVE_OPTION public static int CANCEL_OPTION public void showOpenDialog(Component parent) public File getSelectedFile( ) (Only some of the fields and methods are shown here)
JFileChooser Constructor JFileChooser(File currentDirectory) Constructs a JFileChooser using the given File as the path. JFileChooser() Constructs a JFileChooser pointing to the user's default directory. String cwd = "images"; file = new File(cwd); if (file != null) { fc = new JFileChooser(file); } else { fc = new JFileChooser(); }
The approve option int returnVal = fc.showOpenDialog(Person.this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); // now do something with the file // in our case create an icon image from it }
The cancel option int returnVal = fc.showOpenDialog(Person.this); if (returnVal == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); // now do something with the file // in our case create an icon image from it } else if (returnVal == JFileChooser.CANCEL_OPTION ) { // now do something in case where user // cancels selection }