Java Reference
In-Depth Information
This should be very easy to follow now. We read the data we wrote to the stream - the
Vector
of
Float
objects. The first segment is always a move to the origin. All the succeeding segments are lines
specified by pairs of elements from the
Vector
. The
floatValue()
method for the
Float
objects
that are stored in the
Vector
object returns the numerical coordinate values. We use these to create the
line segments.
Serializing Text
Element.Text
is the last element type we have to deal with. Fortunately,
Font
,
String
, and
java.awt.Rectangle
objects are all serializable already, which means that
Element.Text
is
serializable by default and we have nothing further to do. We can now start implementing the listener
operations for the
Fi
le
menu.
Supporting the File Menu
To support the menu items in the
F
ile
menu, we must add some code to the
actionPerformed()
method
in the
FileAction
class. We can try to put a skeleton together but a problem presents itself immediately:
the ultimate source of an event will be either a toolbar button (a
JButton
object) or a menu item (a
JMenuItem
object) that was created from a
FileAction
object. How do we figure out what the action was
that originated the event? We only have one definition of the
actionPerformed()
method shared
amongst all
FileAction
class objects so we need a way to determine which particular
FileAction
object
caused the event. That way we can decide what we should do in response to the event.
Each
FileAction
object stores a
String
that was passed to the constructor as the
name
argument, and
was then passed on to the base class constructor. If only we had thought of saving it, we could compare the
name for the current object with the name for each of the
FileAction
objects in the
SketchFrame
class.
Then we could tell which object the
actionPerformed()
method was called for.
All is not lost though. We can call the
getValue()
method for the
ActionEvent
object to retrieve
the name for the action object that caused the event. We can then compare that with the name for each
of the
FileAction
objects that we store as members of the
SketchFrame
class. We can therefore
implement the
actionPerformed()
member of the
FileAction
class like this:
public void actionPerformed(ActionEvent e) {
String name = (String)getValue(NAME);
if(name.equals(saveAction.getValue(NAME))) {
// Code to handle file Save operation...
} else if(name.equals(saveAsAction.getValue(NAME))) {
// Code to handle file Save As operation...
} else if(name.equals(openAction.getValue(NAME))) {
// Code to handle file Open operation...
} else if(name.equals(newAction.getValue(NAME))) {
// Code to handle file New operation...
} if(name.equals(printAction.getValue(NAME))) {
// Code to handle Print operation..
}
}