1 19 20 package org.netbeans.modules.java.j2seproject.ui; 21 22 import java.awt.event.ActionEvent ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.SwingUtilities ; 30 import javax.swing.event.ChangeEvent ; 31 import javax.swing.event.ChangeListener ; 32 import org.netbeans.api.java.project.JavaProjectConstants; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.api.project.SourceGroup; 36 import org.netbeans.api.project.Sources; 37 import org.netbeans.modules.java.j2seproject.J2SEProject; 38 import org.netbeans.modules.java.j2seproject.ui.customizer.CustomizerProviderImpl; 39 import org.netbeans.spi.java.project.support.ui.PackageView; 40 import org.netbeans.spi.project.ui.support.NodeFactorySupport; 41 import org.netbeans.spi.project.ui.support.NodeFactory; 42 import org.netbeans.spi.project.ui.support.NodeList; 43 import org.openide.filesystems.FileObject; 44 import org.openide.nodes.FilterNode; 45 import org.openide.nodes.Node; 46 import org.openide.util.NbBundle; 47 48 52 public final class SourceNodeFactory implements NodeFactory { 53 public SourceNodeFactory() { 54 } 55 56 public NodeList createNodes(Project p) { 57 J2SEProject project = (J2SEProject)p.getLookup().lookup(J2SEProject.class); 58 assert project != null; 59 return new SourcesNodeList(project); 60 } 61 62 private static class SourcesNodeList implements NodeList<SourceGroupKey>, ChangeListener { 63 64 private J2SEProject project; 65 66 private List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 67 68 public SourcesNodeList(J2SEProject proj) { 69 project = proj; 70 } 71 72 public List <SourceGroupKey> keys() { 73 if (this.project.getProjectDirectory() == null || !this.project.getProjectDirectory().isValid()) { 74 return Collections.EMPTY_LIST; 75 } 76 Sources sources = getSources(); 77 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 78 79 List result = new ArrayList (groups.length); 80 for( int i = 0; i < groups.length; i++ ) { 81 result.add(new SourceGroupKey(groups[i])); 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(SourceGroupKey key) { 107 return new PackageViewFilterNode(key.group, project); 108 } 109 110 public void addNotify() { 111 getSources().addChangeListener(this); 112 } 113 114 public void removeNotify() { 115 getSources().removeChangeListener(this); 116 } 117 118 public void stateChanged(ChangeEvent e) { 119 SwingUtilities.invokeLater(new Runnable () { 122 public void run() { 123 fireChange(); 124 } 125 }); 126 } 127 128 private Sources getSources() { 129 return ProjectUtils.getSources(project); 130 } 131 132 } 133 134 private static class SourceGroupKey { 135 136 public final SourceGroup group; 137 public final FileObject fileObject; 138 139 SourceGroupKey(SourceGroup group) { 140 this.group = group; 141 this.fileObject = group.getRootFolder(); 142 } 143 144 public int hashCode() { 145 return fileObject.hashCode(); 146 } 147 148 public boolean equals(Object obj) { 149 if (!(obj instanceof SourceGroupKey)) { 150 return false; 151 } else { 152 SourceGroupKey otherKey = (SourceGroupKey) obj; 153 String thisDisplayName = this.group.getDisplayName(); 154 String otherDisplayName = otherKey.group.getDisplayName(); 155 return fileObject.equals(otherKey.fileObject) && 157 thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName); 158 } 159 } 160 161 } 162 163 165 private static class PackageViewFilterNode extends FilterNode { 166 167 private String nodeName; 168 private Project project; 169 170 Action [] actions; 171 172 public PackageViewFilterNode(SourceGroup sourceGroup, Project project) { 173 super(PackageView.createPackageView(sourceGroup)); 174 this.project = project; 175 this.nodeName = "Sources"; 176 } 177 178 179 public Action [] getActions(boolean context) { 180 if (!context) { 181 if (actions == null) { 182 Action superActions[] = super.getActions(context); 183 actions = new Action [superActions.length + 2]; 184 System.arraycopy(superActions, 0, actions, 0, superActions.length); 185 actions[superActions.length] = null; 186 actions[superActions.length + 1] = new PreselectPropertiesAction(project, nodeName); 187 } 188 return actions; 189 } else { 190 return super.getActions(context); 191 } 192 } 193 194 } 195 196 197 199 static class PreselectPropertiesAction extends AbstractAction { 200 201 private final Project project; 202 private final String nodeName; 203 private final String panelName; 204 205 public PreselectPropertiesAction(Project project, String nodeName) { 206 this(project, nodeName, null); 207 } 208 209 public PreselectPropertiesAction(Project project, String nodeName, String panelName) { 210 super(NbBundle.getMessage(J2SELogicalViewProvider.class, "LBL_Properties_Action")); 211 this.project = project; 212 this.nodeName = nodeName; 213 this.panelName = panelName; 214 } 215 216 public void actionPerformed(ActionEvent e) { 217 CustomizerProviderImpl cp = (CustomizerProviderImpl) project.getLookup().lookup(CustomizerProviderImpl.class); 219 if (cp != null) { 220 cp.showCustomizer(nodeName, panelName); 221 } 222 223 } 224 } 225 226 } 227 | Popular Tags |