KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > railsprojects > 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.railsprojects.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.modules.ruby.railsprojects.GenerateAction;
33 import org.netbeans.modules.ruby.railsprojects.Generator;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.api.project.Sources;
38 import org.netbeans.modules.ruby.railsprojects.RailsProject;
39 import org.netbeans.modules.ruby.railsprojects.ui.customizer.CustomizerProviderImpl;
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.AbstractNode;
44 import org.openide.nodes.FilterNode;
45 import org.openide.nodes.Node;
46 import org.openide.util.NbBundle;
47 import org.openide.util.lookup.Lookups;
48
49 /**
50  *
51  * @author mkleint
52  */

53 public final class SourceNodeFactory implements NodeFactory {
54     public SourceNodeFactory() {
55     }
56     
57     public NodeList createNodes(Project p) {
58         RailsProject project = (RailsProject)p.getLookup().lookup(RailsProject.class);
59         assert project != null;
60         return new SourcesNodeList(project);
61     }
62     
63     private static class SourcesNodeList implements NodeList<SourceGroupKey>, ChangeListener JavaDoc {
64         
65         private RailsProject project;
66         
67         private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
68         
69         public SourcesNodeList(RailsProject proj) {
70             project = proj;
71         }
72         
73         public List JavaDoc<SourceGroupKey> keys() {
74             if (this.project.getProjectDirectory() == null || !this.project.getProjectDirectory().isValid()) {
75                 return Collections.EMPTY_LIST;
76             }
77 // Sources sources = getSources();
78
// SourceGroup[] groups = sources.getSourceGroups(RailsProject.SOURCES_TYPE_RUBY);
79
// // Here we're adding sources, tests
80
// List result = new ArrayList(groups.length);
81
// for( int i = 0; i < groups.length; i++ ) {
82
// result.add(new SourceGroupKey(groups[i]));
83
// }
84

85             Sources sources = getSources();
86             SourceGroup[] groups = sources.getSourceGroups(RailsProject.SOURCES_TYPE_RUBY);
87             // Here we're adding sources, tests
88
List JavaDoc result = new ArrayList JavaDoc(groups.length);
89             for( int i = 0; i < groups.length; i++ ) {
90                 result.add(new SourceGroupKey(groups[i], getGenerator(groups[i].getName())));
91             }
92
93             return result;
94         }
95         
96         private Generator getGenerator(String JavaDoc subdir) {
97             if (subdir.equals("app/controllers")) {
98                 return Generator.controller;
99             }
100             if (subdir.equals("app/views")) {
101                 return Generator.controller;
102             }
103             if (subdir.equals("app/models")) {
104                 return Generator.model;
105             }
106             if (subdir.equals("db")) {
107                 return Generator.migration;
108             }
109             return Generator.NONE;
110         }
111         
112         public synchronized void addChangeListener(ChangeListener JavaDoc l) {
113             listeners.add(l);
114         }
115         
116         public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
117             listeners.remove(l);
118         }
119         
120         private void fireChange() {
121             ArrayList JavaDoc<ChangeListener JavaDoc> list = new ArrayList JavaDoc<ChangeListener JavaDoc>();
122             synchronized (this) {
123                 list.addAll(listeners);
124             }
125             Iterator JavaDoc<ChangeListener JavaDoc> it = list.iterator();
126             while (it.hasNext()) {
127                 ChangeListener JavaDoc elem = it.next();
128                 elem.stateChanged(new ChangeEvent JavaDoc( this ));
129             }
130         }
131         
132         public Node node(SourceGroupKey key) {
133             return new PackageViewFilterNode(key.group, key.generator, project);
134         }
135         
136         public void addNotify() {
137             getSources().addChangeListener(this);
138         }
139         
140         public void removeNotify() {
141             getSources().removeChangeListener(this);
142         }
143         
144         public void stateChanged(ChangeEvent JavaDoc e) {
145             // setKeys(getKeys());
146
// The caller holds ProjectManager.mutex() read lock
147
SwingUtilities.invokeLater(new Runnable JavaDoc() {
148                 public void run() {
149                     fireChange();
150                 }
151             });
152         }
153         
154         private Sources getSources() {
155             return ProjectUtils.getSources(project);
156         }
157         
158     }
159     
160     private static class SourceGroupKey {
161         
162         public final SourceGroup group;
163         public final FileObject fileObject;
164         public final Generator generator;
165         
166         SourceGroupKey(SourceGroup group, Generator generator) {
167             this.group = group;
168             this.fileObject = group.getRootFolder();
169             this.generator = generator;
170         }
171         
172         public int hashCode() {
173             return fileObject.hashCode();
174         }
175         
176         public boolean equals(Object JavaDoc obj) {
177             if (!(obj instanceof SourceGroupKey)) {
178                 return false;
179             } else {
180                 SourceGroupKey otherKey = (SourceGroupKey) obj;
181                 String JavaDoc thisDisplayName = this.group.getDisplayName();
182                 String JavaDoc otherDisplayName = otherKey.group.getDisplayName();
183                 // XXX what is the operator binding order supposed to be here??
184
return fileObject.equals(otherKey.fileObject) &&
185                         thisDisplayName == null ? otherDisplayName == null : thisDisplayName.equals(otherDisplayName);
186             }
187         }
188     }
189     
190     // Copied from inner class in Java Projects' PackageView class:
191
/**
192      * FilterNode which listens on the PackageViewSettings and changes the view to
193      * the package view or tree view
194      *
195      */

