Java Reference
In-Depth Information
are done.
}
file = setFileExtension(file, "ske");
// Make
sure extension is .ske
if(Files.exists(file) &&
!file.equals(currentSketchFile) &&
JOptionPane.NO_OPTION ==
// Overwrite
warning
JOptionPane.showConfirmDialog(this,
file.getFileName() + " exists.
Overwrite?",
"Confirm Save As",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE)) {
return;
// No file
selected
}
if(saveSketch(file)) {
// Save the
sketch
currentSketchFile = file;
// Save
successful
setTitle(frameTitle + " - " + currentSketchFile); // Update
title bar
sketchChanged = false;
// Sketch now
unchanged
}
}
Directory "Sketcher 2 with Save As capability"
If you recompile Sketcher with these additions, you have a working Save As option on the File menu.
How It Works
You have a fancy expression as the last argument to the showDialog() method. This is because the Save
As operation could be used with a sketch that has been saved previously or with a sketch that has never
been saved. The expression passes currentSketchFile as the argument if it is not null and creates a
new Path object as the argument from the default file name if currentSketchFile is null . If you get a
Path object back from the showDialog() method that is null, you know there was no file selected by the
user, so you are done.
If file is not null you must check for a potential overwrite of an existing file, and if there is a conflict
you must ask the user if he wants to continue. This is the case if the selected file exists and is also dif-
ferent from currentSketchFile . In this instance you want to display a YES/NO dialog warning of this.
Checking whether the file exists, whether it is the same as the current file, and displaying the dialog, are
all carried out in the if expression. If they are all true you just return from the method. The three checks
are combined with && operators, so from left to right the first check that results in false results in false
for the whole expression, and prevents subsequent operands from being evaluated.
Search WWH ::




Custom Search