blob: a6c4afaf8b26c325ac1ae152ac8556dd6da7b871 [file] [log] [blame]
/**
* Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*/
package org.eclipse.osbp.runtime.common.keystroke;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// TODO: Auto-generated Javadoc
/**
* The Class KeyCodeUtil.
*/
public class KeyCodeUtil {
/** The Constant keyCodeMappings. */
private static final Map<String, Integer> keyCodeMappings = new HashMap<String, Integer>();
/** The Constant modifierMappings. */
private static final Map<String, Integer> modifierMappings = new HashMap<String, Integer>();
static {
keyCodeMappings.put("ENTER", KeyCode.ENTER);
keyCodeMappings.put("ESCAPE", KeyCode.ESCAPE);
keyCodeMappings.put("PAGE_UP", KeyCode.PAGE_UP);
keyCodeMappings.put("PAGE_DOWN", KeyCode.PAGE_DOWN);
keyCodeMappings.put("TAB", KeyCode.TAB);
keyCodeMappings.put("ARROW_LEFT", KeyCode.ARROW_LEFT);
keyCodeMappings.put("ARROW_UP", KeyCode.ARROW_UP);
keyCodeMappings.put("ARROW_RIGHT", KeyCode.ARROW_RIGHT);
keyCodeMappings.put("ARROW_DOWN", KeyCode.ARROW_DOWN);
keyCodeMappings.put("BACKSPACE", KeyCode.BACKSPACE);
keyCodeMappings.put("DELETE", KeyCode.DELETE);
keyCodeMappings.put("INSERT", KeyCode.INSERT);
keyCodeMappings.put("END", KeyCode.END);
keyCodeMappings.put("HOME", KeyCode.HOME);
keyCodeMappings.put("F1", KeyCode.F1);
keyCodeMappings.put("F2", KeyCode.F2);
keyCodeMappings.put("F3", KeyCode.F3);
keyCodeMappings.put("F4", KeyCode.F4);
keyCodeMappings.put("F5", KeyCode.F5);
keyCodeMappings.put("F6", KeyCode.F6);
keyCodeMappings.put("F7", KeyCode.F7);
keyCodeMappings.put("F8", KeyCode.F8);
keyCodeMappings.put("F9", KeyCode.F9);
keyCodeMappings.put("F10", KeyCode.F10);
keyCodeMappings.put("F11", KeyCode.F11);
keyCodeMappings.put("F12", KeyCode.F12);
keyCodeMappings.put("A", KeyCode.A);
keyCodeMappings.put("B", KeyCode.B);
keyCodeMappings.put("C", KeyCode.C);
keyCodeMappings.put("D", KeyCode.D);
keyCodeMappings.put("E", KeyCode.E);
keyCodeMappings.put("F", KeyCode.F);
keyCodeMappings.put("G", KeyCode.G);
keyCodeMappings.put("H", KeyCode.H);
keyCodeMappings.put("I", KeyCode.I);
keyCodeMappings.put("J", KeyCode.J);
keyCodeMappings.put("K", KeyCode.K);
keyCodeMappings.put("L", KeyCode.L);
keyCodeMappings.put("M", KeyCode.M);
keyCodeMappings.put("N", KeyCode.N);
keyCodeMappings.put("O", KeyCode.O);
keyCodeMappings.put("P", KeyCode.P);
keyCodeMappings.put("Q", KeyCode.Q);
keyCodeMappings.put("R", KeyCode.R);
keyCodeMappings.put("S", KeyCode.S);
keyCodeMappings.put("T", KeyCode.T);
keyCodeMappings.put("U", KeyCode.U);
keyCodeMappings.put("V", KeyCode.V);
keyCodeMappings.put("W", KeyCode.W);
keyCodeMappings.put("X", KeyCode.X);
keyCodeMappings.put("Y", KeyCode.Y);
keyCodeMappings.put("Z", KeyCode.Z);
keyCodeMappings.put("0", KeyCode.NUM0);
keyCodeMappings.put("1", KeyCode.NUM1);
keyCodeMappings.put("2", KeyCode.NUM2);
keyCodeMappings.put("3", KeyCode.NUM3);
keyCodeMappings.put("4", KeyCode.NUM4);
keyCodeMappings.put("5", KeyCode.NUM5);
keyCodeMappings.put("6", KeyCode.NUM6);
keyCodeMappings.put("7", KeyCode.NUM7);
keyCodeMappings.put("8", KeyCode.NUM8);
keyCodeMappings.put("9", KeyCode.NUM9);
keyCodeMappings.put("SPACEBAR", KeyCode.SPACEBAR);
modifierMappings.put("SHIFT", ModifierKey.SHIFT);
modifierMappings.put("CTRL", ModifierKey.CTRL);
modifierMappings.put("ALT", ModifierKey.ALT);
modifierMappings.put("META", ModifierKey.META);
}
/**
* Parses the keyCode to a key stroke definition.<br>
* KeyCode follows the pattern "CTRL+ALT+S". All reserved words are
* constants in the KeyCodeUtil.
*
* @param keyCode
* the key code
* @return the key stroke definition
*/
public static KeyStrokeDefinition parse(String keyCode) {
int keyCodeInt = -1;
List<Integer> modifierInts = new ArrayList<Integer>();
String[] tokens = keyCode.split("\\+");
for (String t : tokens) {
String token = t.trim();
if (modifierMappings.containsKey(token)) {
modifierInts.add(modifierMappings.get(token));
} else if (keyCodeMappings.containsKey(token)) {
keyCodeInt = keyCodeMappings.get(token);
}
}
return new KeyStrokeDefinition(keyCode, keyCodeInt,
toModifierInts(modifierInts));
}
/**
* To modifier ints.
*
* @param modifierInts
* the modifier ints
* @return the int[]
*/
private static int[] toModifierInts(List<Integer> modifierInts) {
int[] result = new int[modifierInts.size()];
for (int i = 0; i < modifierInts.size(); i++) {
int modifier = modifierInts.get(i);
result[i] = modifier;
}
return result;
}
/**
* Key lookup integer.
*
* @param name of the key
* @return the integer
*/
public static Integer keyLookupInteger(final String name) {
final Object value = keyCodeMappings.get(name);
if (value instanceof Integer) {
return (Integer) value;
}
return Integer.valueOf(name.charAt(0));
}
/**
* Modifier lookup.
*
* @param name of the modifier
* @return the int
*/
public static int modifierLookup(final String name) {
final Object value = modifierMappings.get(name);
if (value instanceof Integer) {
return ((Integer) value).intValue();
}
return 0;
}
}