1 19 20 package org.netbeans.modules.java.j2seproject.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.java.j2seproject.J2SEProject; 36 import org.netbeans.modules.java.j2seproject.SourceRoots; 37 import org.netbeans.modules.java.j2seproject.UpdateHelper; 38 import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerLibraries; 39 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 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 J2SEProject project = (J2SEProject)p.getLookup().lookup(J2SEProject.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 SourceRoots testSources; 68 private J2SEProject project; 69 private ArrayList <ChangeListener > listeners = new ArrayList <ChangeListener >(); 70 71 private PropertyEvaluator evaluator; 72 private UpdateHelper helper; 73 private ReferenceHelper resolver; 74 75 LibrariesNodeList(J2SEProject proj) { 76 project = proj; 77 testSources = project.getTestSourceRoots(); 78 J2SELogicalViewProvider logView = (J2SELogicalViewProvider)project.getLookup().lookup(J2SELogicalViewProvider.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(NbBundle.getMessage(J2SELogicalViewProvider.class,"CTL_LibrariesNode"), 128 project, evaluator, helper, resolver, J2SEProjectProperties.RUN_CLASSPATH, 129 new String [] {J2SEProjectProperties.BUILD_CLASSES_DIR}, 130 "platform.active", new Action [] { 132 LibrariesNode.createAddProjectAction(project, J2SEProjectProperties.JAVAC_CLASSPATH), 133 LibrariesNode.createAddLibraryAction(project, J2SEProjectProperties.JAVAC_CLASSPATH), 134 LibrariesNode.createAddFolderAction(project, J2SEProjectProperties.JAVAC_CLASSPATH), 135 null, 136 new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE), } 138 ); 139 } else if (key == TEST_LIBRARIES) { 140 return 141 new LibrariesNode(NbBundle.getMessage(J2SELogicalViewProvider.class,"CTL_TestLibrariesNode"), 142 project, evaluator, helper, resolver, J2SEProjectProperties.RUN_TEST_CLASSPATH, 143 new String [] { 144 J2SEProjectProperties.BUILD_TEST_CLASSES_DIR, 145 J2SEProjectProperties.JAVAC_CLASSPATH, 146 J2SEProjectProperties.BUILD_CLASSES_DIR, 147 }, 148 null, 149 new Action [] { 150 LibrariesNode.createAddProjectAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH), 151 LibrariesNode.createAddLibraryAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH), 152 LibrariesNode.createAddFolderAction(project, J2SEProjectProperties.JAVAC_TEST_CLASSPATH), 153 null, 154 new SourceNodeFactory.PreselectPropertiesAction(project, "Libraries", CustomizerLibraries.COMPILE_TESTS), } 156 ); 157 } 158 assert false: "No node for key: " + key; 159 return null; 160 161 } 162 163 public void addNotify() { 164 testSources.addPropertyChangeListener(this); 165 } 166 167 public void removeNotify() { 168 testSources.removePropertyChangeListener(this); 169 } 170 171 public void propertyChange(PropertyChangeEvent evt) { 172 SwingUtilities.invokeLater(new Runnable () { 174 public void run() { 175 fireChange(); 176 } 177 }); 178 } 179 180 } 181 182 } 183 | Popular Tags |