001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.demo;
022:
023: import java.awt.GridBagConstraints;
024: import java.awt.GridBagLayout;
025: import java.awt.GridLayout;
026: import java.awt.Insets;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.ItemEvent;
030: import java.awt.event.ItemListener;
031: import java.awt.event.KeyEvent;
032: import com.javujavu.javux.wings.RadioGroup;
033: import com.javujavu.javux.wings.WingButton;
034: import com.javujavu.javux.wings.WingCheckBox;
035: import com.javujavu.javux.wings.WingLabel;
036: import com.javujavu.javux.wings.WingPanel;
037: import com.javujavu.javux.wings.WingTextField;
038: import com.javujavu.javux.wings.item.LabelItem;
039:
040: public class ShortcutPanel extends WingPanel implements ItemListener,
041: ActionListener {
042: private static int[][][] keyboard = {
043: { { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
044: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
045: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
046: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
047: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
048: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
049: { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 }, { 0, 0, 1 },
050: { 0, 0, 1 }, { 0, 0, 1 } },
051: { { KeyEvent.VK_ESCAPE, -11, 2 }, { 0, 0, 2 },
052: { KeyEvent.VK_F1, 0, 2 }, { KeyEvent.VK_F2, 0, 2 },
053: { KeyEvent.VK_F3, 0, 2 }, { KeyEvent.VK_F4, 0, 2 },
054: { 0, 0, 1 }, { KeyEvent.VK_F5, 0, 2 },
055: { KeyEvent.VK_F6, 0, 2 }, { KeyEvent.VK_F7, 0, 2 },
056: { KeyEvent.VK_F8, 0, 2 }, { 0, 0, 1 },
057: { KeyEvent.VK_F9, 0, 2 },
058: { KeyEvent.VK_F10, 0, 2 },
059: { KeyEvent.VK_F11, 0, 2 },
060: { KeyEvent.VK_F12, 0, 2 }, { 0, 0, 1 },
061: { -KeyEvent.VK_PRINTSCREEN, -8, 1 },
062: { -KeyEvent.VK_SCROLL_LOCK, -9, 1 },
063: { -KeyEvent.VK_PAUSE, -10, 1 } },
064: { { 0, 0, 1 } },
065: { { KeyEvent.VK_BACK_QUOTE, '~', 2 },
066: { KeyEvent.VK_1, 0, 2 }, { KeyEvent.VK_2, 0, 2 },
067: { KeyEvent.VK_3, 0, 2 }, { KeyEvent.VK_4, 0, 2 },
068: { KeyEvent.VK_5, 0, 2 }, { KeyEvent.VK_6, 0, 2 },
069: { KeyEvent.VK_7, 0, 2 }, { KeyEvent.VK_8, 0, 2 },
070: { KeyEvent.VK_9, 0, 2 }, { KeyEvent.VK_0, 0, 2 },
071: { -0x2D, '-', 2 }, { -KeyEvent.VK_EQUALS, '=', 2 },
072: { KeyEvent.VK_BACK_SPACE, -7, 4 }, { 0, 0, 1 },
073: { KeyEvent.VK_INSERT, -1, 1 },
074: { KeyEvent.VK_HOME, -2, 1 },
075: { KeyEvent.VK_PAGE_UP, -3, 1 }, { 0, 0, 1 },
076: { -KeyEvent.VK_NUM_LOCK, 'N', 1 },
077: { KeyEvent.VK_DIVIDE, '/', 1 },
078: { KeyEvent.VK_MULTIPLY, '*', 1 },
079: { KeyEvent.VK_SUBTRACT, '-', 1 } },
080: { { -KeyEvent.VK_TAB, -12, 3 }, { KeyEvent.VK_Q, 0, 2 },
081: { KeyEvent.VK_W, 0, 2 }, { KeyEvent.VK_E, 0, 2 },
082: { KeyEvent.VK_R, 0, 2 }, { KeyEvent.VK_T, 0, 2 },
083: { KeyEvent.VK_Y, 0, 2 }, { KeyEvent.VK_U, 0, 2 },
084: { KeyEvent.VK_I, 0, 2 }, { KeyEvent.VK_O, 0, 2 },
085: { KeyEvent.VK_P, 0, 2 },
086: { -KeyEvent.VK_OPEN_BRACKET, '[', 2 },
087: { -KeyEvent.VK_CLOSE_BRACKET, ']', 2 },
088: { -KeyEvent.VK_BACK_SLASH, '\\', 3 }, { 0, 0, 1 },
089: { KeyEvent.VK_DELETE, -4, 1 },
090: { KeyEvent.VK_END, -5, 1 },
091: { KeyEvent.VK_PAGE_DOWN, -6, 1 }, { 0, 0, 1 },
092: { KeyEvent.VK_NUMPAD7, '7', 1 },
093: { KeyEvent.VK_NUMPAD8, '8', 1 },
094: { KeyEvent.VK_NUMPAD9, '9', 1 },
095: { KeyEvent.VK_ADD, '+', -2 } },
096: { { -KeyEvent.VK_CAPS_LOCK, -13, 4 },
097: { KeyEvent.VK_A, 0, 2 }, { KeyEvent.VK_S, 0, 2 },
098: { KeyEvent.VK_D, 0, 2 }, { KeyEvent.VK_F, 0, 2 },
099: { KeyEvent.VK_G, 0, 2 }, { KeyEvent.VK_H, 0, 2 },
100: { KeyEvent.VK_J, 0, 2 }, { KeyEvent.VK_K, 0, 2 },
101: { KeyEvent.VK_L, 0, 2 },
102: { -KeyEvent.VK_SEMICOLON, ';', 2 },
103: { KeyEvent.VK_QUOTE, '\'', 2 },
104: { KeyEvent.VK_ENTER, -14, 4 }, { 0, 0, 5 },
105: { KeyEvent.VK_NUMPAD4, '4', 1 },
106: { KeyEvent.VK_NUMPAD5, '5', 1 },
107: { KeyEvent.VK_NUMPAD6, '6', 1 }, { 0, 0, 1 } },
108: { { -KeyEvent.VK_SHIFT, 0, 5 }, { KeyEvent.VK_Z, 0, 2 },
109: { KeyEvent.VK_X, 0, 2 }, { KeyEvent.VK_C, 0, 2 },
110: { KeyEvent.VK_V, 0, 2 }, { KeyEvent.VK_B, 0, 2 },
111: { KeyEvent.VK_N, 0, 2 }, { KeyEvent.VK_M, 0, 2 },
112: { -KeyEvent.VK_COMMA, ',', 2 },
113: { -KeyEvent.VK_PERIOD, '.', 2 },
114: { -KeyEvent.VK_SLASH, '/', 2 },
115: { -KeyEvent.VK_SHIFT, 0, 5 }, { 0, 0, 2 },
116: { KeyEvent.VK_UP, -17, 1 }, { 0, 0, 2 },
117: { KeyEvent.VK_NUMPAD1, '1', 1 },
118: { KeyEvent.VK_NUMPAD2, '2', 1 },
119: { KeyEvent.VK_NUMPAD3, '3', 1 },
120: { -KeyEvent.VK_ENTER, 'E', -2 }, },
121: { { -KeyEvent.VK_CONTROL, 0, 4 },
122: { -KeyEvent.VK_ALT, 0, 4 },
123: { KeyEvent.VK_SPACE, 0, 14 },
124: { -KeyEvent.VK_ALT, 0, 4 },
125: { -KeyEvent.VK_CONTROL, 0, 4 }, { 0, 0, 1 },
126: { KeyEvent.VK_LEFT, -15, 1 },
127: { KeyEvent.VK_DOWN, -18, 1 },
128: { KeyEvent.VK_RIGHT, -16, 1 }, { 0, 0, 1 },
129: { KeyEvent.VK_NUMPAD0, '0', 2 },
130: { KeyEvent.VK_DECIMAL, '.', 1 }, { 0, 0, 1 } } };
131: private static String[] titles = { "Ins", "Home", "PgUp", "Del",
132: "End", "Down", "BackSp", "Prt", "Lock", "Pause", "Esc",
133: "Tab", "Caps", "Enter", "<", ">", "^", "v" };
134: private WingButton[][] buttons;
135: private WingCheckBox cbShift;
136: private WingCheckBox cbCtrl;
137: private WingCheckBox cbAlt;
138: private WingCheckBox rbNormal;
139: private WingCheckBox rbFast;
140: private WingCheckBox rbRepeat;
141: private WingTextField tfText;
142:
143: public ShortcutPanel(WingSetPanel owner) {
144: this .setLayout(new GridBagLayout());
145: WingPanel panel = new WingPanel(new GridBagLayout());
146: GridBagConstraints c = new GridBagConstraints();
147: c.insets = new Insets(10, 10, 10, 10);
148: c.anchor = GridBagConstraints.CENTER;
149: c.fill = GridBagConstraints.BOTH;
150: c.weightx = 1.0;
151: c.weighty = 1.0;
152: this .add(panel, c);
153:
154: buttons = new WingButton[keyboard.length][];
155: c = new GridBagConstraints();
156: c.gridy = 0;
157: c.gridwidth = GridBagConstraints.REMAINDER;
158: c.fill = GridBagConstraints.HORIZONTAL;
159: panel.add(tfText = new WingTextField(10), c);
160: tfText
161: .setTooltip(new LabelItem(
162: "focused WingTextField and WingTextPane\nconsume shortcut keys except\naction keys and keys with Ctrl modifier",
163: WingSet.imgCrying));
164:
165: c = new GridBagConstraints();
166: c.fill = GridBagConstraints.BOTH;
167: WingButton b;
168: int code;
169: c.gridy = 1;
170: for (int row = 0; row < keyboard.length; row++) {
171: c.gridx = 0;
172: buttons[row] = new WingButton[keyboard[row].length];
173: for (int key = 0; key < keyboard[row].length; key++) {
174: int w = keyboard[row][key][2];
175: if (w > 0) {
176: c.gridwidth = w;
177: c.gridheight = 1;
178: } else {
179: c.gridheight = -w;
180: c.gridwidth = 1;
181: }
182: code = keyboard[row][key][0];
183: if (code != 0) {
184: int cc = (code < 0) ? -code : code;
185: int t = keyboard[row][key][1];
186: String s = (t == 0) ? KeyEvent.getKeyText(cc)
187: : (t < 0) ? titles[-t - 1]
188: : String
189: .valueOf((char) keyboard[row][key][1]);
190: if (code > 0) {
191: buttons[row][key] = b = new WingButton(s);
192: panel.add(b, c);
193: } else {
194: panel.add(b = new WingButton(s), c);
195: b.setEnabled(false);
196: }
197: b.setStyleId("tool");
198: } else {
199: panel.add(new WingLabel(), c);
200: }
201: c.gridx += c.gridwidth;
202: }
203: c.gridy++;
204: }
205: c.gridx = 0;
206: c.gridwidth = GridBagConstraints.REMAINDER;
207: c.insets = new Insets(10, 10, 10, 10);
208: WingPanel p2;
209: panel.add(p2 = new WingPanel(new GridLayout(3, 0, 5, 5)), c);
210: p2.add(cbShift = new WingCheckBox("use SHIFT modifier"));
211: p2.add(rbNormal = new WingCheckBox("classic buttons", RADIO));
212: p2.add(cbCtrl = new WingCheckBox("use CTRL modifier"));
213: p2.add(rbFast = new WingCheckBox("fast action buttons", RADIO));
214: p2.add(cbAlt = new WingCheckBox("use ALT modifier"));
215: p2.add(rbRepeat = new WingCheckBox("repeating buttons", RADIO));
216: RadioGroup g = new RadioGroup();
217: g.add(rbNormal);
218: g.add(rbFast);
219: g.add(rbRepeat);
220: rbNormal.setSelected(true);
221: setShortcuts(0);
222:
223: this .addItemListener(this );
224: this .addActionListener(this );
225: }
226:
227: private void setShortcuts(int modifiers) {
228: String pref = "Button with shortcut: ";
229: if ((modifiers & KeyEvent.SHIFT_MASK) != 0)
230: pref += KeyEvent.getKeyText(KeyEvent.VK_SHIFT) + "+";
231: if ((modifiers & KeyEvent.CTRL_MASK) != 0)
232: pref += KeyEvent.getKeyText(KeyEvent.VK_CONTROL) + "+";
233: if ((modifiers & KeyEvent.ALT_MASK) != 0)
234: pref += KeyEvent.getKeyText(KeyEvent.VK_ALT) + "+";
235: for (int row = 0; row < keyboard.length; row++) {
236: for (int key = 0; key < keyboard[row].length; key++) {
237: if (buttons[row][key] != null) {
238: buttons[row][key].setShortcut(
239: keyboard[row][key][0], modifiers);
240: buttons[row][key].setTooltip(pref
241: + KeyEvent
242: .getKeyText(keyboard[row][key][0]));
243: }
244: }
245: }
246: }
247:
248: private void setParams(boolean fast, boolean repeat) {
249: for (int row = 0; row < keyboard.length; row++) {
250: for (int key = 0; key < keyboard[row].length; key++) {
251: if (buttons[row][key] != null) {
252: buttons[row][key].setFastAction(fast);
253: buttons[row][key].setRepeat(repeat);
254: }
255: }
256: }
257: }
258:
259: public void itemStateChanged(ItemEvent e) {
260: Object src = e.getSource();
261: if (src == cbShift || src == cbCtrl || src == cbAlt) {
262: int modifiers = 0;
263: if (cbShift.isSelected())
264: modifiers |= KeyEvent.SHIFT_MASK;
265: if (cbCtrl.isSelected())
266: modifiers |= KeyEvent.CTRL_MASK;
267: if (cbAlt.isSelected())
268: modifiers |= KeyEvent.ALT_MASK;
269: setShortcuts(modifiers);
270: } else if (src == rbNormal || src == rbFast || src == rbRepeat) {
271: setParams(rbFast.isSelected(), rbRepeat.isSelected());
272: }
273: }
274:
275: public void actionPerformed(ActionEvent e) {
276: tfText.insert(-1, e.getActionCommand());
277: }
278: }
|