001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * DataProviderBeanInfoGenerator.java
043: *
044: * Created on February 16, 2005, 4:50 PM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.load;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.MethodInfo;
050: import java.io.File;
051: import java.io.FileOutputStream;
052: import java.io.PrintWriter;
053: import java.util.Date;
054: import org.netbeans.modules.visualweb.ejb.datamodel.MethodParam;
055:
056: /**
057: * This class is to generate the BeanInfo class for the generated DataProvider
058: *
059: * @author cao
060: */
061: public class DataProviderBeanInfoGenerator {
062:
063: // Hardcode the iconFileName here
064: public static final String DATA_PROVIDER_ICON_FILE_NAME = "/org/netbeans/modules/visualweb/ejb/resources/methodPublic.gif";
065: public static final String DATA_PROVIDER_ICON_FILE_NAME2 = "/org/netbeans/modules/visualweb/ejb/resources/table_dp_badge.png";
066:
067: private static final String[][] DEFAULT_PROPERTIES = {
068: { "dataClassInstance", "prop_dataClassInstance",
069: "\"getDataClassInstance\"" },
070: { "dataMethod", "prop_dataMethod", "\"getDataMethod\"" },
071: { "dataMethodArguments", "propDataMethodArguments",
072: "\"getDataMethodArguments\"" },
073: { "resultObject", "prop_resultObject",
074: "\"getResultObject\"" },
075: { "resultObjects", "prop_resultObjects",
076: "\"getResultObjects\"" } };
077:
078: private String fullBeanClassName;
079: private MethodInfo method;
080: private PrintWriter out;
081: private String clientWrapperClassName;
082:
083: public DataProviderBeanInfoGenerator(String fullBeanClassName,
084: MethodInfo method, String clientWrapperClassName) {
085: this .fullBeanClassName = fullBeanClassName;
086: this .method = method;
087: this .clientWrapperClassName = clientWrapperClassName;
088: }
089:
090: public ClassDescriptor generateClass(String srcDir)
091: throws EjbLoadException {
092:
093: // Declare it outside the try-catch so that the file name can be logged in case of exception
094: File javaFile = null;
095:
096: try {
097: // Figure out the package name, class name and directory/file name
098:
099: int j = fullBeanClassName.lastIndexOf(".");
100: String beanClassName = fullBeanClassName.substring(j + 1);
101:
102: // Package name
103: if (j == -1) // No package
104: j = 0;
105:
106: String packageName = fullBeanClassName.substring(0, j);
107:
108: String beanInfoClassName = beanClassName + "BeanInfo";
109:
110: String classDir = packageName.replace('.',
111: File.separatorChar);
112: File dirF = new File(srcDir + File.separator + classDir);
113: if (!dirF.exists()) {
114: if (!dirF.mkdirs())
115: System.out.println(".....failed to make dir"
116: + srcDir + File.separator + classDir);
117: }
118:
119: String beanInfoClassFile = beanInfoClassName + ".java";
120: javaFile = new File(dirF, beanInfoClassFile);
121: javaFile.createNewFile();
122:
123: ClassDescriptor classDescriptor = new ClassDescriptor(
124: beanInfoClassName, packageName, javaFile
125: .getAbsolutePath(), classDir
126: + File.separator + beanInfoClassFile);
127:
128: // Generate java code
129:
130: out = new PrintWriter(new FileOutputStream(javaFile));
131:
132: // pacage
133: if (packageName != null && packageName.length() != 0) {
134: out.println("package " + packageName + ";");
135: out.println();
136: }
137:
138: // comments
139: out.println("/**");
140: out.println(" * Source code created on " + new Date());
141: out.println(" */");
142: out.println();
143:
144: // Import
145: out.println("import java.awt.Image;");
146: out.println("import javax.swing.ImageIcon;");
147: out.println("import java.beans.BeanDescriptor;");
148: out.println("import java.beans.PropertyDescriptor;");
149: out.println("import java.beans.SimpleBeanInfo;");
150: out.println();
151:
152: // Start class
153: out.println("public class " + beanInfoClassName
154: + " extends SimpleBeanInfo {");
155: out.println();
156:
157: // Private variables
158: String beanClassVariable = "beanClass";
159: String iconFileNameVariable = "iconFileName";
160: String iconFileNameVariable2 = "iconFileName2";
161: String beanDescriptorVariable = "beanDescriptor";
162: String propDescriptorsVariable = "propDescriptors";
163: out.println(" private Class " + beanClassVariable
164: + " = " + fullBeanClassName + ".class;");
165: out.println(" private PropertyDescriptor[] "
166: + propDescriptorsVariable + " = null; ");
167: out.println(" private String " + iconFileNameVariable
168: + " = \"" + DATA_PROVIDER_ICON_FILE_NAME + "\";");
169: out.println(" private String " + iconFileNameVariable2
170: + " = \"" + DATA_PROVIDER_ICON_FILE_NAME2 + "\";");
171: out.println(" private BeanDescriptor "
172: + beanDescriptorVariable + " = null;");
173: out.println();
174:
175: // Method - getIcon()
176: out.println(" public Image getIcon(int iconKind) {");
177: out
178: .println(" ImageIcon imgIcon1 = new ImageIcon(getClass().getResource( "
179: + iconFileNameVariable + " )); ");
180: out
181: .println(" ImageIcon imgIcon2 = new ImageIcon(getClass().getResource( "
182: + iconFileNameVariable2 + " )); ");
183: out
184: .println(" return mergeImages( imgIcon1.getImage(), imgIcon2.getImage() );");
185: out.println(" }");
186: out.println();
187:
188: // Method - getBeanDescriptor()
189: out
190: .println(" public BeanDescriptor getBeanDescriptor() {");
191: out.println(" if( " + beanDescriptorVariable
192: + " == null ) {");
193: out.println(" " + beanDescriptorVariable
194: + " = new BeanDescriptor( " + beanClassVariable
195: + " );");
196: out.println(" " + beanDescriptorVariable
197: + ".setValue( \"trayComponent\", Boolean.TRUE );");
198: out.println(" }");
199: out.println(" return " + beanDescriptorVariable
200: + ";");
201: out.println(" }");
202: out.println();
203:
204: println(" public PropertyDescriptor[] getPropertyDescriptors() {");
205: println(" if (" + propDescriptorsVariable
206: + " != null) {");
207: println(" return " + propDescriptorsVariable
208: + ";");
209: println(" }");
210: println(" try {");
211:
212: String indent = " ";
213: for (int i = 0; i < DEFAULT_PROPERTIES.length; i++) {
214: String[] p = DEFAULT_PROPERTIES[i];
215: genPropertyDescriptor(indent, p[1], p[0], p[2], "null",
216: true, true);
217: }
218:
219: String clientClass = clientWrapperClassName;
220: String clientProp = Character.toLowerCase(clientClass
221: .charAt(0))
222: + clientClass.substring(1);
223:
224: String clientVar = "prop_" + clientProp;
225: String clientGetter = "\"get" + clientClass + "\"";
226: String clientSetter = "\"set" + clientClass + "\"";
227: genPropertyDescriptor(indent, clientVar, clientProp,
228: clientGetter, clientSetter, false, true);
229:
230: for (Object p : method.getParameters()) {
231: MethodParam param = (MethodParam) p;
232: String name = param.getName();
233: String propVar = "prop_" + name;
234: String nameCaps = Character.toUpperCase(name.charAt(0))
235: + name.substring(1);
236: String getter = "\"get" + nameCaps + "\"";
237: String setter = "\"set" + nameCaps + "\"";
238:
239: genPropertyDescriptor(indent, propVar, name, getter,
240: setter, false, false);
241: }
242:
243: println(" " + propDescriptorsVariable
244: + " = new PropertyDescriptor[] {");
245: for (int i = 0; i < DEFAULT_PROPERTIES.length; i++) {
246: println(" " + DEFAULT_PROPERTIES[i][1]
247: + ",");
248: }
249:
250: for (Object p : method.getParameters()) {
251: MethodParam param = (MethodParam) p;
252: println(" prop_" + param.getName() + ",");
253: }
254:
255: println(" " + clientVar);
256: println(" };");
257:
258: println(" return " + propDescriptorsVariable
259: + ";");
260: println(" }catch (java.beans.IntrospectionException e) {");
261: println(" e.printStackTrace();");
262: println(" return null;");
263: println(" }");
264: println(" }");
265: println();
266:
267: // private method for merging two images into one
268: out
269: .println(" private Image mergeImages (Image image1, Image image2) {");
270: out.println(" int w = image1.getWidth(null);");
271: out.println(" int h = image1.getHeight(null);");
272: out
273: .println(" int x = image1.getWidth(null) - image2.getWidth(null);");
274: out
275: .println(" int y = image1.getHeight(null) - image2.getHeight(null);");
276: out.println();
277: out
278: .println(" java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ().");
279: out
280: .println(" getDefaultScreenDevice ().getDefaultConfiguration ().");
281: out
282: .println(" getColorModel (java.awt.Transparency.BITMASK);");
283: out
284: .println(" java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model,");
285: out
286: .println(" model.createCompatibleWritableRaster (w, h), model.isAlphaPremultiplied (), null);");
287: out.println();
288: out
289: .println(" java.awt.Graphics g = buffImage.createGraphics ();");
290: out.println(" g.drawImage (image1, 0, 0, null);");
291: out.println(" g.drawImage (image2, x, y, null);");
292: out.println(" g.dispose();");
293: out.println();
294: out.println(" return buffImage;");
295: out.println(" }");
296: out.println();
297:
298: // End of class
299: out.println("}");
300:
301: out.flush();
302: out.close();
303:
304: return classDescriptor;
305: } catch (java.io.FileNotFoundException ex) {
306: // Log error
307: /*String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB " + ejbName
308: + ". Could not find file " + javaFile.getAbsolutePath();
309: ErrorManager.getDefault().getInstance( "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator" ).log( errMsg );*/
310: ex.printStackTrace();
311:
312: // Throw up as a SYSTEM_ERROR
313: throw new EjbLoadException(ex.getMessage());
314: } catch (java.io.IOException ex) {
315: // Log error
316: /*String errMsg = "Error occurred when trying to generate the wrapper bean class for EJB " + ejbName
317: + ". Could not create file " + javaFile.getAbsolutePath();
318: ErrorManager.getDefault().getInstance( "org.netbeans.modules.visualweb.ejb.load.ClientBeanInfoGenerator" ).log( errMsg );*/
319: ex.printStackTrace();
320:
321: // Throw up as a SYSTEM_ERROR
322: throw new EjbLoadException(ex.getMessage());
323: }
324: }
325:
326: private void genPropertyDescriptor(String indent, String propVar,
327: String prop, String getter, String setter, boolean hidden,
328: boolean useCustomEditor) {
329: println(indent + "PropertyDescriptor " + propVar
330: + " = new PropertyDescriptor(\"" + prop
331: + "\",beanClass," + getter + "," + setter + ");");
332:
333: println(indent + propVar + ".setExpert(false);");
334:
335: if (hidden) {
336: println(indent + propVar + ".setHidden(true);");
337: } else {
338: println(indent + propVar + ".setHidden(false);");
339: }
340:
341: println(indent + propVar + ".setPreferred(false);");
342:
343: if (useCustomEditor) {
344: println(indent
345: + propVar
346: + ".setPropertyEditorClass(com.sun.rave.propertyeditors.SelectOneDomainEditor.class);");
347: println(indent
348: + propVar
349: + ".setValue(com.sun.rave.designtime.Constants.PropertyDescriptor.CATEGORY,com.sun.rave.designtime.base.CategoryDescriptors.DATA);");
350: println(indent
351: + propVar
352: + ".setValue(\"com.sun.rave.propertyeditors.DOMAIN_CLASS\", com.sun.rave.propertyeditors.domains.InstanceVariableDomain.class);");
353: }
354: }
355:
356: private void println(String string) {
357: out.println(string);
358: }
359:
360: private void println() {
361: out.println();
362: }
363: }
|