jdk/make/tools/swing-beans/GenSwingBeanInfo.java
author katleman
Thu, 16 Feb 2012 13:01:33 -0800
changeset 11857 fb2fa6f6c9df
parent 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk8-b26 for changeset b51c748f410d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2004, 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
import java.beans.BeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.beans.BeanDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.beans.Introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A utlity for generating a BeanInfo source file from a template and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Hashtable with hints that were generated from a doclet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * it's neccessary to write things like the per property descriptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * by hand.  To run the application:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * java GenSwingBeanInfo <class name>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Code for a bean info class is written to out.  If the class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * swing package, you don't need to fully specify its name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Rich Schiavi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Mark Davidson
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class GenSwingBeanInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final static String BEANINFO_SUFFIX = "BeanInfo.java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Tokens in @(...)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final static String TOK_BEANPACKAGE = "BeanPackageName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final static String TOK_BEANCLASS = "BeanClassName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private final static String TOK_BEANOBJECT = "BeanClassObject";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final static String TOK_CLASSDESC = "ClassDescriptors";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final static String TOK_BEANDESC = "BeanDescription";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final static String TOK_PROPDESC = "BeanPropertyDescriptors";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final static String TOK_ENUMVARS = "EnumVariables";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private String enumcode;  // Generated code for enumerated properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private String fileDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private String templateFilename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Public constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param fileDir Location to put the generated source files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param templateFilename Location of the BeanInfo template
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param debug Flag to turn on debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public GenSwingBeanInfo(String fileDir, String templateFilename, boolean debug)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.fileDir = fileDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.templateFilename = templateFilename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.DEBUG = debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Opens a BeanInfo PrintStream for the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private PrintStream initOutputFile(String classname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            OutputStream out = new FileOutputStream(fileDir + File.separator + classname + BEANINFO_SUFFIX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            BufferedOutputStream bout = new BufferedOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return new PrintStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (IOException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        //    System.err.println("GenSwingBeanInfo: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static void messageAndExit(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        System.err.println("\n" + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Load the contents of the BeanInfo template into a string and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * return the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private String loadTemplate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String template = "<no template>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            File file = new File(templateFilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            DataInputStream stream = new DataInputStream(new FileInputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            while((c = reader.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                buffer.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            template = buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            reader.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.println(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            messageAndExit("GenSwingBeanInfo: Couldn't load template: " + templateFilename + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return template;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Generates a string for the BeanDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private String genBeanDescriptor(DocBeanInfo dbi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        String code = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        int beanflags = dbi.beanflags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // we support export, hidden, preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if ((beanflags & DocBeanInfo.EXPERT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            code += " sun.swing.BeanInfoUtils.EXPERT, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ((beanflags & DocBeanInfo.HIDDEN) !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            code += "                    sun.swing.BeanInfoUtils.HIDDEN, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* 1.2 only - make sure build flag build using 1.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if ((beanflags & DocBeanInfo.PREFERRED) !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            code += "                 sun.swing.BeanInfoUtils.PREFERRED, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!(dbi.customizerclass.equals("null")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            code += "            sun.swing.BeanInfoUtils.CUSTOMIZERCLASS, " + dbi.customizerclass + ".class,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (dbi.attribs != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            code += genAttributes(dbi.attribs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Generates the code for the attributes table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private String genAttributes(HashMap attribs)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        StringBuffer code = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Iterator iterator = attribs.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        while(iterator.hasNext())  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            key = (String)iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            value = (String)attribs.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (value.equals("true") || value.equals("false"))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                // Substitute the "true" and "false" for codegen Boolean values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if(value.equals("true"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    value = "Boolean.TRUE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    value = "Boolean.FALSE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                code.append("              \"").append(key).append("\", ").append(value).append(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                code.append("              \"").append(key).append("\", \"").append(value).append("\",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return code.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Generates the code for the enumeration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * XXX - side effect: Modifies the enumcode field variable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private String genEnumeration(String propName, HashMap enums)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String objectName = propName + "Enumeration";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        StringBuffer code = new StringBuffer("\n\t\tObject[] ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        code.append(objectName).append(" = new Object[] { \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Iterator iterator = enums.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        while(iterator.hasNext())  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            key = (String)iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            value = (String)enums.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            code.append("\t\t\t\"").append(key).append("\" ,   new Integer(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            code.append(value).append("), \"").append(value).append("\",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // Close the statically initialized Object[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        code.replace(code.length() - 2, code.length(), "\n\t\t};\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // Add this string to the enumeration code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        enumcode += code.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        // Return the PropertyDescriptor init string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return "         \"enumerationValues\", " + objectName + ",\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Generate the createPropertyDescriptor() calls, one per property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * A fully specified createPropertyDescriptor() call looks like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *      createPropertyDescriptor("contentPane", new Object[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *                           BOUND, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *               CONSTRAINED, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *             PROPERTYEDITORCLASS, package.MyEditor.cl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *               WRITEMETHOD, "setContentPane",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *               DISPLAYNAME, "contentPane",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *                          EXPERT, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *                          HIDDEN, Boolean.FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *                       PREFERRED, Boolean.TRUE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *                SHORTDESCRIPTION, "A top level window with a window manager border",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *               "random attribute","random value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *           );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param info The actual BeanInfo class generated from from the Intospector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param dochash Set of DocBeanInfo pairs for each property. This information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *          is used to suplement the instrospected properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return A snippet of source code which would construct all the PropertyDescriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private String genPropertyDescriptors(BeanInfo info, Hashtable dochash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        String code = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        enumcode = " "; // code for enumerated properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        PropertyDescriptor[] pds = info.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        boolean hash_match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        DocBeanInfo dbi = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        for(int i = 0; i < pds.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            if (pds[i].getReadMethod() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                code += "\ncreatePropertyDescriptor(\"" + pds[i].getName() + "\", new Object[] {\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    System.out.println("Introspected propertyDescriptor:  " + pds[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (dochash.size() > 0 && dochash.containsKey(pds[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    dbi = (DocBeanInfo)dochash.remove(pds[i].getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    // override/set properties on this *introspected*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    // BeanInfo pds using our DocBeanInfo class values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    setDocInfoProps(dbi, pds[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    hash_match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        System.out.println("DocBeanInfo class exists for propertyDescriptor: " + pds[i].getName() + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    hash_match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // Do I need to do anything with this property descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if (hash_match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    if ((dbi.beanflags & DocBeanInfo.BOUND) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        code += "               sun.swing.BeanInfoUtils.BOUND, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        code += "               sun.swing.BeanInfoUtils.BOUND, Boolean.FALSE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (pds[i].isConstrained()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    code += "         sun.swing.BeanInfoUtils.CONSTRAINED, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (pds[i].getPropertyEditorClass() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    String className = pds[i].getPropertyEditorClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    code += " sun.swing.BeanInfoUtils.PROPERTYEDITORCLASS, " + className + ".class,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                } else if ((hash_match) && (!(dbi.propertyeditorclass.equals("null")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    code += " sun.swing.BeanInfoUtils.PROPERTYEDITORCLASS, " + dbi.propertyeditorclass + ".class,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                if ((hash_match) && (!(dbi.customizerclass.equals("null")))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    code += " sun.swing.BeanInfoUtils.CUSTOMIZERCLASS, " + dbi.customizerclass + ".class,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                if ((hash_match) && (dbi.enums != null))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    code += genEnumeration(pds[i].getName(), dbi.enums);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (!pds[i].getDisplayName().equals(pds[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    code += "         sun.swing.BeanInfoUtils.DISPLAYNAME, \"" + pds[i].getDisplayName() + "\",\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (pds[i].isExpert()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    code += "              sun.swing.BeanInfoUtils.EXPERT, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                if (pds[i].isHidden()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    code += "              sun.swing.BeanInfoUtils.HIDDEN, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if (pds[i].isPreferred()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    code += "           sun.swing.BeanInfoUtils.PREFERRED, Boolean.TRUE,\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                // user attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (hash_match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    if (dbi.attribs != null)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                        code += genAttributes(dbi.attribs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                code += "    sun.swing.BeanInfoUtils.SHORTDESCRIPTION, \"" + pds[i].getShortDescription() + "\",\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                // Print the closing brackets.  If this is the last array initializer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // don't print the trailing comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (i == (pds.length - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    code += "  }\n)\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    code += "  }\n),\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } // end if ( readMethod != null )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } // end for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Sets properties from the BeanInfo supplement on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * introspected PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private void setDocInfoProps(DocBeanInfo dbi, PropertyDescriptor pds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        int beanflags = dbi.beanflags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if ((beanflags & DocBeanInfo.BOUND) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            pds.setBound(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        if ((beanflags & DocBeanInfo.EXPERT) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            pds.setExpert(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if ((beanflags & DocBeanInfo.CONSTRAINED) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            pds.setConstrained(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if ((beanflags & DocBeanInfo.HIDDEN) !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            pds.setHidden(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if ((beanflags & DocBeanInfo.PREFERRED) !=0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            pds.setPreferred(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (!(dbi.desc.equals("null"))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            pds.setShortDescription(dbi.desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (!(dbi.displayname.equals("null"))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            pds.setDisplayName(dbi.displayname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Generates the BeanInfo source file using instrospection and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Hashtable full of hints. This the only public method in this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param classname Root name of the class. i.e., JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param dochash A hashtable containing the DocBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public void genBeanInfo(String packageName, String classname, Hashtable dochash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // The following initial values are just examples.  All of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // fields are initialized below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        String beanClassName = "JInternalFrame";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        String beanClassObject = "javax.swing.JInternalFrame.class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        String beanDescription = "<A description of this component>.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        String beanPropertyDescriptors = "<createSwingPropertyDescriptor code>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        String classPropertyDescriptors = "<createSwingClassPropertyDescriptor code>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Class cls = getClass(packageName, classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (cls == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            messageAndExit("Can't find class: " + classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        // Get the output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        PrintStream out = initOutputFile(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // Run the Introspector and initialize the variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        BeanInfo beanInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        BeanDescriptor beanDescriptor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (cls == javax.swing.JComponent.class)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                // Go all the way up the heirarchy for JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                beanInfo = Introspector.getBeanInfo(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                beanInfo = Introspector.getBeanInfo(cls, cls.getSuperclass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            beanDescriptor = beanInfo.getBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            beanDescription = beanDescriptor.getShortDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        } catch (IntrospectionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            messageAndExit("Introspection failed for " + cls.getName() + " " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        beanClassName = beanDescriptor.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        beanClassObject = cls.getName() + ".class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (DEBUG){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            System.out.println(">>>>GenSwingBeanInfo class: "  + beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // Generate the Class BeanDescriptor information first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (dochash.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (dochash.containsKey(beanClassName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    DocBeanInfo dbi = (DocBeanInfo)dochash.remove(beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    classPropertyDescriptors = genBeanDescriptor(dbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (DEBUG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        System.out.println("ClassPropertyDescriptors: " + classPropertyDescriptors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    if (!(dbi.desc.equals("null")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        beanDescription = dbi.desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    beanDescription = beanDescriptor.getShortDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            beanDescription = beanDescriptor.getShortDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // Generate the Property descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        beanPropertyDescriptors = genPropertyDescriptors(beanInfo,dochash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        // Dump the template to out, substituting values for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // @(token) tokens as they're encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        int currentIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        // not loading this to get around build issue for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        String template = loadTemplate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // This loop substitutes the "@(...)" tags in the template with the ones for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        while (currentIndex < template.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // Find the Token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            int tokenStart = template.indexOf("@(", currentIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (tokenStart != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                out.print(template.substring(currentIndex, tokenStart));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                int tokenEnd = template.indexOf(")", tokenStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                if (tokenEnd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    messageAndExit("Bad @(<token>) beginning at " + tokenStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                String token = template.substring(tokenStart+2, tokenEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                if (token.equals(TOK_BEANCLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    out.print(beanClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                } else if (token.equals(TOK_CLASSDESC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    if (!(classPropertyDescriptors.equals("<createSwingClassPropertyDescriptor code>"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        printDescriptors(out, classPropertyDescriptors, template, tokenStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                } else if (token.equals(TOK_BEANPACKAGE)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    out.print(packageName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                } else if (token.equals(TOK_BEANOBJECT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    out.print(beanClassObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                } else if (token.equals(TOK_BEANDESC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    out.print(beanDescription);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                } else if (token.equals(TOK_ENUMVARS)){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    out.print(enumcode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                } else if (token.equals(TOK_PROPDESC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    printDescriptors(out, beanPropertyDescriptors, template, tokenStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                } else if (token.equals("#")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    // Ignore the @(#) Version Control tag if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    messageAndExit("Unrecognized token @(" + token + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                currentIndex = tokenEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                // tokenStart == -1 - We are finsihed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                out.print(template.substring(currentIndex, template.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns the class from the package name and the class root name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param packageName The name of the package of the containing class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param rootname The root name of the class. i.e, JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return The class instance or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private Class getClass(String packageName, String rootname)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        Class cls = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        String classname = rootname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (packageName != null || !packageName.equals(""))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            classname = packageName + "." + rootname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            cls = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // Fail silently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return cls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Prints the formated descriptors to the PrintStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @param out Open PrintStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param s String descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param template Template
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param tokenStart Index into the template
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    private void printDescriptors(PrintStream out, String s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                String template, int tokenStart)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            String indent = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            // Find the newline that preceeds @(BeanPropertyDescriptors) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // calculate the indent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            for (int i = tokenStart; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (template.charAt(i) == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        char[] chars = new char[tokenStart - i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        for (int j = 0; j < chars.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                            chars[j] = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        indent = new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            while(i < s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                int nlIndex = s.indexOf('\n', i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                out.print(s.substring(i, nlIndex+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                out.print(indent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                i = nlIndex + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
}