BeanInfoUtils.java 11.2 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
 */

package sun.swing;

import java.beans.*;
import java.lang.reflect.Method;

public class BeanInfoUtils
{
    /* The values of these createPropertyDescriptor() and
     * createBeanDescriptor() keywords are the names of the
     * properties they're used to set.
     */
    public static final String BOUND = "bound";
    public static final String CONSTRAINED = "constrained";
    public static final String PROPERTYEDITORCLASS = "propertyEditorClass";
    public static final String READMETHOD = "readMethod";
    public static final String WRITEMETHOD = "writeMethod";
    public static final String DISPLAYNAME = "displayName";
    public static final String EXPERT = "expert";
    public static final String HIDDEN = "hidden";
    public static final String PREFERRED = "preferred";
    public static final String SHORTDESCRIPTION = "shortDescription";
    public static final String CUSTOMIZERCLASS = "customizerClass";

    static private void initFeatureDescriptor(FeatureDescriptor fd, String key, Object value)
    {
        if (DISPLAYNAME.equals(key)) {
            fd.setDisplayName((String)value);
        }

        if (EXPERT.equals(key)) {
            fd.setExpert(((Boolean)value).booleanValue());
        }

        if (HIDDEN.equals(key)) {
            fd.setHidden(((Boolean)value).booleanValue());
        }

        if (PREFERRED.equals(key)) {
            fd.setPreferred(((Boolean)value).booleanValue());
        }

        else if (SHORTDESCRIPTION.equals(key)) {
            fd.setShortDescription((String)value);
        }

        /* Otherwise assume that we have an arbitrary FeatureDescriptor
         * "attribute".
         */
        else {
            fd.setValue(key, value);
        }
    }

    /**
     * Create a beans PropertyDescriptor given an of keyword/value
     * arguments.  The following sample call shows all of the supported
     * keywords:
     *<pre>
     *      createPropertyDescriptor("contentPane", new Object[] {
     *                     BOUND, Boolean.TRUE,
     *               CONSTRAINED, Boolean.TRUE,
     *       PROPERTYEDITORCLASS, package.MyEditor.class,
     *                READMETHOD, "getContentPane",
     *               WRITEMETHOD, "setContentPane",
     *               DISPLAYNAME, "contentPane",
     *                    EXPERT, Boolean.FALSE,
     *                    HIDDEN, Boolean.FALSE,
     *                 PREFERRED, Boolean.TRUE,
     *          SHORTDESCRIPTION, "A top level window with a window manager border",
     *         "random attribute","random object value"
     *        }
     *     );
     * </pre>
     * The keywords correspond to <code>java.beans.PropertyDescriptor</code> and
     * <code>java.beans.FeatureDescriptor</code> properties, e.g. providing a value
     * for displayName is comparable to <code>FeatureDescriptor.setDisplayName()</code>.
     * Using createPropertyDescriptor instead of the PropertyDescriptor
     * constructor and set methods is preferrable in that it regularizes
     * the code in a <code>java.beans.BeanInfo.getPropertyDescriptors()</code>
     * method implementation.  One can use <code>createPropertyDescriptor</code>
     * to set <code>FeatureDescriptor</code> attributes, as in "random attribute"
     * "random object value".
     * <p>
     * All properties should provide a reasonable value for the
     * <code>SHORTDESCRIPTION</code> keyword and should set <code>BOUND</code>
     * to <code>Boolean.TRUE</code> if neccessary.  The remaining keywords
     * are optional.  There's no need to provide values for keywords like
     * READMETHOD if the correct value can be computed, i.e. if the properties
     * get/is method follows the standard beans pattern.
     * <p>
     * The PREFERRED keyword is not supported by the JDK1.1 java.beans package.
     * It's still worth setting it to true for properties that are most
     * likely to be interested to the average developer, e.g. AbstractButton.title
     * is a preferred property, AbstractButton.focusPainted is not.
     *
     * @see java.beans#BeanInfo
     * @see java.beans#PropertyDescriptor
     * @see java.beans#FeatureDescriptor
     */
    public static PropertyDescriptor createPropertyDescriptor(Class cls, String name, Object[] args)
    {
        PropertyDescriptor pd = null;
        try {
            pd = new PropertyDescriptor(name, cls);
        } catch (IntrospectionException e) {
            // Try creating a read-only property, in case setter isn't defined.
            try {
                pd = createReadOnlyPropertyDescriptor(name, cls);
            } catch (IntrospectionException ie) {
                throwError(ie, "Can't create PropertyDescriptor for " + name + " ");
            }
        }

        for(int i = 0; i < args.length; i += 2) {
            String key = (String)args[i];
            Object value = args[i + 1];

            if (BOUND.equals(key)) {
                pd.setBound(((Boolean)value).booleanValue());
            }

            else if (CONSTRAINED.equals(key)) {
                pd.setConstrained(((Boolean)value).booleanValue());
            }

            else if (PROPERTYEDITORCLASS.equals(key)) {
                pd.setPropertyEditorClass((Class)value);
            }

            else if (READMETHOD.equals(key)) {
                String methodName = (String)value;
                Method method;
                try {
                    method = cls.getMethod(methodName, new Class[0]);
                    pd.setReadMethod(method);
                }
                catch(Exception e) {
                    throwError(e, cls + " no such method as \"" + methodName + "\"");
                }
            }

            else if (WRITEMETHOD.equals(key)) {
                String methodName = (String)value;
                Method method;
                try {
                    Class type = pd.getPropertyType();
                    method = cls.getMethod(methodName, new Class[]{type});
                    pd.setWriteMethod(method);
                }
                catch(Exception e) {
                    throwError(e, cls + " no such method as \"" + methodName + "\"");
                }
            }

            else {
                initFeatureDescriptor(pd, key, value);
            }
        }

        return pd;
    }


