4
* Created on February 16, 2007, 2:12 PM
6
* To change this template, choose Tools | Template Manager
7
* and open the template in the editor.
10
package org.netbeans.modules.editor.impl;
12
import java.util.ArrayList;
13
import java.util.Collections;
14
import java.util.List;
15
import java.util.logging.Level;
16
import java.util.logging.Logger;
17
import javax.swing.Action;
18
import javax.swing.JSeparator;
19
import org.openide.cookies.InstanceCookie;
20
import org.openide.filesystems.FileObject;
21
import org.openide.loaders.DataFolder;
22
import org.openide.loaders.DataObject;
23
import org.openide.loaders.DataObjectNotFoundException;
24
import org.openide.util.Utilities;
28
* @author Vita Stejskal
30
public class ActionsList {
32
private static final Logger LOG = Logger.getLogger(ActionsList.class.getName());
34
private final List<Object> all;
35
private final List<Action> actions;
38
* Create a new <code>ActionList</code> instance by calling<code>this(keys, false)</code>.
40
* @param keys The list of objects to convert to <code>Action</code>s
42
protected ActionsList(List<FileObject> keys) {
47
* Create a new <code>ActionList</code> instance. The <code>ActionList</code>
48
* converts a list of objects (keys) to the list of <code>Action</code>s
49
* or other instances that can potentially be used in actions based UI such
50
* as popup menus, toolbars, etc. The other instances can be anything, but
51
* usually they are things like <code>JSeparator</code>, <code>DataFolder</code>
52
* or plain <code>String</code> with the name of an editor action.
54
* @param keys The list of objects to convert to <code>Action</code>s
55
* @param ignoreFolders <code>true</code> if the conversion should skipp folders
57
protected ActionsList(List<FileObject> keys, boolean ignoreFolders) {
58
Pair p = convertImpl(keys == null ? Collections.<FileObject>emptyList() : keys, ignoreFolders);
60
this.actions = p.actions;
63
public List<Object> getAllInstances() {
67
public List<Action> getActionsOnly() {
71
public static List<Object> convert(List<FileObject> keys) {
72
return convertImpl(keys, false).all;
75
private static class Pair {
80
private static Pair convertImpl(List<FileObject> keys, boolean ignoreFolders) {
81
List<Object> all = new ArrayList<Object>();
82
List<Action> actions = new ArrayList<Action>();
84
for (FileObject item : keys) {
87
dob = DataObject.find(item);
88
} catch (DataObjectNotFoundException dnfe) {
93
InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class);
96
toAdd = ic.instanceCreate();
97
} catch (Exception e) {
98
LOG.log(Level.WARNING, "Can't instantiate object", e); //NOI18N
101
} else if (dob instanceof DataFolder) {
104
toAdd = dob.getName();
107
// Filter out the same succeding items
108
if (all.size() > 0) {
109
Object lastOne = all.get(all.size() - 1);
110
if (Utilities.compareObjects(lastOne, toAdd)) {
113
if (isSeparator(lastOne) && isSeparator(toAdd)) {
118
if (toAdd instanceof Action) {
119
actions.add((Action) toAdd);
120
} else if (isSeparator(toAdd)) {
127
p.all = all.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(all);
128
p.actions = actions.isEmpty() ? Collections.<Action>emptyList() : Collections.unmodifiableList(actions);
132
private static boolean isSeparator(Object o) {
133
return o == null || o instanceof JSeparator;