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  package org.xnap.gui.shortcut;
20  
21  import java.util.Iterator;
22  import java.util.LinkedList;
23  import javax.swing.Action;
24  import javax.swing.InputMap;
25  import javax.swing.JComponent;
26  import javax.swing.KeyStroke;
27  import org.xnap.XNap;
28  import org.xnap.action.AbstractXNapAction;
29  import org.xnap.util.PreferencesProvider;
30  
31  /***
32   * Provides an action based shortcut.
33   * 
34   * The action has to provide several values to make use of the shortcut.
35   * 
36   * It must provide the following values:
37   * 
38   * Action.ACTION_COMMAND_KEY, the preferences key
39   *
40   * It should provide values for the following keys:
41   * 
42   * AbstractXNapAction.DEFAULT_KEYSTROKE, the default KeyStroke
43   * AbstractXNapAction.SHORTCUT_CATEGORY, category used in the
44   * ShortcutsPrefsPanel
45   */
46  public class ActionShortcut implements Shortcut 
47  {
48  
49      //--- Constant(s) ---
50  
51      //--- Data field(s) ---
52  
53      private String category;
54  	private String description;
55  	
56      private PreferencesProvider prefs;
57      private String prefsKey;
58  
59      private KeyStroke defaultKeyStroke;
60  	private KeyStroke currentKeyStroke;
61  
62  	private LinkedList components = new LinkedList();
63  	
64      //--- Constructor(s) ---
65  
66      public ActionShortcut(Action action, JComponent c, 
67  						  PreferencesProvider prefs)
68      {
69  		String cat = (String)action.getValue
70  			(AbstractXNapAction.SHORTCUT_CATEGORY);
71  		category = (cat != null) ? cat : XNap.tr("General");
72  
73  		String desc = 
74  			(String)action.getValue(AbstractXNapAction.SHORTCUT_DESCRIPTION);
75  		description =  (desc != null) ? desc 
76  			: (String)action.getValue(Action.SHORT_DESCRIPTION);
77  
78      	defaultKeyStroke = (KeyStroke)action.getValue
79  			(AbstractXNapAction.DEFAULT_KEYSTROKE);
80  
81  		currentKeyStroke = defaultKeyStroke;
82      	
83  		this.prefs = prefs;
84      	prefsKey = (String)action.getValue(Action.ACTION_COMMAND_KEY);
85  
86  		if (prefs != null && prefsKey != null) {
87  			currentKeyStroke = prefs.getKeyStroke(prefsKey + "Shortcut");
88  		}
89  
90  		addComponent(c, action);
91      }   
92  
93      //--- Method(s) ---
94  
95      /***
96  	 * Installs the action into the action map of the component and sets up
97  	 * the shortcut mapping.  
98  	 */
99  	public void addComponent(JComponent c, Action a)
100 	{
101 		// install action into action map
102 		c.getActionMap().put(prefsKey, a);
103 		components.add(c);
104 		setKeyStroke(currentKeyStroke, c);
105 	}
106 
107 	/***
108 	 * Removes the action mapping from the given component and removes it
109 	 * from its list of components.
110 	 * @param c the component this shortcuts is registered on
111 	 */
112 	public void removeComponent(JComponent c)
113 	{
114 		// remove action from action map
115 		c.getActionMap().remove(prefsKey);
116 		components.remove(c);
117 	}
118 
119 	public String getPrefsKey()
120 	{
121 		return prefsKey;
122 	}
123 
124 	public int getNumberOfComponents()
125 	{
126 		return components.size();
127 	}
128 
129     /***
130      *  @see org.xnap.gui.shortcut.Shortcut#getCategory()
131      */
132     public String getCategory() 
133     {
134 		return category;
135     }
136 
137     /***
138      *  @see org.xnap.gui.shortcut.Shortcut#getDescription()
139      */
140     public String getDescription() 
141     {
142 		return description;
143     }
144 
145     /***
146      *  @see org.xnap.gui.shortcut.Shortcut#getKeystroke()
147      */
148     public KeyStroke getKeyStroke() 
149     {
150 		return currentKeyStroke;
151     }
152 
153 	private void setKeyStroke(KeyStroke stroke, JComponent c)
154 	{
155 		InputMap m = c.getInputMap
156 			(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
157 
158 		// remove old mapping
159 		if (getKeyStroke() != null) {
160 			m.remove(getKeyStroke());
161 		}
162 		// install new mapping
163 		if (stroke != null) {
164 			m.put(stroke, prefsKey);
165 		}
166 	}
167 
168     /***
169      *  @see org.xnap.gui.shortcut.Shortcut#setKeystroke(javax.swing.KeyStroke)
170      */
171     public void setKeyStroke(KeyStroke stroke) 
172     {
173 		for (Iterator i = components.iterator(); i.hasNext();) {
174 			JComponent c = (JComponent)i.next();
175 			setKeyStroke(stroke, c);
176 		}
177 		
178 		currentKeyStroke = stroke;
179 
180 		if (prefs != null && prefsKey != null) {
181 			prefs.set(prefsKey + "Shortcut", stroke);
182 		}
183     }
184 
185     /***
186      *  @see org.xnap.gui.shortcut.Shortcut#getDefaultKeyStroke()
187      */
188     public KeyStroke getDefaultKeyStroke() 
189     {
190 		return defaultKeyStroke;
191     }
192 }
193