You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DesignShortcutActionConverter.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui.declarative.converters;
  17. import java.util.Collections;
  18. import java.util.HashMap;
  19. import java.util.Locale;
  20. import java.util.Map;
  21. import java.util.Map.Entry;
  22. import com.vaadin.data.Result;
  23. import com.vaadin.data.util.converter.Converter;
  24. import com.vaadin.event.ShortcutAction;
  25. import com.vaadin.event.ShortcutAction.KeyCode;
  26. import com.vaadin.event.ShortcutAction.ModifierKey;
  27. /**
  28. * Converter for {@link ShortcutActions}.
  29. *
  30. * @since 7.4
  31. * @author Vaadin Ltd
  32. */
  33. public class DesignShortcutActionConverter
  34. implements Converter<String, ShortcutAction> {
  35. private final Map<Integer, String> keyCodeMap;
  36. private final Map<String, Integer> presentationMap;
  37. public DesignShortcutActionConverter() {
  38. HashMap<Integer, String> codes = new HashMap<Integer, String>();
  39. // map modifiers
  40. codes.put(ModifierKey.ALT, "alt");
  41. codes.put(ModifierKey.CTRL, "ctrl");
  42. codes.put(ModifierKey.META, "meta");
  43. codes.put(ModifierKey.SHIFT, "shift");
  44. // map keys
  45. codes.put(KeyCode.ENTER, "enter");
  46. codes.put(KeyCode.ESCAPE, "escape");
  47. codes.put(KeyCode.PAGE_UP, "pageup");
  48. codes.put(KeyCode.PAGE_DOWN, "pagedown");
  49. codes.put(KeyCode.TAB, "tab");
  50. codes.put(KeyCode.ARROW_LEFT, "left");
  51. codes.put(KeyCode.ARROW_UP, "up");
  52. codes.put(KeyCode.ARROW_RIGHT, "right");
  53. codes.put(KeyCode.ARROW_DOWN, "down");
  54. codes.put(KeyCode.BACKSPACE, "backspace");
  55. codes.put(KeyCode.DELETE, "delete");
  56. codes.put(KeyCode.INSERT, "insert");
  57. codes.put(KeyCode.END, "end");
  58. codes.put(KeyCode.HOME, "home");
  59. codes.put(KeyCode.F1, "f1");
  60. codes.put(KeyCode.F2, "f2");
  61. codes.put(KeyCode.F3, "f3");
  62. codes.put(KeyCode.F4, "f4");
  63. codes.put(KeyCode.F5, "f5");
  64. codes.put(KeyCode.F6, "f6");
  65. codes.put(KeyCode.F7, "f7");
  66. codes.put(KeyCode.F8, "f8");
  67. codes.put(KeyCode.F9, "f9");
  68. codes.put(KeyCode.F10, "f10");
  69. codes.put(KeyCode.F11, "f11");
  70. codes.put(KeyCode.F12, "f12");
  71. codes.put(KeyCode.NUM0, "0");
  72. codes.put(KeyCode.NUM1, "1");
  73. codes.put(KeyCode.NUM2, "2");
  74. codes.put(KeyCode.NUM3, "3");
  75. codes.put(KeyCode.NUM4, "4");
  76. codes.put(KeyCode.NUM5, "5");
  77. codes.put(KeyCode.NUM6, "6");
  78. codes.put(KeyCode.NUM7, "7");
  79. codes.put(KeyCode.NUM8, "8");
  80. codes.put(KeyCode.NUM9, "9");
  81. codes.put(KeyCode.SPACEBAR, "spacebar");
  82. codes.put(KeyCode.A, "a");
  83. codes.put(KeyCode.B, "b");
  84. codes.put(KeyCode.C, "c");
  85. codes.put(KeyCode.D, "d");
  86. codes.put(KeyCode.E, "e");
  87. codes.put(KeyCode.F, "f");
  88. codes.put(KeyCode.G, "g");
  89. codes.put(KeyCode.H, "h");
  90. codes.put(KeyCode.I, "i");
  91. codes.put(KeyCode.J, "j");
  92. codes.put(KeyCode.K, "k");
  93. codes.put(KeyCode.L, "l");
  94. codes.put(KeyCode.M, "m");
  95. codes.put(KeyCode.N, "n");
  96. codes.put(KeyCode.O, "o");
  97. codes.put(KeyCode.P, "p");
  98. codes.put(KeyCode.Q, "q");
  99. codes.put(KeyCode.R, "r");
  100. codes.put(KeyCode.S, "s");
  101. codes.put(KeyCode.T, "t");
  102. codes.put(KeyCode.U, "u");
  103. codes.put(KeyCode.V, "v");
  104. codes.put(KeyCode.X, "x");
  105. codes.put(KeyCode.Y, "y");
  106. codes.put(KeyCode.Z, "z");
  107. keyCodeMap = Collections.unmodifiableMap(codes);
  108. HashMap<String, Integer> presentations = new HashMap<String, Integer>();
  109. for (Entry<Integer, String> entry : keyCodeMap.entrySet()) {
  110. presentations.put(entry.getValue(), entry.getKey());
  111. }
  112. presentationMap = Collections.unmodifiableMap(presentations);
  113. }
  114. @Override
  115. public Result<ShortcutAction> convertToModel(String value, Locale locale) {
  116. if (value.length() == 0) {
  117. return Result.ok(null);
  118. }
  119. String[] data = value.split(" ", 2);
  120. String[] parts = data[0].split("-");
  121. try {
  122. // handle keycode
  123. String keyCodePart = parts[parts.length - 1];
  124. int keyCode = getKeycodeForString(keyCodePart);
  125. if (keyCode < 0) {
  126. throw new IllegalArgumentException(
  127. "Invalid key '" + keyCodePart + "'");
  128. }
  129. // handle modifiers
  130. int[] modifiers = null;
  131. if (parts.length > 1) {
  132. modifiers = new int[parts.length - 1];
  133. }
  134. for (int i = 0; i < parts.length - 1; i++) {
  135. int modifier = getKeycodeForString(parts[i]);
  136. if (modifier > 0) {
  137. modifiers[i] = modifier;
  138. } else {
  139. throw new IllegalArgumentException(
  140. "Invalid modifier '" + parts[i] + "'");
  141. }
  142. }
  143. return Result.ok(new ShortcutAction(
  144. data.length == 2 ? data[1] : null, keyCode, modifiers));
  145. } catch (Exception e) {
  146. return Result.error("Invalid shortcut '" + value + "'");
  147. }
  148. }
  149. @Override
  150. public String convertToPresentation(ShortcutAction value, Locale locale) {
  151. StringBuilder sb = new StringBuilder();
  152. // handle modifiers
  153. if (value.getModifiers() != null) {
  154. for (int modifier : value.getModifiers()) {
  155. sb.append(getStringForKeycode(modifier)).append("-");
  156. }
  157. }
  158. // handle keycode
  159. sb.append(getStringForKeycode(value.getKeyCode()));
  160. if (value.getCaption() != null) {
  161. sb.append(" ").append(value.getCaption());
  162. }
  163. return sb.toString();
  164. }
  165. public int getKeycodeForString(String attributePresentation) {
  166. Integer code = presentationMap.get(attributePresentation);
  167. return code != null ? code.intValue() : -1;
  168. }
  169. public String getStringForKeycode(int keyCode) {
  170. return keyCodeMap.get(keyCode);
  171. }
  172. }