KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxrpc > nodes > JaxRpcNodeFactory


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
20 package org.netbeans.modules.websvc.jaxrpc.nodes;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.api.java.project.JavaProjectConstants;
32 import org.netbeans.api.project.Project;
33 import org.netbeans.api.project.SourceGroup;
34 import org.netbeans.api.project.Sources;
35 import org.netbeans.modules.websvc.api.client.WebServicesClientSupport;
36 import org.netbeans.modules.websvc.api.client.WebServicesClientView;
37 import org.netbeans.spi.project.ui.support.NodeFactory;
38 import org.netbeans.spi.project.ui.support.NodeList;
39 import org.openide.ErrorManager;
40 import org.openide.filesystems.FileChangeAdapter;
41 import org.openide.filesystems.FileEvent;
42 import org.openide.filesystems.FileObject;
43 import org.openide.nodes.Node;
44
45 /**
46  *
47  * @author Milan Kuchtiak
48  */

49 public class JaxRpcNodeFactory implements NodeFactory {
50     private static final String JavaDoc WSDL_FOLDER = "wsdl"; //NOI18N
51
/** Creates a new instance of WebServicesNodeFactory */
52     public JaxRpcNodeFactory() {
53     }
54     
55     public NodeList createNodes(Project project) {
56         assert project != null;
57         return new WsNodeList(project);
58     }
59     
60     private static class WsNodeList implements NodeList<String JavaDoc> {
61
62         // Web service client
63
private static final String JavaDoc KEY_SERVICE_REFS = "serviceRefs"; // NOI18N
64
private Project project;
65         
66         private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
67         private final WsdlCreationListener wsdlListener;
68         private final MetaInfListener metaInfListener;
69         private FileObject wsdlFolder;
70         
71         public WsNodeList(Project proj) {
72             project = proj;
73             this.metaInfListener = new MetaInfListener();
74             this.wsdlListener = new WsdlCreationListener();
75         }
76         
77         public List JavaDoc keys() {
78             List JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
79             WebServicesClientSupport wscs = WebServicesClientSupport.getWebServicesClientSupport(project.getProjectDirectory());
80             if (wscs != null) {
81                 result.add(KEY_SERVICE_REFS);
82             }
83             return result;
84         }
85         
86         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
87             listeners.add(l);
88         }
89         
90         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
91             listeners.remove(l);
92         }
93         
94         private void fireChange() {
95             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
96             synchronized (this) {
97                 list.addAll(listeners);
98             }
99             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
100             while (it.hasNext()) {
101                 ChangeListener JavaDoc elem = it.next();
102                 elem.stateChanged(new ChangeEvent JavaDoc( this ));
103             }
104         }
105         
106         public Node node(String JavaDoc key) {
107             FileObject clientRoot = project.getProjectDirectory();
108             WebServicesClientView clientView = WebServicesClientView.getWebServicesClientView(clientRoot);
109             if (clientView != null) {
110                 WebServicesClientSupport wss = WebServicesClientSupport.getWebServicesClientSupport(clientRoot);
111                 if (wss!=null) {
112                     FileObject wsdlFolder = wss.getWsdlFolder();
113                     if (wsdlFolder!=null) {
114                         FileObject[] children = wsdlFolder.getChildren();
115                         boolean foundWsdl = false;
116                         for (int i=0;i<children.length;i++) {
117                             if (children[i].getExt().equalsIgnoreCase(WSDL_FOLDER)) { //NOI18N
118
foundWsdl=true;
119                                 break;
120                             }
121                         }
122                         if (foundWsdl) {
123                             return clientView.createWebServiceClientView(wsdlFolder);
124                         }
125                     }
126                 }
127             }
128             return null;
129         }
130         
131         public void addNotify() {
132
133             Sources sources = (Sources)project.getLookup().lookup(Sources.class);
134             if (sources!=null) {
135                 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
136                 if (groups!=null && groups.length>0) {
137                     FileObject srcDir = groups[0].getRootFolder();
138                     srcDir.addFileChangeListener(metaInfListener);
139                     FileObject metaInf = srcDir.getFileObject("META-INF");
140                     if (metaInf!=null) metaInf.addFileChangeListener(metaInfListener);
141                 }
142                     
143             }
144             //XXX: Not very nice, the wsdlFolder should be hold by this class because it listens on it
145
WebServicesClientSupport wsClientSupportImpl = WebServicesClientSupport.getWebServicesClientSupport(project.getProjectDirectory());
146             try {
147                 if (wsClientSupportImpl != null) {
148                     wsdlFolder = wsClientSupportImpl.getWsdlFolder(false);
149                 }
150             } catch (IOException JavaDoc ex) {
151                 ErrorManager.getDefault().notify(ex);
152             }
153             if (wsdlFolder != null) {
154                 wsdlFolder.addFileChangeListener(wsdlListener);
155             }
156         }
157         
158         public void removeNotify() {
159             Sources sources = (Sources)project.getLookup().lookup(Sources.class);
160             if (sources!=null) {
161                 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
162                 if (groups!=null && groups.length>0) {
163                     FileObject srcDir = groups[0].getRootFolder();
164                     srcDir.removeFileChangeListener(metaInfListener);
165                     FileObject metaInf = srcDir.getFileObject("META-INF");
166                     if (metaInf!=null) metaInf.removeFileChangeListener(metaInfListener);
167                 }
168                     
169             }
170             
171             if (wsdlFolder != null) {
172                 wsdlFolder.removeFileChangeListener(wsdlListener);
173             }
174         }
175         private final class WsdlCreationListener extends FileChangeAdapter {
176             
177             public void fileDataCreated(FileEvent fe) {
178                 if (WSDL_FOLDER.equalsIgnoreCase(fe.getFile().getExt())) {
179                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
180                         public void run() {
181                             fireChange();
182                         }
183                     });
184                 }
185             }
186             
187             public void fileDeleted(FileEvent fe) {
188                 if (WSDL_FOLDER.equalsIgnoreCase(fe.getFile().getExt())) {
189                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
190                         public void run() {
191                             fireChange();
192                         }
193                     });
194                 } else if (fe.getFile().isFolder() && WSDL_FOLDER.equals(fe.getFile().getName())) {
195                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
196                         public void run() {
197                             fireChange();
198                         }
199                     });
200                 }
201             }
202         }
203         
204         private final class MetaInfListener extends FileChangeAdapter {
205             
206             public void fileFolderCreated(FileEvent fe) {
207                 if (fe.getFile().isFolder() && WSDL_FOLDER.equals(fe.getFile().getName())) {
208                     fe.getFile().addFileChangeListener(wsdlListener);
209                 } else if (fe.getFile().isFolder() && "META-INF".equals(fe.getFile().getName())) { //NOI18N
210
fe.getFile().addFileChangeListener(metaInfListener);
211                 }
212             }
213             
214             public void fileDeleted(FileEvent fe) {
215                 if (fe.getFile().isFolder() && WSDL_FOLDER.equals(fe.getFile().getName())) {
216                     fe.getFile().removeFileChangeListener(wsdlListener);
217                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
218                         public void run() {
219                             fireChange();
220                         }
221                     });
222                 } else if (fe.getFile().isFolder() && "META-INF".equals(fe.getFile().getName())) { //NOI18N
223
fe.getFile().removeFileChangeListener(metaInfListener);
224                 }
225             }
226         }
227         
228         
229         private final class JaxWsChangeListener implements PropertyChangeListener JavaDoc {
230             public void propertyChange(PropertyChangeEvent JavaDoc evt) {
231                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
232                     public void run() {
233                         fireChange();
234                     }
235                 });
236             }
237         }
238     }
239     
240 }
241
Popular Tags