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.jaxrpc.nodes;
043:
044: import java.io.IOException;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048: import javax.swing.SwingUtilities;
049: import javax.swing.event.ChangeEvent;
050: import javax.swing.event.ChangeListener;
051: import org.netbeans.api.java.project.JavaProjectConstants;
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.client.WebServicesClientSupport;
056: import org.netbeans.modules.websvc.api.client.WebServicesClientView;
057: import org.netbeans.spi.project.support.ant.AntProjectHelper;
058: import org.netbeans.spi.project.ui.support.NodeFactory;
059: import org.netbeans.spi.project.ui.support.NodeList;
060: import org.openide.ErrorManager;
061: import org.openide.filesystems.FileChangeAdapter;
062: import org.openide.filesystems.FileEvent;
063: import org.openide.filesystems.FileObject;
064: import org.openide.filesystems.FileUtil;
065: import org.openide.nodes.Node;
066:
067: /**
068: *
069: * @author Milan Kuchtiak
070: */
071: public class JaxRpcNodeFactory implements NodeFactory {
072: private static final String WSDL_FOLDER = "wsdl"; //NOI18N
073:
074: /** Creates a new instance of WebServicesNodeFactory */
075: public JaxRpcNodeFactory() {
076: }
077:
078: public NodeList createNodes(Project project) {
079: assert project != null;
080: return new WsNodeList(project);
081: }
082:
083: private static class WsNodeList implements NodeList<String> {
084:
085: // Web service client
086: private static final String KEY_SERVICE_REFS = "serviceRefs"; // NOI18N
087: private Project project;
088:
089: private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
090: private final WsdlCreationListener wsdlListener;
091: private final MetaInfListener metaInfListener;
092: private final ProjectXmlListener projectXmlListener;
093: private FileObject wsdlFolder;
094:
095: public WsNodeList(Project proj) {
096: project = proj;
097: this .metaInfListener = new MetaInfListener();
098: this .wsdlListener = new WsdlCreationListener();
099: this .projectXmlListener = new ProjectXmlListener();
100: }
101:
102: public List keys() {
103: List<String> result = new ArrayList<String>();
104: WebServicesClientSupport wscs = WebServicesClientSupport
105: .getWebServicesClientSupport(project
106: .getProjectDirectory());
107:
108: if (wscs != null && !wscs.getServiceClients().isEmpty()) {
109: FileObject wsdlFolder = wscs.getWsdlFolder();
110: if (wsdlFolder != null) {
111: result.add(KEY_SERVICE_REFS);
112: }
113: }
114: return result;
115: }
116:
117: public synchronized void addChangeListener(ChangeListener l) {
118: listeners.add(l);
119: }
120:
121: public synchronized void removeChangeListener(ChangeListener l) {
122: listeners.remove(l);
123: }
124:
125: private void fireChange() {
126: ArrayList<ChangeListener> list = new ArrayList<ChangeListener>();
127: synchronized (this ) {
128: list.addAll(listeners);
129: }
130: Iterator<ChangeListener> it = list.iterator();
131: while (it.hasNext()) {
132: ChangeListener elem = it.next();
133: elem.stateChanged(new ChangeEvent(this ));
134: }
135: }
136:
137: public Node node(String key) {
138: if (key.equals(KEY_SERVICE_REFS)) {
139: FileObject clientRoot = project.getProjectDirectory();
140: WebServicesClientView clientView = WebServicesClientView
141: .getWebServicesClientView(clientRoot);
142: if (clientView != null) {
143: WebServicesClientSupport wss = WebServicesClientSupport
144: .getWebServicesClientSupport(clientRoot);
145: if (wss != null) {
146: FileObject wsdlFolder = wss.getWsdlFolder();
147: if (wsdlFolder != null) {
148: FileObject[] children = wsdlFolder
149: .getChildren();
150: boolean foundWsdl = false;
151: for (int i = 0; i < children.length; i++) {
152: if (children[i].getExt()
153: .equalsIgnoreCase(WSDL_FOLDER)) { //NOI18N
154: foundWsdl = true;
155: break;
156: }
157: }
158: if (foundWsdl) {
159: return clientView
160: .createWebServiceClientView(wsdlFolder);
161: }
162: }
163: }
164: }
165: }
166: return null;
167: }
168:
169: public void addNotify() {
170:
171: Sources sources = (Sources) project.getLookup().lookup(
172: Sources.class);
173: if (sources != null) {
174: SourceGroup[] groups = sources
175: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
176: if (groups != null && groups.length > 0) {
177: FileObject srcDir = groups[0].getRootFolder();
178: srcDir.addFileChangeListener(FileUtil
179: .weakFileChangeListener(metaInfListener,
180: srcDir));
181: FileObject metaInf = srcDir
182: .getFileObject("META-INF");
183: if (metaInf != null) {
184: metaInf.addFileChangeListener(FileUtil
185: .weakFileChangeListener(
186: metaInfListener, metaInf));
187: }
188: }
189:
190: }
191: FileObject projectDir = project.getProjectDirectory();
192: FileObject webInf = projectDir.getFileObject("web/WEB-INF");
193: if (webInf != null) {
194: webInf
195: .addFileChangeListener(FileUtil
196: .weakFileChangeListener(
197: metaInfListener, webInf));
198: }
199:
200: //XXX: Not very nice, the wsdlFolder should be hold by this class because it listens on it
201: WebServicesClientSupport wsClientSupportImpl = WebServicesClientSupport
202: .getWebServicesClientSupport(project
203: .getProjectDirectory());
204: try {
205: if (wsClientSupportImpl != null) {
206: wsdlFolder = wsClientSupportImpl
207: .getWsdlFolder(false);
208: }
209: } catch (IOException ex) {
210: ErrorManager.getDefault().notify(ex);
211: }
212: if (wsdlFolder != null) {
213: wsdlFolder.addFileChangeListener(FileUtil
214: .weakFileChangeListener(wsdlListener,
215: wsdlFolder));
216: }
217: FileObject prjXml = project.getProjectDirectory()
218: .getFileObject(AntProjectHelper.PROJECT_XML_PATH);
219: if (prjXml != null) {
220: prjXml.addFileChangeListener(FileUtil
221: .weakFileChangeListener(projectXmlListener,
222: prjXml));
223: }
224: }
225:
226: private final class WsdlCreationListener extends
227: FileChangeAdapter {
228: public void fileDataCreated(FileEvent fe) {
229: if (WSDL_FOLDER.equalsIgnoreCase(fe.getFile().getExt())) {
230: SwingUtilities.invokeLater(new Runnable() {
231: public void run() {
232: fireChange();
233: }
234: });
235: }
236: }
237:
238: public void fileDeleted(FileEvent fe) {
239: if (WSDL_FOLDER.equalsIgnoreCase(fe.getFile().getExt())) {
240: SwingUtilities.invokeLater(new Runnable() {
241: public void run() {
242: fireChange();
243: }
244: });
245: } else if (fe.getFile().isFolder()
246: && WSDL_FOLDER.equals(fe.getFile().getName())) {
247: SwingUtilities.invokeLater(new Runnable() {
248: public void run() {
249: fireChange();
250: }
251: });
252: }
253: }
254: }
255:
256: private final class MetaInfListener extends FileChangeAdapter {
257:
258: public void fileFolderCreated(FileEvent fe) {
259: FileObject f = fe.getFile();
260: if (f.isFolder() && WSDL_FOLDER.equals(f.getName())) {
261: f.addFileChangeListener(FileUtil
262: .weakFileChangeListener(wsdlListener, f));
263: } else if (f.isFolder()
264: && "META-INF".equals(f.getName())) { //NOI18N
265: f
266: .addFileChangeListener(FileUtil
267: .weakFileChangeListener(
268: metaInfListener, f));
269: }
270: }
271:
272: public void fileDeleted(FileEvent fe) {
273: if (fe.getFile().isFolder()
274: && WSDL_FOLDER.equals(fe.getFile().getName())) {
275: fe.getFile().removeFileChangeListener(wsdlListener);
276: SwingUtilities.invokeLater(new Runnable() {
277: public void run() {
278: fireChange();
279: }
280: });
281: } else if (fe.getFile().isFolder()
282: && "META-INF".equals(fe.getFile().getName())) { //NOI18N
283: fe.getFile().removeFileChangeListener(
284: metaInfListener);
285: }
286: }
287: }
288:
289: private final class ProjectXmlListener extends
290: FileChangeAdapter {
291: public void fileChanged(FileEvent fe) {
292: SwingUtilities.invokeLater(new Runnable() {
293: public void run() {
294: fireChange();
295: }
296: });
297: }
298: }
299:
300: public void removeNotify() {
301: }
302:
303: }
304:
305: }
|