KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > DescribeKeyDialog


1 /*
2  * DescribeKeyDialog.java
3  *
4  * Copyright (C) 2000-2004 Peter Graves
5  * $Id: DescribeKeyDialog.java,v 1.6 2004/08/08 01:46:48 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.event.InputEvent JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29
30 public final class DescribeKeyDialog extends AbstractDialog
31 {
32     private static final String JavaDoc title = "Describe Key";
33     private static final String JavaDoc prompt = "Describe key:";
34
35     private final Editor editor;
36     private final Buffer buffer;
37     private boolean seenKeyPressed;
38     private boolean disposed;
39     private String JavaDoc keyStrokeText;
40
41     private DescribeKeyDialog(Editor editor)
42     {
43         super(editor, title, true); // Modal.
44
this.editor = editor;
45         buffer = editor.getBuffer();
46         JTextField JavaDoc textField = new JTextField JavaDoc(20);
47         addLabelAndTextField(new JLabel JavaDoc(prompt), textField);
48         addVerticalStrut();
49         addCancel();
50         pack();
51         textField.setFocusTraversalKeysEnabled(false);
52         textField.requestFocus();
53     }
54
55     public void keyPressed(KeyEvent JavaDoc e)
56     {
57         final int keycode = e.getKeyCode();
58         // Ignore modifier keystrokes.
59
if (keycode == KeyEvent.VK_SHIFT || keycode == KeyEvent.VK_CONTROL ||
60             keycode == KeyEvent.VK_ALT || keycode == KeyEvent.VK_META)
61             return;
62         seenKeyPressed = true;
63         final int modifiers = e.getModifiers();
64         KeyMapping mapping = new KeyMapping(keycode, modifiers, null);
65         keyStrokeText = mapping.getKeyText();
66         describeKey(e.getKeyChar(), keycode, modifiers);
67     }
68
69     public void keyTyped(KeyEvent JavaDoc e)
70     {
71         final char c = e.getKeyChar();
72         final int modifiers = e.getModifiers();
73         if (modifiers == 0 || modifiers == InputEvent.SHIFT_MASK) {
74             // Ignore whitespace key chars (e.g. Space, Shift Tab).
75
if (c > ' ') {
76                 FastStringBuffer sb = new FastStringBuffer('\'');
77                 sb.append(c);
78                 sb.append('\'');
79                 keyStrokeText = sb.toString();
80             }
81         }
82         describeKey(c, e.getKeyCode(), modifiers);
83     }
84
85     public void keyReleased(KeyEvent JavaDoc e)
86     {
87         if (seenKeyPressed && !disposed) {
88             dispose();
89             // Use invokeLater() so message dialog will get focus.
90
Runnable JavaDoc r = new Runnable JavaDoc() {
91                 public void run()
92                 {
93                     MessageDialog.showMessageDialog(editor,
94                         keyStrokeText + " is not mapped", title);
95                 }
96             };
97             SwingUtilities.invokeLater(r);
98         }
99     }
100
101     private void describeKey(char keyChar, int keyCode, int modifiers)
102     {
103         if (disposed)
104             return;
105         // Mask off the bits we don't care about (Java 1.4).
106
modifiers &= 0x0f;
107         if (keyCode == 0 && modifiers == InputEvent.SHIFT_MASK) // Shift only.
108
modifiers = 0; // Ignore modifier.
109
boolean local = false;
110         final Mode mode = buffer.getMode();
111         KeyMapping mapping =
112             mode.getKeyMap().lookup(keyChar, keyCode, modifiers);
113         if (mapping != null)
114             local = true;
115         else {
116             mapping =
117                 KeyMap.getGlobalKeyMap().lookup(keyChar, keyCode, modifiers);
118         }
119         final FastStringBuffer sb = new FastStringBuffer();
120         if (mapping != null) {
121             sb.append(mapping.getKeyText());
122             sb.append(" is mapped to ");
123             sb.append(mapping.getCommand());
124             if (local) {
125                 sb.append(" (");
126                 sb.append(mode.toString());
127                 sb.append(" mode)");
128             } else
129                 sb.append(" (global mapping)");
130             dispose();
131             // Use invokeLater() so message dialog will get focus.
132
Runnable JavaDoc r = new Runnable JavaDoc() {
133                 public void run()
134                 {
135                     MessageDialog.showMessageDialog(editor, sb.toString(),
136                         "Describe Key");
137                 }
138             };
139             SwingUtilities.invokeLater(r);
140         }
141     }
142
143     public void dispose()
144     {
145         disposed = true;
146         super.dispose();
147     }
148
149     public static void describeKey()
150     {
151         DescribeKeyDialog d = new DescribeKeyDialog(Editor.currentEditor());
152         d.centerDialog();
153         d.show();
154     }
155 }
156
Popular Tags