jdk/make/data/swingbeaninfo/sun/swing/BeanInfoUtils.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 21805 c7d7946239de
child 25776 654b0255bbae
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class BeanInfoUtils
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    /* The values of these createPropertyDescriptor() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
     * createBeanDescriptor() keywords are the names of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     * properties they're used to set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public static final String BOUND = "bound";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public static final String CONSTRAINED = "constrained";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static final String PROPERTYEDITORCLASS = "propertyEditorClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static final String READMETHOD = "readMethod";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static final String WRITEMETHOD = "writeMethod";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static final String DISPLAYNAME = "displayName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static final String EXPERT = "expert";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static final String HIDDEN = "hidden";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static final String PREFERRED = "preferred";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static final String SHORTDESCRIPTION = "shortDescription";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static final String CUSTOMIZERCLASS = "customizerClass";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static private void initFeatureDescriptor(FeatureDescriptor fd, String key, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (DISPLAYNAME.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            fd.setDisplayName((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (EXPERT.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            fd.setExpert(((Boolean)value).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (HIDDEN.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            fd.setHidden(((Boolean)value).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (PREFERRED.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            fd.setPreferred(((Boolean)value).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        else if (SHORTDESCRIPTION.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            fd.setShortDescription((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* Otherwise assume that we have an arbitrary FeatureDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         * "attribute".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            fd.setValue(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Create a beans PropertyDescriptor given an of keyword/value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * arguments.  The following sample call shows all of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * keywords:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *      createPropertyDescriptor("contentPane", new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *                     BOUND, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *               CONSTRAINED, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *       PROPERTYEDITORCLASS, package.MyEditor.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *                READMETHOD, "getContentPane",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *               WRITEMETHOD, "setContentPane",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *               DISPLAYNAME, "contentPane",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *                    EXPERT, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *                    HIDDEN, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *                 PREFERRED, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *          SHORTDESCRIPTION, "A top level window with a window manager border",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *         "random attribute","random object value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The keywords correspond to <code>java.beans.PropertyDescriptor</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>java.beans.FeatureDescriptor</code> properties, e.g. providing a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * for displayName is comparable to <code>FeatureDescriptor.setDisplayName()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Using createPropertyDescriptor instead of the PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * constructor and set methods is preferrable in that it regularizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * the code in a <code>java.beans.BeanInfo.getPropertyDescriptors()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * method implementation.  One can use <code>createPropertyDescriptor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to set <code>FeatureDescriptor</code> attributes, as in "random attribute"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * "random object value".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * All properties should provide a reasonable value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <code>SHORTDESCRIPTION</code> keyword and should set <code>BOUND</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * to <code>Boolean.TRUE</code> if neccessary.  The remaining keywords
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * are optional.  There's no need to provide values for keywords like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * READMETHOD if the correct value can be computed, i.e. if the properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * get/is method follows the standard beans pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The PREFERRED keyword is not supported by the JDK1.1 java.beans package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * It's still worth setting it to true for properties that are most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * likely to be interested to the average developer, e.g. AbstractButton.title
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * is a preferred property, AbstractButton.focusPainted is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see java.beans#BeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see java.beans#PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see java.beans#FeatureDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static PropertyDescriptor createPropertyDescriptor(Class cls, String name, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        PropertyDescriptor pd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            pd = new PropertyDescriptor(name, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } catch (IntrospectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Try creating a read-only property, in case setter isn't defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                pd = createReadOnlyPropertyDescriptor(name, cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } catch (IntrospectionException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throwError(ie, "Can't create PropertyDescriptor for " + name + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        for(int i = 0; i < args.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            String key = (String)args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            Object value = args[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (BOUND.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                pd.setBound(((Boolean)value).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            else if (CONSTRAINED.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                pd.setConstrained(((Boolean)value).booleanValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            else if (PROPERTYEDITORCLASS.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                pd.setPropertyEditorClass((Class)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            else if (READMETHOD.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                String methodName = (String)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                Method method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    method = cls.getMethod(methodName, new Class[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    pd.setReadMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    throwError(e, cls + " no such method as \"" + methodName + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            else if (WRITEMETHOD.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                String methodName = (String)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                Method method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    Class type = pd.getPropertyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    method = cls.getMethod(methodName, new Class[]{type});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    pd.setWriteMethod(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    throwError(e, cls + " no such method as \"" + methodName + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                initFeatureDescriptor(pd, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Create a BeanDescriptor object given an of keyword/value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * arguments.  The following sample call shows all of the supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * keywords:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *      createBeanDescriptor(JWindow..class, new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *           CUSTOMIZERCLASS, package.MyCustomizer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *               DISPLAYNAME, "JFrame",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                    EXPERT, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *                    HIDDEN, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *                 PREFERRED, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *          SHORTDESCRIPTION, "A top level window with a window manager border",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         "random attribute","random object value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * The keywords correspond to <code>java.beans.BeanDescriptor</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>java.beans.FeatureDescriptor</code> properties, e.g. providing a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * for displayName is comparable to <code>FeatureDescriptor.setDisplayName()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Using createBeanDescriptor instead of the BeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * constructor and set methods is preferrable in that it regularizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * the code in a <code>java.beans.BeanInfo.getBeanDescriptor()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * method implementation.  One can use <code>createBeanDescriptor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * to set <code>FeatureDescriptor</code> attributes, as in "random attribute"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * "random object value".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see java.beans#BeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see java.beans#PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public static BeanDescriptor createBeanDescriptor(Class cls, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Class customizerClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /* For reasons I don't understand, customizerClass is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * readOnly property.  So we have to find it and pass it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * to the constructor here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for(int i = 0; i < args.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (CUSTOMIZERCLASS.equals((String)args[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                customizerClass = (Class)args[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        BeanDescriptor bd = new BeanDescriptor(cls, customizerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        for(int i = 0; i < args.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            String key = (String)args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            Object value = args[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            initFeatureDescriptor(bd, key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return bd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    static private PropertyDescriptor createReadOnlyPropertyDescriptor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        String name, Class cls) throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        Method readMethod = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        String base = capitalize(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Class[] parameters = new Class[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // Is it a boolean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            readMethod = cls.getMethod("is" + base, parameters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (Exception ex) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (readMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // Try normal accessor pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                readMethod = cls.getMethod("get" + base, parameters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } catch (Exception ex2) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (readMethod != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return new PropertyDescriptor(name, readMethod, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // Try indexed accessor pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            parameters = new Class[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            parameters[0] = int.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            readMethod = cls.getMethod("get" + base, parameters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new IntrospectionException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                "cannot find accessor method for " + name + " property.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return new IndexedPropertyDescriptor(name, null, null, readMethod, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // Modified methods from java.beans.Introspector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private static String capitalize(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (s.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        char chars[] = s.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        chars[0] = Character.toUpperCase(chars[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Fatal errors are handled by calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public static void throwError(Exception e, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        throw new Error(e.toString() + " " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}