diff -r fd16c54261b3 -r 90ce3da70b43 jdk/make/tools/swing-beans/GenSwingBeanInfo.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/make/tools/swing-beans/GenSwingBeanInfo.java Sat Dec 01 00:00:00 2007 +0000 @@ -0,0 +1,530 @@ +/* + * Copyright 1998-2004 Sun Microsystems, Inc. All Rights Reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +import java.beans.BeanInfo; +import java.beans.BeanDescriptor; +import java.beans.Introspector; +import java.beans.IntrospectionException; +import java.beans.PropertyDescriptor; + +import java.io.*; + +import java.util.Hashtable; +import java.util.HashMap; +import java.util.Iterator; + +/** + * A utlity for generating a BeanInfo source file from a template and a + * Hashtable with hints that were generated from a doclet. + * it's neccessary to write things like the per property descriptions + * by hand. To run the application: + *
+ * java GenSwingBeanInfo+ * Code for a bean info class is written to out. If the class is + * swing package, you don't need to fully specify its name. + * + * @author Hans Muller + * @author Rich Schiavi + * @author Mark Davidson + */ +public class GenSwingBeanInfo { + private final static String BEANINFO_SUFFIX = "BeanInfo.java"; + + // Tokens in @(...) + private final static String TOK_BEANPACKAGE = "BeanPackageName"; + private final static String TOK_BEANCLASS = "BeanClassName"; + private final static String TOK_BEANOBJECT = "BeanClassObject"; + private final static String TOK_CLASSDESC = "ClassDescriptors"; + private final static String TOK_BEANDESC = "BeanDescription"; + private final static String TOK_PROPDESC = "BeanPropertyDescriptors"; + private final static String TOK_ENUMVARS = "EnumVariables"; + + private String enumcode; // Generated code for enumerated properties. + + private boolean DEBUG = false; + + private String fileDir; + private String templateFilename; + + /** + * Public constructor + * @param fileDir Location to put the generated source files. + * @param templateFilename Location of the BeanInfo template + * @param debug Flag to turn on debugging + */ + public GenSwingBeanInfo(String fileDir, String templateFilename, boolean debug) { + this.fileDir = fileDir; + this.templateFilename = templateFilename; + this.DEBUG = debug; + } + + /** + * Opens a BeanInfo PrintStream for the class. + */ + private PrintStream initOutputFile(String classname) { + try { + OutputStream out = new FileOutputStream(fileDir + File.separator + classname + BEANINFO_SUFFIX); + BufferedOutputStream bout = new BufferedOutputStream(out); + return new PrintStream(out); + } catch (IOException e){ + // System.err.println("GenSwingBeanInfo: " + e.toString()); + } + return null; + } + + private static void messageAndExit(String msg) { + System.err.println("\n" + msg); + System.exit(1); + } + + + /** + * Load the contents of the BeanInfo template into a string and + * return the string. + */ + private String loadTemplate() { + String template = "+ *
+ * createPropertyDescriptor("contentPane", new Object[] { + * BOUND, Boolean.TRUE, + * CONSTRAINED, Boolean.TRUE, + * PROPERTYEDITORCLASS, package.MyEditor.cl + * WRITEMETHOD, "setContentPane", + * DISPLAYNAME, "contentPane", + * EXPERT, Boolean.FALSE, + * HIDDEN, Boolean.FALSE, + * PREFERRED, Boolean.TRUE, + * SHORTDESCRIPTION, "A top level window with a window manager border", + * "random attribute","random value" + * } + * ); + *+ * + * @param info The actual BeanInfo class generated from from the Intospector. + * @param dochash Set of DocBeanInfo pairs for each property. This information + * is used to suplement the instrospected properties. + * @return A snippet of source code which would construct all the PropertyDescriptors. + */ + private String genPropertyDescriptors(BeanInfo info, Hashtable dochash) { + String code = ""; + enumcode = " "; // code for enumerated properties. + PropertyDescriptor[] pds = info.getPropertyDescriptors(); + boolean hash_match = false; + DocBeanInfo dbi = null; + + for(int i = 0; i < pds.length; i++) { + if (pds[i].getReadMethod() != null) { + code += "\ncreatePropertyDescriptor(\"" + pds[i].getName() + "\", new Object[] {\n"; + + if (DEBUG) + System.out.println("Introspected propertyDescriptor: " + pds[i].getName()); + + if (dochash.size() > 0 && dochash.containsKey(pds[i].getName())) { + dbi = (DocBeanInfo)dochash.remove(pds[i].getName()); + // override/set properties on this *introspected* + // BeanInfo pds using our DocBeanInfo class values + setDocInfoProps(dbi, pds[i]); + hash_match = true; + if (DEBUG) + System.out.println("DocBeanInfo class exists for propertyDescriptor: " + pds[i].getName() + "\n"); + } else { + hash_match = false; + } + + // Do I need to do anything with this property descriptor + if (hash_match) { + if ((dbi.beanflags & DocBeanInfo.BOUND) != 0) { + code += " sun.swing.BeanInfoUtils.BOUND, Boolean.TRUE,\n"; + } else { + code += " sun.swing.BeanInfoUtils.BOUND, Boolean.FALSE,\n"; + } + } + + if (pds[i].isConstrained()) { + code += " sun.swing.BeanInfoUtils.CONSTRAINED, Boolean.TRUE,\n"; + } + + if (pds[i].getPropertyEditorClass() != null) { + String className = pds[i].getPropertyEditorClass().getName(); + code += " sun.swing.BeanInfoUtils.PROPERTYEDITORCLASS, " + className + ".class,\n"; + } else if ((hash_match) && (!(dbi.propertyeditorclass.equals("null")))) { + code += " sun.swing.BeanInfoUtils.PROPERTYEDITORCLASS, " + dbi.propertyeditorclass + ".class,\n"; + } + + if ((hash_match) && (!(dbi.customizerclass.equals("null")))) { + code += " sun.swing.BeanInfoUtils.CUSTOMIZERCLASS, " + dbi.customizerclass + ".class,\n"; + } + + if ((hash_match) && (dbi.enums != null)) { + code += genEnumeration(pds[i].getName(), dbi.enums); + } + + if (!pds[i].getDisplayName().equals(pds[i].getName())) { + code += " sun.swing.BeanInfoUtils.DISPLAYNAME, \"" + pds[i].getDisplayName() + "\",\n"; + } + + if (pds[i].isExpert()) { + code += " sun.swing.BeanInfoUtils.EXPERT, Boolean.TRUE,\n"; + } + + if (pds[i].isHidden()) { + code += " sun.swing.BeanInfoUtils.HIDDEN, Boolean.TRUE,\n"; + } + + if (pds[i].isPreferred()) { + code += " sun.swing.BeanInfoUtils.PREFERRED, Boolean.TRUE,\n"; + } + + // user attributes + if (hash_match) { + if (dbi.attribs != null) { + code += genAttributes(dbi.attribs); + } + } + code += " sun.swing.BeanInfoUtils.SHORTDESCRIPTION, \"" + pds[i].getShortDescription() + "\",\n"; + + // Print the closing brackets. If this is the last array initializer, + // don't print the trailing comma. + if (i == (pds.length - 1)) { + code += " }\n)\n"; + } else { + code += " }\n),\n"; + } + + } // end if ( readMethod != null ) + } // end for + return code; + } + + /** + * Sets properties from the BeanInfo supplement on the + * introspected PropertyDescriptor + */ + private void setDocInfoProps(DocBeanInfo dbi, PropertyDescriptor pds) { + int beanflags = dbi.beanflags; + + if ((beanflags & DocBeanInfo.BOUND) != 0) + pds.setBound(true); + if ((beanflags & DocBeanInfo.EXPERT) != 0) + pds.setExpert(true); + if ((beanflags & DocBeanInfo.CONSTRAINED) != 0) + pds.setConstrained(true); + if ((beanflags & DocBeanInfo.HIDDEN) !=0) + pds.setHidden(true); + if ((beanflags & DocBeanInfo.PREFERRED) !=0) + pds.setPreferred(true); + + if (!(dbi.desc.equals("null"))){ + pds.setShortDescription(dbi.desc); + } + if (!(dbi.displayname.equals("null"))){ + pds.setDisplayName(dbi.displayname); + } + } + + /** + * Generates the BeanInfo source file using instrospection and a + * Hashtable full of hints. This the only public method in this class. + * + * @param classname Root name of the class. i.e., JButton + * @param dochash A hashtable containing the DocBeanInfo. + */ + public void genBeanInfo(String packageName, String classname, Hashtable dochash) { + // The following initial values are just examples. All of these + // fields are initialized below. + String beanClassName = "JInternalFrame"; + String beanClassObject = "javax.swing.JInternalFrame.class"; + String beanDescription = "."; + String beanPropertyDescriptors = "