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:
042: package org.netbeans.modules.websvc.core.jaxws.actions;
043:
044: import org.netbeans.modules.websvc.core._RetoucheUtil;
045: import org.netbeans.modules.websvc.core.AddWsOperationHelper;
046: import org.netbeans.modules.websvc.core.AddOperationCookie;
047: import org.openide.filesystems.FileObject;
048: import java.io.IOException;
049: import java.util.List;
050: import org.netbeans.api.java.project.JavaProjectConstants;
051: import org.netbeans.api.project.FileOwnerQuery;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.SourceGroup;
054: import org.netbeans.api.project.Sources;
055: import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
056: import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
057: import org.openide.util.RequestProcessor;
058: import static org.netbeans.api.java.source.JavaSource.Phase;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.util.NbBundle;
063:
064: /** JaxWsAddOperation.java
065: * Created on December 12, 2006, 4:36 PM
066: *
067: * @author mkuchtiak
068: */
069: public class JaxWsAddOperation implements AddOperationCookie {
070: private FileObject implClassFo;
071: private Service service;
072:
073: /** Creates a new instance of JaxWsAddOperation */
074: public JaxWsAddOperation(FileObject implClassFo) {
075: this .implClassFo = implClassFo;
076: service = getService();
077: }
078:
079: public void addOperation(final FileObject implementationClass) {
080: final AddWsOperationHelper strategy = new AddWsOperationHelper(
081: NbBundle.getMessage(AddWsOperationHelper.class,
082: "LBL_OperationAction"));
083: RequestProcessor.getDefault().post(new Runnable() {
084: public void run() {
085: try {
086: String className = _RetoucheUtil
087: .getMainClassName(implementationClass);
088: if (className != null) {
089: strategy.addMethod(implementationClass,
090: className);
091: }
092: } catch (IOException ex) {
093: ErrorManager.getDefault().notify(ex);
094: }
095: }
096: });
097: }
098:
099: public boolean isEnabledInEditor(FileObject implClass) {
100: return isJaxWsImplementationClass() && !isFromWSDL();
101: }
102:
103: private boolean isJaxWsImplementationClass() {
104: return service != null;
105: }
106:
107: private Service getService() {
108: JAXWSSupport jaxWsSupport = JAXWSSupport
109: .getJAXWSSupport(implClassFo);
110: if (jaxWsSupport != null) {
111: List services = jaxWsSupport.getServices();
112: for (int i = 0; i < services.size(); i++) {
113: Service serv = (Service) services.get(i);
114: if (serv.getWsdlUrl() == null) {
115: String implClass = serv.getImplementationClass();
116: if (implClass.equals(getPackageName(implClassFo))) {
117: return serv;
118: }
119: }
120: }
121: }
122: return null;
123: }
124:
125: private boolean isFromWSDL() {
126: if (service != null) {
127: return service.getWsdlUrl() != null;
128: }
129: return false;
130: }
131:
132: private String getPackageName(FileObject fo) {
133: Project project = FileOwnerQuery.getOwner(fo);
134: Sources sources = project.getLookup().lookup(Sources.class);
135: if (sources != null) {
136: SourceGroup[] groups = sources
137: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
138: if (groups != null) {
139: for (SourceGroup group : groups) {
140: FileObject rootFolder = group.getRootFolder();
141: if (FileUtil.isParentOf(rootFolder, fo)) {
142: String relativePath = FileUtil.getRelativePath(
143: rootFolder, fo).replace('/', '.');
144: return (relativePath.endsWith(".java") ? //NOI18N
145: relativePath.substring(0,
146: relativePath.length() - 5)
147: : relativePath);
148: }
149: }
150: }
151: }
152: return null;
153: }
154:
155: }
|