001: /*
002: * DescribeKeyDialog.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: DescribeKeyDialog.java,v 1.6 2004/08/08 01:46:48 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 java.awt.event.InputEvent;
025: import java.awt.event.KeyEvent;
026: import javax.swing.JLabel;
027: import javax.swing.JTextField;
028: import javax.swing.SwingUtilities;
029:
030: public final class DescribeKeyDialog extends AbstractDialog {
031: private static final String title = "Describe Key";
032: private static final String prompt = "Describe key:";
033:
034: private final Editor editor;
035: private final Buffer buffer;
036: private boolean seenKeyPressed;
037: private boolean disposed;
038: private String keyStrokeText;
039:
040: private DescribeKeyDialog(Editor editor) {
041: super (editor, title, true); // Modal.
042: this .editor = editor;
043: buffer = editor.getBuffer();
044: JTextField textField = new JTextField(20);
045: addLabelAndTextField(new JLabel(prompt), textField);
046: addVerticalStrut();
047: addCancel();
048: pack();
049: textField.setFocusTraversalKeysEnabled(false);
050: textField.requestFocus();
051: }
052:
053: public void keyPressed(KeyEvent e) {
054: final int keycode = e.getKeyCode();
055: // Ignore modifier keystrokes.
056: if (keycode == KeyEvent.VK_SHIFT
057: || keycode == KeyEvent.VK_CONTROL
058: || keycode == KeyEvent.VK_ALT
059: || keycode == KeyEvent.VK_META)
060: return;
061: seenKeyPressed = true;
062: final int modifiers = e.getModifiers();
063: KeyMapping mapping = new KeyMapping(keycode, modifiers, null);
064: keyStrokeText = mapping.getKeyText();
065: describeKey(e.getKeyChar(), keycode, modifiers);
066: }
067:
068: public void keyTyped(KeyEvent e) {
069: final char c = e.getKeyChar();
070: final int modifiers = e.getModifiers();
071: if (modifiers == 0 || modifiers == InputEvent.SHIFT_MASK) {
072: // Ignore whitespace key chars (e.g. Space, Shift Tab).
073: if (c > ' ') {
074: FastStringBuffer sb = new FastStringBuffer('\'');
075: sb.append(c);
076: sb.append('\'');
077: keyStrokeText = sb.toString();
078: }
079: }
080: describeKey(c, e.getKeyCode(), modifiers);
081: }
082:
083: public void keyReleased(KeyEvent e) {
084: if (seenKeyPressed && !disposed) {
085: dispose();
086: // Use invokeLater() so message dialog will get focus.
087: Runnable r = new Runnable() {
088: public void run() {
089: MessageDialog.showMessageDialog(editor,
090: keyStrokeText + " is not mapped", title);
091: }
092: };
093: SwingUtilities.invokeLater(r);
094: }
095: }
096:
097: private void describeKey(char keyChar, int keyCode, int modifiers) {
098: if (disposed)
099: return;
100: // Mask off the bits we don't care about (Java 1.4).
101: modifiers &= 0x0f;
102: if (keyCode == 0 && modifiers == InputEvent.SHIFT_MASK) // Shift only.
103: modifiers = 0; // Ignore modifier.
104: boolean local = false;
105: final Mode mode = buffer.getMode();
106: KeyMapping mapping = mode.getKeyMap().lookup(keyChar, keyCode,
107: modifiers);
108: if (mapping != null)
109: local = true;
110: else {
111: mapping = KeyMap.getGlobalKeyMap().lookup(keyChar, keyCode,
112: modifiers);
113: }
114: final FastStringBuffer sb = new FastStringBuffer();
115: if (mapping != null) {
116: sb.append(mapping.getKeyText());
117: sb.append(" is mapped to ");
118: sb.append(mapping.getCommand());
119: if (local) {
120: sb.append(" (");
121: sb.append(mode.toString());
122: sb.append(" mode)");
123: } else
124: sb.append(" (global mapping)");
125: dispose();
126: // Use invokeLater() so message dialog will get focus.
127: Runnable r = new Runnable() {
128: public void run() {
129: MessageDialog.showMessageDialog(editor, sb
130: .toString(), "Describe Key");
131: }
132: };
133: SwingUtilities.invokeLater(r);
134: }
135: }
136:
137: public void dispose() {
138: disposed = true;
139: super .dispose();
140: }
141:
142: public static void describeKey() {
143: DescribeKeyDialog d = new DescribeKeyDialog(Editor
144: .currentEditor());
145: d.centerDialog();
146: d.show();
147: }
148: }
|