196     private static final class RootNode extends FilterNode { // implements PropertyChangeListener {
197

198         private SourceGroup sourceGroup;
199         
200         private RootNode (SourceGroup group, Generator generator) {
201             // XXX?
202
super(getOriginalNode(group, generator));
203             this.sourceGroup = group;
204             //JavaProjectSettings.addPropertyChangeListener(WeakListeners.propertyChange(this, JavaProjectSettings.class));
205
}
206         
207 // public void propertyChange (PropertyChangeEvent event) {
208
// if (JavaProjectSettings.PROP_PACKAGE_VIEW_TYPE.equals(event.getPropertyName())) {
209
// changeOriginal(getOriginalNode(sourceGroup), true);
210
// }
211
// }
212

213         private static Node getOriginalNode(SourceGroup group, Generator generator) {
214             FileObject root = group.getRootFolder();
215             //Guard condition, if the project is (closed) and deleted but not yet gced
216
// and the view is switched, the source group is not valid.
217
if ( root == null || !root.isValid()) {
218                 return new AbstractNode (Children.LEAF, Lookups.singleton(Generator.NONE));
219             }
220 // switch (JavaProjectSettings.getPackageViewType()) {
221
// case JavaProjectSettings.TYPE_PACKAGE_VIEW:
222
// return new PackageRootNode(group);
223
// case JavaProjectSettings.TYPE_TREE:
224
return new TreeRootNode(group, generator);
225 // default:
226
// assert false : "Unknown PackageView Type"; //NOI18N
227
// return new PackageRootNode(group);
228
// }
229
}
230     }
231     
232     
233     
234     /** Yet another cool filter node just to add properties action
235      */

236     private static class PackageViewFilterNode extends FilterNode {
237         
238         private String JavaDoc nodeName;
239         private Project project;
240         
241         Action JavaDoc[] actions;
242         
243         public PackageViewFilterNode(SourceGroup sourceGroup, Generator generator, Project project) {
244             //super(PackageView.createPackageView(sourceGroup));
245
super(new RootNode(sourceGroup, generator));
246             
247             this.project = project;
248             this.nodeName = "Sources";
249         }
250         
251         
252         public Action JavaDoc[] getActions(boolean context) {
253             if (!context) {
254                 if (actions == null) {
255                     Action JavaDoc superActions[] = super.getActions(context);
256                     actions = new Action JavaDoc[superActions.length + 2];
257                     System.arraycopy(superActions, 0, actions, 0, superActions.length);
258                     actions[superActions.length] = null;
259                     actions[superActions.length + 1] = new PreselectPropertiesAction(project, nodeName);
260                 }
261                 return actions;
262             } else {
263                 return super.getActions(context);
264             }
265         }
266         
267     }
268     
269     
270     /** The special properties action
271      */

272     static class PreselectPropertiesAction extends AbstractAction JavaDoc {
273         
274         private final Project project;
275         private final String JavaDoc nodeName;
276         private final String JavaDoc panelName;
277         
278         public PreselectPropertiesAction(Project project, String JavaDoc nodeName) {
279             this(project, nodeName, null);
280         }
281         
282         public PreselectPropertiesAction(Project project, String JavaDoc nodeName, String JavaDoc panelName) {
283             super(NbBundle.getMessage(SourceNodeFactory.class, "LBL_Properties_Action"));
284             this.project = project;
285             this.nodeName = nodeName;
286             this.panelName = panelName;
287         }
288         
289         public void actionPerformed(ActionEvent JavaDoc e) {
290             // RubyCustomizerProvider cp = (RubyCustomizerProvider) project.getLookup().lookup(RubyCustomizerProvider.class);
291
CustomizerProviderImpl cp = (CustomizerProviderImpl) project.getLookup().lookup(CustomizerProviderImpl.class);
292             if (cp != null) {
293                 cp.showCustomizer(nodeName, panelName);
294             }
295             
296         }
297     }
298     
299 }
300
Popular Tags