JMU JMU - Department of Computer Science
Help Tools
Lab: Gaining Experience with Modifying an Existing GUI


Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.

Getting Ready: Before going any further, you should:

  1. Setup your development environment.
  2. Download the following files:
    to an appropriate directory/folder. (In most browsers/OSs, the easiest way to do this is by right-clicking/control-clicking on each of the links above and then selecting Save as... or Save link as....)
  3. This lab is a simulation of a part of the process that is being used to create a product named Wherez for the (hypothetical) company Nearby. The team is using an incremental process known as Scrum, which divides the work up into sprints.

    If you have not already done so, you must complete the lab on serialization before you can start this lab.

1. Understanding an Existing GUI: This part of the lab will give you experience understanding an existing GUI.
  1. Review the WherezWindow class, paying particular attention to the sections that involve the menu bar.
  2. Review the WherezController class, paying particular attention to the sections of the actionPerformed() method that involve the menu bar.
  3. Review the read() method in the WherezController class, paying particular attention to how the FileNameExtensionFilter is used and to how geocoding is enabled after the files are read.
2. Tasks: Recall that in the earlier lab you did not satisfy all of the acceptance criteria. The remaining aspects of the sprintable stories have been decomposed into the following tasks.
  1. Copy Open.png into the project (from a file explorer into Eclipse).
  2. Add the "constants" OPEN and WRITE to the WherezWindow class.
      static final String OPEN           = "Open";
      static final String WRITE          = "Write";	  
    
    Expand
  3. Add an OPEN JButton (with the icon Open.png) to the left of the GEOCODE button.
      private JButton            geocodeButton, openButton;
    
      // ...
      
      openButton    = createJButton("Open.png",    OPEN);
    
      //
    
      toolBar.add(openButton);
    
    Expand
  4. Add an OPEN JMenuItem to the FILE menu.
          item = new JMenuItem(OPEN);
          item.addActionListener(controller);
          menu.add(item);
    
    Expand
  5. Add the WRITE JMenuItem to the UTILITIES menu (and disable it in an appropriate place).
      private JMenuItem          writeItem;
    
      // ...
    	  
      item = new JMenuItem(WRITE);
      writeItem = item;
      writeItem.setEnabled(false);
      item.addActionListener(controller);
      menu.add(item);
    
    
    Expand
  6. Why does the WRITE JMenuItem need to be an attribute and not a local variable?
    Because it will need to be enabled by the WherezController after the .seg and .int file are read.
    Expand
  7. Add a method with the following signature to the WherezWindow class:
        public void enableWrite(boolean enable)
        

    that can be used to disable/enable writeItem.

      public void enableWrite(boolean enable)
      {
        writeItem.setEnabled(enable);
      }
    
    Expand
  8. Declare and instantiate a FileNameExtensionFilter in the WherezController class that can be used for .snk files.
        private FileNameExtensionFilter snkFilter;
    
        // ...
          
        snkFilter = new FileNameExtensionFilter(DATA_FILES, "snk");
    
    Expand
  9. Add a write() method to the WherezController class (modeled after the read() method) that can be called in response to WRITE requests.
     private void write()
      {
        fileChooser.setFileFilter(snkFilter);
        fileChooser.setDialogTitle(WRITE);
        fileChooser.setSelectedFile(new File(window.getFileName() + ".snk"));
        int result = fileChooser.showSaveDialog(null);
        if (result == JFileChooser.APPROVE_OPTION)
        {
          String path = fileChooser.getSelectedFile().getPath();
          String extension = snkFilter.getExtensions()[0];
          if (path.endsWith(extension))
          {
            String name = path.substring(0, path.length()-4);
            try
            {
              network.write(name);
            }
            catch (IOException ioe)
            {
              JOptionPane.showMessageDialog(null, ERROR_OPENING_FILE, ERROR, JOptionPane.ERROR_MESSAGE);
            }
          }
          else
          {
            JOptionPane.showMessageDialog(null, INVALID_FILE_TYPE, ERROR, JOptionPane.ERROR_MESSAGE);
          }
        }
      }	  
    
    Expand
  10. Add code to the actionPerformed() method in the WherezController class that calls the write() method in response to WRITE requests.
          else if (ac.equals(WRITE))          write();
    
    Expand
  11. Add an open() method to the WherezController class (modeled after the read() method) that can be called in response to OPEN requests.
      private void open()
      {
        fileChooser.setFileFilter(snkFilter);
        String currentStreetNetwork = window.getFileName();
        setStreetNetworkIsAvailable(null);
        int result = fileChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) // Retrieve a StreetNetwork
        {
          String path = fileChooser.getSelectedFile().getPath();
          String extension = snkFilter.getExtensions()[0];
          if (path.endsWith(extension))
          {
            String name = path.substring(0, path.length()-4);
            try
            {
              setStreetNetwork(StreetNetwork.open(name));
              geocoder = new AddressGeocoder(network);
    
              setStreetNetworkIsAvailable(name);
            }
            catch (IOException ioe)
            {
              JOptionPane.showMessageDialog(null, ERROR_OPENING_FILE, ERROR, JOptionPane.ERROR_MESSAGE);
            }
          }
          else
          {
            JOptionPane.showMessageDialog(null, INVALID_FILE_TYPE, ERROR, JOptionPane.ERROR_MESSAGE);
          }
        }
        else // Continue to use the current StreetNetwork
        {
          setStreetNetworkIsAvailable(currentStreetNetwork);
        }
      }
    
    Expand
  12. Add code to the actionPerformed() method in the WherezController class that calls the open() method in response to OPEN requests.
        else if (ac.equals(OPEN))           open();
    
    Expand
  13. Add code to the setStreetNetworkIsAvailable() method in the WherezController class that enables the WRITE menu item.
       window.enableWrite(available);
     
    Expand
  14. Test this new functionality.
  15. Refactor your code to eliminate duplication in the read() and open() methods.
      // Replace read() and open() with the following load() method.
    
      private void load(FileNameExtensionFilter filter, String title)
      {
        fileChooser.setFileFilter(filter);
        fileChooser.setDialogTitle(title);
        String currentStreetNetwork = window.getFileName();
        setStreetNetworkIsAvailable(null);
        int result = fileChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) // Retrieve a StreetNetwork
        {
          String path = fileChooser.getSelectedFile().getPath();
          String extension = filter.getExtensions()[0];
          if (path.endsWith(extension))
          {
            String name = path.substring(0, path.length()-4);
            try
            {
              if (extension.equals("snk")) setStreetNetwork(StreetNetwork.open(name));
              else                         setStreetNetwork(StreetNetwork.read(name));
              geocoder = new AddressGeocoder(network);
    
              setStreetNetworkIsAvailable(name);
            }
            catch (IOException ioe)
            {
              JOptionPane.showMessageDialog(null, ERROR_OPENING_FILE, ERROR, JOptionPane.ERROR_MESSAGE);
            }
          }
          else
          {
            JOptionPane.showMessageDialog(null, INVALID_FILE_TYPE, ERROR, JOptionPane.ERROR_MESSAGE);
          }
        }
        else // Continue to use the existing StreetNetwork
        {
          setStreetNetworkIsAvailable(currentStreetNetwork);
        }
      }
    
      // Change actionPerformed() as follows
    
      else if (ac.equals(OPEN))           load(snkFilter, OPEN);
      else if (ac.equals(READ))           load(segFilter, READ);
    
     
    Expand

Copyright 2021