KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > impl > ActionsList


1 /*
2  * ActionsList.java
3  *
4  * Created on February 16, 2007, 2:12 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.editor.impl;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.logging.Level JavaDoc;
16 import java.util.logging.Logger JavaDoc;
17 import javax.swing.Action JavaDoc;
18 import org.openide.cookies.InstanceCookie;
19 import org.openide.filesystems.FileObject;
20 import org.openide.loaders.DataFolder;
21 import org.openide.loaders.DataObject;
22 import org.openide.loaders.DataObjectNotFoundException;
23 import org.openide.util.actions.SystemAction;
24
25 /**
26  *
27  * @author Vita Stejskal
28  */

29 public class ActionsList {
30     
31     private static final Logger JavaDoc LOG = Logger.getLogger(ActionsList.class.getName());
32     
33     private final List JavaDoc all;
34     private final List JavaDoc actions;
35
36     /**
37      * Create a new <code>ActionList</code> instance by calling<code>this(keys, false)</code>.
38      *
39      * @param keys The list of objects to convert to <code>Action</code>s
40      */

41     protected ActionsList(List JavaDoc keys) {
42         this(keys, false);
43     }
44     
45     /**
46      * Create a new <code>ActionList</code> instance. The <code>ActionList</code>
47      * converts a list of objects (keys) to the list of <code>Action</code>s
48      * or other instances that can potentially be used in actions based UI such
49      * as popup menus, toolbars, etc. The other instances can be anything, but
50      * usually they are things like <code>JSeparator</code>, <code>DataFolder</code>
51      * or plain <code>String</code> with the name of an editor action.
52      *
53      * @param keys The list of objects to convert to <code>Action</code>s
54      * @param ignoreFolders <code>true</code> if the conversion should skipp folders
55      */

56     protected ActionsList(List JavaDoc keys, boolean ignoreFolders) {
57         List JavaDoc [] lists = convertImpl(keys == null ? Collections.emptyList() : keys, ignoreFolders);
58         this.all = lists[0];
59         this.actions = lists[1];
60     }
61
62     public List JavaDoc getAllInstances() {
63         return all;
64     }
65
66     public List JavaDoc getActionsOnly() {
67         return actions;
68     }
69
70     public static List JavaDoc convert(List JavaDoc keys) {
71         List JavaDoc [] lists = convertImpl(keys, false);
72         return lists[0];
73     }
74     
75     private static List JavaDoc [] convertImpl(List JavaDoc keys, boolean ignoreFolders) {
76         List JavaDoc all = new ArrayList JavaDoc();
77         List JavaDoc actions = new ArrayList JavaDoc();
78
79         for (int i = 0; i < keys.size(); i++){
80             Object JavaDoc item = keys.get(i);
81             DataObject dob = null;
82
83             if (item instanceof DataObject) {
84                 dob = (DataObject) item;
85             } else if (item instanceof FileObject) {
86                 try {
87                     dob = DataObject.find((FileObject) item);
88                 } catch (DataObjectNotFoundException dnfe) {
89                     // ignore
90
}
91             }
92
93             if (dob != null) {
94                 InstanceCookie ic = (InstanceCookie) dob.getLookup().lookup(InstanceCookie.class);
95                 if (ic != null){
96                     try{
97                         if (Action JavaDoc.class.isAssignableFrom(ic.instanceClass()) ||
98                             SystemAction.class.isAssignableFrom(ic.instanceClass()))
99                         {
100                             Object JavaDoc instance = ic.instanceCreate();
101                             all.add(instance);
102                             actions.add(instance);
103                         } else if (!DataFolder.class.isAssignableFrom(ic.instanceClass()) || !ignoreFolders) {
104                             Object JavaDoc instance = ic.instanceCreate();
105                             all.add(instance);
106                         }
107                     } catch (Exception JavaDoc e) {
108                         LOG.log(Level.WARNING, "Can't instantiate object", e);
109                     }
110                 } else if (dob instanceof DataFolder) {
111                     all.add(dob);
112                 } else {
113                     all.add(dob.getName());
114                 }
115             } else {
116                 all.add(item);
117                 if (item instanceof Action JavaDoc || item instanceof SystemAction) {
118                     actions.add(item);
119                 }
120             }
121         }
122
123         return new List JavaDoc [] {
124             all.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(all),
125             actions.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(actions),
126         };
127     }
128 }
129
Popular Tags