1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.event;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.vaadin.server.Resource;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
/**
* Shortcuts are a special type of {@link Action}s used to create keyboard
* shortcuts.
* <p>
* The ShortcutAction is triggered when the user presses a given key in
* combination with the (optional) given modifier keys.
* </p>
* <p>
* ShortcutActions can be global (by attaching to the {@link Window}), or
* attached to different parts of the UI so that a specific shortcut is only
* valid in part of the UI. For instance, one can attach shortcuts to a specific
* {@link Panel} - look for {@link ComponentContainer}s implementing
* {@link Handler Action.Handler} or {@link Notifier Action.Notifier}.
* </p>
* <p>
* ShortcutActions have a caption that may be used to display the shortcut
* visually. This allows the ShortcutAction to be used as a plain Action while
* still reacting to a keyboard shortcut. Note that this functionality is not
* very well supported yet, but it might still be a good idea to give a caption
* to the shortcut.
* </p>
*
* @author Vaadin Ltd.
* @since 4.0.1
*/
@SuppressWarnings("serial")
public class ShortcutAction extends Action {
private final int keyCode;
private final int[] modifiers;
/**
* Creates a shortcut that reacts to the given {@link KeyCode} and
* (optionally) {@link ModifierKey}s. <br/>
* The shortcut might be shown in the UI (e.g context menu), in which case
* the caption will be used.
*
* @param caption
* used when displaying the shortcut visually
* @param kc
* KeyCode that the shortcut reacts to
* @param m
* optional modifier keys
*/
public ShortcutAction(String caption, int kc, int... m) {
super(caption);
keyCode = kc;
modifiers = m;
}
/**
* Creates a shortcut that reacts to the given {@link KeyCode} and
* (optionally) {@link ModifierKey}s. <br/>
* The shortcut might be shown in the UI (e.g context menu), in which case
* the caption and icon will be used.
*
* @param caption
* used when displaying the shortcut visually
* @param icon
* used when displaying the shortcut visually
* @param kc
* KeyCode that the shortcut reacts to
* @param m
* optional modifier keys
*/
public ShortcutAction(String caption, Resource icon, int kc, int... m) {
super(caption, icon);
keyCode = kc;
modifiers = m;
}
/**
* Used in the caption shorthand notation to indicate the ALT modifier.
*/
public static final char SHORTHAND_CHAR_ALT = '&';
/**
* Used in the caption shorthand notation to indicate the SHIFT modifier.
*/
public static final char SHORTHAND_CHAR_SHIFT = '_';
/**
* Used in the caption shorthand notation to indicate the CTRL modifier.
*/
public static final char SHORTHAND_CHAR_CTRL = '^';
// regex-quote (escape) the characters
private static final String SHORTHAND_ALT = Pattern.quote(Character
.toString(SHORTHAND_CHAR_ALT));
private static final String SHORTHAND_SHIFT = Pattern.quote(Character
.toString(SHORTHAND_CHAR_SHIFT));
private static final String SHORTHAND_CTRL = Pattern.quote(Character
.toString(SHORTHAND_CHAR_CTRL));
// Used for replacing escaped chars, e.g && with &
private static final Pattern SHORTHAND_ESCAPE = Pattern.compile("("
+ SHORTHAND_ALT + "?)" + SHORTHAND_ALT + "|(" + SHORTHAND_SHIFT
+ "?)" + SHORTHAND_SHIFT + "|(" + SHORTHAND_CTRL + "?)"
+ SHORTHAND_CTRL);
// Used for removing escaped chars, only leaving real shorthands
private static final Pattern SHORTHAND_REMOVE = Pattern.compile("(["
+ SHORTHAND_ALT + "|" + SHORTHAND_SHIFT + "|" + SHORTHAND_CTRL
+ "])\\1");
// Mnemonic char, optionally followed by another, and optionally a third
private static final Pattern SHORTHANDS = Pattern.compile("("
+ SHORTHAND_ALT + "|" + SHORTHAND_SHIFT + "|" + SHORTHAND_CTRL
+ ")(?!\\1)(?:(" + SHORTHAND_ALT + "|" + SHORTHAND_SHIFT + "|"
+ SHORTHAND_CTRL + ")(?!\\1|\\2))?(?:(" + SHORTHAND_ALT + "|"
+ SHORTHAND_SHIFT + "|" + SHORTHAND_CTRL + ")(?!\\1|\\2|\\3))?.");
/**
* Constructs a ShortcutAction using a shorthand notation to encode the
* keycode and modifiers in the caption.
* <p>
* Insert one or more modifier characters before the character to use as
* keycode. E.g <code>"&Save"</code> will make a shortcut responding to
* ALT-S, <code>"E^xit"</code> will respond to CTRL-X.<br/>
* Multiple modifiers can be used, e.g <code>"&^Delete"</code> will respond
* to CTRL-ALT-D (the order of the modifier characters is not important).
* </p>
* <p>
* The modifier characters will be removed from the caption. The modifier
* character is be escaped by itself: two consecutive characters are turned
* into the original character w/o the special meaning. E.g
* <code>"Save&&&close"</code> will respond to ALT-C, and the caption will
* say "Save&close".
* </p>
*
* @param shorthandCaption
* the caption in modifier shorthand
*/
public ShortcutAction(String shorthandCaption) {
this(shorthandCaption, null);
}
/**
* Constructs a ShortcutAction using a shorthand notation to encode the
* keycode a in the caption.
* <p>
* This works the same way as {@link #ShortcutAction(String)}, with the
* exception that the modifiers given override those indicated in the
* caption. I.e use any of the modifier characters in the caption to
* indicate the keycode, but the modifier will be the given set.<br/>
* E.g
* <code>new ShortcutAction("Do &stuff", new int[]{ShortcutAction.ModifierKey.CTRL}));</code>
* will respond to CTRL-S.
* </p>
*
* @param shorthandCaption
* @param modifierKeys
*/
public ShortcutAction(String shorthandCaption, int... modifierKeys) {
// && -> & etc
super(SHORTHAND_ESCAPE.matcher(shorthandCaption).replaceAll("$1$2$3"));
// replace escaped chars with something that won't accidentally match
shorthandCaption = SHORTHAND_REMOVE.matcher(shorthandCaption)
.replaceAll("\u001A");
Matcher matcher = SHORTHANDS.matcher(shorthandCaption);
if (matcher.find()) {
String match = matcher.group();
// KeyCode from last char in match, uppercase
keyCode = Character.toUpperCase(matcher.group().charAt(
match.length() - 1));
// Given modifiers override this indicated in the caption
if (modifierKeys != null) {
modifiers = modifierKeys;
} else {
// Read modifiers from caption
int[] mod = new int[match.length() - 1];
for (int i = 0; i < mod.length; i++) {
int kc = match.charAt(i);
switch (kc) {
case SHORTHAND_CHAR_ALT:
mod[i] = ModifierKey.ALT;
break;
case SHORTHAND_CHAR_CTRL:
mod[i] = ModifierKey.CTRL;
break;
case SHORTHAND_CHAR_SHIFT:
mod[i] = ModifierKey.SHIFT;
break;
}
}
modifiers = mod;
}
} else {
keyCode = -1;
modifiers = modifierKeys;
}
}
/**
* Get the {@link KeyCode} that this shortcut reacts to (in combination with
* the {@link ModifierKey}s).
*
* @return keycode for this shortcut
*/
public int getKeyCode() {
return keyCode;
}
/**
* Get the {@link ModifierKey}s required for the shortcut to react.
*
* @return modifier keys for this shortcut
*/
public int[] getModifiers() {
return modifiers;
}
/**
* Key codes that can be used for shortcuts
*
*/
public interface KeyCode extends Serializable {
public static final int ENTER = 13;
public static final int ESCAPE = 27;
public static final int PAGE_UP = 33;
public static final int PAGE_DOWN = 34;
public static final int TAB = 9;
public static final int ARROW_LEFT = 37;
public static final int ARROW_UP = 38;
public static final int ARROW_RIGHT = 39;
public static final int ARROW_DOWN = 40;
public static final int BACKSPACE = 8;
public static final int DELETE = 46;
public static final int INSERT = 45;
public static final int END = 35;
public static final int HOME = 36;
public static final int F1 = 112;
public static final int F2 = 113;
public static final int F3 = 114;
public static final int F4 = 115;
public static final int F5 = 116;
public static final int F6 = 117;
public static final int F7 = 118;
public static final int F8 = 119;
public static final int F9 = 120;
public static final int F10 = 121;
public static final int F11 = 122;
public static final int F12 = 123;
public static final int A = 65;
public static final int B = 66;
public static final int C = 67;
public static final int D = 68;
public static final int E = 69;
public static final int F = 70;
public static final int G = 71;
public static final int H = 72;
public static final int I = 73;
public static final int J = 74;
public static final int K = 75;
public static final int L = 76;
public static final int M = 77;
public static final int N = 78;
public static final int O = 79;
public static final int P = 80;
public static final int Q = 81;
public static final int R = 82;
public static final int S = 83;
public static final int T = 84;
public static final int U = 85;
public static final int V = 86;
public static final int W = 87;
public static final int X = 88;
public static final int Y = 89;
public static final int Z = 90;
public static final int NUM0 = 48;
public static final int NUM1 = 49;
public static final int NUM2 = 50;
public static final int NUM3 = 51;
public static final int NUM4 = 52;
public static final int NUM5 = 53;
public static final int NUM6 = 54;
public static final int NUM7 = 55;
public static final int NUM8 = 56;
public static final int NUM9 = 57;
public static final int SPACEBAR = 32;
}
/**
* Modifier key constants
*
*/
public interface ModifierKey extends Serializable {
public static final int SHIFT = 16;
public static final int CTRL = 17;
public static final int ALT = 18;
public static final int META = 91;
}
}
|