1 19 20 package org.netbeans.modules.web.project.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.web.project.WebProject; 38 import org.netbeans.modules.web.project.ui.customizer.CustomizerProviderImpl; 39 import org.netbeans.spi.java.project.support.ui.PackageView; 40 import org.netbeans.spi.project.ui.support.NodeFactory; 41 import org.netbeans.spi.project.ui.support.NodeList; 42 import org.openide.filesystems.FileObject; 43 import org.openide.nodes.FilterNode; 44 import org.openide.nodes.Node; 45 import org.openide.util.NbBundle; 46 47 51 public final class SourceNodeFactory implements NodeFactory { 52 public SourceNodeFactory() { 53 } 54 55 public NodeList createNodes(Project p) { 56 WebProject project = (WebProject)p.getLookup().lookup(WebProject.class); 57 assert project != null; 58 return new SourcesNodeList(project); 59 } 60 61 private static class SourcesNodeList implements NodeList<SourceGroupKey>, ChangeListener { 62 63 private final WebProject project; 64 65 private final List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 66 67 public SourcesNodeList(WebProject proj) { 68 project = proj; 69 } 70 71 public List <SourceGroupKey> keys() { 72 if (this.project.getProjectDirectory() == null || !this.project.getProjectDirectory().isValid()) { 73 return Collections.EMPTY_LIST; 74 } 75 Sources sources = getSources(); 76 SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 77 78 List result = new ArrayList (groups.length); 79 for( int i = 0; i < groups.length; i++ ) { 80 result.add(new SourceGroupKey(groups[i])); 81 } 82 return result; 83 } 84 85 public synchronized void addChangeListener(ChangeListener l) { 86 listeners.add(l); 87 } 88 89 public synchronized void removeChangeListener(ChangeListener l) { 90 listeners.remove(l); 91 } 92 93 private void fireChange() { 94 ArrayList <ChangeListener > list = new ArrayList <ChangeListener >(); 95 synchronized (this) { 96 list.addAll(listeners); 97 } 98 Iterator <ChangeListener > it = list.iterator(); 99 while (it.hasNext()) { 100 ChangeListener elem = it.next(); 101 elem.stateChanged(new ChangeEvent ( this )); 102 } 103 } 104 105 public Node node(SourceGroupKey key) { 106 return new PackageViewFilterNode(key.group, project); 107 } 108 109 public void addNotify() { 110 getSources().addChangeListener(this); 111 } 112 113 public void removeNotify() { 114 getSources().removeChangeListener(this); 115 } 116 117 public void stateChanged(ChangeEvent e) { 118 SwingUtilities.invokeLater(new Runnable () { 121 public void run() { 122 fireChange(); 123 } 124 }); 125 } 126 127 private Sources getSources() { 128 return ProjectUtils.getSources(project); 129 } 130 131 } 132 133 private static class SourceGroupKey { 134 135 public final SourceGroup group; 136 public final FileObject fileObject; 137 138 SourceGroupKey(SourceGroup group) { 139 this.group = group; 140 this.fileObject = group.getRootFolder(); 141 } 142 143 public int hashCode() { 144 return fileObject.hashCode(); 145 } 146 147 public boolean equals(Object obj) { 148 if (!(obj instanceof SourceGroupKey)) { 149 return false; 150 } else { 151 SourceGroupKey otherKey = (SourceGroupKey) obj; 152 String thisDisplayName = this.group.getDisplayName(); 153 String otherDisplayName = otherKey.group.getDisplayName(); 154 return fileObject.equals(otherKey.fileObject) && 156 thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName); 157 } 158 } 159 160 } 161 162 164 private static class PackageViewFilterNode extends FilterNode { 165 166 private final String nodeName; 167 private final Project project; 168 169 Action [] actions; 170 171 public PackageViewFilterNode(SourceGroup sourceGroup, Project project) { 172 super(PackageView.createPackageView(sourceGroup)); 173 this.project = project; 174 this.nodeName = "Sources"; 175 } 176 177 178 public Action [] getActions(boolean context) { 179 if (!context) { 180 if (actions == null) { 181 Action superActions[] = super.getActions(context); 182 actions = new Action [superActions.length + 2]; 183 System.arraycopy(superActions, 0, actions, 0, superActions.length); 184 actions[superActions.length] = null; 185 actions[superActions.length + 1] = new PreselectPropertiesAction(project, nodeName); 186 } 187 return actions; 188 } else { 189 return super.getActions(context); 190 } 191 } 192 193 } 194 195 196 198 static class PreselectPropertiesAction extends AbstractAction { 199 200 private final Project project; 201 private final String nodeName; 202 private final String panelName; 203 204 public PreselectPropertiesAction(Project project, String nodeName) { 205 this(project, nodeName, null); 206 } 207 208 public PreselectPropertiesAction(Project project, String nodeName, String panelName) { 209 super(NbBundle.getMessage(SourceNodeFactory.class, "LBL_Properties_Action")); this.project = project; 211 this.nodeName = nodeName; 212 this.panelName = panelName; 213 } 214 215 public void actionPerformed(ActionEvent e) { 216 CustomizerProviderImpl cp = (CustomizerProviderImpl) project.getLookup().lookup(CustomizerProviderImpl.class); 218 if (cp != null) { 219 cp.showCustomizer(nodeName, panelName); 220 } 221 222 } 223 } 224 225 } 226 | Popular Tags |