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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.websvc.saas.ui.nodes;
041:
042: import com.sun.tools.ws.processor.model.java.JavaMethod;
043: import com.sun.tools.ws.processor.model.java.JavaParameter;
044: import java.awt.Image;
045: import java.awt.datatransfer.Transferable;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import javax.swing.Action;
053: import org.netbeans.modules.websvc.saas.model.Saas;
054: import org.netbeans.modules.websvc.saas.model.WsdlSaasMethod;
055: import org.netbeans.modules.websvc.saas.spi.SaasNodeActionsProvider;
056: import org.netbeans.modules.websvc.saas.ui.actions.TestMethodAction;
057: import org.netbeans.modules.websvc.saas.util.SaasTransferable;
058: import org.netbeans.modules.websvc.saas.util.SaasUtil;
059: import org.netbeans.modules.websvc.saas.util.TypeUtil;
060: import org.openide.nodes.AbstractNode;
061: import org.openide.nodes.Children;
062: import org.openide.nodes.PropertySupport.Reflection;
063: import org.openide.nodes.Sheet;
064: import org.openide.nodes.Sheet.Set;
065: import org.openide.util.NbBundle;
066: import org.openide.util.Utilities;
067: import org.openide.util.actions.SystemAction;
068: import org.openide.util.datatransfer.ExTransferable;
069: import org.openide.util.lookup.AbstractLookup;
070: import org.openide.util.lookup.InstanceContent;
071:
072: /**
073: *
074: * @author nam
075: */
076: public class WsdlMethodNode extends AbstractNode {
077: WsdlSaasMethod method;
078: private Transferable transferable;
079:
080: public WsdlMethodNode(WsdlSaasMethod method) {
081: this (method, new InstanceContent());
082: }
083:
084: protected WsdlMethodNode(WsdlSaasMethod method,
085: InstanceContent content) {
086: super (Children.LEAF, new AbstractLookup(content));
087: this .method = method;
088: content.add(method);
089: transferable = ExTransferable
090: .create(new SaasTransferable<WsdlSaasMethod>(method,
091: SaasTransferable.WSDL_METHOD_FLAVORS));
092: }
093:
094: @Override
095: public String getDisplayName() {
096: return method.getName();
097: }
098:
099: @Override
100: public String getShortDescription() {
101: JavaMethod javaMethod = method.getJavaMethod();
102: String signature = javaMethod.getReturnType().getFormalName()
103: + " " + javaMethod.getName() + "(";
104: Iterator parameterIterator = javaMethod.getParametersList()
105: .iterator();
106: JavaParameter currentParam = null;
107: while (parameterIterator.hasNext()) {
108: currentParam = (JavaParameter) parameterIterator.next();
109: String parameterType = TypeUtil
110: .getParameterType(currentParam);
111: signature += parameterType + " " + currentParam.getName();
112: if (parameterIterator.hasNext()) {
113: signature += ", ";
114: }
115: }
116: signature += ")";
117: return signature;
118: }
119:
120: @Override
121: public Action[] getActions(boolean context) {
122: List<Action> actions = SaasNode.getActions(getLookup());
123: //TODO maybe ???
124: actions.add(SystemAction.get(TestMethodAction.class));
125: return actions.toArray(new Action[actions.size()]);
126: }
127:
128: public Action getPreferredAction() {
129: Action[] actions = getActions(true);
130: return actions.length > 0 ? actions[0] : null;
131: }
132:
133: public Image getIcon(int type) {
134: return getMethodIcon();
135: }
136:
137: public Image getOpenedIcon(int type) {
138: return getMethodIcon();
139: }
140:
141: private Image getMethodIcon() {
142: JavaMethod javaMethod = method.getJavaMethod();
143: if (javaMethod != null
144: && !"void".equals(javaMethod.getReturnType()
145: .getRealName())) {
146: Image image1 = Utilities
147: .loadImage("org/netbeans/modules/websvc/manager/resources/methodicon.png");
148: Image image2 = Utilities
149: .loadImage("org/netbeans/modules/websvc/manager/resources/table_dp_badge.png");
150: int x = image1.getWidth(null) - image2.getWidth(null);
151: int y = image1.getHeight(null) - image2.getHeight(null);
152: return Utilities.mergeImages(image1, image2, x, y);
153: } else
154: return Utilities
155: .loadImage("org/netbeans/modules/websvc/manager/resources/methodicon.png");
156: }
157:
158: /**
159: * Create a property sheet for the individual method node
160: * @return property sheet for the data source nodes
161: */
162: protected Sheet createSheet() {
163: JavaMethod javaMethod = method.getJavaMethod();
164: Sheet sheet = super .createSheet();
165: Set ss = sheet.get("data"); // NOI18N
166:
167: if (ss == null) {
168: ss = new Set();
169: ss.setName("data"); // NOI18N
170: ss.setDisplayName(NbBundle.getMessage(WsdlMethodNode.class,
171: "METHOD_INFO"));
172: ss.setShortDescription(NbBundle.getMessage(
173: WsdlMethodNode.class, "METHOD_INFO"));
174: sheet.put(ss);
175: }
176:
177: if (javaMethod == null) {
178: return sheet;
179: }
180:
181: try {
182: Reflection p;
183:
184: p = new Reflection(javaMethod, String.class, "getName",
185: null); // NOI18N
186: p.setName("name"); // NOI18N
187: p.setDisplayName(NbBundle.getMessage(WsdlMethodNode.class,
188: "METHOD_NAME"));
189: p.setShortDescription(NbBundle.getMessage(
190: WsdlMethodNode.class, "METHOD_NAME"));
191: ss.put(p);
192: String signature = javaMethod.getReturnType().getRealName()
193: + " " + javaMethod.getName() + "(";
194:
195: Iterator tempIterator = javaMethod.getParametersList()
196: .iterator();
197: JavaParameter currentparam = null;
198: while (tempIterator.hasNext()) {
199: currentparam = (JavaParameter) tempIterator.next();
200: signature += currentparam.getType().getRealName() + " "
201: + currentparam.getName();
202: if (tempIterator.hasNext()) {
203: signature += ", ";
204: }
205: }
206:
207: signature += ")";
208:
209: Iterator excpIterator = javaMethod.getExceptions();
210: if (excpIterator.hasNext()) {
211: signature += " throws";
212: while (excpIterator.hasNext()) {
213: String currentExcp = (String) excpIterator.next();
214: signature += " " + currentExcp;
215: if (excpIterator.hasNext()) {
216: signature += ",";
217: }
218: }
219:
220: }
221:
222: p = new Reflection(signature, String.class, "toString",
223: null); // NOI18N
224: p.setName("signature"); // NOI18N
225: p.setDisplayName(NbBundle.getMessage(WsdlMethodNode.class,
226: "METHOD_SIGNATURE"));
227: p.setShortDescription(NbBundle.getMessage(
228: WsdlMethodNode.class, "METHOD_SIGNATURE"));
229: ss.put(p);
230:
231: p = new Reflection(javaMethod.getReturnType(),
232: String.class, "getRealName", null); // NOI18N
233: p.setName("returntype"); // NOI18N
234: p.setDisplayName(NbBundle.getMessage(WsdlMethodNode.class,
235: "METHOD_RETURNTYPE"));
236: p.setShortDescription(NbBundle.getMessage(
237: WsdlMethodNode.class, "METHOD_RETURNTYPE"));
238: ss.put(p);
239:
240: Set paramSet = sheet.get("parameters"); // NOI18N
241: if (paramSet == null) {
242: paramSet = new Sheet.Set();
243: paramSet.setName("parameters"); // NOI18N
244: paramSet.setDisplayName(NbBundle.getMessage(
245: WsdlMethodNode.class, "METHOD_PARAMDIVIDER")); // NOI18N
246: paramSet.setShortDescription(NbBundle.getMessage(
247: WsdlMethodNode.class, "METHOD_PARAMDIVIDER")); // NOI18N
248: sheet.put(paramSet);
249: }
250: Iterator paramIterator = javaMethod.getParametersList()
251: .iterator();
252: if (paramIterator.hasNext()) {
253: p = new Reflection(NbBundle.getMessage(
254: WsdlMethodNode.class, "METHOD_PARAMTYPE"),
255: String.class, "toString", null); // NOI18N
256: p.setName("paramdivider2"); // NOI18N
257:
258: JavaParameter currentParameter = null;
259: for (int ii = 0; paramIterator.hasNext(); ii++) {
260: currentParameter = (JavaParameter) paramIterator
261: .next();
262: if (currentParameter.getType().isHolder()) {
263: p = new Reflection(TypeUtil
264: .getParameterType(currentParameter),
265: String.class, "toString", null); // NOI18N
266: } else {
267: p = new Reflection(currentParameter.getType(),
268: String.class, "getRealName", null); // NOI18N
269: }
270: p.setName("paramname" + ii); // NOI18N
271: p.setDisplayName(currentParameter.getName());
272: p.setShortDescription(currentParameter.getName()
273: + "-"
274: + currentParameter.getType().getRealName());
275: paramSet.put(p);
276: }
277: }
278: Set exceptionSet = sheet.get("exceptions"); // NOI18N
279: if (exceptionSet == null) {
280: exceptionSet = new Sheet.Set();
281: exceptionSet.setName("exceptions"); // NOI18N
282: exceptionSet.setDisplayName(NbBundle
283: .getMessage(WsdlMethodNode.class,
284: "METHOD_EXCEPTIONDIVIDER")); // NOI18N
285: exceptionSet.setShortDescription(NbBundle
286: .getMessage(WsdlMethodNode.class,
287: "METHOD_EXCEPTIONDIVIDER")); // NOI18N
288: sheet.put(exceptionSet);
289: }
290:
291: Iterator exceptionIterator = javaMethod.getExceptions();
292: String currentException = null;
293: for (int ii = 0; exceptionIterator.hasNext(); ii++) {
294: currentException = (String) exceptionIterator.next();
295: p = new Reflection(currentException, String.class,
296: "toString", null); // NOI18N
297: p.setName("exception" + ii); // NOI18N
298: p.setDisplayName(NbBundle.getMessage(
299: WsdlMethodNode.class, "METHOD_PARAMTYPE"));
300: p.setShortDescription(NbBundle.getMessage(
301: WsdlMethodNode.class, "METHOD_PARAMTYPE"));
302: exceptionSet.put(p);
303: }
304: } catch (NoSuchMethodException nsme) {
305: Logger.getLogger(getClass().getName()).log(Level.INFO,
306: nsme.getLocalizedMessage(), nsme);
307: }
308:
309: return sheet;
310: }
311:
312: // Handle copying and cutting specially:
313:
314: public boolean canCopy() {
315: return true;
316: }
317:
318: public boolean canCut() {
319: return false;
320: }
321:
322: @Override
323: public Transferable clipboardCopy() throws IOException {
324: if (method.getSaas().getState() == Saas.State.READY) {
325: return SaasTransferable.addFlavors(transferable);
326: } else {
327: method.getSaas().toStateReady(false);
328: return super.clipboardCopy();
329: }
330:
331: }
332:
333: }
|