next up previous contents
Next: Serialization Up: Property Editor Previous: PropertyEditorManager

BeanInfoとエディタの設定

findEditorは、先に見たように、クラス名+"Editor"という名前のクラスを探すのです が、BeanInfoを使うと、任意の名前のクラスをエディタとしてbeanに設定できます。 基本は、プロパティ・ディスクリプタが、そのプロパティのエディタについての情報を 持つということです。次の例では、setPropertyEditorClassというメソッドで、 MoleculeクラスのmoleculeNameという名前のプロパティに、MoleculeNameEditorという エディタ・クラスを設定しています。

この例は、また、getPropertyDescriptorsメソッドが返す配列が、このプロパティ しか含んでいませんので、一つのプロパティしか編集可能でないbeanの例ともなって います。

public class MoleculeBeanInfo extends SimpleBeanInfo {

    public PropertyDescriptor[] getPropertyDescriptors() {
        try {
            PropertyDescriptor pd = new PropertyDescriptor("moleculeName",
                                                            Molecule.class);
            pd.setPropertyEditorClass(MoleculeNameEditor.class);
            PropertyDescriptor result[] = { pd };
            return result;
        } catch (Exception ex) {
            System.err.println("MoleculeBeanInfo: unexpected exeption: " + ex);
            return null;
        }
    }

}

MoleculeNameEditorの中身は、PropertyEditorSupportクラスを継承して、 そのgetTagsメソッドを置き換えただけのものですl。

public class MoleculeNameEditor
                extends java.beans.PropertyEditorSupport {

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

}


maruyama@wakhok.ac.jp