Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
KeyEventUtils |
|
| 23.5;23.5 |
1 | /* $Id: KeyEventUtils.java 17887 2010-01-12 21:17:18Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 2006-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.util; | |
40 | ||
41 | import java.awt.event.InputEvent; | |
42 | import java.awt.event.KeyEvent; | |
43 | import java.lang.reflect.Field; | |
44 | import java.lang.reflect.Modifier; | |
45 | ||
46 | import javax.swing.KeyStroke; | |
47 | ||
48 | /** | |
49 | * Utility class for KeyEvents | |
50 | * | |
51 | * @author andrea.nironi@gmail.com | |
52 | */ | |
53 | 0 | public class KeyEventUtils { |
54 | ||
55 | /** | |
56 | * The expression between modifier/modifier and between modifier/text | |
57 | */ | |
58 | public static final String MODIFIER_JOINER = " + "; | |
59 | /** | |
60 | * The text for the shift modifier | |
61 | */ | |
62 | public static final String SHIFT_MODIFIER = "SHIFT"; | |
63 | /** | |
64 | * The text for the ctrl modifier | |
65 | */ | |
66 | public static final String CTRL_MODIFIER = "CTRL"; | |
67 | /** | |
68 | * The text for the meta modifier | |
69 | */ | |
70 | public static final String META_MODIFIER = "META"; | |
71 | /** | |
72 | * The text for the alt modifier | |
73 | */ | |
74 | public static final String ALT_MODIFIER = "ALT"; | |
75 | /** | |
76 | * The text for the alt-gr modifier | |
77 | */ | |
78 | public static final String ALT_GRAPH_MODIFIER = "altGraph"; | |
79 | ||
80 | ||
81 | /** | |
82 | * Returns whether the key in this event is an "action" key. This is a | |
83 | * customization of KeyEvent#isActionKey() | |
84 | * | |
85 | * @param evt | |
86 | * the event to be verified | |
87 | * @return true if the event is an | |
88 | * @see java.awt.event.KeyEvent#isActionKey() | |
89 | */ | |
90 | public static final boolean isActionEvent(KeyEvent evt) { | |
91 | 0 | int keyCode = evt.getKeyCode(); |
92 | ||
93 | 0 | switch (keyCode) { |
94 | ||
95 | // Argo customization | |
96 | case KeyEvent.VK_BACK_SPACE: | |
97 | case KeyEvent.VK_DELETE: | |
98 | case KeyEvent.VK_CANCEL: | |
99 | ||
100 | // KeyEvent.isActionKey() method | |
101 | case KeyEvent.VK_HOME: | |
102 | case KeyEvent.VK_END: | |
103 | case KeyEvent.VK_PAGE_UP: | |
104 | case KeyEvent.VK_PAGE_DOWN: | |
105 | case KeyEvent.VK_UP: | |
106 | case KeyEvent.VK_DOWN: | |
107 | case KeyEvent.VK_LEFT: | |
108 | case KeyEvent.VK_RIGHT: | |
109 | ||
110 | case KeyEvent.VK_KP_LEFT: | |
111 | case KeyEvent.VK_KP_UP: | |
112 | case KeyEvent.VK_KP_RIGHT: | |
113 | case KeyEvent.VK_KP_DOWN: | |
114 | ||
115 | case KeyEvent.VK_F1: | |
116 | case KeyEvent.VK_F2: | |
117 | case KeyEvent.VK_F3: | |
118 | case KeyEvent.VK_F4: | |
119 | case KeyEvent.VK_F5: | |
120 | case KeyEvent.VK_F6: | |
121 | case KeyEvent.VK_F7: | |
122 | case KeyEvent.VK_F8: | |
123 | case KeyEvent.VK_F9: | |
124 | case KeyEvent.VK_F10: | |
125 | case KeyEvent.VK_F11: | |
126 | case KeyEvent.VK_F12: | |
127 | case KeyEvent.VK_F13: | |
128 | case KeyEvent.VK_F14: | |
129 | case KeyEvent.VK_F15: | |
130 | case KeyEvent.VK_F16: | |
131 | case KeyEvent.VK_F17: | |
132 | case KeyEvent.VK_F18: | |
133 | case KeyEvent.VK_F19: | |
134 | case KeyEvent.VK_F20: | |
135 | case KeyEvent.VK_F21: | |
136 | case KeyEvent.VK_F22: | |
137 | case KeyEvent.VK_F23: | |
138 | case KeyEvent.VK_F24: | |
139 | case KeyEvent.VK_PRINTSCREEN: | |
140 | case KeyEvent.VK_SCROLL_LOCK: | |
141 | case KeyEvent.VK_CAPS_LOCK: | |
142 | case KeyEvent.VK_NUM_LOCK: | |
143 | case KeyEvent.VK_PAUSE: | |
144 | case KeyEvent.VK_INSERT: | |
145 | ||
146 | case KeyEvent.VK_FINAL: | |
147 | case KeyEvent.VK_CONVERT: | |
148 | case KeyEvent.VK_NONCONVERT: | |
149 | case KeyEvent.VK_ACCEPT: | |
150 | case KeyEvent.VK_MODECHANGE: | |
151 | case KeyEvent.VK_KANA: | |
152 | case KeyEvent.VK_KANJI: | |
153 | case KeyEvent.VK_ALPHANUMERIC: | |
154 | case KeyEvent.VK_KATAKANA: | |
155 | case KeyEvent.VK_HIRAGANA: | |
156 | case KeyEvent.VK_FULL_WIDTH: | |
157 | case KeyEvent.VK_HALF_WIDTH: | |
158 | case KeyEvent.VK_ROMAN_CHARACTERS: | |
159 | case KeyEvent.VK_ALL_CANDIDATES: | |
160 | case KeyEvent.VK_PREVIOUS_CANDIDATE: | |
161 | case KeyEvent.VK_CODE_INPUT: | |
162 | case KeyEvent.VK_JAPANESE_KATAKANA: | |
163 | case KeyEvent.VK_JAPANESE_HIRAGANA: | |
164 | case KeyEvent.VK_JAPANESE_ROMAN: | |
165 | case KeyEvent.VK_KANA_LOCK: | |
166 | case KeyEvent.VK_INPUT_METHOD_ON_OFF: | |
167 | ||
168 | case KeyEvent.VK_AGAIN: | |
169 | case KeyEvent.VK_UNDO: | |
170 | case KeyEvent.VK_COPY: | |
171 | case KeyEvent.VK_PASTE: | |
172 | case KeyEvent.VK_CUT: | |
173 | case KeyEvent.VK_FIND: | |
174 | case KeyEvent.VK_PROPS: | |
175 | case KeyEvent.VK_STOP: | |
176 | ||
177 | case KeyEvent.VK_HELP: | |
178 | 0 | return true; |
179 | } | |
180 | 0 | return false; |
181 | } | |
182 | ||
183 | /** | |
184 | * Returns a unique text for a KeyEvent code | |
185 | * | |
186 | * @param keyCode the keyCode to be "translated" | |
187 | * @return the corrisponding text for the keyCode | |
188 | */ | |
189 | public static String getKeyText(int keyCode) { | |
190 | 0 | int expectedModifiers = |
191 | (Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL); | |
192 | ||
193 | 0 | Field[] fields = KeyEvent.class.getDeclaredFields(); |
194 | 0 | for (int i = 0; i < fields.length; i++) { |
195 | try { | |
196 | 0 | if (fields[i].getModifiers() == expectedModifiers |
197 | && fields[i].getType() == Integer.TYPE | |
198 | && fields[i].getName().startsWith("VK_") | |
199 | && fields[i].getInt(KeyEvent.class) == keyCode) { | |
200 | ||
201 | 0 | return fields[i].getName().substring(3); |
202 | } | |
203 | 0 | } catch (IllegalAccessException e) { |
204 | ||
205 | 0 | } |
206 | } | |
207 | 0 | return "UNKNOWN"; |
208 | } | |
209 | ||
210 | /** | |
211 | * Returns a unique text for the given key modifiers | |
212 | * | |
213 | * @param modifiers the modifiers to be "translated" | |
214 | * @return the corrisponding text for the keyCode | |
215 | */ | |
216 | public static String getModifiersText(int modifiers) { | |
217 | 0 | StringBuffer buf = new StringBuffer(); |
218 | ||
219 | 0 | if ((modifiers & InputEvent.SHIFT_MASK) != 0) { |
220 | 0 | buf.append(SHIFT_MODIFIER).append(MODIFIER_JOINER); |
221 | } | |
222 | 0 | if ((modifiers & InputEvent.CTRL_MASK) != 0) { |
223 | 0 | buf.append(CTRL_MODIFIER).append(MODIFIER_JOINER); |
224 | } | |
225 | 0 | if ((modifiers & InputEvent.META_MASK) != 0) { |
226 | 0 | buf.append(META_MODIFIER).append(MODIFIER_JOINER); |
227 | } | |
228 | 0 | if ((modifiers & InputEvent.ALT_MASK) != 0) { |
229 | 0 | buf.append(ALT_MODIFIER).append(MODIFIER_JOINER); |
230 | } | |
231 | 0 | if ((modifiers & InputEvent.ALT_GRAPH_MASK) != 0) { |
232 | 0 | buf.append(ALT_GRAPH_MODIFIER).append(MODIFIER_JOINER); |
233 | } | |
234 | 0 | return buf.toString(); |
235 | } | |
236 | ||
237 | /** | |
238 | * Formats a given KeyStroke into a string | |
239 | * | |
240 | * @param keyStroke the KeyStroke to be formatted | |
241 | * @return the formatted String | |
242 | */ | |
243 | public static String formatKeyStroke(KeyStroke keyStroke) { | |
244 | 0 | if (keyStroke != null) { |
245 | 0 | return getModifiersText(keyStroke.getModifiers()) |
246 | + KeyEventUtils.getKeyText(keyStroke.getKeyCode()); | |
247 | } else { | |
248 | 0 | return ""; |
249 | } | |
250 | } | |
251 | } |