Java Reference
In-Depth Information
2. The current sketch has never been saved, indicated by currentSketchFile being null :
• Display the file chooser dialog.
• If the dialog returns null when it closes, end the operation.
• If a non-null File reference is returned from the dialog, check whether the selected file exists:
a. If the file does not exist:
• Save the sketch with the selected file path.
• Record the file path in currentSketchFile .
• Set sketchChanged to false .
b. If the file does exist:
• Display a dialog asking if the file is to be overwritten.
• If NO, end the operation.
• If YES, write the sketch with the selected path, record the path in currentSketchFile , set
sketchChanged to false .
All the complications arise with a new sketch that has never been saved. You can package up these checks
for when you need to save and for when you should display the dialog in another method in the Sketcher-
Frame class. You can call it saveOperation() and make it a private member of the SketcherFrame class:
// Save the sketch if it is necessary
private void saveOperation() {
if(!sketchChanged) {
// If the sketch is unchanged...
return;
// ... do nothing
}
if(currentSketchFile != null) {
// If the sketch has been saved...
if(saveSketch(currentSketchFile)) {
// .. just save it.
sketchChanged = false;
// Write successful
}
return;
}
// Here, the sketch was never saved...
Path file = showDialog("Save Sketch",
// ...so display Save dialog
"Save",
"Save the sketch",
sketchFilter,
Paths.get(DEFAULT_FILENAME));
if(file == null) {
// No file selected...
return;
// ... so we are done.
}
file = setFileExtension(file, "ske");
// Make sure extension is .ske
if(Files.exists(file) &&
// If the path exists and...
JOptionPane.NO_OPTION ==
// .. NO selected in dialog...
JOptionPane.showConfirmDialog(
this,
file.getFileName() + " exists. Overwrite?",
Search WWH ::




Custom Search