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.

ShortcutActionHandler.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2000-2018 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.client.ui;
  17. import java.util.ArrayList;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.dom.client.Element;
  20. import com.google.gwt.user.client.DOM;
  21. import com.google.gwt.user.client.Event;
  22. import com.google.gwt.user.client.ui.HasWidgets;
  23. import com.google.gwt.user.client.ui.KeyboardListener;
  24. import com.google.gwt.user.client.ui.KeyboardListenerCollection;
  25. import com.vaadin.client.ApplicationConnection;
  26. import com.vaadin.client.ComponentConnector;
  27. import com.vaadin.client.UIDL;
  28. import com.vaadin.client.Util;
  29. /**
  30. * A helper class to implement keyboard shortcut handling. Keeps a list of
  31. * owners actions and fires actions to server. User class needs to delegate
  32. * keyboard events to handleKeyboardEvents function.
  33. *
  34. * @author Vaadin Ltd
  35. */
  36. public class ShortcutActionHandler {
  37. /**
  38. * An interface implemented by those users of this helper class that want to
  39. * support special components like {@code VRichTextArea} that don't properly
  40. * propagate key down events. Those components can build support for
  41. * shortcut actions by traversing the closest
  42. * {@link ShortcutActionHandlerOwner} from the component hierarchy an
  43. * passing keydown events to {@link ShortcutActionHandler}.
  44. */
  45. public interface ShortcutActionHandlerOwner extends HasWidgets {
  46. /**
  47. * Returns the ShortCutActionHandler currently used or null if there is
  48. * currently no shortcutactionhandler.
  49. */
  50. ShortcutActionHandler getShortcutActionHandler();
  51. }
  52. private final ArrayList<ShortcutAction> actions = new ArrayList<>();
  53. private ApplicationConnection client;
  54. private String paintableId;
  55. /**
  56. *
  57. * @param pid
  58. * Paintable id
  59. * @param c
  60. * reference to application connections
  61. */
  62. public ShortcutActionHandler(String pid, ApplicationConnection c) {
  63. paintableId = pid;
  64. client = c;
  65. }
  66. /**
  67. * Updates list of actions this handler listens to.
  68. *
  69. * @param c
  70. * UIDL snippet containing actions
  71. */
  72. public void updateActionMap(UIDL c) {
  73. actions.clear();
  74. for (final Object child : c) {
  75. final UIDL action = (UIDL) child;
  76. int[] modifiers = null;
  77. if (action.hasAttribute("mk")) {
  78. modifiers = action.getIntArrayAttribute("mk");
  79. }
  80. final ShortcutKeyCombination kc = new ShortcutKeyCombination(
  81. action.getIntAttribute("kc"), modifiers);
  82. final String key = action.getStringAttribute("key");
  83. final String caption = action.getStringAttribute("caption");
  84. actions.add(new ShortcutAction(key, kc, caption));
  85. }
  86. }
  87. public void handleKeyboardEvent(final Event event,
  88. ComponentConnector target) {
  89. final char keyCode = (char) DOM.eventGetKeyCode(event);
  90. if (keyCode == 0) {
  91. return;
  92. }
  93. final int modifiers = KeyboardListenerCollection
  94. .getKeyboardModifiers(event);
  95. final ShortcutKeyCombination kc = new ShortcutKeyCombination(keyCode,
  96. modifiers);
  97. for (final ShortcutAction a : actions) {
  98. if (a.getShortcutCombination().equals(kc)) {
  99. fireAction(event, a, target);
  100. break;
  101. }
  102. }
  103. }
  104. public void handleKeyboardEvent(final Event event) {
  105. handleKeyboardEvent(event, null);
  106. }
  107. private void fireAction(final Event event, final ShortcutAction a,
  108. ComponentConnector target) {
  109. final Element et = DOM.eventGetTarget(event);
  110. if (target == null) {
  111. target = Util.findPaintable(client, et);
  112. }
  113. final ComponentConnector finalTarget = target;
  114. event.preventDefault();
  115. /*
  116. * The focused component might have unpublished changes, try to
  117. * synchronize them before firing shortcut action.
  118. */
  119. client.flushActiveConnector();
  120. /*
  121. * Legacy components don't have built-in logic for flushing, they need a
  122. * workaround with blur and focus to trigger the value change.
  123. */
  124. ComponentConnector activeConnector = getActiveConnector(client);
  125. if (activeConnector != null) {
  126. Class<?> clz = activeConnector.getClass();
  127. while (clz != null) {
  128. if (clz.getName().equals(
  129. "com.vaadin.v7.client.ui.AbstractLegacyComponentConnector")) {
  130. shakeTarget(et);
  131. Scheduler.get().scheduleDeferred(() -> {
  132. shakeTarget(et);
  133. });
  134. break;
  135. }
  136. clz = clz.getSuperclass();
  137. }
  138. }
  139. Scheduler.get().scheduleDeferred(() -> {
  140. if (finalTarget != null) {
  141. client.updateVariable(paintableId, "actiontarget", finalTarget,
  142. false);
  143. }
  144. client.updateVariable(paintableId, "action", a.getKey(), true);
  145. });
  146. }
  147. /**
  148. * We try to fire value change in the component the key combination was
  149. * typed. E.g. TextField may contain newly typed text that is expected to be
  150. * sent to server before the shortcut action is triggered. This is done by
  151. * removing focus and then returning it immediately back to target element.
  152. * <p>
  153. * This is a hack copied over from V7 in order to keep the compatibility
  154. * classes working. Main V8 classes don't require shaking.
  155. */
  156. private static void shakeTarget(final Element e) {
  157. blur(e);
  158. focus(e);
  159. }
  160. private static native ComponentConnector getActiveConnector(
  161. ApplicationConnection ac)
  162. /*-{
  163. return ac.@com.vaadin.client.ApplicationConnection::getActiveConnector()();
  164. }-*/;
  165. private static native void blur(Element e)
  166. /*-{
  167. if (e.blur) {
  168. e.blur();
  169. }
  170. }-*/;
  171. private static native void focus(Element e)
  172. /*-{
  173. if (e.blur) {
  174. e.focus();
  175. }
  176. }-*/;
  177. }
  178. class ShortcutKeyCombination {
  179. public static final int SHIFT = 16;
  180. public static final int CTRL = 17;
  181. public static final int ALT = 18;
  182. public static final int META = 91;
  183. char keyCode = 0;
  184. private int modifiersMask;
  185. public ShortcutKeyCombination() {
  186. }
  187. ShortcutKeyCombination(char kc, int modifierMask) {
  188. keyCode = kc;
  189. modifiersMask = modifierMask;
  190. }
  191. ShortcutKeyCombination(int kc, int[] modifiers) {
  192. keyCode = (char) kc;
  193. modifiersMask = 0;
  194. if (modifiers != null) {
  195. for (int modifier : modifiers) {
  196. switch (modifier) {
  197. case ALT:
  198. modifiersMask = modifiersMask
  199. | KeyboardListener.MODIFIER_ALT;
  200. break;
  201. case CTRL:
  202. modifiersMask = modifiersMask
  203. | KeyboardListener.MODIFIER_CTRL;
  204. break;
  205. case SHIFT:
  206. modifiersMask = modifiersMask
  207. | KeyboardListener.MODIFIER_SHIFT;
  208. break;
  209. case META:
  210. modifiersMask = modifiersMask
  211. | KeyboardListener.MODIFIER_META;
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. public boolean equals(ShortcutKeyCombination other) {
  220. if (keyCode == other.keyCode && modifiersMask == other.modifiersMask) {
  221. return true;
  222. }
  223. return false;
  224. }
  225. }
  226. class ShortcutAction {
  227. private final ShortcutKeyCombination sc;
  228. private final String caption;
  229. private final String key;
  230. public ShortcutAction(String key, ShortcutKeyCombination sc,
  231. String caption) {
  232. this.sc = sc;
  233. this.key = key;
  234. this.caption = caption;
  235. }
  236. public ShortcutKeyCombination getShortcutCombination() {
  237. return sc;
  238. }
  239. public String getCaption() {
  240. return caption;
  241. }
  242. public String getKey() {
  243. return key;
  244. }
  245. }