KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > ui > TreeRootNode


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.Image JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import javax.swing.event.EventListenerList JavaDoc;
33 import org.netbeans.api.project.SourceGroup;
34 import org.netbeans.api.queries.VisibilityQuery;
35 import org.openide.DialogDisplayer;
36 import org.openide.NotifyDescriptor;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.loaders.ChangeableDataFilter;
40 import org.openide.loaders.DataFilter;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.nodes.FilterNode;
44 import org.openide.nodes.Node;
45 import org.openide.nodes.NodeNotFoundException;
46 import org.openide.nodes.NodeOp;
47 import org.openide.util.NbBundle;
48 import org.openide.util.Utilities;
49 import org.openide.util.WeakListeners;
50 import org.openide.util.lookup.Lookups;
51 import org.openide.util.lookup.ProxyLookup;
52
53 // XXX need unit test
54

55 /**
56  * (Copied from Java Project Source (org.netbeans.spi.java.project.support.ui)
57  * Displays a package root in a tree.
58  * @see "#42151"
59  * @author Jesse Glick
60  */

61 public final class TreeRootNode extends FilterNode implements PropertyChangeListener JavaDoc {
62     
63     private static final DataFilter VISIBILITY_QUERY_FILTER = new VisibilityQueryDataFilter();
64     
65     private final SourceGroup g;
66     
67     public TreeRootNode(SourceGroup g) {
68         this(DataFolder.findFolder(g.getRootFolder()), g);
69     }
70     
71     private TreeRootNode(DataFolder folder, SourceGroup g) {
72         this (new FilterNode (folder.getNodeDelegate(), folder.createNodeChildren(VISIBILITY_QUERY_FILTER)),g);
73     }
74     
75     private TreeRootNode (Node originalNode, SourceGroup g) {
76         super(originalNode, new PackageFilterChildren(originalNode),
77             new ProxyLookup(
78                 originalNode.getLookup(),
79                 Lookups.singleton(new PathFinder(g))
80                 // no need for explicit search info
81
));
82         this.g = g;
83         g.addPropertyChangeListener(WeakListeners.propertyChange(this, g));
84     }
85
86     static Image JavaDoc PACKAGE_BADGE = Utilities.loadImage( "org/netbeans/modules/ruby/rubyproject/ui/packageBadge.gif" ); // NOI18N
87

88     /** Copied from PackageRootNode with modifications. */
89     private Image JavaDoc computeIcon(boolean opened, int type) {
90         Icon JavaDoc icon = g.getIcon(opened);
91         if (icon == null) {
92             Image JavaDoc image = opened ? super.getOpenedIcon(type) : super.getIcon(type);
93             return Utilities.mergeImages(image, /*PackageRootNode.*/PACKAGE_BADGE, 7, 7);
94         } else {
95             return Utilities.icon2Image(icon);
96         }
97     }
98     
99     public Image JavaDoc getIcon(int type) {
100         return computeIcon(false, type);
101     }
102
103     public Image JavaDoc getOpenedIcon(int type) {
104         return computeIcon(true, type);
105     }
106
107     public String JavaDoc getName() {
108         return g.getName();
109     }
110
111     public String JavaDoc getDisplayName() {
112         return g.getDisplayName();
113     }
114
115     public boolean canRename() {
116         return false;
117     }
118
119     public boolean canDestroy() {
120         return false;
121     }
122
123     public boolean canCut() {
124         return false;
125     }
126
127     public void propertyChange(PropertyChangeEvent JavaDoc ev) {
128         // XXX handle SourceGroup.rootFolder change too
129
fireNameChange(null, null);
130         fireDisplayNameChange(null, null);
131         fireIconChange();
132         fireOpenedIconChange();
133     }
134
135     /** Copied from PhysicalView and PackageRootNode. */
136     public static final class PathFinder {
137         
138         private final SourceGroup g;
139         
140         PathFinder(SourceGroup g) {
141             this.g = g;
142         }
143         
144         public Node findPath(Node rootNode, Object JavaDoc o) {
145             FileObject fo;
146             if (o instanceof FileObject) {
147                 fo = (FileObject) o;
148             } else if (o instanceof DataObject) {
149                 fo = ((DataObject) o).getPrimaryFile();
150             } else {
151                 return null;
152             }
153             FileObject groupRoot = g.getRootFolder();
154             if (FileUtil.isParentOf(groupRoot, fo) /* && group.contains(fo) */) {
155                 FileObject folder = fo.isFolder() ? fo : fo.getParent();
156                 String JavaDoc relPath = FileUtil.getRelativePath(groupRoot, folder);
157                 List JavaDoc<String JavaDoc> path = new ArrayList JavaDoc<String JavaDoc>();
158                 StringTokenizer JavaDoc strtok = new StringTokenizer JavaDoc(relPath, "/"); // NOI18N
159
while (strtok.hasMoreTokens()) {
160                     String JavaDoc token = strtok.nextToken();
161                    path.add(token);
162                 }
163                 try {
164                     Node folderNode = folder.equals(groupRoot) ? rootNode : NodeOp.findPath(rootNode, Collections.enumeration(path));
165                     if (fo.isFolder()) {
166                         return folderNode;
167                     } else {
168                         Node[] childs = folderNode.getChildren().getNodes(true);
169                         for (int i = 0; i < childs.length; i++) {
170                            DataObject dobj = childs[i].getLookup().lookup(DataObject.class);
171                            if (dobj != null && dobj.getPrimaryFile().getNameExt().equals(fo.getNameExt())) {
172                                return childs[i];
173                            }
174                         }
175                     }
176                 } catch (NodeNotFoundException e) {
177                     e.printStackTrace();
178                 }
179             } else if (groupRoot.equals(fo)) {
180                 return rootNode;
181             }
182             return null;
183         }
184     }
185     
186     /** Copied from PhysicalView. */
187     private static final class VisibilityQueryDataFilter implements ChangeListener JavaDoc, ChangeableDataFilter {
188         
189         private static final long serialVersionUID = 1L; // in case a DataFolder.ClonedFilterHandle saves me
190

191         private final EventListenerList JavaDoc ell = new EventListenerList JavaDoc();
192         
193         public VisibilityQueryDataFilter() {
194             VisibilityQuery.getDefault().addChangeListener(this);
195         }
196         
197         public boolean acceptDataObject(DataObject obj) {
198             FileObject fo = obj.getPrimaryFile();
199             return VisibilityQuery.getDefault().isVisible(fo);
200         }
201         
202         public void stateChanged(ChangeEvent JavaDoc e) {
203             Object JavaDoc[] listeners = ell.getListenerList();
204             ChangeEvent JavaDoc event = null;
205             for (int i = listeners.length - 2; i >= 0; i -= 2) {
206                 if (listeners[i] == ChangeListener JavaDoc.class) {
207                     if (event == null) {
208                         event = new ChangeEvent JavaDoc(this);
209                     }
210                     ((ChangeListener JavaDoc) listeners[i+1]).stateChanged(event);
211                 }
212             }
213         }
214         
215         public void addChangeListener(ChangeListener JavaDoc listener) {
216             ell.add(ChangeListener JavaDoc.class, listener);
217         }
218         
219         public void removeChangeListener(ChangeListener JavaDoc listener) {
220             ell.remove(ChangeListener JavaDoc.class, listener);
221         }
222         
223     }
224     
225     
226     private static final class PackageFilterChildren extends FilterNode.Children {
227         
228         public PackageFilterChildren (final Node originalNode) {
229             super (originalNode);
230         }
231                 
232         @Override JavaDoc
233         protected Node copyNode(final Node originalNode) {
234             DataObject dobj = originalNode.getLookup().lookup(DataObject.class);
235             return (dobj instanceof DataFolder) ? new PackageFilterNode (originalNode) : super.copyNode(originalNode);
236         }
237     }
238     
239     private static final class PackageFilterNode extends FilterNode {
240         
241         public PackageFilterNode (final Node origNode) {
242             super (origNode, new PackageFilterChildren (origNode));
243         }
244         
245         @Override JavaDoc
246         public void setName (final String JavaDoc name) {
247             if (Utilities.isJavaIdentifier (name)) {
248                 super.setName (name);
249             }
250             else {
251                 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (
252                     NbBundle.getMessage(TreeRootNode.class,"MSG_InvalidPackageName"), NotifyDescriptor.INFORMATION_MESSAGE));
253             }
254         }
255         
256     }
257     
258 }
259
Popular Tags