1 19 20 package org.netbeans.modules.web.project.ui; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.net.URI ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.modules.web.project.SourceRoots; 36 import org.netbeans.modules.web.project.UpdateHelper; 37 import org.netbeans.modules.web.project.WebProject; 38 import org.netbeans.modules.web.project.ui.customizer.CustomizerLibraries; 39 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties; 40 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 41 import org.netbeans.spi.project.support.ant.ReferenceHelper; 42 import org.netbeans.spi.project.ui.support.NodeFactory; 43 import org.netbeans.spi.project.ui.support.NodeList; 44 import org.openide.nodes.Node; 45 import org.openide.util.NbBundle; 46 47 51 public final class LibrariesNodeFactory implements NodeFactory { 52 53 54 public LibrariesNodeFactory() { 55 } 56 57 public NodeList createNodes(Project p) { 58 WebProject project = (WebProject)p.getLookup().lookup(WebProject.class); 59 assert project != null; 60 return new LibrariesNodeList(project); 61 } 62 63 private static class LibrariesNodeList implements NodeList<String >, PropertyChangeListener { 64 private static final String LIBRARIES = "Libs"; private static final String TEST_LIBRARIES = "TestLibs"; 67 private final SourceRoots testSources; 68 private final WebProject project; 69 private final ArrayList <ChangeListener > listeners = new ArrayList <ChangeListener >(); 70 71 private final PropertyEvaluator evaluator; 72 private final UpdateHelper helper; 73 private final ReferenceHelper resolver; 74 75 LibrariesNodeList(WebProject proj) { 76 project = proj; 77 testSources = project.getTestSourceRoots(); 78 WebLogicalViewProvider logView = (WebLogicalViewProvider)project.getLookup().lookup(WebLogicalViewProvider.class); 79 assert logView != null; 80 evaluator = logView.getEvaluator(); 81 helper = logView.getUpdateHelper(); 82 resolver = logView.getRefHelper(); 83 } 84 85 public List <String > keys() { 86 List <String > result = new ArrayList <String >(); 87 result.add(LIBRARIES); 88 URL [] testRoots = testSources.getRootURLs(); 89 boolean addTestSources = false; 90 for (int i = 0; i < testRoots.length; i++) { 91 File f = new File (URI.create(testRoots[i].toExternalForm())); 92 if (f.exists()) { 93 addTestSources = true; 94 break; 95 } 96 } 97 if (addTestSources) { 98 result.add(TEST_LIBRARIES); 99 } 100 return result; 101 } 102 103 public synchronized void addChangeListener(ChangeListener l) { 104 listeners.add(l); 105 } 106 107 public synchronized void removeChangeListener(ChangeListener l) { 108 listeners.remove(l); 109 } 110 111 private void fireChange() { 112 ArrayList <ChangeListener > list = new ArrayList <ChangeListener >(); 113 synchronized (this) { 114 list.addAll(listeners); 115 } 116 Iterator <ChangeListener > it = list.iterator(); 117 while (it.hasNext()) { 118 ChangeListener elem = it.next(); 119 elem.stateChanged(new ChangeEvent ( this )); 120 } 121 } 122 123 public Node node(String key) { 124 if (key == LIBRARIES) { 125 return 127 new LibrariesNode( 128 NbBundle.getMessage(LibrariesNodeFactory.class,"CTL_LibrariesNode"), 129 project, 130 evaluator, 131 helper, 132 resolver, 133 WebProjectProperties.JAVAC_CLASSPATH, 134 new String [] {WebProjectProperties.BUILD_CLASSES_DIR}, 135 "platform.active", WebProjectProperties.J2EE_SERVER_INSTANCE, 137 new Action [] { 138 LibrariesNode.createAddProjectAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES), 139 LibrariesNode.createAddLibraryAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES), 140 LibrariesNode.createAddFolderAction(project, WebProjectProperties.JAVAC_CLASSPATH, WebProjectProperties.TAG_WEB_MODULE_LIBRARIES), 141 null, 142 new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE), }, 144 WebProjectProperties.TAG_WEB_MODULE_LIBRARIES 145 ); 146 } else if (key == TEST_LIBRARIES) { 147 return 148 new LibrariesNode( 149 NbBundle.getMessage(LibrariesNodeFactory.class,"CTL_TestLibrariesNode"), 150 project, 151 evaluator, 152 helper, 153 resolver, 154 WebProjectProperties.JAVAC_TEST_CLASSPATH, 155 new String [] { 156 WebProjectProperties.BUILD_TEST_CLASSES_DIR, 157 WebProjectProperties.JAVAC_CLASSPATH, 158 WebProjectProperties.BUILD_CLASSES_DIR, 159 }, 160 null, 161 null, 162 new Action [] { 163 LibrariesNode.createAddProjectAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null), 164 LibrariesNode.createAddLibraryAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null), 165 LibrariesNode.createAddFolderAction(project, WebProjectProperties.JAVAC_TEST_CLASSPATH, null), 166 null, 167 new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE_TESTS), }, 169 null 170 ); 171 } 172 assert false: "No node for key: " + key; 173 return null; 174 175 } 176 177 public void addNotify() { 178 testSources.addPropertyChangeListener(this); 179 } 180 181 public void removeNotify() { 182 testSources.removePropertyChangeListener(this); 183 } 184 185 public void propertyChange(PropertyChangeEvent evt) { 186 SwingUtilities.invokeLater(new Runnable () { 188 public void run() { 189 fireChange(); 190 } 191 }); 192 } 193 194 } 195 196 } 197 | Popular Tags |