KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > nodes > WebServiceMethodNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.registry.nodes;
21
22 import org.netbeans.modules.websvc.api.registry.WebServiceMethod;
23 import org.openide.nodes.PropertySupport.ReadOnly;
24 import org.openide.nodes.Sheet;
25 import org.openide.nodes.*;
26 import org.openide.nodes.Node.Cookie;
27 import org.openide.nodes.PropertySupport.Reflection;
28 import org.openide.nodes.Sheet.Set;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.NbBundle;
31 import org.openide.util.actions.SystemAction;
32 import org.openide.util.Utilities;
33
34 import org.netbeans.modules.websvc.registry.actions.TestWebServiceMethodAction;
35 import org.netbeans.modules.websvc.registry.util.Util;
36
37 import com.sun.xml.rpc.processor.model.Operation;
38 import com.sun.xml.rpc.processor.model.Port;
39 import com.sun.xml.rpc.processor.model.java.JavaMethod;
40 import com.sun.xml.rpc.processor.model.java.JavaType;
41 import com.sun.xml.rpc.processor.model.java.JavaParameter;
42
43 import java.awt.Image JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import javax.swing.Action JavaDoc;
46
47 /**
48  * A simple node with no children.
49  * Often used in conjunction with some kind of underlying data model, where
50  * each node represents an element in that model. In this case, you should see
51  * the Container Node template which will permit you to create a whole tree of
52  * such nodes with the proper behavior.
53  * @author octav
54  */

