KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > webservices > action > AddOperationEditorAction


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 package org.netbeans.modules.websvc.core.webservices.action;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.netbeans.api.java.project.JavaProjectConstants;
24 import org.netbeans.api.project.FileOwnerQuery;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.SourceGroup;
27 import org.netbeans.api.project.Sources;
28 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
29 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
30 import org.netbeans.modules.websvc.core.AddOperationCookie;
31 import org.netbeans.modules.websvc.core.WebServiceActionProvider;
32 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.loaders.DataObject;
36 import org.openide.util.HelpCtx;
37 import org.openide.util.NbBundle;
38 import org.openide.nodes.Node;
39 import org.openide.util.actions.NodeAction;
40
41 /** Editor action that opens the Add Operation dialog and adds new methods to
42  * the service
43  */

44 public class AddOperationEditorAction extends NodeAction {
45     private Service service;
46     public String JavaDoc getName() {
47         return NbBundle.getMessage(AddOperationEditorAction.class, "LBL_AddOperationEditorAction");
48     }
49     
50     public HelpCtx getHelpCtx() {
51         // If you will provide context help then use:
52
return HelpCtx.DEFAULT_HELP;
53   }
54     
55     protected boolean asynchronous() {
56         return false;
57     }
58     
59     protected boolean enable(Node[] activatedNodes) {
60         if (activatedNodes.length == 1) {
61             FileObject fo = getFileObjectFromNode(activatedNodes[0]);
62             if (fo!=null) {
63                 AddOperationCookie cookie = WebServiceActionProvider.getAddOperationAction(fo);
64                 return cookie!=null && activatedNodes.length == 1 &&
65 // Retouche
66
// ( activatedNodes[0].getLookup().lookup(ClassMember.class) != null || JMIUtils.getClassMemberFromNode(activatedNodes[0])!=null ) &&
67
// JMIUtils.getJavaClassFromNode(activatedNodes[0]) != null &&
68
(isJaxWsImplementationClass(activatedNodes[0]) /*|| (isWsImplBeanOrInterface(activatedNodes[0]) */
69                     && !isFromWSDL(activatedNodes[0]));
70             }
71         }
72         return false;
73     }
74     
75     
76     private FileObject getFileObjectFromNode(Node n) {
77         DataObject dObj = (DataObject)n.getCookie(DataObject.class);
78         if (dObj!=null) return dObj.getPrimaryFile();
79         else return null;
80     }
81     
82     private boolean isWsImplBeanOrInterface(Node node) {
83 // Retouche
84
// JavaClass ce = JMIUtils.getJavaClassFromNode(node);
85
// Resource r = ce.getResource();
86
// FileObject f = JavaModel.getFileObject(r);
87
// if (f != null) {
88
//
89
// WebserviceDescription wsDesc = WebServiceCookieFactory.findWSDescriptionFromClass(ce, f);
90
// if (wsDesc != null) {
91
// return true;
92
// }
93
// }
94
return false;
95     }
96
97     
98     private boolean isJaxWsImplementationClass(Node node) {
99         FileObject fo = getFileObjectFromNode(node);
100         JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(fo);
101         if (jaxWsSupport!=null) {
102             List JavaDoc services = jaxWsSupport.getServices();
103             for (int i=0;i<services.size();i++) {
104                 Service serv = (Service)services.get(i);
105                 if (serv.getWsdlUrl()==null) {
106                     String JavaDoc implClass = serv.getImplementationClass();
107                     if (implClass.equals(getPackageName(fo))) {
108                         service=serv;
109                         return true;
110                     }
111                 }
112             }
113         }
114         service=null;
115         return false;
116     }
117     
118     private boolean isFromWSDL(Node node) {
119         if (service!=null) {
120             return service.getWsdlUrl()!=null;
121         }
122 // Retouche
123
// JavaClass ce = JMIUtils.getJavaClassFromNode(node);
124
// Resource r = ce.getResource();
125
// FileObject f = JavaModel.getFileObject(r);
126
// if (f != null) {
127
// WebserviceDescription wsDesc = WebServiceCookieFactory.findWSDescriptionFromClass(ce, f);
128
// if (wsDesc != null) {
129
// String wsName = wsDesc.getWebserviceDescriptionName();
130
// WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(f);
131
// assert wsSupport != null;
132
// return wsSupport.isFromWSDL(wsName);
133
// }
134
// }
135
return false;
136     }
137     
138     private String JavaDoc getPackageName(FileObject fo) {
139         Project project = FileOwnerQuery.getOwner(fo);
140         Sources sources = (Sources)project.getLookup().lookup(Sources.class);
141         if (sources!=null) {
142             SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
143             if (groups!=null) {
144                 List JavaDoc<FileObject> roots = new ArrayList JavaDoc<FileObject>();
145                 for (SourceGroup group: groups) {
146                     FileObject rootFolder = group.getRootFolder();
147                     if (FileUtil.isParentOf(rootFolder, fo)) {
148                         String JavaDoc relativePath = FileUtil.getRelativePath(rootFolder, fo).replace('/', '.');
149                         return (relativePath.endsWith(".java")? //NOI18N
150
relativePath.substring(0,relativePath.length()-5):
151                             relativePath);
152                     }
153                 }
154             }
155         }
156         return null;
157     }
158     
159     protected void performAction(Node[] activatedNodes) {
160             FileObject fo = getFileObjectFromNode(activatedNodes[0]);
161             AddOperationCookie cookie = WebServiceActionProvider.getAddOperationAction(fo);
162             cookie.addOperation(fo);
163     }
164 }
165
Popular Tags