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 javax.swing.KeyStroke;
22  
23  import org.apache.log4j.Logger;
24  import org.xnap.XNap;
25  import org.xnap.util.Preferences;
26  import org.xnap.util.PreferencesProvider;
27  
28  public class DefaultShortcut implements Shortcut
29  {
30  	//--- Constant(s) ---
31  
32  	//--- Data field(s) ---
33  
34  	private String category;
35      private PreferencesProvider prefs;
36  	private String description;
37      private String prefsKey;
38      private KeyStroke defaultKeyStroke;
39  	private KeyStroke keyStroke;
40  
41      private static Logger logger = Logger.getLogger(DefaultShortcut.class);
42  	
43  	//--- Constructor(s) ---
44  
45      public DefaultShortcut(KeyStroke defaultKeyStroke, String category,
46  						   String description, String prefsKey,
47  						   PreferencesProvider prefs)
48  	{
49  		this.category = category;
50  		this.description = description;
51      	this.defaultKeyStroke = defaultKeyStroke;
52  		this.keyStroke = defaultKeyStroke;
53  		this.prefsKey = prefsKey;
54      	this.prefs = prefs;
55  
56  		if (prefs != null && prefsKey != null) {
57  			setKeyStroke(prefs.getKeyStroke(prefsKey + "Shortcut"));
58      	}
59  	}
60  
61  	public DefaultShortcut(KeyStroke defaultKeyStroke, String category,
62  						   String description, String prefsKey)
63  	{
64  		this(defaultKeyStroke, category, description, prefsKey,
65  			 Preferences.getInstance());
66  	}
67  
68  	public DefaultShortcut(KeyStroke defaultKeyStroke, String description, 
69  						   String prefsKey)
70  	{
71  		this(defaultKeyStroke, XNap.tr("General"), description, prefsKey);
72  	}
73  
74      //--- Method(s) ---
75  
76  	public String getCategory()
77  	{
78  		return category;
79  	}
80  	
81  	public String getKey()
82  	{
83  		return prefsKey;
84  	}
85  
86  	public String getDescription()
87  	{
88  		return description;
89  	}
90  
91  	public KeyStroke getDefaultKeyStroke()
92  	{
93  		return defaultKeyStroke;
94  	}
95  
96  	public KeyStroke getKeyStroke()
97  	{
98  		return keyStroke;
99  	}
100 
101 	public void setKeyStroke(KeyStroke stroke)
102 	{
103 		keyStroke = stroke;
104 		if (prefs != null && prefsKey != null) {
105 			prefs.set(prefsKey + "Shortcut", keyStroke);
106 		}
107 	}
108 }
109 
110 
111 
112