In this section, you're going to add a PropertyEditor
class
associated with your NervousText
Bean. The name of the class
will be NervousText07TextPropertyEditor
. Here's the full text for
the source file:
NervousText07TextPropertyEditor.java:
package sun.beanbox.beans;
import java.beans.*;
public class NervousText07TextPropertyEditor
extends PropertyEditorSupport {
public String[] getTags() {
String values[] = {
"Nervous Text",
"Anxious Text",
"Funny Text",
"Wobbly Text"};
return values;
}
}
Defining this class is the only real work you need to do.
NervousText07
NervousText07BeanInfo
, are
also required, but these can effectively be copied and modified with a
global search and replace.
Step 1. Create NervousText07
and
NervousText07BeanInfo
classes
Copy the NervousText06.java
and
NervousText06BeanInfo.java
source files to
NervousText07.java
and NervousText07BeanInfo.java
respectively.
Do a global search and replace in both files changing NervousText06
to NervousText07
and rename the classes. Compared to adding the
BeanInfo in the previous section, adding the Property editor will be easy.
Actually, two minor changes are required for NervousText07BeanInfo.java
.
For consistency, you'll want to change the name of the Bean in the property
descriptor to distinguish it from NervousText06BeanInfo.java
.
Change:
bd.setDisplayName("Uneasy Text");
to
bd.setDisplayName("Uneasy Text 07");
The second change causes the property editor you're about to create to be
attached to instances of the BeanInfo class associated with your Bean. You'll
see how that's done at the end of the section after you've created
NervousText07TextPropertyEditor
.
Step 2. Create NervousText07TextPropertyEditor
Start by creating a file called: NervousText07TextPropertyEditor.java
.
Implement the class skeleton:
package sun.beanbox.beans;
import java.beans.*;
public class NervousText07TextPropertyEditor
extends PropertyEditorSupport {
....
}
}
Note that NervousText07TextPropertyEditor.java
extends the
PropertyEditorSupport
class that implements the PropertyEditor
interface.
Next override the getTags
method. This method simply returns a
list of strings that the property editor will display when a programmer tries
to edit the text
property of NervousText07
in a
builder tool.
public String[] getTags() {
String values[] = {
"Nervous Text",
"Anxious Text",
"Funny Text",
"Wobbly Text"};
return values;
}
Step 3. Set the PropertyEditor in the text Property's
PropertyDescriptor
Now it's time for that second change to NervousText07BeanInfo
mentioned earlier. Add the line:
textPD.setPropertyEditorClass(
NervousText07TextPropertyEditor.class);
to the getPropertyDescriptors
in NervousText07BeanInfo
.
The full definition of the class should now look like this:
public class NervousText07BeanInfo
extends SimpleBeanInfo {
private final static Class beanClass =
NervousText07.class;
public BeanDescriptor getBeanDescriptor() {
System.err.println(
"ENTER---> " +
"NervousText07BeanInfo.getBeanDescriptor");
BeanDescriptor bd = new BeanDescriptor(beanClass);
bd.setDisplayName("Uneasy Text 07");
System.err.println(
"EXIT----> " +
"NervousText07BeanInfo.getBeanDescriptor");
return bd;
}
public PropertyDescriptor[] getPropertyDescriptors() {
try {
PropertyDescriptor textPD =
new PropertyDescriptor("text", beanClass);
PropertyDescriptor rv[] = {textPD};
textPD.setPropertyEditorClass(
NervousText07TextPropertyEditor.class);
return rv;
} catch (IntrospectionException e) {
throw new Error(e.toString());
}
}
}
That's it. Now you're ready to compile.
Step 5. Compile the programs
The routine is pretty much the same as in the previous section:
javac -d . NervousText07.java
javac -d . NervousText07BeanInfo.java
javac -d . NervousText07TextPropertyEditor.java
Step 6. Build and install the JAR
The manifest should look like this:
Manifest-Version: 1.0
Name: sun/beanbox/beans/NervousText07.class
Java-Bean: True
Name: sun/beanbox/beans/NervousText07BeanInfo.class
Name:
sun/beanbox/beans/NervousText07TextPropertyEditor.class
As before, there are three classes in the JAR, but only one is a Bean.
The manifest can be created with the following set of commands. See the
makefile in the example-1.1 directory.
echo "Manifest-Version: 1.0" >
manifest.tmp
echo "" >>
manifest.tmp
echo "Name:
sun/beanbox/beans/NervousText07.class" >>
manifest.tmp
echo "Java-Bean: True" >>
manifest.tmp
echo "" >>
manifest.tmp
echo "Name:
sun/beanbox/beans/NervousText07BeanInfo.class" >>
manifest.tmp
echo "" >>
manifest.tmp
echo "Name:
sun/beanbox/beans/
NervousText07TextPropertyEditor.class" >>
manifest.tmp
jar cfm NervousText07.jar manifest.tmp \
sun/beanbox/beans/NervousText07.class \
sun/beanbox/beans/NervousText07BeanInfo.class \
sun/beanbox/beans/
NervousText07TextPropertyEditor.class
cp -p NervousText07.jar BDK_HOME/beans/jars
Be sure to subsitute your BDK installation directory for BDK_HOME in the above
commands.
A makefile will make the manifest for
you if you don't want to do it by hand. It will also build the JAR file and
copy it to the right location provided that you have set the target directories
at the top of the file.
Step 7. Test the Bean in the BeanBox
Now when you see the property editor associated with the NervousText08 Bean,
instead of a editable text field you'll get a drop-down choice choice box
containing the array of strings in the getTags
method of
NervousText07TextPropertyEditor
.
For the final versions of the examples in this section, see:
-
NervousText07.java
-
NervousText07BeanInfo.java
-
NervousText07TextPropertyEditor.java