001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit;
025:
026: import javax.swing.KeyStroke;
027:
028: /**
029: * Some useful utilities for character manipulation.
030: *
031: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
032: * @version 1.0
033: */
034: public class CharacterToolkit {
035: /**
036: * Creates a new CharacterToolkit.
037: */
038: public CharacterToolkit() {
039: }
040:
041: /**
042: * Returns a string representation of the given character ("G", "Shift", "Control", "Space",...
043: * etc).
044: *
045: * @param keyChar the character
046: * @param keyCode the code of the character
047: *
048: * @return the string representation
049: */
050: public static String getString(char keyChar, int keyCode) {
051: String result = null;
052:
053: if (Character.isLetterOrDigit(keyChar)) {
054: result = "" + keyChar;
055: } else {
056: switch (keyCode) {
057: case 8:
058: result = "Backspace";
059:
060: break;
061:
062: case 9:
063: result = "Tab";
064:
065: break;
066:
067: case 10:
068: result = "Enter";
069:
070: break;
071:
072: case 16:
073: result = "Shift";
074:
075: break;
076:
077: case 17:
078: result = "Control";
079:
080: break;
081:
082: case 18:
083: result = "Alt";
084:
085: break;
086:
087: case 19:
088: result = "Pause";
089:
090: break;
091:
092: case 20:
093: result = "CapsLock";
094:
095: break;
096:
097: case 27:
098: result = "Escape";
099:
100: break;
101:
102: case 32:
103: result = "Space";
104:
105: break;
106:
107: case 33:
108: result = "PageUp";
109:
110: break;
111:
112: case 34:
113: result = "PageDown";
114:
115: break;
116:
117: case 35:
118: result = "End";
119:
120: break;
121:
122: case 36:
123: result = "Home";
124:
125: break;
126:
127: case 37:
128: result = "Left";
129:
130: break;
131:
132: case 38:
133: result = "Up";
134:
135: break;
136:
137: case 39:
138: result = "Right";
139:
140: break;
141:
142: case 40:
143: result = "Down";
144:
145: break;
146:
147: case 112:
148: result = "F1";
149:
150: break;
151:
152: case 113:
153: result = "F2";
154:
155: break;
156:
157: case 114:
158: result = "F3";
159:
160: break;
161:
162: case 115:
163: result = "F4";
164:
165: break;
166:
167: case 116:
168: result = "F5";
169:
170: break;
171:
172: case 117:
173: result = "F6";
174:
175: break;
176:
177: case 118:
178: result = "F7";
179:
180: break;
181:
182: case 119:
183: result = "F8";
184:
185: break;
186:
187: case 120:
188: result = "F9";
189:
190: break;
191:
192: case 121:
193: result = "F10";
194:
195: break;
196:
197: case 122:
198: result = "F11";
199:
200: break;
201:
202: case 123:
203: result = "F12";
204:
205: break;
206:
207: case 154:
208: result = "PrintScreen";
209:
210: break;
211:
212: case 155:
213: result = "Insert";
214:
215: break;
216:
217: default:
218:
219: KeyStroke stroke = KeyStroke.getKeyStroke(keyCode, 0);
220: String strokeString = "" + stroke;
221:
222: if (strokeString.startsWith("keyCode ")
223: && strokeString.endsWith("-P")) {
224: strokeString = strokeString.substring(8);
225: strokeString = strokeString.substring(0,
226: strokeString.length() - 2);
227: result = strokeString;
228: } else {
229: result = "code=" + keyCode;
230: }
231:
232: break;
233: }
234: }
235:
236: return result;
237: }
238:
239: /**
240: * Returns a string representation of the given character ("G", ...) as if the key event would
241: * have been dispatched on a text field. "Space" is returned as " ", and so on...
242: *
243: * @param keyChar the character
244: * @param keyCode the code of the character
245: *
246: * @return the string representation
247: */
248: public static String getInterpretedString(char keyChar, int keyCode) {
249: String result = null;
250:
251: if (Character.isLetterOrDigit(keyChar)) {
252: result = "" + keyChar;
253: } else {
254: if (((33 <= keyCode) && (keyCode <= 40))
255: || ((112 <= keyCode) && (keyCode <= 123))) {
256: result = "";
257: } else {
258: switch (keyCode) {
259: case 8:
260: case 9:
261: case 10:
262: case 16:
263: case 17:
264: case 18:
265: case 19:
266: case 20:
267: case 27:
268: case 154:
269: case 155:
270: result = "";
271:
272: break;
273:
274: case 32:
275: result = " ";
276:
277: break;
278:
279: default:
280:
281: KeyStroke stroke = KeyStroke.getKeyStroke(keyCode,
282: 0);
283: String strokeString = "" + stroke;
284:
285: if (strokeString.startsWith("keyCode ")
286: && strokeString.endsWith("-P")) {
287: strokeString = strokeString.substring(8);
288: strokeString = strokeString.substring(0,
289: strokeString.length() - 2);
290: result = strokeString;
291: } else {
292: result = "code=" + keyCode;
293: }
294:
295: break;
296: }
297: }
298: }
299:
300: return result;
301: }
302: }
|