001: /*
002: * StoreableKeyStroke.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 java.awt.event.KeyEvent;
015:
016: import javax.swing.KeyStroke;
017:
018: /**
019: * A class which wraps keyStroke and can be serialized
020: * using the XMLDecode/XMLEncoder classes
021: * @author support@sql-workbench.net
022: *
023: */
024: public class StoreableKeyStroke {
025: private int keyCode;
026: private int modifier;
027:
028: private boolean keyCodeSet = false;
029: private boolean modifierSet = false;
030:
031: public StoreableKeyStroke() {
032: }
033:
034: public StoreableKeyStroke(KeyStroke aKey) {
035: this .keyCode = aKey.getKeyCode();
036: this .modifier = aKey.getModifiers();
037: this .modifierSet = true;
038: this .keyCodeSet = true;
039: }
040:
041: public KeyStroke getKeyStroke() {
042: if (keyCodeSet || modifierSet) {
043: return KeyStroke.getKeyStroke(this .keyCode, this .modifier);
044: }
045: return null;
046: }
047:
048: public int getKeyCode() {
049: KeyStroke theKey = getKeyStroke();
050: if (theKey != null)
051: return theKey.getKeyCode();
052: return 0;
053: }
054:
055: public void setKeyCode(int c) {
056: this .keyCode = c;
057: this .keyCodeSet = true;
058: }
059:
060: public void setKeyModifier(int mod) {
061: this .modifier = mod;
062: this .modifierSet = true;
063: }
064:
065: public int getKeyModifier() {
066: KeyStroke theKey = getKeyStroke();
067: if (theKey != null)
068: return theKey.getModifiers();
069: return 0;
070: }
071:
072: public boolean equals(Object other) {
073: KeyStroke this Key = getKeyStroke();
074: if (other instanceof StoreableKeyStroke) {
075: KeyStroke otherKey = ((StoreableKeyStroke) other)
076: .getKeyStroke();
077: if (this Key == null && otherKey == null)
078: return true;
079: if (this Key == null && otherKey != null)
080: return false;
081: if (this Key != null && otherKey == null)
082: return false;
083: return this Key.equals(otherKey);
084: } else if (other instanceof KeyStroke) {
085: if (this Key == null && other == null)
086: return true;
087: return this Key.equals((KeyStroke) other);
088: }
089: return false;
090: }
091:
092: public String toString() {
093: KeyStroke this Key = getKeyStroke();
094: if (this Key == null)
095: return "";
096:
097: int mod = this Key.getModifiers();
098: int code = this Key.getKeyCode();
099:
100: String modText = KeyEvent.getKeyModifiersText(mod);
101: if (modText.length() == 0)
102: return KeyEvent.getKeyText(code);
103: else
104: return modText + "-" + KeyEvent.getKeyText(code);
105: }
106: }
|