KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > favorites > Favorites


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.favorites;
21
22 import java.awt.Image JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.Action JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32
33 import org.netbeans.api.queries.VisibilityQuery;
34 import org.openide.actions.CopyAction;
35 import org.openide.actions.CutAction;
36 import org.openide.actions.DeleteAction;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.filesystems.Repository;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataObject;
43 import org.openide.loaders.DataShadow;
44 import org.openide.nodes.FilterNode;
45 import org.openide.nodes.Node;
46 import org.openide.util.Exceptions;
47 import org.openide.util.NbBundle;
48 import org.openide.util.Utilities;
49
50 /**
51  *
52  * @author Jaroslav Tulach
53  */

54 final class Favorites extends FilterNode {
55     /** default node */
56     private static Node node;
57     /** node that represents root of filesystems */
58     private static Node root;
59
60     /** Creates new ProjectRootFilterNode. */
61     private Favorites(Node node) {
62         super(node, new Chldrn (node, false));
63     }
64     
65     public Image JavaDoc getIcon (int type) {
66         return Utilities.loadImage("org/netbeans/modules/favorites/resources/actionSelect.gif"); // NOI18N
67
}
68     
69     public Image JavaDoc getOpenedIcon (int type) {
70         return Utilities.loadImage("org/netbeans/modules/favorites/resources/actionSelect.gif"); // NOI18N
71
}
72     
73     public boolean canCopy () {
74         return false;
75     }
76     
77     public boolean canCut () {
78         return false;
79     }
80     
81     public boolean canRename () {
82         return false;
83     }
84     
85     public static DataFolder getFolder () {
86         try {
87             FileObject fo = FileUtil.createFolder (
88                 Repository.getDefault().getDefaultFileSystem().getRoot(),
89                 "Favorites" // NOI18N
90
);
91             DataFolder folder = DataFolder.findFolder(fo);
92             return folder;
93         } catch (IOException JavaDoc ex) {
94             Exceptions.printStackTrace(ex);
95             return DataFolder.findFolder (
96                 Repository.getDefault().getDefaultFileSystem().getRoot()
97             );
98         }
99         
100     }
101     
102     /** Getter for default filter node.
103      */

104     public static synchronized Node getNode () {
105         if (node == null) {
106             node = new Favorites (getFolder().getNodeDelegate ());
107         }
108         return node;
109     }
110     
111     /** Get name of home directory. Used from layer.
112      */

113     public static URL JavaDoc getHome ()
114     throws FileStateInvalidException, MalformedURLException JavaDoc {
115         String JavaDoc s = System.getProperty("user.home"); // NOI18N
116

117         File JavaDoc home = new File JavaDoc (s);
118         home = FileUtil.normalizeFile (home);
119         
120         return home.toURI ().toURL ();
121     }
122
123     /** Finds file for a given node
124      */

125     static File JavaDoc fileForNode (Node n) {
126         DataObject obj = (DataObject)n.getCookie (DataObject.class);
127         if (obj == null) return null;
128         
129         return FileUtil.toFile (
130             obj.getPrimaryFile()
131         );
132     }
133
134     public Handle getHandle () {
135         return new RootHandle ();
136     }
137     
138     public Action JavaDoc[] getActions(boolean context) {
139         return new Action JavaDoc[] {Actions.addOnFavoritesNode()};
140     }
141     
142     private static class RootHandle implements Node.Handle {
143         static final long serialVersionUID = 1907300072945111595L;
144
145         /** Return a node for the current project.
146         */

147         public Node getNode () {
148             return Favorites.getNode ();
149         }
150     }
151
152     private static class Chldrn extends FilterNode.Children
153     implements ChangeListener JavaDoc, Runnable JavaDoc {
154         private ChangeListener JavaDoc weak;
155         private boolean hideHidden;
156         /** Creates new Chldrn. */
157         public Chldrn (Node node, boolean hideHidden) {
158             super (node);
159             this.hideHidden = hideHidden;
160             
161             weak = org.openide.util.WeakListeners.change(this, VisibilityQuery.getDefault());
162             VisibilityQuery.getDefault().addChangeListener(weak);
163         }
164         
165         protected Node[] createNodes(Node node) {
166             if (hideHidden) {
167                 DataObject obj = (DataObject)node.getCookie(DataObject.class);
168                 if (obj != null && !VisibilityQuery.getDefault().isVisible(obj.getPrimaryFile())) {
169                     return null;
170                 }
171             }
172             
173             return new Node[] { new ProjectFilterNode (
174                 node,
175                 (node.isLeaf ()) ? org.openide.nodes.Children.LEAF : new Chldrn (node, true)
176             )};
177         }
178
179         public void stateChanged(ChangeEvent JavaDoc e) {
180             MUTEX.postWriteRequest(this);
181         }
182         
183         public void run() {
184             Node[] arr = original.getChildren().getNodes();
185             for (int i = 0; i < arr.length; i++) {
186                 refreshKey(arr[i]);
187             }
188         }
189     } // end of Chldrn
190

191     /** This FilterNode is sensitive to 'Delete Original Files' property of {@link ProjectOption}.
192      * When this property is true then original DataObjects pointed to by links under the project's node
193      * are deleted as the Delete is performed on the link's node.
194      */

195     private static class ProjectFilterNode extends FilterNode {
196
197         /** Creates new ProjectFilterNode. */
198         public ProjectFilterNode (Node node, org.openide.nodes.Children children) {
199             super (node, children);
200         }
201         
202         public String JavaDoc getDisplayName () {
203             //Change display name only for favorite nodes (links) under Favorites node.
204
if (Favorites.getNode().equals(this.getParentNode())) {
205                 DataShadow ds = (DataShadow) getCookie(DataShadow.class);
206                 if (ds != null) {
207                     String JavaDoc name = ds.getOriginal().getName();
208                     String JavaDoc path = FileUtil.getFileDisplayName(ds.getOriginal().getPrimaryFile());
209                     return NbBundle.getMessage(Favorites.class, "CTL_DisplayNameTemplate", name, path);
210                 } else {
211                     return super.getDisplayName();
212                 }
213             } else {
214                 return super.getDisplayName();
215             }
216             /*String s = super.getDisplayName ();
217             for (;;) {
218                 int indx = s.indexOf("(->)"); // NOI18N
219                 if (indx == -1) return s;
220                 
221                 s = s.substring(0, indx) + s.substring (indx + 4);
222             }*/

223         }
224         
225         // Must be overridden since getDisplayName is.
226
public String JavaDoc getHtmlDisplayName() {
227             return getOriginal().getHtmlDisplayName();
228         }
229         
230         public boolean canDestroy () {
231             boolean canDestroy = super.canDestroy ();
232             DataShadow link = (DataShadow) getCookie (DataShadow.class);
233
234             // if the DO of this node can be destroyed and the original DO should be destroyed too
235
// ask the original if it's allowed to delete it
236
if (canDestroy && isDeleteOriginal (link)) {
237                 canDestroy = link.getOriginal ().isDeleteAllowed ();
238             }
239
240             return canDestroy;
241         }
242
243         public void destroy () throws IOException JavaDoc {
244             if (canDestroy ()) {
245                 DataShadow link = (DataShadow) getCookie (DataShadow.class);
246                 DataObject original = isDeleteOriginal (link) ? link.getOriginal () : null;
247
248                 super.destroy ();
249
250                 if (original != null) {
251                     original.delete ();
252                 }
253             }
254         }
255
256         private boolean isDeleteOriginal (DataShadow link) {
257             return false;
258         }
259         
260         public Action JavaDoc[] getActions(boolean context) {
261             Action JavaDoc[] arr;
262             arr = super.getActions(context);
263             
264             //Find if given node is root
265
boolean isRoot = false;
266             DataObject dataObject = (DataObject) getCookie(DataObject.class);
267             if (dataObject != null) {
268                 FileObject fo = dataObject.getPrimaryFile();
269                 if (fo != null) {
270                     //Check if it is root.
271
File JavaDoc file = FileUtil.toFile(fo);
272                     if (file != null) {
273                         if (file.getParent() == null) {
274                             isRoot = true;
275                         }
276                     }
277                 }
278             }
279             
280             if (isRoot) {
281                 return createActionsForRoot(arr);
282             } else {
283                 if (Favorites.getNode().equals(this.getParentNode())) {
284                     DataShadow ds = (DataShadow) getCookie(DataShadow.class);
285                     if (ds != null) {
286                         if (ds.getOriginal().getPrimaryFile().isFolder()) {
287                             return createActionsForFavoriteFolder(arr);
288                         } else {
289                             return createActionsForFavoriteFile(arr);
290                         }
291                     }
292                 } else {
293                     DataObject dObj = (DataObject) getCookie(DataObject.class);
294                     if (dObj != null) {
295                         if (dObj.getPrimaryFile().isFolder()) {
296                             return createActionsForFolder(arr);
297                         } else {
298                             return createActionsForFile(arr);
299                         }
300                     }
301                 }
302             }
303             //Unknown node - return unmodified actions.
304
return arr;
305         }
306         
307         /** Do not change original actions. */
308         private Action JavaDoc [] createActionsForRoot (Action JavaDoc [] arr) {
309             //Actions are not modified.
310
return arr;
311         }
312         
313         /** Add action 'Remove from Favorites'. */
314         private Action JavaDoc [] createActionsForFavoriteFolder (Action JavaDoc [] arr) {
315             boolean added = false;
316             List JavaDoc<Action JavaDoc> newArr = new ArrayList JavaDoc<Action JavaDoc>();
317             for (int i = 0; i < arr.length; i++) {
318                 //Add before CopyAction or CutAction
319
if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) {
320                     added = true;
321                     newArr.add(Actions.remove());
322                     newArr.add(null);
323                 }
324                 //Do not add Delete action
325
if (!(arr[i] instanceof DeleteAction)) {
326                     newArr.add(arr[i]);
327                 }
328             }
329             if (!added) {
330                 added = true;
331                 newArr.add(null);
332                 newArr.add(Actions.remove());
333             }
334             
335             return newArr.toArray (new Action JavaDoc[newArr.size()]);
336         }
337         
338         /** Add action 'Remove from Favorites'. */
339         private Action JavaDoc [] createActionsForFavoriteFile (Action JavaDoc [] arr) {
340             boolean added = false;
341             List JavaDoc<Action JavaDoc> newArr = new ArrayList JavaDoc<Action JavaDoc>();
342             for (int i = 0; i < arr.length; i++) {
343                 //Add before CopyAction or CutAction
344
if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) {
345                     added = true;
346                     newArr.add(Actions.remove());
347                     newArr.add(null);
348                 }
349                 //Do not add Delete action
350
if (!(arr[i] instanceof DeleteAction)) {
351                     newArr.add(arr[i]);
352                 }
353             }
354             if (!added) {
355                 added = true;
356                 newArr.add(null);
357                 newArr.add(Actions.remove());
358             }
359             return newArr.toArray (new Action JavaDoc[newArr.size()]);
360         }
361         
362         /** Add action 'Add to Favorites'. */
363         private Action JavaDoc [] createActionsForFolder (Action JavaDoc [] arr) {
364             boolean added = false;
365             List JavaDoc<Action JavaDoc> newArr = new ArrayList JavaDoc<Action JavaDoc>();
366             for (int i = 0; i < arr.length; i++) {
367                 //Add before CopyAction or CutAction
368
if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) {
369                     added = true;
370                     newArr.add(Actions.add());
371                     newArr.add(null);
372                 }
373                 newArr.add(arr[i]);
374             }
375             if (!added) {
376                 added = true;
377                 newArr.add(null);
378                 newArr.add(Actions.add());
379             }
380             return newArr.toArray (new Action JavaDoc[newArr.size()]);
381         }
382         
383         /** Add action 'Add to Favorites'. */
384         private Action JavaDoc [] createActionsForFile (Action JavaDoc [] arr) {
385             boolean added = false;
386             List JavaDoc<Action JavaDoc> newArr = new ArrayList JavaDoc<Action JavaDoc>();
387             for (int i = 0; i < arr.length; i++) {
388                 //Add before CopyAction or CutAction
389
if (!added && ((arr[i] instanceof CopyAction) || (arr[i] instanceof CutAction))) {
390                     added = true;
391                     newArr.add(Actions.add());
392                     newArr.add(null);
393                 }
394                 newArr.add(arr[i]);
395             }
396             if (!added) {
397                 added = true;
398                 newArr.add(null);
399                 newArr.add(Actions.add());
400             }
401             return newArr.toArray (new Action JavaDoc[newArr.size()]);
402         }
403         
404     }
405 }
406
Popular Tags