Help is available for each task, or you can go straight to
the solution source code.
Task 1
Use the final CentralPerk.java,
CentralPerkBeanInfo.java, PerkEvent.java,
and PerkListener.java files from the
Packaging Central Perk with Marcel exercise, which we have here,
and place them in their own package. (or just leave them where they are and work from there).
Create a new subdirectory and copy the files to subdirectory. Use the same package name as the previous
exercise to keep things simple. Otherwise, you have to change the package statement in each of the files.
Task 2
Since we are going to be working in our own 'BeanBox' like application, we need to provide our own
means of updating a Bean's properties. One way is to create a Customizer for the Bean. Another is
to create a Property Sheet. For this task, create a Customizer for the Font and Message properties.
Creating a Property Sheet is left as an exercise for the student (and not necessary for this exercise).
-
Make sure the class implements the
Customizer interface.
- Starting with the
BillboardCustomizer.java skeleton,
add two labels and two text fields for font and message input.
- Make the customizer the
ActionListener for the buttons so that the event handling can be
self-contained.
- Fill in the
actionPerformed
method so that when the user updates the font, the font from the
CentralPerk Bean is updated. Do the same thing for the message.
For the font setting, use the encode method provided to display the
current font textually. Use the Font.decode method to change the font.
The name needs to be in the format of:
fontname-style-size, where fontname is Serif, SansSerif,
or Monospaced, (among others), style is plain, bold, bolditalic, or italic,
and size is an integer size.
- Normally, you would have to
add the property change listening add/remove methods. However, they
are already defined in the skeleton for you.
Task 3
Update the BeanInfo so that it knows about the Customizer
Only the getBeanDescriptor method changes.
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(
CentralPerk.class, BillboardCustomizer.class);
Task 4
Compile everything in the package. There is no need to create a .jar file,
unless you want to.
Task 5
Now the fun begins. In the Top40 framework, there are four methods to be
completed. The first is newBean which creates a new Bean. Have the
newBean method instantiate the CentralPerk Bean you've
been working on. Add it into the center quadrant of the screen, call the
checkCustomize method provided (this turns a menu choice on
if a Customizer is available) and save a reference to the Bean in the
theComp variable.
The four lines will look something like:
Component c = (Component)Beans.instantiate (
null, "Marcel.CentralPerk");
add (c, BorderLayout.CENTER);
checkCustomize(c.getClass());
theComp = c;
The Bean must be in the CLASSPATH for this to succeed. You can test
out the program if you would like.
Task 6
Next is our save method, saveBean . Through Serialization, save
the Bean to the file CentralPerk.ser. Remember that we saved a
reference to it in theComp .
Use an ObjectOutputStream and its writeObject
method to save the object.
File f = new File ("CentralPerk.ser");
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos =
new ObjectOutputStream (fos);
oos.writeObject (theComp);
oos.close();
fos.close();
Task 7
And our read method, loadBean is next. Loading is just like the
newBean method, except the object is read from an
ObjectInputStream instead of being instantiated. Don't forget
to add it to the center quadrant, call checkCustomize , and
save a reference in theComp .
Use an ObjectInputStream and its readObject method
to load the object.
File f = new File ("CentralPerk.ser");
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream (fis);
Component c = (Component)ois.readObject();
add (c, BorderLayout.CENTER);
ois.close();
fis.close();
checkCustomize(c.getClass());
theComp = c;
Task 8
Finally, when the user selects the Customize Bean menu item,
we need to display the Bean's customizer. Through the Introspector ,
you get the BeanInfo , then the BeanDescriptor , then
the CustomizerClass , and create an instance of it. After creating an
instance of it, you need to tell it which CentralPerk to customize,
via its setObject method. Now you can display it. Since the
Customizer is a Panel , we need to wrap it up in
a Dialog with a Done button. Since the
Customizer handles all its own actions, you only need to
handle the button selection to hide the dialog.
Just start with what you have, theComp , and work your way to
its Customizer .
BeanInfo bi =
Introspector.getBeanInfo (theComp.getClass());
BeanDescriptor bd = bi.getBeanDescriptor();
Class c = bd.getCustomizerClass();
Customizer customizer = (Customizer) c.newInstance();
customizer.setObject (theComp);
The actual Dialog handling code is all included for you in the skeleton.
Task 9
Compile Top40 .
Make sure CLASSPATH is set to find the package where
CentralPerk is located.
Task 10
Run the program. The first thing you have to do is load the CentralPerk
Bean. Once loaded, you can save it, open it, customize things, and quit. After
quitting and restarting, you can reopen the last saved Bean.
The solution only supports loading and saving to one file. However, you can
easily modify the program to support multiple saved beans.
Copyright © 1997 MageLang Institute. All Rights Reserved.
|
|