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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.client.ui;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import com.google.gwt.core.client.Scheduler;
  21. import com.google.gwt.dom.client.Element;
  22. import com.google.gwt.user.client.Command;
  23. import com.google.gwt.user.client.DOM;
  24. import com.google.gwt.user.client.Event;
  25. import com.google.gwt.user.client.ui.HasWidgets;
  26. import com.google.gwt.user.client.ui.KeyboardListener;
  27. import com.google.gwt.user.client.ui.KeyboardListenerCollection;
  28. import com.vaadin.client.ApplicationConnection;
  29. import com.vaadin.client.ComponentConnector;
  30. import com.vaadin.client.UIDL;
  31. import com.vaadin.client.Util;
  32. /**
  33. * A helper class to implement keyboard shorcut handling. Keeps a list of owners
  34. * actions and fires actions to server. User class needs to delegate keyboard
  35. * events to handleKeyboardEvents function.
  36. *
  37. * @author Vaadin Ltd
  38. */
  39. public class ShortcutActionHandler {
  40. /**
  41. * An interface implemented by those users of this helper class that want to
  42. * support special components like {@code VRichTextArea} that don't properly
  43. * propagate key down events. Those components can build support for
  44. * shortcut actions by traversing the closest
  45. * {@link ShortcutActionHandlerOwner} from the component hierarchy an
  46. * passing keydown events to {@link ShortcutActionHandler}.
  47. */
  48. public interface ShortcutActionHandlerOwner extends HasWidgets {
  49. /**
  50. * Returns the ShortCutActionHandler currently used or null if there is
  51. * currently no shortcutactionhandler
  52. */
  53. ShortcutActionHandler getShortcutActionHandler();
  54. }
  55. private final List<ShortcutAction> actions = new ArrayList<>();
  56. private ApplicationConnection client;
  57. private String paintableId;
  58. /**
  59. *
  60. * @param pid
  61. * Paintable id
  62. * @param c
  63. * reference to application connections
  64. */
  65. public ShortcutActionHandler(String pid, ApplicationConnection c) {
  66. paintableId = pid;
  67. client = c;
  68. }
  69. /**
  70. * Updates list of actions this handler listens to.
  71. *
  72. * @param c
  73. * UIDL snippet containing actions
  74. */
  75. public void updateActionMap(UIDL c) {
  76. actions.clear();
  77. for (final Object child : c) {
  78. final UIDL action = (UIDL) child;
  79. int[] modifiers = null;
  80. if (action.hasAttribute("mk")) {
  81. modifiers = action.getIntArrayAttribute("mk");
  82. }
  83. final ShortcutKeyCombination kc = new ShortcutKeyCombination(
  84. action.getIntAttribute("kc"), modifiers);
  85. final String key = action.getStringAttribute("key");
  86. final String caption = action.getStringAttribute("caption");
  87. actions.add(new ShortcutAction(key, kc, caption));
  88. }
  89. }
  90. public void handleKeyboardEvent(final Event event,
  91. ComponentConnector target) {
  92. final int modifiers = KeyboardListenerCollection
  93. .getKeyboardModifiers(event);
  94. final char keyCode = (char) DOM.eventGetKeyCode(event);
  95. if (keyCode == 0) {
  96. return;
  97. }
  98. final ShortcutKeyCombination kc = new ShortcutKeyCombination(keyCode,
  99. modifiers);
  100. final Iterator<ShortcutAction> it = actions.iterator();
  101. while (it.hasNext()) {
  102. final ShortcutAction a = it.next();
  103. if (a.getShortcutCombination().equals(kc)) {
  104. fireAction(event, a, target);
  105. break;
  106. }
  107. }
  108. }
  109. public void handleKeyboardEvent(final Event event) {
  110. handleKeyboardEvent(event, null);
  111. }
  112. private void fireAction(final Event event, final ShortcutAction a,
  113. ComponentConnector target) {
  114. final Element et = DOM.eventGetTarget(event);
  115. if (target == null) {
  116. target = Util.findPaintable(client, et);
  117. }
  118. final ComponentConnector finalTarget = target;
  119. event.preventDefault();
  120. /*
  121. * The focused component might have unpublished changes, try to
  122. * synchronize them before firing shortcut action.
  123. */
  124. client.flushActiveConnector();
  125. Scheduler.get().scheduleDeferred(new Command() {
  126. @Override
  127. public void execute() {
  128. if (finalTarget != null) {
  129. client.updateVariable(paintableId, "actiontarget",
  130. finalTarget, false);
  131. }
  132. client.updateVariable(paintableId, "action", a.getKey(), true);
  133. }
  134. });
  135. }
  136. private static native void blur(Element e)
  137. /*-{
  138. if(e.blur) {
  139. e.blur();
  140. }
  141. }-*/;
  142. private static native void focus(Element e)
  143. /*-{
  144. if(e.blur) {
  145. e.focus();
  146. }
  147. }-*/;
  148. }
  149. class ShortcutKeyCombination {
  150. public static final int SHIFT = 16;
  151. public static final int CTRL = 17;
  152. public static final int ALT = 18;
  153. public static final int META = 91;
  154. char keyCode = 0;
  155. private int modifiersMask;
  156. public ShortcutKeyCombination() {
  157. }
  158. ShortcutKeyCombination(char kc, int modifierMask) {
  159. keyCode = kc;
  160. modifiersMask = modifierMask;
  161. }
  162. ShortcutKeyCombination(int kc, int[] modifiers) {
  163. keyCode = (char) kc;
  164. modifiersMask = 0;
  165. if (modifiers != null) {
  166. for (int i = 0; i < modifiers.length; i++) {
  167. switch (modifiers[i]) {
  168. case ALT:
  169. modifiersMask = modifiersMask
  170. | KeyboardListener.MODIFIER_ALT;
  171. break;
  172. case CTRL:
  173. modifiersMask = modifiersMask
  174. | KeyboardListener.MODIFIER_CTRL;
  175. break;
  176. case SHIFT:
  177. modifiersMask = modifiersMask
  178. | KeyboardListener.MODIFIER_SHIFT;
  179. break;
  180. case META:
  181. modifiersMask = modifiersMask
  182. | KeyboardListener.MODIFIER_META;
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. public boolean equals(ShortcutKeyCombination other) {
  191. if (keyCode == other.keyCode && modifiersMask == other.modifiersMask) {
  192. return true;
  193. }
  194. return false;
  195. }
  196. }
  197. class ShortcutAction {
  198. private final ShortcutKeyCombination sc;
  199. private final String caption;
  200. private final String key;
  201. public ShortcutAction(String key, ShortcutKeyCombination sc,
  202. String caption) {
  203. this.sc = sc;
  204. this.key = key;
  205. this.caption = caption;
  206. }
  207. public ShortcutKeyCombination getShortcutCombination() {
  208. return sc;
  209. }
  210. public String getCaption() {
  211. return caption;
  212. }
  213. public String getKey() {
  214. return key;
  215. }
  216. }