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