    /**
     * Create a BeanDescriptor object given an of keyword/value
     * arguments.  The following sample call shows all of the supported
     * keywords:
     *<pre>
     *      createBeanDescriptor(JWindow..class, new Object[] {
     *           CUSTOMIZERCLASS, package.MyCustomizer.class,
     *               DISPLAYNAME, "JFrame",
     *                    EXPERT, Boolean.FALSE,
     *                    HIDDEN, Boolean.FALSE,
     *                 PREFERRED, Boolean.TRUE,
     *          SHORTDESCRIPTION, "A top level window with a window manager border",
     *         "random attribute","random object value"
     *        }
     *     );
     * </pre>
     * The keywords correspond to <code>java.beans.BeanDescriptor</code> and
     * <code>java.beans.FeatureDescriptor</code> properties, e.g. providing a value
     * for displayName is comparable to <code>FeatureDescriptor.setDisplayName()</code>.
     * Using createBeanDescriptor instead of the BeanDescriptor
     * constructor and set methods is preferrable in that it regularizes
     * the code in a <code>java.beans.BeanInfo.getBeanDescriptor()</code>
     * method implementation.  One can use <code>createBeanDescriptor</code>
     * to set <code>FeatureDescriptor</code> attributes, as in "random attribute"
     * "random object value".
     *
     * @see java.beans#BeanInfo
     * @see java.beans#PropertyDescriptor
     */
    public static BeanDescriptor createBeanDescriptor(Class cls, Object[] args)
    {
        Class customizerClass = null;

        /* For reasons I don't understand, customizerClass is a
         * readOnly property.  So we have to find it and pass it
         * to the constructor here.
         */
        for(int i = 0; i < args.length; i += 2) {
            if (CUSTOMIZERCLASS.equals((String)args[i])) {
                customizerClass = (Class)args[i + 1];
                break;
            }
        }

        BeanDescriptor bd = new BeanDescriptor(cls, customizerClass);

        for(int i = 0; i < args.length; i += 2) {
            String key = (String)args[i];
            Object value = args[i + 1];
            initFeatureDescriptor(bd, key, value);
        }

        return bd;
    }

    static private PropertyDescriptor createReadOnlyPropertyDescriptor(
        String name, Class cls) throws IntrospectionException {

        Method readMethod = null;
        String base = capitalize(name);
        Class[] parameters = new Class[0];

        // Is it a boolean?
        try {
            readMethod = cls.getMethod("is" + base, parameters);
        } catch (Exception ex) {}
        if (readMethod == null) {
            try {
                // Try normal accessor pattern.
                readMethod = cls.getMethod("get" + base, parameters);
            } catch (Exception ex2) {}
        }
        if (readMethod != null) {
            return new PropertyDescriptor(name, readMethod, null);
        }

        try {
            // Try indexed accessor pattern.
            parameters = new Class[1];
            parameters[0] = int.class;
            readMethod = cls.getMethod("get" + base, parameters);
        } catch (NoSuchMethodException nsme) {
            throw new IntrospectionException(
                "cannot find accessor method for " + name + " property.");
        }
        return new IndexedPropertyDescriptor(name, null, null, readMethod, null);
    }

    // Modified methods from java.beans.Introspector
    private static String capitalize(String s) {
        if (s.length() == 0) {
            return s;
        }
        char chars[] = s.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);
        return new String(chars);
    }

    /**
     * Fatal errors are handled by calling this method.
     */
    public static void throwError(Exception e, String s) {
        throw new Error(e.toString() + " " + s);
    }
}