Java Reference
In-Depth Information
"Directory Creation Failed",
JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
}
Directory "Sketcher 1 saving a sketch to a file"
You have seen this sort of code for ensuring a directory exists back in Chapter 9. This method checks
whether the directory passed as the argument exists. If it doesn't, it pops an information dialog telling the
user the directory is created. The first argument to the showMessageDialog() method is null because
the SketcherFrame object has not been created when the checkDirectory() method is called from the
SketcherFrame constructor. The null argument causes a default frame to be used to position the dialog
window.
It is possible that the createDirectories() method could fail and throw an exception. In this case, an-
other message dialog is displayed and the stack trace is recorded on the command line before terminating
Sketcher. You can test that this all works and see what the stack trace looks like by specifying a string for
DEFAULT_DIRECTORY that is not a valid path.
You can call the new method from the constructor with the following code:
public SketcherFrame (String title, Sketcher theApp) {
checkDirectory(DEFAULT_DIRECTORY);
setTitle(title); // Set the window title
frameTitle = title; // Remember original title
// Rest of the code in the constructor as before...
}
Directory "Sketcher 1 saving a sketch to a file"
After calling the method that verifies the directory is available, you record the window title in
frameTitle .
When you close Sketcher, there should be a means of checking whether the sketch needs to be saved.
Otherwise, it is all too easy to close the application and lose the brilliant sketch that you have spent many
hours crafting. Checking whether the sketch needs to be saved isn't difficult. You just need to record the fact
that the model has changed.
Recording Changes to a Sketch
To provide the means of recording whether or not a sketch has been changed, you can add a boolean field
to the SketcherFrame class that you set to true when the SketcherModel object changes and set to false
when it is in a new and original condition — as is the case when it has just been loaded or saved in a file.
Add the following data member definition to the SketcherFrame class:
Search WWH ::




Custom Search