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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. Scheduler.get().scheduleDeferred(() -> {
  121. if (finalTarget != null) {
  122. client.updateVariable(paintableId, "actiontarget", finalTarget,
  123. false);
  124. }
  125. client.updateVariable(paintableId, "action", a.getKey(), true);
  126. });
  127. }
  128. private static native void blur(Element e)
  129. /*-{
  130. if (e.blur) {
  131. e.blur();
  132. }
  133. }-*/;
  134. private static native void focus(Element e)
  135. /*-{
  136. if (e.blur) {
  137. e.focus();
  138. }
  139. }-*/;
  140. }
  141. class ShortcutKeyCombination {
  142. public static final int SHIFT = 16;
  143. public static final int CTRL = 17;
  144. public static final int ALT = 18;
  145. public static final int META = 91;
  146. char keyCode = 0;
  147. private int modifiersMask;
  148. public ShortcutKeyCombination() {
  149. }
  150. ShortcutKeyCombination(char kc, int modifierMask) {
  151. keyCode = kc;
  152. modifiersMask = modifierMask;
  153. }
  154. ShortcutKeyCombination(int kc, int[] modifiers) {
  155. keyCode = (char) kc;
  156. modifiersMask = 0;
  157. if (modifiers != null) {
  158. for (int modifier : modifiers) {
  159. switch (modifier) {
  160. case ALT:
  161. modifiersMask = modifiersMask
  162. | KeyboardListener.MODIFIER_ALT;
  163. break;
  164. case CTRL:
  165. modifiersMask = modifiersMask
  166. | KeyboardListener.MODIFIER_CTRL;
  167. break;
  168. case SHIFT:
  169. modifiersMask = modifiersMask
  170. | KeyboardListener.MODIFIER_SHIFT;
  171. break;
  172. case META:
  173. modifiersMask = modifiersMask
  174. | KeyboardListener.MODIFIER_META;
  175. break;
  176. default:
  177. break;
  178. }
  179. }
  180. }
  181. }
  182. public boolean equals(ShortcutKeyCombination other) {
  183. if (keyCode == other.keyCode && modifiersMask == other.modifiersMask) {
  184. return true;
  185. }
  186. return false;
  187. }
  188. }
  189. class ShortcutAction {
  190. private final ShortcutKeyCombination sc;
  191. private final String caption;
  192. private final String key;
  193. public ShortcutAction(String key, ShortcutKeyCombination sc,
  194. String caption) {
  195. this.sc = sc;
  196. this.key = key;
  197. this.caption = caption;
  198. }
  199. public ShortcutKeyCombination getShortcutCombination() {
  200. return sc;
  201. }
  202. public String getCaption() {
  203. return caption;
  204. }
  205. public String getKey() {
  206. return key;
  207. }
  208. }