1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.shortcut;
21
22 import java.util.Hashtable;
23 import java.util.LinkedList;
24 import java.util.List;
25 import javax.swing.Action;
26 import javax.swing.JComponent;
27 import org.apache.log4j.Logger;
28 import org.xnap.gui.XNapFrame;
29 import org.xnap.util.Preferences;
30 import org.xnap.util.PreferencesProvider;
31
32 /***
33 * Manages Shortcut objects.
34 */
35 public class ShortcutManager
36 {
37
38
39
40
41
42 private static Logger logger = Logger.getLogger(ShortcutManager.class);
43
44 private static ShortcutManager instance = new ShortcutManager();
45 /***
46 * The list of unique shortcut objects.
47 */
48 private List shortcuts = new LinkedList();
49
50 private Hashtable shortcutsByCommandKey = new Hashtable();
51
52
53
54 private ShortcutManager()
55 {
56 }
57
58
59
60 /***
61 * Returns the instance of <code>ShortcutManager</code>.
62 */
63 public static ShortcutManager getInstance()
64 {
65 return instance;
66 }
67
68 /***
69 * Adds a shortcut.
70 */
71 public synchronized void add(Shortcut shortcut)
72 {
73 shortcuts.add(shortcut);
74 }
75
76 /***
77 * Creates a new ActionShortcut for the action if there isn't already one
78 * and returns it.
79 *
80 * Only one action shortcut object can exist for the same preferences
81 * key. Additional components are all managed by the same action shortcut
82 * object.
83 *
84 * @param action the action which should be performed when the shortcut is
85 * typed in the respective component
86 * @param c the component for which the shortcut and the action are
87 * registered
88 * @param prefs the preferences provider used for permantly storing the
89 * actual key stroke value of the shortcut
90 */
91 public synchronized ActionShortcut add(Action action, JComponent c,
92 PreferencesProvider prefs)
93 {
94 String key = (String)action.getValue(Action.ACTION_COMMAND_KEY);
95 if (shortcutsByCommandKey.containsKey(key)) {
96 ActionShortcut a = (ActionShortcut)shortcutsByCommandKey.get(key);
97 a.addComponent(c, action);
98 return a;
99 }
100 else {
101 ActionShortcut a = new ActionShortcut(action, c, prefs);
102 shortcuts.add(a);
103 shortcutsByCommandKey.put(key, a);
104 return a;
105 }
106 }
107
108 /***
109 * A convenience wrapper for {@link add(Action, JComponent,
110 * PreferencesProvider)}.
111 *
112 * The PreferencesProvider defaults to
113 * <code>Preferences.getInstance()</code>
114 */
115 public synchronized ActionShortcut add(Action action, JComponent c)
116 {
117 return add(action, c, Preferences.getInstance());
118 }
119
120 /***
121 * A convenience wrapper for {@link add(Action, JComponent)}.
122 *
123 * The JComponent defaults to
124 * <code>XNapFrame.getInstance().getContentPane()</code>
125 */
126 public synchronized ActionShortcut add(Action action)
127 {
128 return add(action,
129 (JComponent)XNapFrame.getInstance().getContentPane());
130 }
131
132 /***
133 * Returns the array of currently registered shorcuts.
134 */
135 public synchronized Shortcut[] getShortcuts()
136 {
137 return (Shortcut[])shortcuts.toArray(new Shortcut[0]);
138 }
139
140 /***
141 * Removes a currently registered shortcut.
142 */
143 public synchronized void remove(Shortcut shortcut)
144 {
145 shortcuts.remove(shortcut);
146 }
147
148 /***
149 * Removes a component from a currently registered action shortcut.
150 *
151 * If the action shortcut's component count {@link
152 * ActionShortcut#getNumberOfComponents()} drops to 0, it is removed from
153 * the shortcut manager.
154 */
155 public synchronized void remove(ActionShortcut shortcut, JComponent c)
156 {
157 shortcut.removeComponent(c);
158 if (shortcut.getNumberOfComponents() == 0) {
159 shortcutsByCommandKey.remove(shortcut.getPrefsKey());
160 remove(shortcut);
161 }
162 }
163
164 }