Java Reference
In-Depth Information
"Confirm Save As",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE)) {
return;
// ...do nothing
}
if(saveSketch(file)) { // Save the sketch
currentSketchFile = file; // Save successful
setTitle(frameTitle + " - " + currentSketchFile); // Update title bar
sketchChanged = false;
// Sketch now unchanged
}
}
Directory "Sketcher 1 saving a sketch to a file"
You first check the
sketchChanged
flag. If the flag is
false
, either the sketch is empty or it hasn't been
changed since the last save. Either way, there's no point in writing the sketch to disk, so you return immedi-
ately.
If the
sketchChanged
flag is
true
, you check the reference stored in
currentSketchFile
. If it is
not
null
, then you just call the
saveSketch()
method to write the sketch to the file specified by
cur-
rentSketchFile
— you will implement the
saveSketch()
method in a moment. You don't need to update
the title bar here because it already shows the sketch file path.
The sketch was never saved if
currentSketchFile
is
null
, in which case you display the file chooser
dialog with the default file name selected. If the
showDialog()
method returns
null
, then the dialog was
closed without choosing a file so you end the save operation.
You also need to check whether the file exists. If it does, you want to give the user the option not to over-
write it with the current sketch. The condition tested in the
if
looks rather complicated, but this is primarily
due to the plethora of arguments in the
showConfirmDialog()
call. You can break the condition down into
its component parts quite easily. The condition comprises two logical expressions separated by the
&&
op-
erator. So if both expressions are
true
then you execute a
return
. The first expression results in
false
if
file
does not exist, so if this is the case, you return immediately.
When
file
references a file that already exists, , the second expression in the
if
is evaluated, otherwise
it isn't. The second expression is
true
if the value returned from the
showConfirmDialog()
method
is
JOptionPane.NO_OPTION
. The confirm dialog just warns of the overwrite potential, so if
JOp-
tionPane.NO_OPTION
is returned, then the user has elected not to overwrite the file.
Remember that if the left operand for the
&&
operator is
false
, then the right operand is not evaluated.
This means that the
showConfirmDialog()
method is called only when
file
references a file that already
exists. Thus if the NO option is chosen in the dialog, the return is executed and the sketch is not saved. If
the YES option is selected, then you don't execute the return statement in the
if
block, but you continue to
execute the code that follows, which calls
saveSketch()
with
file
as the argument. After successfully sav-
ing the sketch, you update
currentSketchFile
to contain the new file path, set
sketchChanged
to
false
because the sketch has not yet been changed relative to the file contents, and record the path for the current
sketch file in the title bar for the application window.
If
showDialog()
returns a non-
null
reference, the path should end in
.ske
. It won't if the user keys a
new name for the file in the dialog without the
.ske
extension. The user might have added their own exten-
sion, in which case you want to leave it as it is. However, if the path string does not end in
.ske
and there
is no other extension (indicated by the presence of a period in the file name), you should append
.ske
. You
achieve this by calling the following method: