KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > 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.ruby.rubyproject.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.project.Project;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.api.project.SourceGroup;
35 import org.netbeans.api.project.Sources;
36 import org.netbeans.modules.ruby.rubyproject.RubyProject;
37 import org.netbeans.modules.ruby.rubyproject.ui.customizer.CustomizerProviderImpl;
38 import org.netbeans.spi.project.ui.support.NodeFactorySupport;
39 import org.netbeans.spi.project.ui.support.NodeFactory;
40 import org.netbeans.spi.project.ui.support.NodeList;
41 import org.openide.filesystems.FileObject;
42 import org.openide.nodes.AbstractNode;
43 import org.openide.nodes.FilterNode;
44 import org.openide.nodes.Node;
45 import org.openide.util.NbBundle;
46
47 /**
48  *
49  * @author mkleint
50  */

51 public final class SourceNodeFactory implements NodeFactory {
52     public SourceNodeFactory() {
53     }
54     
55     public NodeList createNodes(Project p) {
56         RubyProject project = (RubyProject)p.getLookup().lookup(RubyProject.class);
57         assert project != null;
58         return new SourcesNodeList(project);
59     }
60     
61     private static class SourcesNodeList implements NodeList<SourceGroupKey>, ChangeListener JavaDoc {
62         
63         private RubyProject project;
64         
65         private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
66         
67         public SourcesNodeList(RubyProject proj) {
68             project = proj;
69         }
70         
71         public List JavaDoc<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(RubyProject.SOURCES_TYPE_RUBY);
77             // Here we're adding sources, tests
78
List JavaDoc result = new ArrayList JavaDoc(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 JavaDoc l) {
86             listeners.add(l);
87         }
88         
89         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
90             listeners.remove(l);
91         }
92         
93         private void fireChange() {
94             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
95             synchronized (this) {
96                 list.addAll(listeners);
97             }
98             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
99             while (it.hasNext()) {
100                 ChangeListener JavaDoc elem = it.next();
101                 elem.stateChanged(new ChangeEvent JavaDoc( 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 JavaDoc e) {
118             // setKeys(getKeys());
119
// The caller holds ProjectManager.mutex() read lock
120
SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc obj) {
148             if (!(obj instanceof SourceGroupKey)) {
149                 return false;
150             } else {
151                 SourceGroupKey otherKey = (SourceGroupKey) obj;
152                 String JavaDoc thisDisplayName = this.group.getDisplayName();
153                 String JavaDoc otherDisplayName = otherKey.group.getDisplayName();
154                 // XXX what is the operator binding order supposed to be here??
155
return fileObject.equals(otherKey.fileObject) &&
156                         thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName);
157             }
158         }
159         
160     }
161     
162     // Copied from inner class in Java Projects' PackageView class:
163
/**
164      * FilterNode which listens on the PackageViewSettings and changes the view to
165      * the package view or tree view
166      *
167      */

168     private static final class RootNode extends FilterNode { // implements PropertyChangeListener {
169

170         private SourceGroup sourceGroup;
171         
172         private RootNode (SourceGroup group) {
173             super(getOriginalNode(group));
174             this.sourceGroup = group;
175             //JavaProjectSettings.addPropertyChangeListener(WeakListeners.propertyChange(this, JavaProjectSettings.class));
176
}
177         
178 // public void propertyChange (PropertyChangeEvent event) {
179
// if (JavaProjectSettings.PROP_PACKAGE_VIEW_TYPE.equals(event.getPropertyName())) {
180
// changeOriginal(getOriginalNode(sourceGroup), true);
181
// }
182
// }
183

184         private static Node getOriginalNode(SourceGroup group) {
185             FileObject root = group.getRootFolder();
186             //Guard condition, if the project is (closed) and deleted but not yet gced
187
// and the view is switched, the source group is not valid.
188
if ( root == null || !root.isValid()) {
189                 return new AbstractNode (Children.LEAF);
190             }
191 // switch (JavaProjectSettings.getPackageViewType()) {
192
// case JavaProjectSettings.TYPE_PACKAGE_VIEW:
193
// return new PackageRootNode(group);
194
// case JavaProjectSettings.TYPE_TREE:
195
return new TreeRootNode(group);
196 // default:
197
// assert false : "Unknown PackageView Type"; //NOI18N
198
// return new PackageRootNode(group);
199
// }
200
}
201     }
202     
203     
204     
205     /** Yet another cool filter node just to add properties action
206      */

207     private static class PackageViewFilterNode extends FilterNode {
208         
209         private String JavaDoc nodeName;
210         private Project project;
211         
212         Action JavaDoc[] actions;
213         
214         public PackageViewFilterNode(SourceGroup sourceGroup, Project project) {
215             //super(PackageView.createPackageView(sourceGroup));
216
super(new RootNode(sourceGroup));
217             
218             this.project = project;
219             this.nodeName = "Sources";
220         }
221         
222         
223         public Action JavaDoc[] getActions(boolean context) {
224             if (!context) {
225                 if (actions == null) {
226                     Action JavaDoc superActions[] = super.getActions(context);
227                     actions = new Action JavaDoc[superActions.length + 2];
228                     System.arraycopy(superActions, 0, actions, 0, superActions.length);
229                     actions[superActions.length] = null;
230                     actions[superActions.length + 1] = new PreselectPropertiesAction(project, nodeName);
231                 }
232                 return actions;
233             } else {
234                 return super.getActions(context);
235             }
236         }
237         
238     }
239     
240     
241     /** The special properties action
242      */

243     static class PreselectPropertiesAction extends AbstractAction JavaDoc {
244         
245         private final Project project;
246         private final String JavaDoc nodeName;
247         private final String JavaDoc panelName;
248         
249         public PreselectPropertiesAction(Project project, String JavaDoc nodeName) {
250             this(project, nodeName, null);
251         }
252         
253         public PreselectPropertiesAction(Project project, String JavaDoc nodeName, String JavaDoc panelName) {
254             super(NbBundle.getMessage(SourceNodeFactory.class, "LBL_Properties_Action"));
255             this.project = project;
256             this.nodeName = nodeName;
257             this.panelName = panelName;
258         }
259         
260         public void actionPerformed(ActionEvent JavaDoc e) {
261             // RubyCustomizerProvider cp = (RubyCustomizerProvider) project.getLookup().lookup(RubyCustomizerProvider.class);
262
CustomizerProviderImpl cp = (CustomizerProviderImpl) project.getLookup().lookup(CustomizerProviderImpl.class);
263             if (cp != null) {
264                 cp.showCustomizer(nodeName, panelName);
265             }
266             
267         }
268     }
269     
270 }
271
Popular Tags