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.nodes;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.util.ArrayList;
047: import java.util.Iterator;
048: import java.util.List;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.swing.SwingUtilities;
052: import javax.swing.event.ChangeEvent;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.api.fileinfo.NonRecursiveFolder;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.modules.refactoring.api.Problem;
057: import org.netbeans.modules.refactoring.api.RefactoringSession;
058: import org.netbeans.modules.refactoring.api.RenameRefactoring;
059: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientSupport;
060: import org.netbeans.modules.websvc.api.jaxws.client.JAXWSClientView;
061: import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
062: import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
063: import org.netbeans.modules.websvc.jaxws.api.JAXWSView;
064: import org.netbeans.spi.project.ui.support.NodeFactory;
065: import org.netbeans.spi.project.ui.support.NodeList;
066: import org.openide.filesystems.FileObject;
067: import org.openide.filesystems.FileUtil;
068: import org.openide.nodes.Node;
069: import org.openide.util.RequestProcessor;
070: import org.openide.util.lookup.Lookups;
071:
072: /**
073: *
074: * @author Milan Kuchtiak
075: */
076: public class WebServicesNodeFactory implements NodeFactory {
077:
078: /** Creates a new instance of WebServicesNodeFactory */
079: public WebServicesNodeFactory() {
080: }
081:
082: public NodeList createNodes(Project p) {
083: assert p != null;
084: return new WsNodeList(p);
085: }
086:
087: private static class WsNodeList implements NodeList<String> {
088: // Web Services
089: private static final String KEY_SERVICES = "web_services"; // NOI18N
090: // Web Service Client
091: private static final String KEY_SERVICE_REFS = "serviceRefs"; // NOI18N
092:
093: private Project project;
094:
095: private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
096: private final JaxWsChangeListener jaxWsListener;
097:
098: public WsNodeList(Project proj) {
099: project = proj;
100: this .jaxWsListener = new JaxWsChangeListener();
101: }
102:
103: public List<String> keys() {
104: List<String> result = new ArrayList<String>();
105: JaxWsModel jaxWsModel = (JaxWsModel) project.getLookup()
106: .lookup(JaxWsModel.class);
107: FileObject projectDir = project.getProjectDirectory();
108: JAXWSSupport jaxwsSupport = JAXWSSupport
109: .getJAXWSSupport(projectDir);
110: JAXWSClientSupport jaxwsClientSupport = JAXWSClientSupport
111: .getJaxWsClientSupport(projectDir);
112: if (jaxWsModel != null) {
113: if (jaxwsSupport != null
114: && jaxWsModel.getServices().length > 0) {
115: result.add(KEY_SERVICES);
116: }
117: if (jaxwsClientSupport != null
118: && jaxWsModel.getClients().length > 0) {
119: result.add(KEY_SERVICE_REFS);
120: }
121: }
122: return result;
123: }
124:
125: public synchronized void addChangeListener(ChangeListener l) {
126: listeners.add(l);
127: }
128:
129: public synchronized void removeChangeListener(ChangeListener l) {
130: listeners.remove(l);
131: }
132:
133: private void fireChange() {
134: ArrayList<ChangeListener> list = new ArrayList<ChangeListener>();
135: synchronized (this ) {
136: list.addAll(listeners);
137: }
138: Iterator<ChangeListener> it = list.iterator();
139: while (it.hasNext()) {
140: ChangeListener elem = it.next();
141: elem.stateChanged(new ChangeEvent(this ));
142: }
143: }
144:
145: public Node node(String key) {
146: if (KEY_SERVICES.equals(key)) {
147: JAXWSView view = JAXWSView.getJAXWSView();
148: return view.createJAXWSView(project);
149: } else if (KEY_SERVICE_REFS.equals(key)) {
150: JAXWSClientView view = JAXWSClientView
151: .getJAXWSClientView();
152: return view.createJAXWSClientView(project);
153: }
154: return null;
155: }
156:
157: public void addNotify() {
158: JaxWsModel jaxWsModel = (JaxWsModel) project.getLookup()
159: .lookup(JaxWsModel.class);
160: if (jaxWsModel != null)
161: jaxWsModel.addPropertyChangeListener(jaxWsListener);
162: }
163:
164: public void removeNotify() {
165: JaxWsModel jaxWsModel = (JaxWsModel) project.getLookup()
166: .lookup(JaxWsModel.class);
167: if (jaxWsModel != null)
168: jaxWsModel.removePropertyChangeListener(jaxWsListener);
169: }
170:
171: private final class JaxWsChangeListener implements
172: PropertyChangeListener {
173: public void propertyChange(PropertyChangeEvent evt) {
174: SwingUtilities.invokeLater(new Runnable() {
175: public void run() {
176: fireChange();
177: }
178: });
179: String propName = evt.getPropertyName();
180:
181: if (propName.endsWith("PackageName")) { // NOI18N
182: String oldValue = (String) evt.getOldValue();
183: String newValue = (String) evt.getNewValue();
184: if (oldValue != null && newValue != null) {
185: try {
186: if (propName.startsWith("/JaxWs/Clients")) { //NOI18N
187: FileObject artifactsFolder = project
188: .getProjectDirectory()
189: .getFileObject(
190: "build/generated/wsimport/client"); //NOI18N
191: if (artifactsFolder == null
192: || !artifactsFolder.isValid()) {
193: artifactsFolder = createArtifactsFolder(
194: project
195: .getProjectDirectory(),
196: true);
197: }
198: if (artifactsFolder != null) {
199: refactorPackage(artifactsFolder,
200: oldValue, newValue);
201: }
202: } else if (propName
203: .startsWith("/JaxWs/Services")) { //NOI18N
204: FileObject artifactsFolder = project
205: .getProjectDirectory()
206: .getFileObject(
207: "build/generated/wsimport/services"); // NOI18N
208: if (artifactsFolder == null
209: || !artifactsFolder.isValid()) {
210: artifactsFolder = createArtifactsFolder(
211: project
212: .getProjectDirectory(),
213: false);
214: }
215: if (artifactsFolder != null) {
216: refactorPackage(artifactsFolder,
217: oldValue, newValue);
218:
219: }
220: }
221: } catch (java.io.IOException ex) {
222: Logger
223: .getLogger(
224: this .getClass().getName())
225: .log(
226: Level.FINE,
227: "cannot create artifacts folder",
228: ex); // NOI18N
229: }
230: }
231: }
232: }
233: }
234:
235: private void refactorPackage(FileObject artifactsFolder,
236: String oldName, final String newName)
237: throws java.io.IOException {
238:
239: FileObject packageFolder = artifactsFolder
240: .getFileObject(oldName.replace('.', '/'));
241: if (packageFolder == null || !packageFolder.isValid()) {
242: packageFolder = artifactsFolder.createFolder(oldName
243: .replace('.', '/'));
244: }
245:
246: if (packageFolder != null) {
247: final FileObject oldPackage = packageFolder;
248: NonRecursiveFolder nF = new NonRecursiveFolder() {
249:
250: public FileObject getFolder() {
251: return oldPackage;
252: }
253:
254: };
255: final RefactoringSession renameSession = RefactoringSession
256: .create("Rename JAX-WS package"); //NOI18N
257:
258: final RenameRefactoring refactoring = new RenameRefactoring(
259: Lookups.fixed(nF));
260:
261: RequestProcessor.getDefault().post(new Runnable() {
262: public void run() {
263: Problem p = refactoring.preCheck();
264:
265: if (p != null && p.isFatal()) {
266: //fatal problem in precheck
267: Logger.getLogger(this .getClass().getName())
268: .log(Level.FINE, p.getMessage());
269: return;
270: }
271: refactoring.setNewName(newName);
272: p = refactoring.prepare(renameSession);
273: if (p != null && p.isFatal()) {
274: // fatal problem in prepare
275: Logger.getLogger(this .getClass().getName())
276: .log(Level.FINE, p.getMessage());
277: return;
278: }
279:
280: p = renameSession.doRefactoring(true);
281: }
282: });
283:
284: }
285: }
286:
287: private FileObject createArtifactsFolder(FileObject projectDir,
288: boolean forClient) throws java.io.IOException {
289: if (forClient) {
290: return FileUtil.createFolder(projectDir,
291: "build/generated/wsimport/client"); //NOI18N
292: } else {
293: return FileUtil.createFolder(projectDir,
294: "build/generated/wsimport/service"); //NOI18N
295: }
296: }
297: }
298:
299: }
|