Initial Commit
[packages] / xemacs-packages / jde / java / bsh-commands / bsh / commands / beanInfoMaker.bsh
1 // JTEM - Java Tools for Experimental Mathematics\r
2 // GPL Copyright (C) 2002 JTEM-Group\r
3 //\r
4 // This program is free software; you can redistribute it and/or modify\r
5 // it under the terms of the GNU General Public License as published by\r
6 // the Free Software Foundation; either version 2 of the License, or\r
7 // any later version.\r
8 //\r
9 // This program is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12 // GNU General Public License for more details.\r
13 //\r
14 // You should have received a copy of the GNU General Public License\r
15 // along with this program; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
17 \r
18 String beanInfoMaker(){ return beanInfoMaker(self);}\r
19 \r
20 \r
21 String beanInfoMaker(String fullClassName){\r
22      import java.beans.BeanInfo;\r
23      import java.beans.MethodDescriptor;\r
24      import java.beans.PropertyDescriptor;\r
25      import java.beans.Introspector;\r
26   \r
27      final int lastP =  fullClassName.lastIndexOf('.');\r
28      final String packageName = fullClassName.substring(0,lastP);    \r
29      final String myClassName = fullClassName.substring\r
30          (lastP+((lastP==0)?0:1));\r
31     \r
32 //   final      String  myclassName = myClassName.substring(0,1).toLowerCase() \r
33 //      +  myClassName.substring(1);\r
34 \r
35      final     BeanInfo bI= Introspector.getBeanInfo\r
36         (Class.forName(fullClassName));\r
37 \r
38 //   final MethodDescriptor[] mDA = bI.getMethodDescriptors();      \r
39      final PropertyDescriptor[] pDA = bI.getPropertyDescriptors();      \r
40      final StringBuffer pDS = new StringBuffer(500*pDA.length);\r
41 \r
42      for (int i=0; i<pDA.length; i++) {\r
43         final PropertyDescriptor pDAi = pDA[i];\r
44         final String mGetName = pDAi.getReadMethod().getName();\r
45 //      final java.lang.reflect.Method mSetMethod = pDAig.getWriteMethod();\r
46 //      final String mSetName = (mSetMethod==null)?"":mSetMethod.getName();\r
47         final String pName = mGetName.substring(3);\r
48         final String pname = pName.substring(0,1).toLowerCase() \r
49             +  pName.substring(1);\r
50 //      final String pTypeUnreadable = pDAi.getPropertyType().getName();\r
51 \r
52         pDS.append\r
53 ("\n"+\r
54  "      pD=new PropertyDescriptor(\""+pname+"\",myClass);\n"+\r
55  "      pD.setDisplayName(\""+pName+"\");\n"+\r
56  "      pD.setPropertyEditorClass("+pName+"Editor.class);\n"+\r
57  "      pD.setBound(false);\n"+\r
58  "      pD.setConstrained(false);\n"+\r
59  "      pD.setHidden(false);\n"+\r
60  "      pD.setPreferred(true);\n"+\r
61  "      pD.setShortDescription(\""+pName+"\");\n"+\r
62  "      pD.setExpert(false);\n"+\r
63  "      properties[i++]=pD;\n");\r
64     }\r
65 \r
66      return (// Sorry not to respect usual identation here.\r
67  "// Brought to you by JTEM - JavaTtools for Experimental Mathematics\n"+\r
68  "// http://www.jtem.de\n"+\r
69  "// BeanInfo Maker GPL Copyright (C) 2002 JTEM-Group\n"+\r
70  "//\n"+\r
71  "// This program is free software; you can redistribute it and/or modify\n"+\r
72  "// it under the terms of the GNU General Public License as published by\n"+\r
73  "// the Free Software Foundation; either version 2 of the License, or\n"+\r
74  "// any later version.\n"+\r
75  "//\n"+\r
76  "// This program is distributed in the hope that it will be useful,\n"+\r
77  "// but WITHOUT ANY WARRANTY; without even the implied warranty of\n"+\r
78  "// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"+\r
79  "// GNU General Public License for more details.\n"+\r
80  "//\n"+\r
81  "// You should have received a copy of the GNU General Public License\n"+\r
82  "// along with this program; if not, write to the Free Software\n"+\r
83  "// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"+\r
84  "\n"+\r
85  ((packageName=="")?"": // Unknown package.\r
86  "package "+packageName+";\n")+\r
87  "\n"+\r
88  "\n"+\r
89  "import  java.beans.*;\n"+\r
90  "\n"+\r
91  "/**\n"+\r
92  " * Bean support class for {@link "+myClassName+"}\n"+\r
93  " * components.\n"+\r
94  " * @see BeanInfo\n"+\r
95  " */\n"+\r
96  "public class "+myClassName+"BeanInfo implements BeanInfo\n"+\r
97  "{\n"+\r
98  "  private static final BeanDescriptor BD\n"+\r
99  "                       =new BeanDescriptor("+myClassName+".class);\n"+\r
100  "  private static final PropertyDescriptor[] properties\n"+\r
101  "                       =new PropertyDescriptor["+pDA.length+"];\n"+\r
102  "  private static IntrospectionException initialError=null;\n"+\r
103  "  static\n"+\r
104  "  {\n"+\r
105  "    try\n"+\r
106  "    {\n"+\r
107  "      Class myClass="+myClassName+".class;\n"+\r
108  "      BD.setDisplayName(\""+myClassName+" nice name\");\n"+\r
109  "      PropertyDescriptor pD;\n"+\r
110  "      int i=0;\n"+\r
111  pDS+\r
112  "    }\n"+\r
113  "    catch(IntrospectionException ex)\n"+\r
114  "    {\n"+\r
115  "      initialError=ex;\n"+\r
116  "      ex.printStackTrace();\n"+\r
117  "    }\n"+\r
118  "  }\n"+\r
119  "  public "+myClassName+"BeanInfo() throws IntrospectionException\n"+\r
120  "  {\n"+\r
121  "    if(initialError!=null) throw initialError;\n"+\r
122  "  }\n"+\r
123  "  public BeanDescriptor getBeanDescriptor()\n"+\r
124  "  {\n"+\r
125  "    return BD;\n"+\r
126  "  }\n"+\r
127  "  public EventSetDescriptor[] getEventSetDescriptors()\n"+\r
128  "  {\n"+\r
129  "    return null;\n"+\r
130  "  }\n"+\r
131  "  public int getDefaultEventIndex()\n"+\r
132  "  {\n"+\r
133  "    return -1;\n"+\r
134  "  }\n"+\r
135  "  public PropertyDescriptor[] getPropertyDescriptors()\n"+\r
136  "  {\n"+\r
137  "//  System.out.println(\"providing good PropertyDescriptors\");\n"+\r
138  "    return properties;\n"+\r
139  "  }\n"+\r
140  "  public int getDefaultPropertyIndex()\n"+\r
141  "  {\n"+\r
142  "    return -1;\n"+\r
143  "  }\n"+\r
144  "  public MethodDescriptor[] getMethodDescriptors()\n"+\r
145  "  {\n"+\r
146  "    return null;\n"+\r
147  "  }\n"+\r
148  "  public BeanInfo[] getAdditionalBeanInfo()\n"+\r
149  "  {\n"+\r
150  "    return null;\n"+\r
151  "  }\n"+\r
152  "  public java.awt.Image getIcon(int iconKind)\n"+\r
153  "  {\n"+\r
154  "    return null;\n"+\r
155  "  }\n"+\r
156  "}\n"\r
157 );\r
158 \r
159 \r
160 }\r
161 \r
162 String beanInfoMaker(Object o){\r
163 \r
164     final           String fullClassName;\r
165     if (o.getClass()==Class) {\r
166         fullClassName = o.getName();    \r
167     } else {\r
168         fullClassName = o.getClass().getName();\r
169     }\r
170     return beanInfoMaker(fullClassName);\r
171 }\r
172 \r
173 void beanInfoMaker(Object o, String fileName) // throws IOException \r
174 {\r
175     \r
176     File outputFile = new File(fileName);\r
177 \r
178     FileWriter out = new FileWriter(outputFile);\r
179     out.write( beanInfoMaker(o));\r
180  \r
181     if (!fileName.endsWith("BeanInfo.java")) {\r
182         System.out.println("Name it \"MyClassBeanInfo.java\"");\r
183     }\r
184 \r
185     out.close();\r
186 }\r
187 \r