![]()
(chat, newsreader, calendar)
|
![]() |
|
|
|
Course Notes Table of Contents Exercises | Online Training Index
|
|
Help: Saving Billboards with Casey Kasem
MageLang Institute
|
|
Help is available for each task, or you can go straight to
the solution source code.
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 2Since 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).
Task 3Update the BeanInfo so that it knows about the CustomizerOnly the getBeanDescriptor method changes.
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(
CentralPerk.class, BillboardCustomizer.class);
Task 4Compile everything in the package. There is no need to create a .jar file, unless you want to.Task 5Now 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 6Next 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 7And 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 8Finally, 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 9Compile Top40.Make sure CLASSPATH is set to find the package where CentralPerk is located. Task 10Run 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 May-97 Copyright © 1996, 1997 Sun Microsystems Inc. All Rights Reserved |