MoleculeNameEditor Color-Annotated Source


The Molecule example provides a good example of how to provide a custom Property Editor for a specific property. The custom Property Editor is implemented in the MoleculeNameEditor class. The MoleculeBeanInfo class specifies MoleculeNameEditor as the Property Editor.

MoleculeBeanInfo.java

package sunw.demo.molecule;
import java.beans.*;

public class MoleculeBeanInfo extends SimpleBeanInfo {

//Since only the molecule name property is returned from this function,
//only the molecule name is exposed as an editable property.

    public PropertyDescriptor[] getPropertyDescriptors() {
        try {
            PropertyDescriptor pd = new PropertyDescriptor("moleculeName",
                                                        Molecule.class);
	//a custom property editor is indicated..
            pd.setPropertyEditorClass(MoleculeNameEditor.class);
	//and the array with the single property is returned..
            PropertyDescriptor result[] = { pd };
            return result;
        } catch (Exception ex) {
            System.err.println("MoleculeBeanInfo: unexpected exeption: " + ex);
            return null;
        }
    }    

}

MoleculeNameEditor.jav


package sunw.demo.molecule;

/**
 * Special case property editor for molecule names.
 */

public class MoleculeNameEditor
		extends java.beans.PropertyEditorSupport {

    public String[] getTags() {
	String result[] = {
	    "HyaluronicAcid",
	    "benzene",
	    "buckminsterfullerine",
	    "cyclohexane",
	    "ethane",
	    "water"};
	return result;
    }

}

Java, JavaBeans, and JavaSoft are trademarks of Sun Microsystems Inc.

Copyright © 1996 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043-1100 USA.
All rights reserved.