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-2006 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: package org.netbeans.modules.websvc.jaxrpc.nodes;
042:
043: import java.util.List;
044: import java.util.ArrayList;
045: import java.util.Set;
046: import java.io.IOException;
047: import javax.swing.SwingUtilities;
048:
049: import javax.lang.model.element.Element;
050: import javax.lang.model.element.ExecutableElement;
051: import javax.lang.model.element.Modifier;
052: import javax.lang.model.element.TypeElement;
053: import javax.lang.model.element.ExecutableElement;
054: import javax.lang.model.util.ElementFilter;
055:
056: import org.openide.filesystems.FileObject;
057: import org.openide.nodes.Node;
058: import org.openide.nodes.AbstractNode;
059: import org.openide.nodes.Children;
060: import org.openide.ErrorManager;
061:
062: import org.netbeans.api.java.source.JavaSource;
063: import org.netbeans.api.java.source.CancellableTask;
064: import org.netbeans.api.java.source.CompilationController;
065: import static org.netbeans.api.java.source.JavaSource.Phase;
066:
067: import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
068: import org.netbeans.modules.websvc.api.support.java.SourceUtils;
069: import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
070: import org.openide.filesystems.FileChangeAdapter;
071: import org.openide.filesystems.FileEvent;
072:
073: public class WebServiceChildren extends Children.Keys {
074:
075: private static final String OPERATION_ICON = "org/netbeans/modules/websvc/core/webservices/ui/resources/wsoperation.png"; //NOI18N
076: private java.awt.Image cachedIcon;
077:
078: private WebserviceDescription webServiceDescription;
079: private FileObject implClass;
080: private FileObject srcRoot;
081: private FileChangeAdapter implFileListener;
082:
083: public WebServiceChildren(
084: WebserviceDescription webServiceDescription,
085: FileObject srcRoot, FileObject implClass) {
086: super ();
087: this .webServiceDescription = webServiceDescription;
088: this .srcRoot = srcRoot;
089: this .implClass = implClass;
090: }
091:
092: protected Node[] createNodes(Object key) {
093: if (key instanceof ExecutableElement) {
094: final ExecutableElement method = (ExecutableElement) key;
095: Node n = new AbstractNode(Children.LEAF) {
096:
097: @java.lang.Override
098: public java.awt.Image getIcon(int type) {
099: if (cachedIcon == null) {
100: cachedIcon = org.openide.util.Utilities
101: .loadImage(OPERATION_ICON);
102: }
103: return cachedIcon;
104: }
105:
106: @Override
107: public String getDisplayName() {
108: return method.getSimpleName().toString();
109: }
110: };
111:
112: return new Node[] { n };
113: }
114: return new Node[0];
115: }
116:
117: private boolean isFromWsdl() {
118: WebServicesSupport wsSupport = WebServicesSupport
119: .getWebServicesSupport(implClass);
120: assert wsSupport != null;
121: return wsSupport.isFromWSDL(webServiceDescription
122: .getWebserviceDescriptionName());
123: }
124:
125: protected void addNotify() {
126: super .addNotify();
127: if (isFromWsdl()) {
128: //TODO
129: } else {
130: updateKeys();
131: }
132: if (implFileListener == null) {
133: implFileListener = new FileChangeAdapter() {
134: public void fileChanged(FileEvent fe) {
135: super .fileChanged(fe);
136: updateKeys();
137: }
138: };
139: }
140: implClass.addFileChangeListener(implFileListener);
141: }
142:
143: protected void removeNotify() {
144: super .removeNotify();
145: if (implFileListener != null) {
146: implClass.removeFileChangeListener(implFileListener);
147: }
148: }
149:
150: private void updateKeys() {
151: if (isFromWsdl()) {
152: List keys = new ArrayList();
153: //TODO
154: setKeys(keys);
155: } else {
156: SwingUtilities.invokeLater(new Runnable() {
157: public void run() {
158: final List keys = new ArrayList();
159: if (implClass != null) {
160: JavaSource javaSource = JavaSource
161: .forFileObject(implClass);
162: if (javaSource != null) {
163: CancellableTask<CompilationController> task = new CancellableTask<CompilationController>() {
164: public void run(
165: CompilationController controller)
166: throws IOException {
167: controller
168: .toPhase(Phase.ELEMENTS_RESOLVED);
169: TypeElement typeElement = SourceUtils
170: .getPublicTopLevelElement(controller);
171: if (typeElement != null) {
172: // find WS operations as all public methods
173: List<ExecutableElement> publicMethods = getPublicMethods(
174: controller, typeElement);
175: keys.addAll(publicMethods);
176: }
177: }
178:
179: public void cancel() {
180: }
181: };
182: try {
183: javaSource
184: .runUserActionTask(task, true);
185: } catch (IOException ex) {
186: ErrorManager.getDefault().notify(ex);
187: }
188: }
189: }
190: setKeys(keys);
191: }
192: });
193: }
194: }
195:
196: private List<ExecutableElement> getPublicMethods(
197: CompilationController controller, TypeElement classElement)
198: throws IOException {
199: List<? extends Element> members = classElement
200: .getEnclosedElements();
201: List<ExecutableElement> methods = ElementFilter
202: .methodsIn(members);
203: List<ExecutableElement> publicMethods = new ArrayList<ExecutableElement>();
204: for (ExecutableElement method : methods) {
205: Set<Modifier> modifiers = method.getModifiers();
206: if (modifiers.contains(Modifier.PUBLIC)) {
207: publicMethods.add(method);
208: }
209: }
210: return publicMethods;
211: }
212:
213: // Retouche
214: // public ComponentMethodViewStrategy createViewStrategy() {
215: // WSComponentMethodViewStrategy strategy = WSComponentMethodViewStrategy.instance();
216: // return strategy;
217: // }
218: //
219: // protected JavaClass getImplBean() {
220: // return getImplBeanClass(webServiceDescription);
221: // }
222: //
223: // protected Collection getInterfaces() {
224: // Set set = new HashSet();
225: // set.add(getServiceEndpointInterface(webServiceDescription));
226: // return set;
227: // }
228: //
229: // private JavaClass getServiceEndpointInterface(WebserviceDescription webServiceDescription){
230: // PortComponent portComponent = webServiceDescription.getPortComponent(0);
231: // String sei = portComponent.getServiceEndpointInterface();
232: // if(sei != null) {
233: // sei = sei.trim(); // IZ 56889: must trim white space, if any, before using this information.
234: // }
235: // return JMIUtils.findClass(sei);
236: // }
237: //
238: // private JavaClass getImplBeanClass(WebserviceDescription webServiceDescription) {
239: // PortComponent portComponent = webServiceDescription.getPortComponent(0); //assume one port per ws
240: // ServiceImplBean serviceImplBean = portComponent.getServiceImplBean();
241: // String link =serviceImplBean.getServletLink();
242: // if(link == null) {
243: // link = serviceImplBean.getEjbLink();
244: // }
245: // if(link != null) {
246: // link = link.trim(); // Related to IZ 56889: must trim white space, if any, before using this information.
247: // }
248: // WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(srcRoot);
249: // String implBean = wsSupport.getImplementationBean(link);
250: // if(implBean != null) {
251: // return JMIUtils.findClass(implBean);
252: // }
253: // return null;
254: // }
255: //
256: // public static class WSComponentMethodViewStrategy implements ComponentMethodViewStrategy {
257: // // private Image NOT_OPERATION_BADGE = Utilities.loadImage("org/openide/src/resources/error.gif");
258: // private static WSComponentMethodViewStrategy wsmvStrategy;
259: // private WSComponentMethodViewStrategy(){
260: // }
261: //
262: // public static WSComponentMethodViewStrategy instance(){
263: // if(wsmvStrategy == null){
264: // wsmvStrategy = new WSComponentMethodViewStrategy();
265: // }
266: // return wsmvStrategy;
267: // }
268: // public Image getBadge(Method method, Collection interfaces){
269: //
270: // /* no need to badge this, it sometimes not a sign for bad operation see 55679 Set paramTypes = new HashSet();
271: // //FIX-ME:Need a better way to find out if method is in SEI
272: // MethodParameter[] parameters = method.getParameters();
273: // for(int i = 0; i < parameters.length; i++){
274: // paramTypes.add(parameters[i].getType());
275: // }
276: // Iterator iter = interfaces.iterator();
277: // while(iter.hasNext()){
278: // ClassElement intf = (ClassElement)iter.next();
279: // if(intf.getMethod(method.getName(), (Type[])paramTypes.toArray(new Type[paramTypes.size()])) == null){
280: // return NOT_OPERATION_BADGE;
281: // }
282: //
283: // }*/
284: //
285: // return null;
286: // }
287: // public void deleteImplMethod(Method m, JavaClass implClass, Collection interfaces) throws IOException{
288: // //delete method in the SEI
289: // Iterator iter = interfaces.iterator();
290: // while (iter.hasNext()){
291: // JavaClass intf = (JavaClass)iter.next();
292: // try {
293: // intf.getContents().remove(m);
294: // } catch (JmiException e) {
295: // throw new IOException(e.getMessage());
296: // }
297: // }
298: // //delete method from Impl class
299: // Method[] methods = JMIUtils.getMethods(implClass);
300: // for(int i = 0; i < methods.length; i++){
301: // Method method = methods[i];
302: // if (JMIUtils.equalMethods(m, method)) {
303: // try {
304: // implClass.getContents().remove(method);
305: // break;
306: // } catch (JmiException e) {
307: // throw new IOException(e.getMessage());
308: // }
309: // }
310: // }
311: // }
312: //
313: // public OpenCookie getOpenCookie(Method m, JavaClass implClass, Collection interfaces) {
314: // Method[] methods = JMIUtils.getMethods(implClass);
315: // for(int i = 0; i < methods.length; i++) {
316: // Method method = methods[i];
317: // if (JMIUtils.equalMethods(m, method)) {
318: // return (OpenCookie)JMIUtils.getCookie(method, OpenCookie.class);
319: // }
320: // }
321: // return null;
322: // }
323: //
324: // public Image getIcon(Method me, Collection interfaces) {
325: // return Utilities.loadImage("org/openide/src/resources/methodPublic.gif");
326: // }
327: //
328: // }
329:
330: }
|