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.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.List;
047: import javax.swing.SwingUtilities;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050: import org.netbeans.api.java.project.JavaProjectConstants;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.SourceGroup;
053: import org.netbeans.api.project.Sources;
054: import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
055: import org.netbeans.modules.websvc.api.webservices.WebServicesView;
056: import org.netbeans.spi.project.ui.support.NodeFactory;
057: import org.netbeans.spi.project.ui.support.NodeList;
058: import org.openide.filesystems.FileChangeAdapter;
059: import org.openide.filesystems.FileEvent;
060: import org.openide.filesystems.FileObject;
061: import org.openide.nodes.Node;
062:
063: /**
064: *
065: * @author Milan Kuchtiak
066: */
067: public class JaxRpcWebServiceNodeFactory implements NodeFactory {
068: private static final String WS_DD = "webservices.xml"; //NOI18N
069:
070: /** Creates a new instance of WebServicesNodeFactory */
071: public JaxRpcWebServiceNodeFactory() {
072: }
073:
074: public NodeList createNodes(Project project) {
075: assert project != null;
076: return new WsNodeList(project);
077: }
078:
079: private static class WsNodeList implements NodeList<String> {
080:
081: // Web Services
082: private static final String KEY_SERVICES = "web_services"; // NOI18N
083:
084: private Project project;
085:
086: private List<ChangeListener> listeners = new ArrayList<ChangeListener>();
087:
088: private final WSDDCreationListener wsdlListener;
089:
090: private FileObject wsddFolder;
091:
092: public WsNodeList(Project proj) {
093: project = proj;
094: this .wsdlListener = new WSDDCreationListener();
095: }
096:
097: public List keys() {
098: List<String> result = new ArrayList<String>();
099: WebServicesSupport wss = WebServicesSupport
100: .getWebServicesSupport(project
101: .getProjectDirectory());
102: if (wss != null && !wss.getServices().isEmpty()) {
103: result.add(KEY_SERVICES);
104: }
105: return result;
106: }
107:
108: public synchronized void addChangeListener(ChangeListener l) {
109: listeners.add(l);
110: }
111:
112: public synchronized void removeChangeListener(ChangeListener l) {
113: listeners.remove(l);
114: }
115:
116: private void fireChange() {
117: ArrayList<ChangeListener> list = new ArrayList<ChangeListener>();
118: synchronized (this ) {
119: list.addAll(listeners);
120: }
121: Iterator<ChangeListener> it = list.iterator();
122: while (it.hasNext()) {
123: ChangeListener elem = it.next();
124: elem.stateChanged(new ChangeEvent(this ));
125: }
126: }
127:
128: public Node node(String key) {
129: if (KEY_SERVICES.equals(key)) {
130: WebServicesView view = WebServicesView
131: .getWebServicesView(project
132: .getProjectDirectory());
133: if (view != null) {
134: Sources sources = (Sources) project.getLookup()
135: .lookup(Sources.class);
136: if (sources != null) {
137: SourceGroup[] groups = sources
138: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
139: if (groups != null && groups.length > 0) {
140: FileObject srcDir = groups[0]
141: .getRootFolder();
142: return view.createWebServicesView(srcDir);
143: }
144: }
145: }
146: }
147: return null;
148: }
149:
150: public void addNotify() {
151: WebServicesSupport wss = WebServicesSupport
152: .getWebServicesSupport(project
153: .getProjectDirectory());
154: if (wss != null) {
155: wsddFolder = wss.getWsDDFolder();
156: if (wsddFolder != null) {
157: wsddFolder.addFileChangeListener(wsdlListener);
158: }
159: }
160: }
161:
162: public void removeNotify() {
163: if (wsddFolder != null) {
164: wsddFolder.removeFileChangeListener(wsdlListener);
165: }
166: }
167:
168: private final class WSDDCreationListener extends
169: FileChangeAdapter {
170:
171: public void fileDataCreated(FileEvent fe) {
172: if (WS_DD.equalsIgnoreCase(fe.getFile().getNameExt())) {
173: SwingUtilities.invokeLater(new Runnable() {
174: public void run() {
175: fireChange();
176: }
177: });
178: }
179: }
180:
181: public void fileDeleted(FileEvent fe) {
182: if (WS_DD.equalsIgnoreCase(fe.getFile().getNameExt())) {
183: SwingUtilities.invokeLater(new Runnable() {
184: public void run() {
185: fireChange();
186: }
187: });
188: }
189: }
190: }
191:
192: }
193:
194: }
|