Java Reference
In-Depth Information
tations of most
PropertyEditor
methods. It also implements the methods for
adding and removing event listeners.
A property that has an enumerated value requires a simple property editor. The
alignment
property of the
YesNoPanel
bean is an example of this common type of
property. The property has only the three legal values defined by the
Alignment
class. The
AlignmentEditor
class shown in Example 14-7 is a property editor that
tells a beanbox how to display and edit the value of this property. Because
Align-
mentEditor
follows a JavaBeans naming convention, a beanbox automatically uses
it for any property of type
Alignment
.
Example 14−7: AlignmentEditor.java
package com.davidflanagan.examples.beans;
import java.beans.*;
import java.awt.*;
/**
* This PropertyEditor defines the enumerated values of the alignment property
* so that a bean box or IDE can present those values to the user for selection
**/
public class AlignmentEditor extends PropertyEditorSupport {
/** Return the list of value names for the enumerated type. */
public String[] getTags() {
return new String[] { "left", "center", "right" };
}
/** Convert each of those value names into the actual value. */
public void setAsText(String s) {
if (s.equals("left")) setValue(Alignment.LEFT);
else if (s.equals("center")) setValue(Alignment.CENTER);
else if (s.equals("right")) setValue(Alignment.RIGHT);
else throw new IllegalArgumentException(s);
}
/** This is an important method for code generation. */
public String getJavaInitializationString() {
Object o = getValue();
if (o == Alignment.LEFT)
return "com.davidflanagan.examples.beans.Alignment.LEFT";
if (o == Alignment.CENTER)
return "com.davidflanagan.examples.beans.Alignment.CENTER";
if (o == Alignment.RIGHT)
return "com.davidflanagan.examples.beans.Alignment.RIGHT";
return null;
}
}
Defining a Complex Property Editor
There is another
YesNoPanel
property value that requires a property editor. The
messageText
property of
YesNoPanel
can specify a multiline message to be dis-
played in the panel. This property requires a property editor because the
beanbox
program doesn't distinguish between single-line and multiline string types; the
TextField
objects it uses for text input don't allow the user to enter multiple lines
of text. For this reason, you define the
YesNoPanelMessageEditor
class and register