Java Reference
In-Depth Information
sketchChanged = false;
// Status is
unchanged
} catch(Exception e) {
System.err.println(e);
JOptionPane.showMessageDialog(this,
"Error reading a sketch file.",
"File Input Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
Directory "Sketcher 3 opening sketch files"
This method returns
true
if the file read operation is successful and returns
false
if an error occurs. You
pass the
SketcherModel
object that is read from the file to a new method in the
Sketcher
class,
inser-
tModel()
. This method has to replace the current sketch with the new one that is passed as the argument.
You can implement the
insertModel()
method in the
Sketcher
class like this:
// Insert a new sketch model
public void insertModel(SketcherModel newSketch) {
sketch = newSketch;
// Store the
new sketch
sketch.addObserver(view);
// Add the view
as observer
sketch.addObserver(window);
// Add the app
window as observer
view.repaint();
// Repaint the
view
}
Directory "Sketcher 3 opening sketch files"
After you have loaded the new model, you update the window title bar and record the status as unchanged
in the
SketcherFrame
object. If you compile Sketcher once more, you can give the file open operation a
workout. The Open dialog should be as shown in
Figure 21-3
.