001: /*
002: * ShortcutDefinition.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.resource;
013:
014: import javax.swing.KeyStroke;
015:
016: /**
017: * @author support@sql-workbench.net
018: *
019: */
020: public class ShortcutDefinition {
021: private String actionClass;
022: private StoreableKeyStroke currentKey;
023: private StoreableKeyStroke defaultKey;
024: private StoreableKeyStroke alternateKey;
025: private boolean shortcutRemoved;
026:
027: public ShortcutDefinition() {
028: }
029:
030: public boolean isMappedTo(KeyStroke key) {
031: if (key == null)
032: return false;
033: KeyStroke mykey = this .getActiveKeyStroke();
034: if (mykey != null) {
035: return mykey.equals(key);
036: }
037: return false;
038: }
039:
040: public ShortcutDefinition(String aClass) {
041: this .setActionClass(aClass);
042: }
043:
044: public void setShortcutRemoved(boolean aFlag) {
045: this .shortcutRemoved = aFlag;
046: if (aFlag) {
047: this .clearKeyStroke();
048: }
049: }
050:
051: public boolean getShortcutRemoved() {
052: return this .shortcutRemoved;
053: }
054:
055: /**
056: * Clears the currently defined shortcut.
057: * If a default shortcut is defined, this will make this Definition "customized"
058: * as the default shortcut is "overwritten" with nothing
059: */
060: public void clearKeyStroke() {
061: this .currentKey = null;
062: }
063:
064: /**
065: * Get the default (storeable) KeyStroke.
066: * This is only here to statisfy the XMLEncoder, so that the whole thing can be saved
067: */
068: public KeyStroke getDefaultKeyStroke() {
069: if (this .defaultKey != null)
070: return this .defaultKey.getKeyStroke();
071: return null;
072: }
073:
074: /**
075: * Set the default (storeable) alternate KeyStroke.
076: * This is only here to statisfy the XMLEncoder, so that the whole thing can be saved
077: * @param aKey
078: */
079: public void setAlternateKey(StoreableKeyStroke aKey) {
080: this .alternateKey = aKey;
081: }
082:
083: /**
084: * Get the default (storeable) alternate KeyStroke.
085: * This is only here to statisfy the XMLEncoder, so that the whole thing can be saved
086: */
087: public KeyStroke getAlternateKeyStroke() {
088: if (this .alternateKey != null)
089: return this .alternateKey.getKeyStroke();
090: return null;
091: }
092:
093: /**
094: * Set the current/active (storeable) KeyStroke.
095: * This is only here to statisfy the XMLEncoder, so that the whole thing can be saved
096: * @param aKey
097: */
098: public void setCurrentKey(StoreableKeyStroke aKey) {
099: this .currentKey = aKey;
100: }
101:
102: /**
103: * Get the current/active (storeable) KeyStroke.
104: * This is only here to statisfy the XMLEncoder, so that the whole thing can be saved
105: */
106: public StoreableKeyStroke getCurrentKey() {
107: return currentKey;
108: }
109:
110: public KeyStroke getCurrentKeyStroke() {
111: if (this .currentKey == null)
112: return null;
113: return this .currentKey.getKeyStroke();
114: }
115:
116: /**
117: * Assign a KeyStroke to this action.
118: * @param aKey
119: */
120: public void assignKey(KeyStroke aKey) {
121: this .assignKey(new StoreableKeyStroke(aKey));
122: }
123:
124: public void assignKey(StoreableKeyStroke aKey) {
125: this .currentKey = aKey;
126: this .shortcutRemoved = false;
127: }
128:
129: /**
130: * Assign a default key for this action class.
131: * This method is called assign so that the XMLEncoder does not consider
132: * reading or writing this "property" as KeyStrokes cannot be serialized
133: * to XML
134: * @param aKey
135: */
136: public void assignDefaultKey(KeyStroke aKey) {
137: this .defaultKey = new StoreableKeyStroke(aKey);
138: //if (this.currentKey == null) this.currentKey = this.defaultKey;
139: }
140:
141: /**
142: * Return the current default key. This is the "matching"
143: * getter for the assignDefaultKey() method.
144: * @return KeyStroke
145: */
146: public StoreableKeyStroke getDefaultKey() {
147: return this .defaultKey;
148: }
149:
150: /**
151: * Assign an alternate key for this action class.
152: * This method is called assign so that the XMLEncoder does not consider
153: * reading or writing this "property" as KeyStrokes cannot be serialized
154: * to XML
155: * @param aKey
156: */
157: public void assignAlternateKey(KeyStroke aKey) {
158: this .alternateKey = new StoreableKeyStroke(aKey);
159: }
160:
161: /**
162: * Return the defined alternate key as a KeyStroke.
163: * If no alternate key is define null is returned
164: * @return the alternate keystroke
165: */
166: public StoreableKeyStroke getAlternateKey() {
167: return this .alternateKey;
168: }
169:
170: public boolean hasDefault() {
171: return this .defaultKey != null;
172: }
173:
174: /**
175: * Return if the this shortcut definition is customized.
176: * It's customized if a default exists, and currently no shortcut is defined.
177: * Or if a shortcut is defined, that is different to the default.
178: * @return true if this shortcut definition differs from the default.
179: */
180: public boolean isCustomized() {
181: if (this .defaultKey == null && this .currentKey == null)
182: return false;
183: if (this .defaultKey == null && this .currentKey != null)
184: return true;
185: if (this .defaultKey != null && this .currentKey == null)
186: return this .shortcutRemoved;
187:
188: return (!this .currentKey.equals(this .defaultKey));
189: }
190:
191: /**
192: * Restores the default mapping for this shortcut.
193: * After a call to resetToDefault() isCustomized() will return false
194: */
195: public void resetToDefault() {
196: this .shortcutRemoved = false;
197: this .currentKey = null;
198: }
199:
200: /**
201: * Returns the active KeyStroke.
202: * This is either the current or the default, or null if the shortcut
203: * has been removed completely.
204: * @return the keystroke that is active
205: */
206: public KeyStroke getActiveKeyStroke() {
207: if (this .shortcutRemoved)
208: return null;
209: if (this .currentKey != null)
210: return this .currentKey.getKeyStroke();
211: if (this .defaultKey != null)
212: return this .defaultKey.getKeyStroke();
213: return null;
214: }
215:
216: public StoreableKeyStroke getActiveKey() {
217: if (this .shortcutRemoved)
218: return null;
219: if (this .currentKey != null)
220: return this .currentKey;
221: return this .defaultKey;
222: }
223:
224: public String getActionClass() {
225: return this .actionClass;
226: }
227:
228: public void setActionClass(String aClass) {
229: if (aClass == null)
230: throw new IllegalArgumentException(
231: "ClassName cannot be null");
232: this .actionClass = aClass;
233: }
234:
235: public String toString() {
236: StringBuilder result = new StringBuilder(50);
237: result.append(this .actionClass);
238: StoreableKeyStroke active = (this .currentKey != null) ? this .currentKey
239: : this .defaultKey;
240: if (active != null) {
241: result.append(" [");
242: result.append(active.toString());
243: result.append(']');
244: } else {
245: result.append("[no shortcut]");
246: }
247: if (isCustomized())
248: result.append(" (customized)");
249:
250: return result.toString();
251: }
252: }
|