55 public class WebServiceMethodNode extends AbstractNode implements WebServiceMethod {
56     
57     private JavaMethod javaMethod;
58     private Port port;
59     
60     public WebServiceMethodNode() {
61         this(null,null);
62     }
63     
64     public Object JavaDoc getJavaMethod() {
65         return javaMethod;
66     }
67     // will frequently accept an element from some data model in the constructor:
68
public WebServiceMethodNode(Port inPort, Operation inOperation) {
69         super(Children.LEAF);
70 // setDefaultAction(SystemAction.get(TestWebServiceMethodAction.class));
71

72         /**
73          * Bug fix: 5059732
74          * We need to get the port that this method is on to correctly get the type for the parameter.
75          *
76          */

77         
78         port = inPort;
79         if(null == inOperation) {
80             return;
81         }
82         javaMethod = inOperation.getJavaMethod();
83         setName(javaMethod.getName());
84         setIconBaseWithExtension("org/netbeans/modules/websvc/registry/resources/methodicon.png");
85         
86         /**
87          * Set the shortDescription (tooltip) to the method signature
88          */

89         String JavaDoc signature = javaMethod.getReturnType().getFormalName() + " " + javaMethod.getName() + "(";
90         Iterator JavaDoc parameterIterator = javaMethod.getParameters();
91         JavaParameter currentParam = null;
92         while(parameterIterator.hasNext()) {
93             currentParam = (JavaParameter)parameterIterator.next();
94             String JavaDoc parameterType = Util.getParameterType(inPort, currentParam);
95             signature += parameterType + " " + currentParam.getName();
96             if(parameterIterator.hasNext()) {
97                 signature += ", ";
98             }
99         }
100         signature += ")";
101         
102         setShortDescription(signature);
103         // Add cookies, e.g.:
104
/*
105         getCookieSet().add(new OpenCookie() {
106                 public void open() {
107                     // Open something useful...
108                     // will typically use the data model somehow
109                 }
110             });
111          */

112         // If this node represents an element in a data model of some sort, consider
113
// creating your own cookie which captures the existence of that underlying data,
114
// and add it to the cookie set. Then you can write actions sensitive to that cookie,
115
// and they will not need to directly refer to this node class - only to the cookie
116
// and the data model.
117

118         getCookieSet().add(this);
119     }
120     
121     // Create the popup menu:
122
// protected SystemAction[] createActions() {
123
// return new SystemAction[] {
124
// SystemAction.get(TestWebServiceMethodAction.class)
125
// };
126
// }
127

128     public Action[] getActions(boolean context) {
129         return new SystemAction[] {
130             SystemAction.get(TestWebServiceMethodAction.class)
131         };
132     }
133     
134     public Action getPreferredAction() {
135         return SystemAction.get(TestWebServiceMethodAction.class);
136     }
137     
138     public HelpCtx getHelpCtx() {
139         return HelpCtx.DEFAULT_HELP;
140         // When you have help, change to:
141
// return new HelpCtx(WebServiceMethodNode.class);
142
}
143     
144     // RECOMMENDED - handle cloning specially (so as not to invoke the overhead of FilterNode):
145
/*
146     public Node cloneNode() {
147         // Try to pass in similar constructor params to what you originally got,
148         // typically meaning passing in the same data model element:
149         return new WebServiceMethodNode();
150     }
151      */

152     
153     /**
154      * Create a property sheet for the individual method node
155      * @return property sheet for the data source nodes
156      */

157     protected Sheet createSheet() {
158         Sheet sheet = super.createSheet();
159         Set ss = sheet.get("data"); // NOI18N
160

161         if (ss == null) {
162             ss = new Set();
163             ss.setName("data"); // NOI18N
164
ss.setDisplayName(NbBundle.getMessage(WebServiceMethodNode.class, "METHOD_INFO"));
165             ss.setShortDescription(NbBundle.getMessage(WebServiceMethodNode.class, "METHOD_INFO"));
166             sheet.put(ss);
167         }
168         
169         try {
170             Reflection p;
171             
172             p = new Reflection(javaMethod, String JavaDoc.class, "getName", null); // NOI18N
173
p.setName("name"); // NOI18N
174
p.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_NAME"));
175             p.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_NAME"));
176             ss.put(p);
177             String JavaDoc signature = javaMethod.getReturnType().getRealName() + " " +
178             javaMethod.getName() + "(";
179             
180             Iterator JavaDoc tempIterator = javaMethod.getParameters();
181             JavaParameter currentparam = null;
182             while(tempIterator.hasNext()) {
183                 currentparam = (JavaParameter)tempIterator.next();
184                 signature += currentparam.getType().getRealName() + " " + currentparam.getName();
185                 if(tempIterator.hasNext()) {
186                     signature += ", ";
187                 }
188             }
189             
190             signature += ")";
191             
192             Iterator JavaDoc excpIterator = javaMethod.getExceptions();
193             if(excpIterator.hasNext()) {
194                 signature += " throws";
195                 while(excpIterator.hasNext()) {
196                     String JavaDoc currentExcp = (String JavaDoc)excpIterator.next();
197                     signature += " " + currentExcp;
198                     if(excpIterator.hasNext()) {
199                         signature += ",";
200                     }
201                 }
202                 
203                 
204             }
205             
206             p = new Reflection(signature, String JavaDoc.class, "toString", null); // NOI18N
207
p.setName("signature"); // NOI18N
208
p.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_SIGNATURE"));
209             p.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_SIGNATURE"));
210             ss.put(p);
211             
212             p = new Reflection(javaMethod.getReturnType(), String JavaDoc.class, "getRealName", null); // NOI18N
213
p.setName("returntype"); // NOI18N
214
p.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_RETURNTYPE"));
215             p.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_RETURNTYPE"));
216             ss.put(p);
217             
218             Set paramSet = sheet.get("parameters"); // NOI18N
219
if (paramSet == null) {
220                 paramSet = new Sheet.Set();
221                 paramSet.setName("parameters"); // NOI18N
222
paramSet.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMDIVIDER")); // NOI18N
223
paramSet.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMDIVIDER")); // NOI18N
224
sheet.put(paramSet);
225             }
226             Iterator JavaDoc paramIterator = javaMethod.getParameters();
227             if(paramIterator.hasNext()) {
228                 p = new Reflection(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMTYPE"),
229                 String JavaDoc.class,
230                 "toString",
231                 null); // NOI18N
232
p.setName("paramdivider2"); // NOI18N
233
p.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMNAME"));
234                 p.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMNAME") +
235                 "-" + NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMTYPE"));
236                 paramSet.put(p);
237                 
238                 
239                 JavaParameter currentParameter = null;
240                 for(int ii=0;paramIterator.hasNext();ii++) {
241                     currentParameter = (JavaParameter)paramIterator.next();
242                     if(currentParameter.getType().isHolder()) {
243                         p = new Reflection(Util.getParameterType(port,currentParameter), String JavaDoc.class, "toString", null); // NOI18N
244
} else {
245                         p = new Reflection(currentParameter.getType(), String JavaDoc.class, "getRealName", null); // NOI18N
246
}
247                     p.setName("paramname" + ii); // NOI18N
248
p.setDisplayName(currentParameter.getName());
249                     p.setShortDescription(currentParameter.getName() + "-" +
250                     currentParameter.getType().getRealName());
251                     paramSet.put(p);
252                 }
253             }
254             Set exceptionSet = sheet.get("exceptions"); // NOI18N
255
if (exceptionSet == null) {
256                 exceptionSet = new Sheet.Set();
257                 exceptionSet.setName("exceptions"); // NOI18N
258
exceptionSet.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_EXCEPTIONDIVIDER")); // NOI18N
259
exceptionSet.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_EXCEPTIONDIVIDER")); // NOI18N
260
sheet.put(exceptionSet);
261             }
262             
263             Iterator JavaDoc exceptionIterator = javaMethod.getExceptions();
264             String JavaDoc currentException = null;
265             for(int ii=0;exceptionIterator.hasNext();ii++) {
266                 currentException = (String JavaDoc)exceptionIterator.next();
267                 p = new Reflection(currentException, String JavaDoc.class, "toString", null); // NOI18N
268
p.setName("exception" + ii); // NOI18N
269
p.setDisplayName(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMTYPE"));
270                 p.setShortDescription(NbBundle.getMessage(WebServicesNode.class, "METHOD_PARAMTYPE"));
271                 exceptionSet.put(p);
272             }
273         } catch (NoSuchMethodException JavaDoc nsme) {
274             nsme.printStackTrace();
275         }
276         
277         return sheet;
278     }
279     
280     // Handle renaming:
281
/*
282     public boolean canRename() {
283         return true;
284     }
285     public void setName(String nue) {
286         // Typically implemented by changing the name of an element from an underlying
287         // data model. This node class should be listening to changes in the name of the
288         // element and calling super.setName when it notices any (or better, override getName
289         // and perhaps getDisplayName and call fireNameChange).
290         // For example, if there is an instance field
291         // private final MyDataElement data;
292         // then you might write this method as:
293         // data.setID(nue);
294         // where you would also have:
295         // public String getName() {return data.getID();}
296         // and in the constructor, if WebServiceMethodNode implements ModelListener:
297         // data.addModelListener((ModelListener)WeakListener.create(ModelListener.class, this, data));
298         // where the interface is implemented as:
299         // public void modelChanged(ModelEvent ev) {fireNameChange(null, null);}
300     }
301      */

302     
303     // Handle deleting:
304
/*
305     public boolean canDestroy() {
306         return true;
307     }
308     public void destroy() throws IOException {
309         // Typically implemented by removing an element from an underlying data model.
310         // For example, if there is an instance field
311         // private final MyDataElement data;
312         // then you might write this method as:
313         // data.getContainingModel().removeElement(data);
314         // The parent container children should be listening to the model, notice
315         // the removal, set a new key list without this data element, and thus
316         // remove this node from its children list.
317     }
318      */

319     
320     // Handle copying and cutting specially:
321
/*
322     public boolean canCopy() {
323         return true;
324     }
325     public boolean canCut() {
326         return true;
327     }
328     public Transferable clipboardCopy() {
329         // Add to, do not replace, the default node copy flavor:
330         ExTransferable et = ExTransferable.create(super.clipboardCopy());
331         et.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
332                 protected Object getData() {
333                     // just an example:
334                     return WebServiceMethodNode.this.getDisplayName();
335                     // more commonly, will use some underlying data model
336                 }
337             });
338         return et;
339     }
340     public Transferable clipboardCut() {
341         // Add to, do not replace, the default node cut flavor:
342         ExTransferable et = ExTransferable.create(super.clipboardCut());
343         // This is not so useful because this node will not be destroyed afterwards
344         // (it is up to the paste type to decide whether to remove the "original",
345         // and it is not safe to assume that getData will only be called once):
346         et.put(new ExTransferable.Single(DataFlavor.stringFlavor) {
347                 protected Object getData() {
348                     // just an example:
349                     return WebServiceMethodNode.this.getDisplayName();
350                     // more commonly, will use some underlying data model
351                 }
352             });
353         return et;
354     }
355      */

356     
357     // Permit user to customize whole node at once (instead of per-property):
358
/*
359     public boolean hasCustomizer() {
360         return true;
361     }
362     public Component getCustomizer() {
363         // more commonly, will pass in underlying data:
364         return new MyCustomizingPanel(this);
365     }
366      */

367     
368 }
369
Popular Tags