01: /*
02: * Javu WingS - Lightweight Java Component Set
03: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
04: * e-mail: ksadlocha@programics.com
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: package com.javujavu.javux.wings.extra;
22:
23: import java.awt.Component;
24: import java.awt.event.KeyEvent;
25: import java.awt.event.KeyListener;
26: import com.javujavu.javux.wings.Shortcut;
27:
28: /**
29: * This class implement a key event listener that
30: * fire interactive shortcuts, and optionally maps key code
31: * into other one
32: * <br>
33: * <b>This in an extra class, NOT a part of base WingS component set.</b>
34: * <br>
35: * <br>
36: * <b>This class is thread safe.</b>
37: **/
38: public class ShortcutLink implements KeyListener {
39: private final Shortcut shortcut;
40: private final int mapCode;
41: private final int mapModifiers;
42: private final boolean map;
43:
44: public ShortcutLink(Shortcut shortcut) {
45: this .shortcut = shortcut;
46: this .mapCode = shortcut.getShortcutCode();
47: this .mapModifiers = shortcut.getShortcutModifiers();
48: map = false;
49: }
50:
51: public ShortcutLink(Shortcut shortcut, int mapCode, int mapModifiers) {
52: this .shortcut = shortcut;
53: this .mapCode = mapCode;
54: this .mapModifiers = mapModifiers;
55: map = true;
56: }
57:
58: public void keyPressed(KeyEvent e) {
59: if (e.getKeyCode() == mapCode
60: && e.getModifiers() == mapModifiers) {
61: KeyEvent e2 = mapEvent(e);
62: shortcut.processShortcut(e2);
63: if (e2.isConsumed())
64: e.consume();
65: }
66: }
67:
68: public void keyReleased(KeyEvent e) {
69: if (e.getKeyCode() == mapCode
70: && e.getModifiers() == mapModifiers) {
71: KeyEvent e2 = mapEvent(e);
72: shortcut.processShortcut(e2);
73: if (e2.isConsumed())
74: e.consume();
75: }
76: }
77:
78: private KeyEvent mapEvent(KeyEvent e) {
79: if (!map)
80: return e;
81: return new KeyEvent((Component) e.getSource(), e.getID(), e
82: .getWhen(), shortcut.getShortcutModifiers(), shortcut
83: .getShortcutCode(), e.getKeyChar());
84: }
85:
86: public void keyTyped(KeyEvent e) {
87: }
88: }
|