~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to editor/src/org/netbeans/modules/editor/impl/ActionsList.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
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;
 
25
 
 
26
/**
 
27
 *
 
28
 * @author Vita Stejskal
 
29
 */
 
30
public class ActionsList {
 
31
    
 
32
    private static final Logger LOG = Logger.getLogger(ActionsList.class.getName());
 
33
    
 
34
    private final List<Object> all;
 
35
    private final List<Action> actions;
 
36
 
 
37
    /**
 
38
     * Create a new <code>ActionList</code> instance by calling<code>this(keys, false)</code>.
 
39
     * 
 
40
     * @param keys The list of objects to convert to <code>Action</code>s
 
41
     */
 
42
    protected ActionsList(List<FileObject> keys) {
 
43
        this(keys, false);
 
44
    }
 
45
    
 
46
    /**
 
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.
 
53
     * 
 
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
 
56
     */
 
57
    protected ActionsList(List<FileObject> keys, boolean ignoreFolders) {
 
58
        Pair p = convertImpl(keys == null ? Collections.<FileObject>emptyList() : keys, ignoreFolders);
 
59
        this.all = p.all;
 
60
        this.actions = p.actions;
 
61
    }
 
62
 
 
63
    public List<Object> getAllInstances() {
 
64
        return all;
 
65
    }
 
66
 
 
67
    public List<Action> getActionsOnly() {
 
68
        return actions;
 
69
    }
 
70
 
 
71
    public static List<Object> convert(List<FileObject> keys) {
 
72
        return convertImpl(keys, false).all;
 
73
    }
 
74
    
 
75
    private static class Pair {
 
76
        List<Object> all;
 
77
        List<Action> actions;
 
78
    }
 
79
    
 
80
    private static Pair convertImpl(List<FileObject> keys, boolean ignoreFolders) {
 
81
        List<Object> all = new ArrayList<Object>();
 
82
        List<Action> actions = new ArrayList<Action>();
 
83
 
 
84
        for (FileObject item : keys) {
 
85
            DataObject dob;
 
86
            try {
 
87
                dob = DataObject.find(item);
 
88
            } catch (DataObjectNotFoundException dnfe) {
 
89
                continue; // ignore
 
90
            }
 
91
 
 
92
            Object toAdd = null;
 
93
            InstanceCookie ic = dob.getLookup().lookup(InstanceCookie.class);
 
94
            if (ic != null) {
 
95
                try {
 
96
                    toAdd = ic.instanceCreate();
 
97
                } catch (Exception e) {
 
98
                    LOG.log(Level.WARNING, "Can't instantiate object", e); //NOI18N
 
99
                    continue;
 
100
                }
 
101
            } else if (dob instanceof DataFolder) {
 
102
                toAdd = dob;
 
103
            } else {
 
104
                toAdd = dob.getName();
 
105
            }
 
106
 
 
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)) {
 
111
                    continue;
 
112
                }
 
113
                if (isSeparator(lastOne) && isSeparator(toAdd)) {
 
114
                    continue;
 
115
                }
 
116
            }
 
117
            
 
118
            if (toAdd instanceof Action) {
 
119
                actions.add((Action) toAdd);
 
120
            } else if (isSeparator(toAdd)) {
 
121
                actions.add(null);
 
122
            }
 
123
            all.add(toAdd);
 
124
        }
 
125
 
 
126
        Pair p = new Pair();
 
127
        p.all = all.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(all);
 
128
        p.actions = actions.isEmpty() ? Collections.<Action>emptyList() : Collections.unmodifiableList(actions);
 
129
        return p;
 
130
    }
 
131
    
 
132
    private static boolean isSeparator(Object o) {
 
133
        return o == null || o instanceof JSeparator;
 
134
    }
 
135
}