001: /*
002: * KeyMapping.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: KeyMapping.java,v 1.3 2003/07/18 15:19:35 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import javax.swing.KeyStroke;
025:
026: public class KeyMapping implements Constants {
027: private final char keyChar;
028: private final int keyCode;
029: private final int modifiers;
030: private final Object command;
031:
032: public KeyMapping(int keyCode, int modifiers, Object command) {
033: this .keyChar = 0;
034: this .keyCode = keyCode;
035: this .modifiers = modifiers;
036: if (command instanceof String)
037: this .command = ((String) command).intern();
038: else
039: this .command = command;
040: }
041:
042: public KeyMapping(char keyChar, Object command) {
043: this .keyChar = keyChar;
044: this .keyCode = 0;
045: this .modifiers = 0;
046: if (command instanceof String)
047: this .command = ((String) command).intern();
048: else
049: this .command = command;
050: }
051:
052: private KeyMapping(KeyStroke keyStroke, String command) {
053: char c = keyStroke.getKeyChar();
054: keyChar = c == 0xffff ? 0 : c;
055: keyCode = keyStroke.getKeyCode();
056: // Mask off the bits we don't care about (Java 1.4).
057: modifiers = keyStroke.getModifiers() & 0x0f;
058: if (command != null)
059: this .command = command.intern();
060: else
061: this .command = null;
062: }
063:
064: // Returns null if string can't be parsed.
065: public static KeyMapping createKeyMapping(String s) {
066: s = s.trim();
067: String parameters = null;
068: int index = s.indexOf('(');
069: if (index >= 0) {
070: parameters = s.substring(index);
071: s = s.substring(0, index).trim();
072: }
073: index = s.lastIndexOf(' ');
074: if (index < 0)
075: return null;
076: String keyText = s.substring(0, index).trim();
077: String command = s.substring(index + 1).trim();
078: if (parameters != null)
079: command += parameters;
080: return createKeyMapping(keyText, command);
081: }
082:
083: private static KeyMapping createKeyMapping(String keyText,
084: String command) {
085: KeyStroke keyStroke = Utilities.getKeyStroke(keyText);
086: if (keyStroke == null)
087: return null;
088: if (command != null)
089: command = command.trim();
090: return new KeyMapping(keyStroke, command);
091: }
092:
093: public final char getKeyChar() {
094: return keyChar;
095: }
096:
097: public final int getKeyCode() {
098: return keyCode;
099: }
100:
101: public final int getModifiers() {
102: return modifiers;
103: }
104:
105: public final Object getCommand() {
106: return command;
107: }
108:
109: public String toString() {
110: FastStringBuffer sb = new FastStringBuffer(64);
111: sb.append(Utilities.getKeyText(keyChar, keyCode, modifiers));
112: if (command != null) {
113: while (sb.length() < 32)
114: sb.append(' ');
115: sb.append(command);
116: }
117: return sb.toString();
118: }
119:
120: public final String getKeyText() {
121: return Utilities.getKeyText(keyChar, keyCode, modifiers);
122: }
123: }
|