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:
-
Setup your development environment.
-
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....)
-
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.
-
Review the
WherezWindow
class, paying particular
attention to the sections that involve the menu bar.
-
Review the
WherezController
class, paying particular
attention to the sections of the actionPerformed()
method that involve the menu bar.
-
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.
-
Copy
Open.png
into the project (from a file explorer into
Eclipse).
-
Add the "constants"
OPEN
and WRITE
to the
WherezWindow
class.
static final String OPEN = "Open";
static final String WRITE = "Write";
-
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);
-
Add an
OPEN
JMenuItem
to the FILE
menu.
item = new JMenuItem(OPEN);
item.addActionListener(controller);
menu.add(item);
-
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);
-
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.
-
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);
}
-
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");
-
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);
}
}
}
-
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();
-
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);
}
}
-
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();
-
Add code to the
setStreetNetworkIsAvailable()
method in the
WherezController
class that enables the
WRITE
menu item.
window.enableWrite(available);
-
Test this new functionality.
-
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);