1 19 20 package org.netbeans.modules.websvc.jaxrpc.nodes; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.ChangeEvent ; 30 import javax.swing.event.ChangeListener ; 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 49 public class JaxRpcNodeFactory implements NodeFactory { 50 private static final String WSDL_FOLDER = "wsdl"; 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 > { 61 62 private static final String KEY_SERVICE_REFS = "serviceRefs"; private Project project; 65 66 private List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 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 keys() { 78 List <String > result = new ArrayList <String >(); 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 l) { 87 listeners.add(l); 88 } 89 90 public synchronized void removeChangeListener(ChangeListener l) { 91 listeners.remove(l); 92 } 93 94 private void fireChange() { 95 ArrayList <ChangeListener > list = new ArrayList <ChangeListener >(); 96 synchronized (this) { 97 list.addAll(listeners); 98 } 99 Iterator <ChangeListener > it = list.iterator(); 100 while (it.hasNext()) { 101 ChangeListener elem = it.next(); 102 elem.stateChanged(new ChangeEvent ( this )); 103 } 104 } 105 106 public Node node(String 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)) { 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 WebServicesClientSupport wsClientSupportImpl = WebServicesClientSupport.getWebServicesClientSupport(project.getProjectDirectory()); 146 try { 147 if (wsClientSupportImpl != null) { 148 wsdlFolder = wsClientSupportImpl.getWsdlFolder(false); 149 } 150 } catch (IOException 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 () { 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 () { 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 () { 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())) { 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 () { 218 public void run() { 219 fireChange(); 220 } 221 }); 222 } else if (fe.getFile().isFolder() && "META-INF".equals(fe.getFile().getName())) { fe.getFile().removeFileChangeListener(metaInfListener); 224 } 225 } 226 } 227 228 229 private final class JaxWsChangeListener implements PropertyChangeListener { 230 public void propertyChange(PropertyChangeEvent evt) { 231 SwingUtilities.invokeLater(new Runnable () { 232 public void run() { 233 fireChange(); 234 } 235 }); 236 } 237 } 238 } 239 240 } 241 | Popular Tags |