01: /*
02: * CustomFocusManager.java
03: *
04: * Copyright (C) 1999-2003 Peter Graves
05: * $Id: CustomFocusManager.java,v 1.7 2003/12/04 16:55:03 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * 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.Component;
25: import java.awt.event.KeyEvent;
26: import javax.swing.DefaultFocusManager;
27: import javax.swing.JDialog;
28:
29: public final class CustomFocusManager extends DefaultFocusManager {
30: public void processKeyEvent(Component focusedComponent, KeyEvent e) {
31: if (e.getID() == KeyEvent.KEY_PRESSED) {
32: if (isComponentHookable(focusedComponent)) {
33: KeyMapping km;
34: int keyCode = e.getKeyCode();
35: if (keyCode != 0)
36: km = new KeyMapping(keyCode, e.getModifiers(), null);
37: else
38: km = new KeyMapping(e.getKeyChar(), null);
39: String keyText = km.toString();
40: // Escape the escape character!
41: if (keyText.equals("\\"))
42: keyText = "\\\\";
43: Editor.invokeHook("key-pressed-hook", "\"" + keyText
44: + "\"");
45: }
46: }
47: super .processKeyEvent(focusedComponent, e);
48: }
49:
50: private static final boolean isComponentHookable(Component c) {
51: if (c instanceof Display)
52: return false;
53: if (c == null)
54: return false;
55: if (c instanceof HistoryTextField) {
56: HistoryTextField textField = (HistoryTextField) c;
57: if (textField.getHandler() instanceof IncrementalFindTextFieldHandler)
58: return false;
59: }
60: if (!Editor.preferences().getBooleanProperty(
61: Property.ENABLE_KEY_PRESSED_HOOK))
62: return false;
63: while (true) {
64: if (c instanceof JDialog)
65: return false;
66: c = c.getParent();
67: if (c == null)
68: return true;
69: }
70: }
71: }
|