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: package org.netbeans.modules.visualweb.websvcmgr.codegen;
043:
044: import java.io.Writer;
045: import java.util.Date;
046: import org.netbeans.modules.websvc.manager.util.ManagerUtil;
047:
048: public class DataProviderBeanInfoWriter extends java.io.PrintWriter {
049:
050: // Hardcode the iconFileName here
051: public static final String DATA_PROVIDER_ICON_FILE_NAME = "methodicon.png";
052: public static final String DATA_PROVIDER_ICON_FILE_NAME2 = "table_dp_badge.png";
053:
054: private static final String[][] DEFAULT_PROPERTIES = {
055: { "dataClassInstance", "prop_dataClassInstance",
056: "\"getDataClassInstance\"" },
057: { "dataMethod", "prop_dataMethod", "\"getDataMethod\"" },
058: { "dataMethodArguments", "propDataMethodArguments",
059: "\"getDataMethodArguments\"" },
060: { "resultObject", "prop_resultObject",
061: "\"getResultObject\"" },
062: { "resultObjects", "prop_resultObjects",
063: "\"getResultObjects\"" } };
064:
065: private DataProviderInfo dataProviderInfo;
066:
067: public DataProviderBeanInfoWriter(Writer writer,
068: DataProviderInfo dataProviderInfo) {
069: super (writer);
070: this .dataProviderInfo = dataProviderInfo;
071: }
072:
073: public void writeClass() {
074: // package
075: println("package " + dataProviderInfo.getPackageName() + ";");
076: println();
077:
078: // comments
079: println("/**");
080: println(" * Source code created on " + new Date());
081: println(" */");
082: println();
083:
084: // Import
085: println("import java.awt.Image;");
086: println("import javax.swing.ImageIcon;");
087: println("import java.beans.BeanDescriptor;");
088: println("import java.beans.PropertyDescriptor;");
089: println("import java.beans.SimpleBeanInfo;");
090: println();
091:
092: // Start class
093: String beanInfoClassName = dataProviderInfo.getClassName()
094: + "BeanInfo";
095: println("public class " + beanInfoClassName
096: + " extends SimpleBeanInfo {");
097: println();
098:
099: // Private variables
100: String beanClassVariable = "beanClass";
101: String iconFileNameVariable = "iconFileName";
102: String iconFileNameVariable2 = "iconFileName2";
103: String beanDescriptorVariable = "beanDescriptor";
104: String propDescriptorsVariable = "propDescriptors";
105: println(" private Class " + beanClassVariable + " = "
106: + dataProviderInfo.getClassName() + ".class;");
107: println(" private PropertyDescriptor[] "
108: + propDescriptorsVariable + " = null; ");
109: println(" private String " + iconFileNameVariable + " = \""
110: + DATA_PROVIDER_ICON_FILE_NAME + "\";");
111: println(" private String " + iconFileNameVariable2 + " = \""
112: + DATA_PROVIDER_ICON_FILE_NAME2 + "\";");
113: println(" private BeanDescriptor " + beanDescriptorVariable
114: + " = null;");
115: println();
116:
117: // Method - getIcon()
118: println(" public Image getIcon(int iconKind) {");
119: println(" ImageIcon imgIcon1 = new ImageIcon(getClass().getResource( "
120: + iconFileNameVariable + " )); ");
121: println(" ImageIcon imgIcon2 = new ImageIcon(getClass().getResource( "
122: + iconFileNameVariable2 + " )); ");
123: println(" return mergeImages( imgIcon1.getImage(), imgIcon2.getImage() );");
124: println(" }");
125: println();
126:
127: println(" public PropertyDescriptor[] getPropertyDescriptors() {");
128: println(" if (" + propDescriptorsVariable
129: + " != null) {");
130: println(" return " + propDescriptorsVariable + ";");
131: println(" }");
132: println(" try {");
133:
134: String indent = " ";
135: for (int i = 0; i < DEFAULT_PROPERTIES.length; i++) {
136: String[] p = DEFAULT_PROPERTIES[i];
137: genPropertyDescriptor(indent, p[1], p[0], p[2], "null",
138: true, true);
139: }
140: String clientClass = dataProviderInfo
141: .getClientWrapperClassName();
142: String clientProp = ManagerUtil
143: .makeValidJavaBeanName(ManagerUtil
144: .decapitalize(clientClass));
145: String clientVar = "prop_" + clientProp;
146: String clientGetter = "\"get" + clientClass + "\"";
147: String clientSetter = "\"set" + clientClass + "\"";
148: genPropertyDescriptor(indent, clientVar, clientProp,
149: clientGetter, clientSetter, false, true);
150:
151: for (DataProviderParameter p : dataProviderInfo.getMethod()
152: .getParameters()) {
153: String name = p.getName();
154: String propVar = "prop_" + name;
155: String nameCaps = ManagerUtil.upperCaseFirstChar(name);
156: String getter = "\"get" + nameCaps + "\"";
157: String setter = "\"set" + nameCaps + "\"";
158:
159: genPropertyDescriptor(indent, propVar, name, getter,
160: setter, false, false);
161: }
162:
163: println(" " + propDescriptorsVariable
164: + " = new PropertyDescriptor[] {");
165: for (int i = 0; i < DEFAULT_PROPERTIES.length; i++) {
166: println(" " + DEFAULT_PROPERTIES[i][1] + ",");
167: }
168: for (DataProviderParameter p : dataProviderInfo.getMethod()
169: .getParameters()) {
170: println(" prop_" + p.getName() + ",");
171: }
172:
173: println(" " + clientVar);
174: println(" };");
175:
176: println(" return " + propDescriptorsVariable + ";");
177: println(" }catch (java.beans.IntrospectionException e) {");
178: println(" e.printStackTrace();");
179: println(" return null;");
180: println(" }");
181: println(" }");
182: println();
183:
184: // private method for merging two images into one
185: println(" private Image mergeImages (Image image1, Image image2) {");
186: println(" int w = image1.getWidth(null);");
187: println(" int h = image1.getHeight(null);");
188: println(" int x = image1.getWidth(null) - image2.getWidth(null);");
189: println(" int y = image1.getHeight(null) - image2.getHeight(null);");
190: println();
191: println(" java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment ().");
192: println(" getDefaultScreenDevice ().getDefaultConfiguration ().");
193: println(" getColorModel (java.awt.Transparency.BITMASK);");
194: println(" java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model,");
195: println(" model.createCompatibleWritableRaster (w, h), model.isAlphaPremultiplied (), null);");
196: println();
197: println(" java.awt.Graphics g = buffImage.createGraphics ();");
198: println(" g.drawImage (image1, 0, 0, null);");
199: println(" g.drawImage (image2, x, y, null);");
200: println(" g.dispose();");
201: println();
202: println(" return buffImage;");
203: println(" }");
204: println();
205:
206: // Method - getBeanDescriptor()
207: println(" public BeanDescriptor getBeanDescriptor() {");
208: println(" if( " + beanDescriptorVariable
209: + " == null ) {");
210: println(" " + beanDescriptorVariable
211: + " = new BeanDescriptor( " + beanClassVariable + " );");
212: println(" " + beanDescriptorVariable
213: + ".setValue( \"trayComponent\", Boolean.TRUE );");
214: println(" }");
215: println(" return " + beanDescriptorVariable + ";");
216: println(" }");
217: println("}");
218: }
219:
220: private void genPropertyDescriptor(String indent, String propVar,
221: String prop, String getter, String setter, boolean hidden,
222: boolean useCustomEditor) {
223: println(indent + "PropertyDescriptor " + propVar
224: + " = new PropertyDescriptor(\"" + prop
225: + "\",beanClass," + getter + "," + setter + ");");
226:
227: println(indent + propVar + ".setExpert(false);");
228:
229: if (hidden) {
230: println(indent + propVar + ".setHidden(true);");
231: } else {
232: println(indent + propVar + ".setHidden(false);");
233: }
234:
235: println(indent + propVar + ".setPreferred(false);");
236:
237: if (useCustomEditor) {
238: println(indent
239: + propVar
240: + ".setPropertyEditorClass(com.sun.rave.propertyeditors.SelectOneDomainEditor.class);");
241: println(indent
242: + propVar
243: + ".setValue(com.sun.rave.designtime.Constants.PropertyDescriptor.CATEGORY,com.sun.rave.designtime.base.CategoryDescriptors.DATA);");
244: println(indent
245: + propVar
246: + ".setValue(\"com.sun.rave.propertyeditors.DOMAIN_CLASS\", com.sun.rave.propertyeditors.domains.InstanceVariableDomain.class);");
247: }
248: }
249:
250: }
|