View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
39  
40      //--- Data field(s) ---
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      //--- Constructor(s) ---
53  
54      private ShortcutManager()
55      {
56      }
57  
58      //--- Method(s) ---
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 }