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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 2000-2014 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 com.google.gwt.core.client.Scheduler;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.user.client.Command;
  22. import com.google.gwt.user.client.DOM;
  23. import com.google.gwt.user.client.Event;
  24. import com.google.gwt.user.client.ui.HasWidgets;
  25. import com.google.gwt.user.client.ui.KeyboardListener;
  26. import com.google.gwt.user.client.ui.KeyboardListenerCollection;
  27. import com.vaadin.client.ApplicationConnection;
  28. import com.vaadin.client.BrowserInfo;
  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 {@link 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. /**
  56. * A focusable {@link ComponentConnector} implementing this interface will
  57. * be notified before shortcut actions are handled if it will be the target
  58. * of the action (most commonly means it is the focused component during the
  59. * keyboard combination is triggered by the user).
  60. */
  61. public interface BeforeShortcutActionListener extends ComponentConnector {
  62. /**
  63. * This method is called by ShortcutActionHandler before firing the
  64. * shortcut if the Paintable is currently focused (aka the target of the
  65. * shortcut action). Eg. a field can update its possibly changed value
  66. * to the server before shortcut action is fired.
  67. *
  68. * @param e
  69. * the event that triggered the shortcut action
  70. */
  71. public void onBeforeShortcutAction(Event e);
  72. }
  73. private final ArrayList<ShortcutAction> actions = new ArrayList<ShortcutAction>();
  74. private ApplicationConnection client;
  75. private String paintableId;
  76. /**
  77. *
  78. * @param pid
  79. * Paintable id
  80. * @param c
  81. * reference to application connections
  82. */
  83. public ShortcutActionHandler(String pid, ApplicationConnection c) {
  84. paintableId = pid;
  85. client = c;
  86. }
  87. /**
  88. * Updates list of actions this handler listens to.
  89. *
  90. * @param c
  91. * UIDL snippet containing actions
  92. */
  93. public void updateActionMap(UIDL c) {
  94. actions.clear();
  95. final Iterator<?> it = c.getChildIterator();
  96. while (it.hasNext()) {
  97. final UIDL action = (UIDL) it.next();
  98. int[] modifiers = null;
  99. if (action.hasAttribute("mk")) {
  100. modifiers = action.getIntArrayAttribute("mk");
  101. }
  102. final ShortcutKeyCombination kc = new ShortcutKeyCombination(
  103. action.getIntAttribute("kc"), modifiers);
  104. final String key = action.getStringAttribute("key");
  105. final String caption = action.getStringAttribute("caption");
  106. actions.add(new ShortcutAction(key, kc, caption));
  107. }
  108. }
  109. public void handleKeyboardEvent(final Event event, ComponentConnector target) {
  110. final int modifiers = KeyboardListenerCollection
  111. .getKeyboardModifiers(event);
  112. final char keyCode = (char) DOM.eventGetKeyCode(event);
  113. final ShortcutKeyCombination kc = new ShortcutKeyCombination(keyCode,
  114. modifiers);
  115. final Iterator<ShortcutAction> it = actions.iterator();
  116. while (it.hasNext()) {
  117. final ShortcutAction a = it.next();
  118. if (a.getShortcutCombination().equals(kc)) {
  119. fireAction(event, a, target);
  120. break;
  121. }
  122. }
  123. }
  124. public void handleKeyboardEvent(final Event event) {
  125. handleKeyboardEvent(event, null);
  126. }
  127. private void fireAction(final Event event, final ShortcutAction a,
  128. ComponentConnector target) {
  129. final Element et = DOM.eventGetTarget(event);
  130. if (target == null) {
  131. target = Util.findPaintable(client, et);
  132. }
  133. final ComponentConnector finalTarget = target;
  134. event.preventDefault();
  135. /*
  136. * The target component might have unpublished changes, try to
  137. * synchronize them before firing shortcut action.
  138. */
  139. if (finalTarget instanceof BeforeShortcutActionListener) {
  140. ((BeforeShortcutActionListener) finalTarget)
  141. .onBeforeShortcutAction(event);
  142. } else {
  143. shakeTarget(et);
  144. Scheduler.get().scheduleDeferred(new Command() {
  145. @Override
  146. public void execute() {
  147. shakeTarget(et);
  148. }
  149. });
  150. }
  151. Scheduler.get().scheduleDeferred(new Command() {
  152. @Override
  153. public void execute() {
  154. if (finalTarget != null) {
  155. client.updateVariable(paintableId, "actiontarget",
  156. finalTarget, false);
  157. }
  158. client.updateVariable(paintableId, "action", a.getKey(), true);
  159. }
  160. });
  161. }
  162. /**
  163. * We try to fire value change in the component the key combination was
  164. * typed. Eg. textfield may contain newly typed text that is expected to be
  165. * sent to server. This is done by removing focus and then returning it
  166. * immediately back to target element.
  167. * <p>
  168. * This is practically a hack and should be replaced with an interface
  169. * {@link BeforeShortcutActionListener} via widgets could be notified when
  170. * they should fire value change. Big task for TextFields, DateFields and
  171. * various selects.
  172. *
  173. * <p>
  174. * TODO separate opera impl with generator
  175. */
  176. private static void shakeTarget(final Element e) {
  177. blur(e);
  178. if (BrowserInfo.get().isOpera()) {
  179. // will mess up with focus and blur event if the focus is not
  180. // deferred. Will cause a small flickering, so not doing it for all
  181. // browsers.
  182. Scheduler.get().scheduleDeferred(new Command() {
  183. @Override
  184. public void execute() {
  185. focus(e);
  186. }
  187. });
  188. } else {
  189. focus(e);
  190. }
  191. }
  192. private static native void blur(Element e)
  193. /*-{
  194. if(e.blur) {
  195. e.blur();
  196. }
  197. }-*/;
  198. private static native void focus(Element e)
  199. /*-{
  200. if(e.blur) {
  201. e.focus();
  202. }
  203. }-*/;
  204. }
  205. class ShortcutKeyCombination {
  206. public static final int SHIFT = 16;
  207. public static final int CTRL = 17;
  208. public static final int ALT = 18;
  209. public static final int META = 91;
  210. char keyCode = 0;
  211. private int modifiersMask;
  212. public ShortcutKeyCombination() {
  213. }
  214. ShortcutKeyCombination(char kc, int modifierMask) {
  215. keyCode = kc;
  216. modifiersMask = modifierMask;
  217. }
  218. ShortcutKeyCombination(int kc, int[] modifiers) {
  219. keyCode = (char) kc;
  220. modifiersMask = 0;
  221. if (modifiers != null) {
  222. for (int i = 0; i < modifiers.length; i++) {
  223. switch (modifiers[i]) {
  224. case ALT:
  225. modifiersMask = modifiersMask
  226. | KeyboardListener.MODIFIER_ALT;
  227. break;
  228. case CTRL:
  229. modifiersMask = modifiersMask
  230. | KeyboardListener.MODIFIER_CTRL;
  231. break;
  232. case SHIFT:
  233. modifiersMask = modifiersMask
  234. | KeyboardListener.MODIFIER_SHIFT;
  235. break;
  236. case META:
  237. modifiersMask = modifiersMask
  238. | KeyboardListener.MODIFIER_META;
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. public boolean equals(ShortcutKeyCombination other) {
  247. if (keyCode == other.keyCode && modifiersMask == other.modifiersMask) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. }
  253. class ShortcutAction {
  254. private final ShortcutKeyCombination sc;
  255. private final String caption;
  256. private final String key;
  257. public ShortcutAction(String key, ShortcutKeyCombination sc, String caption) {
  258. this.sc = sc;
  259. this.key = key;
  260. this.caption = caption;
  261. }
  262. public ShortcutKeyCombination getShortcutCombination() {
  263. return sc;
  264. }
  265. public String getCaption() {
  266. return caption;
  267. }
  268. public String getKey() {
  269. return key;
  270. }
  271. }