The JavaBeans Component Library supplies a few property editors for the primitive Java data types. When you create your own components, however, you might create your own property classes and want to have editors capable of editing their values.
This following sections will get you started writing your own property editors. These topics are covered:
To begin creating a property editor class, create a class that implements the PropertyEditor interface.
You can use the PropertyEditorSupport class as a starting point, or implement PropertyEditor directly in the class you create. This is the recommended naming convention:
class <propertyName>Editor
public String getJavaInitializationString()The returned string must be suitable for a Java assignment. Here are some examples:
To permit the display and editing of a property value as text, write code that implements the getAsText() and setAsText() methods. Here are their declarations:
public String getAsText() public void setAsText(String text) throws IllegalArgumentExceptionsetAsText() throws the IllegalArgumentException exception if the string isn't formatted properly or if the property can't be represented as a text string.
To permit the display and editing of a property value as a list of text values,
public String[] getTags()getTags() returns an array of tags that are used to represent enumerated values of the property. These tags are the strings the user sees in a choice menu (drop-down list). The user chooses the desired value from the list.
Here's an example of a getTags() method that presents the user a choice of beverages:
public String[] getTags() { String[] choices = new String {"Coffee", "Tea", "Milk", "Orange juice"}; return choices; }The getAsText() method must be able to display one of the tagged values, and the setAsText() method must be able to set the property's value using one of the tagged values.
public boolean isPaintable() public void paintValue(Graphics gfx, Rectangle box)To make your property editor capable of painting a property value,
There are two methods you must write: supportsCustomEditor() and getCustomEditor().
public boolean supportsCustomEditor() public Component getCustomEditor()To provide a custom editor for your property editor class,
Note that getCustomEditor() returns a Component. You can create just about any kind of property editor you want. You must take on the task of connecting the custom editor with the property editor.
public void addPropertyChangeListener(PropertyChangeListener listener) public void removePropertyChangeListener(PropertyChangeListener listener)By implementing these methods, the property editor can notify all listeners that a change in a property value has occurred.
See also:
Working with events
package borland.jbcl.editors; import java.beans.*; import borland.jbcl.util.*; public class IntegerTagEditor implements PropertyEditor { int[] values; // the array of values (null counts from 0:n) String[] resourceStrings; // strings the user will see in the drop down String[] sourceCodeStrings; // strings this will generate in source code public IntegerTagEditor(int[] values, String[] resourceStrings, String[] sourceCodeStrings) { // A null list of integer values assumes an incrementing enumeration from 0 if (values == null) { values = new int[resourceStrings.length]; for (int i = 0; i < values.length; ++i) values[i] = i; } this.values = values; this.resourceStrings = resourceStrings; // A null set of sourceCode Strings assumes the resourceStrings are the same this.sourceCodeStrings = (sourceCodeStrings != null) ? sourceCodeStrings : resourceStrings; } // PropertyEditor Implementation public void setValue(Object o) { value = o; fire(); } public Object getValue() { return value; } public boolean isPaintable() { // the property editor does support painting the property value return false; } public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { // Leave empty. // no painting occurs } private String getAsText(boolean forSourceCode) { int iVal = (value == null || !(value instanceof Integer)) ? values[0] : ((Integer)value).intValue(); int iPos; for (iPos = 0; iPos < values.length; ++iPos) if (values[iPos] == iVal) break; if (iPos >= values.length) return ""; return (forSourceCode) ? sourceCodeStrings[iPos] : resourceStrings[iPos]; } public String getAsText() { // returns one of the strings the user sees in the choice menu return getAsText(false); } public String getJavaInitializationString() { // returns the one of the source code strings return getAsText(true); } public void setAsText(String text) throws java.lang.IllegalArgumentException { int iPos = 0; if (text != null) { for (; iPos < resourceStrings.length; ++iPos) if (text.equals(resourceStrings[iPos])) break; if (iPos >= resourceStrings.length) throw new java.lang.IllegalArgumentException(); value = new Integer(values[iPos]); // generates a PropertyChange event fire(); } } public String[] getTags() { // returns the strings the user sees in the choice menu return resourceStrings; } public java.awt.Component getCustomEditor() { // no custom editor available return null; } public boolean supportsCustomEditor() { // doesn't support a custom editor return false; } private void fire() { // calls the propertyChange() method in the listener and passes it a PropertyChangeEvent if (listener != null) { listener.propertyChange(new PropertyChangeEvent(this, "???", null/*???*/, value)); } } addPropertyChangeListener(PropertyChangeListener l) { // registers the listener for property change events listener = l; } public void removePropertyChangeListener(PropertyChangeListener l) { // removes the listener for property change events listener = null; } private PropertyChangeListener listener; private Object value; }
package borland.jbcl.editors; public class TreeStyleEditor extends IntegerTagEditor { public TreeStyleEditor() { // creates the values array super(new int[] { borland.jbcl.view.TreeView.STYLE_PLUSES, borland.jbcl.view.TreeView.STYLE_ARROWS}, // creates the resourceStrings array new String[] { "Pluses", "Arrows"}, // creates the array that holds the strings JBuilder uses when generating source code new String[] { "borland.jbcl.view.TreeView.STYLE_PLUSES", "borland.jbcl.view.TreeView.STYLE_ARROWS"}); } }
JBuilder provides several default property editors that are based on property types. If you fail to specify a property editor for a property in the BeanInfo class, JBuilder attempts to use one of these default editors. For example, it supplies a string type editor for entering and editing a string value such as the value of the text property for LabelControl. Another example is the int type editor for entering and editing integer values such as the value of the itemWidth property of ListControl.
If JBuilder can't find an editor that can edit the property type, it uses a scoped object list editor, which displays each of the objects in scope that match the given property type. For example, the dataSet property for the various JBuilder controls use the scoped object list editor and lists all the objects in scope of the type DataSet.