This topic discusses classes found in the com.ibm.ivj.examples.vc.propertyeditors package.You can try this editor from PersonTester only.
The name property has a custom editor (NameEditor) and custom editor panel (NameCustomEditor) associated with it. Support of the PropertyEditor interface and the edit function itself are decoupled in order to optimize performance in Person's property sheet. By decoupling them, we can delay construction of the custom editor panel until the user requests it; only the property editor itself is instantiated when the property sheet is first opened. This functional separation is significant when the type being supported can occur several times in a single property sheet (like a custom String editor), because each property requires its own instance of the custom editor.
When you browse the sample, note the public constructor for NameEditor. All of the other property editor classes in this sample have protected constructors, generated by default because the superclass constructor is protected. When you explicitly assign a property editor in BeanInfo, access to the protected constructor is not a problem. For the name property, however, we have not explicitly assigned an editor in BeanInfo. In this case, the PropertyEditorManager class becomes involved in coordinating edit support for the property, so we must provide a public constructor.
The NameEditor property editor looks like this:
public class NameEditor extends java.beans.PropertyEditorSupport { java.beans.PropertyChangeSupport iPropertyChange = new java.beans.PropertyChangeSupport(this); NameCustomEditor iNameCustomEditor = null; Name iName = null; public String getAsText() { return ((Name) getValue()).toString(); } public java.awt.Component getCustomEditor() { if (iNameCustomEditor == null) { iNameCustomEditor = new NameCustomEditor(); iNameCustomEditor.setTheNameThis( getName() ); } return iNameCustomEditor; } public String getJavaInitializationString() { Name tName = ( (Name) getValue() ); return "new propertypditors.Name(\"" + tName.getTitle() + "\", \"" + tName.getFirstName() + "\", \"" + tName.getMiddleName() + "\", \"" + tName.getLastName() + "\")"; } public Name getName() { if (iName == null) iName = new Name(); return iName; } public Object getValue() { if (iNameCustomEditor == null) return getName(); else return iNameCustomEditor.getTheNameThis(); } public void setValue(Object value) { Object tValue = getName(); if (iNameCustomEditor == null) { iName = ((Name) value); iPropertyChange.firePropertyChange("value", tValue, value); } else iNameCustomEditor.setTheNameThis( (Name) value ); } public boolean supportsCustomEditor() { return true; } }
NameEditor manages the getting and setting of property values through the property sheet; NameCustomEditor collects the information from the user. The common currency between the property editor and the custom editor panel is a Name instance. The NameCustomEditor panel looks like this:
In this composite, property-to-property connections link Name properties in the variable bean to the text properties of the TextField beans. The Choice bean requires two connections: one from its selectedItem property to the title property of the variable, and one from the title property of the variable to the select( ) method of the Choice bean (passing in the current value of title as a parameter of the select( ) method). The variable's this property is promoted to the interface of the composite so that it can be set from NameEditor during initialization.
To understand how these two classes interact, follow this partial program flow: