KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CustomFocusManager.java
3  *
4  * Copyright (C) 1999-2003 Peter Graves
5  * $Id: CustomFocusManager.java,v 1.7 2003/12/04 16:55:03 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.Component JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import javax.swing.DefaultFocusManager JavaDoc;
27 import javax.swing.JDialog JavaDoc;
28
29 public final class CustomFocusManager extends DefaultFocusManager JavaDoc
30 {
31     public void processKeyEvent(Component JavaDoc focusedComponent, KeyEvent JavaDoc e)
32     {
33         if (e.getID() == KeyEvent.KEY_PRESSED) {
34             if (isComponentHookable(focusedComponent)) {
35                 KeyMapping km;
36                 int keyCode = e.getKeyCode();
37                 if (keyCode != 0)
38                     km = new KeyMapping(keyCode, e.getModifiers(), null);
39                 else
40                     km = new KeyMapping(e.getKeyChar(), null);
41                 String JavaDoc keyText = km.toString();
42                 // Escape the escape character!
43
if (keyText.equals("\\"))
44                     keyText = "\\\\";
45                 Editor.invokeHook("key-pressed-hook",
46                                   "\"" + keyText + "\"");
47             }
48         }
49         super.processKeyEvent(focusedComponent, e);
50     }
51
52     private static final boolean isComponentHookable(Component JavaDoc c)
53     {
54         if (c instanceof Display)
55             return false;
56         if (c == null)
57             return false;
58         if (c instanceof HistoryTextField) {
59             HistoryTextField textField = (HistoryTextField) c;
60             if (textField.getHandler() instanceof IncrementalFindTextFieldHandler)
61                 return false;
62         }
63         if (!Editor.preferences().getBooleanProperty(Property.ENABLE_KEY_PRESSED_HOOK))
64             return false;
65         while (true) {
66             if (c instanceof JDialog JavaDoc)
67                 return false;
68             c = c.getParent();
69             if (c == null)
70                 return true;
71         }
72     }
73 }
74
Popular Tags