KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.modules.websvc.jaxrpc.nodes;
20
21 import org.openide.nodes.Children;
22 import org.openide.nodes.Node;
23 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
24 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import org.openide.filesystems.FileObject;
29 import org.netbeans.api.project.FileOwnerQuery;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
32 import org.netbeans.modules.j2ee.dd.api.webservices.DDProvider;
33 import org.openide.filesystems.FileChangeListener;
34 import org.openide.filesystems.FileChangeAdapter;
35 import java.beans.PropertyChangeListener JavaDoc;
36
37 public class WebServicesChildren extends Children.Keys implements PropertyChangeListener JavaDoc{
38     Webservices webServices;
39     WebServicesSupport wsSupport;
40     FileObject srcRoot;
41     DDProvider wsDDProvider;
42     FileObject ddFolder;
43     FileChangeListener ddFolderListener;
44     
45     public WebServicesChildren(FileObject srcRoot) {
46         this.srcRoot = srcRoot;
47         Project project = FileOwnerQuery.getOwner(srcRoot);
48         this.wsSupport = WebServicesSupport.
49         getWebServicesSupport(project.getProjectDirectory());
50         this.wsDDProvider = DDProvider.getDefault();
51         ddFolderListener = new DDFolderFileChangeListener();
52     }
53     
54     protected void addNotify() {
55         super.addNotify();
56         ddFolder = wsSupport.getWsDDFolder();
57         //always listen to folder containing webservices.xml
58
//for the case where the webservices.xml is deleted and recreated again
59
// IZ 55633: if no WEB-INF/META-INF, then ddFolder can return null.
60
if(ddFolder != null) {
61             ddFolder.addFileChangeListener(ddFolderListener);
62             FileObject wsDD = ddFolder.getFileObject("webservices", "xml");
63
64             if(wsDD != null){
65                 try {
66                     webServices = wsDDProvider.getDDRoot(wsSupport.getWebservicesDD());
67                     webServices.addPropertyChangeListener(this);
68                 }
69                 catch(java.io.IOException JavaDoc e) {
70                     webServices = null;
71                     //Don't do anything
72
//FIX-ME: Log
73
}
74             }
75             
76             updateKeys();
77         }
78     }
79     
80     protected void removeNotify() {
81         setKeys(Collections.EMPTY_SET);
82         if(ddFolder != null){
83             ddFolder.removeFileChangeListener(ddFolderListener);
84         }
85         super.removeNotify();
86     }
87     
88     private void updateKeys() {
89         List JavaDoc keys = new ArrayList JavaDoc();
90         try {
91             webServices = wsDDProvider.getDDRoot(wsSupport.getWebservicesDD());
92         }
93         catch(java.io.IOException JavaDoc e) {
94             webServices = null;
95             //Don't do anything
96
//FIX-ME: Log
97
}
98         if (webServices != null) {
99             WebserviceDescription[] webServiceDescriptions
100             = webServices.getWebserviceDescription();
101             for (int i = 0; i < webServiceDescriptions.length; i++) {
102                 WebserviceDescription webServiceDescription = webServiceDescriptions[i];
103                 WebServiceWrapper key = new WebServiceWrapper(webServiceDescription);
104                 keys.add(key);
105             }
106         }
107         setKeys(keys);
108     }
109     
110     static class WebServiceWrapper {
111         WebserviceDescription webServiceDescription;
112         
113         public WebServiceWrapper(WebserviceDescription webServiceDescription) {
114             this.webServiceDescription = webServiceDescription;
115         }
116         public WebserviceDescription getWebServiceDescription() {
117             return webServiceDescription;
118         }
119         
120         public boolean equals(Object JavaDoc obj) {
121             if(obj instanceof WebServiceWrapper) {
122                 WebServiceWrapper otherObject = (WebServiceWrapper)obj;
123                 return this.getWebServiceDescription().getWebserviceDescriptionName().
124                 equals(otherObject.getWebServiceDescription().getWebserviceDescriptionName());
125             }
126             return false;
127         }
128         
129         public int hashCode() {
130             return getWebServiceDescription().getWebserviceDescriptionName().hashCode();
131         }
132         
133     }
134     
135     protected Node[] createNodes(Object JavaDoc key) {
136         if(key instanceof WebServiceWrapper) {
137             WebServiceWrapper wrapper =
138             (WebServiceWrapper)key;
139             return new Node[] {new WebServiceNode(webServices, wrapper.getWebServiceDescription(), srcRoot)};
140         }
141         return new Node[0];
142     }
143     
144     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
145         updateKeys();
146     }
147     
148     class DDFolderFileChangeListener extends FileChangeAdapter{
149         public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
150             FileObject fo = fe.getFile();
151             if(fo.getNameExt().equals("webservices.xml")){
152                 try {
153                     webServices = wsDDProvider.getDDRoot(wsSupport.getWebservicesDD());
154                     //FIX-ME: change to WeakListener
155
webServices.addPropertyChangeListener(WebServicesChildren.this);
156                 }
157                 catch(java.io.IOException JavaDoc e) {
158                     webServices = null;
159                     //Don't do anything
160
//FIX-ME: Log
161
}
162
163             }
164         }
165     }
166
167 }
168
Popular Tags