001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package wingset;
014:
015: import org.wings.*;
016:
017: import javax.swing.*;
018: import java.awt.event.ActionEvent;
019: import java.awt.event.KeyEvent;
020: import java.awt.*;
021:
022: /**
023: * @author Holger Engels
024: */
025: public class KeyboardBindingsExample extends WingSetPane {
026: private final SLabel actionEventLabel = new SLabel();
027: private final STextField textField = new STextField();
028: private final SPanel panel = new KeyboardPanel(
029: new SFlowDownLayout());
030:
031: public KeyboardBindingsExample() {
032:
033: /**
034: * Input/Action map locally for the textfield
035: */
036: final InputMap textfieldInputMap = new InputMap();
037: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0,
038: false), "F1");
039: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0,
040: false), "F2");
041: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0,
042: false), "F3");
043: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0,
044: false), "F4");
045: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0,
046: false), "F5");
047: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0,
048: false), "F6");
049: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0,
050: false), "F7");
051: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0,
052: false), "F8");
053: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0,
054: false), "F9");
055: textfieldInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10,
056: 0, false), "F10");
057:
058: /**
059: * Input/Action map locally for the SForm element
060: */
061: final InputMap formInputMap = new InputMap();
062: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1,
063: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F1");
064: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2,
065: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F2");
066: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3,
067: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F3");
068: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
069: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F4");
070: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5,
071: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F5");
072: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6,
073: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F6");
074: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7,
075: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F7");
076: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8,
077: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F8");
078: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9,
079: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F9");
080: formInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10,
081: KeyEvent.SHIFT_DOWN_MASK, false), "Shift F10");
082:
083: /**
084: * Input/Action map globally on the whole dialog/page, but assigned via the textfield
085: */
086: final InputMap pageInputMap = new InputMap();
087: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1,
088: KeyEvent.ALT_DOWN_MASK, false), "Alt F1");
089: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F2,
090: KeyEvent.ALT_DOWN_MASK, false), "Alt F2");
091: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F3,
092: KeyEvent.ALT_DOWN_MASK, false), "Alt F3");
093: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4,
094: KeyEvent.ALT_DOWN_MASK, false), "Alt F4");
095: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5,
096: KeyEvent.ALT_DOWN_MASK, false), "Alt F5");
097: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6,
098: KeyEvent.ALT_DOWN_MASK, false), "Alt F6");
099: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7,
100: KeyEvent.ALT_DOWN_MASK, false), "Alt F7");
101: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F8,
102: KeyEvent.ALT_DOWN_MASK, false), "Alt F8");
103: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9,
104: KeyEvent.ALT_DOWN_MASK, false), "Alt F9");
105: pageInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10,
106: KeyEvent.ALT_DOWN_MASK, false), "Alt F10");
107:
108: final Action action = new AbstractAction() {
109: public void actionPerformed(ActionEvent e) {
110: actionEventLabel.setText(e.getActionCommand());
111: }
112: };
113:
114: final ActionMap actionMap = new ActionMap();
115: actionMap.put("F1", action);
116: actionMap.put("F2", action);
117: actionMap.put("F3", action);
118: actionMap.put("F4", action);
119: actionMap.put("F5", action);
120: actionMap.put("F6", action);
121: actionMap.put("F7", action);
122: actionMap.put("F8", action);
123: actionMap.put("F9", action);
124: actionMap.put("F10", action);
125: actionMap.put("Shift F1", action);
126: actionMap.put("Shift F2", action);
127: actionMap.put("Shift F3", action);
128: actionMap.put("Shift F4", action);
129: actionMap.put("Shift F5", action);
130: actionMap.put("Shift F6", action);
131: actionMap.put("Shift F7", action);
132: actionMap.put("Shift F8", action);
133: actionMap.put("Shift F9", action);
134: actionMap.put("Shift F10", action);
135: actionMap.put("Alt F1", action);
136: actionMap.put("Alt F2", action);
137: actionMap.put("Alt F3", action);
138: actionMap.put("Alt F4", action);
139: actionMap.put("Alt F5", action);
140: actionMap.put("Alt F6", action);
141: actionMap.put("Alt F7", action);
142: actionMap.put("Alt F8", action);
143: actionMap.put("Alt F9", action);
144: actionMap.put("Alt F10", action);
145:
146: // Define key bindinings only if focus inside textfield
147: textField.setInputMap(textfieldInputMap);
148: textField.setActionMap(actionMap);
149:
150: // Define key bindings on the textfield, valid all over the page
151: textField.setInputMap(WHEN_IN_FOCUSED_FRAME, pageInputMap);
152:
153: // Define key bindinings if focus inside the containing SForm
154: panel.setInputMap(formInputMap);
155: // equal to : panel.setInputMap(formInputMap, WHEN_FOCUSED_OR_ANCESTOR_OF_FOCUSED_COMPONENT);
156: panel.setActionMap(actionMap);
157:
158: final SLabel titleLabel = new SLabel(
159: "wingS key binding feature demonstration\n ");
160: titleLabel.setFont(new SFont("sans-serif", SFont.PLAIN, 16));
161: panel.add(titleLabel);
162:
163: final SLabel instructions = new SLabel(
164: "This page demonstrates the feature of attaching key binding to specific components. "
165: + "Some components provide already default bindings like STabbedPane (try [Alt]-[LEFT] "
166: + "or [Alt]-[RIGHT].\n"
167: + "\n"
168: + "On this page The keys [F1] through [F10] are captured locally by the STextField. This means "
169: + "the keypress will only trigger an event for thesekeys if the focus is inside the textfield."
170: + "\n"
171: + "[Shift]-[F1] through [Shift]-[F10] are attached up to the containing SForm, so these keys should "
172: + "trigger events if the focus is inside the textfield or on the checkbox.\n"
173: + "\n"
174: + "[Alt]-[F1] through [Alt]-[F10] are also attached to the textfield but with global focus, hence "
175: + "captured by the whole page, then routed back to the STextField. These shortcuts will work "
176: + "everythere on the page.\n\n");
177: instructions.setWordWrap(true);
178: panel.add(instructions);
179:
180: panel.add(textField);
181: panel.add(new SCheckBox(
182: "Checkbox: Use as alternative focus point"));
183:
184: panel.add(actionEventLabel);
185: actionEventLabel.setForeground(Color.RED);
186:
187: panel.setHorizontalAlignment(CENTER);
188: panel
189: .setPreferredSize(new SDimension(550,
190: SDimension.AUTO_INT));
191: }
192:
193: protected SComponent createControls() {
194: return null;
195: }
196:
197: public SComponent createExample() {
198: return panel;
199: }
200:
201: private static class KeyboardPanel extends SPanel implements
202: LowLevelEventListener {
203: public KeyboardPanel(SLayoutManager l) {
204: super (l);
205: }
206:
207: public void processLowLevelEvent(String name, String[] values) {
208: processKeyEvents(values);
209: }
210:
211: public void fireIntermediateEvents() {
212: }
213:
214: public boolean isEpochCheckEnabled() {
215: return true;
216: }
217: }
218: }
|