KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > ui > SourceNodeFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.j2seproject.ui;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
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 /**
49  *
50  * @author mkleint
51  */

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 JavaDoc {
63         
64         private J2SEProject project;
65         
66         private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
67         
68         public SourcesNodeList(J2SEProject proj) {
69             project = proj;
70         }
71         
72         public List JavaDoc<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 JavaDoc result = new ArrayList JavaDoc(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 JavaDoc l) {
87             listeners.add(l);
88         }
89         
90         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
91             listeners.remove(l);
92         }
93         
94         private void fireChange() {
95             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
96             synchronized (this) {
97                 list.addAll(listeners);
98             }
99             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
100             while (it.hasNext()) {
101                 ChangeListener JavaDoc elem = it.next();
102                 elem.stateChanged(new ChangeEvent JavaDoc( 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 JavaDoc e) {
119             // setKeys(getKeys());
120
// The caller holds ProjectManager.mutex() read lock
121
SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc obj) {
149             if (!(obj instanceof SourceGroupKey)) {
150                 return false;
151             } else {
152                 SourceGroupKey otherKey = (SourceGroupKey) obj;
153                 String JavaDoc thisDisplayName = this.group.getDisplayName();
154                 String JavaDoc otherDisplayName = otherKey.group.getDisplayName();
155                 // XXX what is the operator binding order supposed to be here??
156
return fileObject.equals(otherKey.fileObject) &&
157                         thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName);
158             }
159         }
160         
161     }
162     
163     /** Yet another cool filter node just to add properties action
164      */

165     private static class PackageViewFilterNode extends FilterNode {
166         
167         private String JavaDoc nodeName;
168         private Project project;
169         
170         Action JavaDoc[] 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 JavaDoc[] getActions(boolean context) {
180             if (!context) {
181                 if (actions == null) {
182                     Action JavaDoc superActions[] = super.getActions(context);
183                     actions = new Action JavaDoc[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     /** The special properties action
198      */

199     static class PreselectPropertiesAction extends AbstractAction JavaDoc {
200         
201         private final Project project;
202         private final String JavaDoc nodeName;
203         private final String JavaDoc panelName;
204         
205         public PreselectPropertiesAction(Project project, String JavaDoc nodeName) {
206             this(project, nodeName, null);
207         }
208         
209         public PreselectPropertiesAction(Project project, String JavaDoc nodeName, String JavaDoc 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 JavaDoc e) {
217             // J2SECustomizerProvider cp = (J2SECustomizerProvider) project.getLookup().lookup(J2SECustomizerProvider.class);
218
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