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.

FocusShortcut.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.event;
  17. import com.vaadin.ui.Component.Focusable;
  18. /**
  19. * A ready-made {@link ShortcutListener} that focuses the given
  20. * {@link Focusable} (usually a {@link Field}) when the keyboard shortcut is
  21. * invoked.
  22. *
  23. * @author Vaadin Ltd
  24. * @since
  25. */
  26. public class FocusShortcut extends ShortcutListener {
  27. protected Focusable focusable;
  28. /**
  29. * Creates a keyboard shortcut for focusing the given {@link Focusable}
  30. * using the shorthand notation defined in {@link ShortcutAction}.
  31. *
  32. * @param focusable
  33. * to focused when the shortcut is invoked
  34. * @param shorthandCaption
  35. * caption with keycode and modifiers indicated
  36. */
  37. public FocusShortcut(Focusable focusable, String shorthandCaption) {
  38. super(shorthandCaption);
  39. this.focusable = focusable;
  40. }
  41. /**
  42. * Creates a keyboard shortcut for focusing the given {@link Focusable}.
  43. *
  44. * @param focusable
  45. * to focused when the shortcut is invoked
  46. * @param keyCode
  47. * keycode that invokes the shortcut
  48. * @param modifiers
  49. * modifiers required to invoke the shortcut
  50. */
  51. public FocusShortcut(Focusable focusable, int keyCode, int... modifiers) {
  52. super(null, keyCode, modifiers);
  53. this.focusable = focusable;
  54. }
  55. /**
  56. * Creates a keyboard shortcut for focusing the given {@link Focusable}.
  57. *
  58. * @param focusable
  59. * to focused when the shortcut is invoked
  60. * @param keyCode
  61. * keycode that invokes the shortcut
  62. */
  63. public FocusShortcut(Focusable focusable, int keyCode) {
  64. this(focusable, keyCode, null);
  65. }
  66. @Override
  67. public void handleAction(Object sender, Object target) {
  68. focusable.focus();
  69. }
  70. }