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.

KeyboardShortcut.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo;
  5. import java.util.Date;
  6. import com.itmill.toolkit.Application;
  7. import com.itmill.toolkit.event.Action;
  8. import com.itmill.toolkit.event.ShortcutAction;
  9. import com.itmill.toolkit.event.Action.Handler;
  10. import com.itmill.toolkit.ui.Button;
  11. import com.itmill.toolkit.ui.ExpandLayout;
  12. import com.itmill.toolkit.ui.Label;
  13. import com.itmill.toolkit.ui.OrderedLayout;
  14. import com.itmill.toolkit.ui.Panel;
  15. import com.itmill.toolkit.ui.TextField;
  16. import com.itmill.toolkit.ui.Window;
  17. /**
  18. * Test application for KeyboardShortcuts
  19. */
  20. public class KeyboardShortcut extends Application implements Handler {
  21. private OrderedLayout loki;
  22. private final Label instructions = new Label(
  23. "<p>Keyboard shortcuts is a must have feature for applications in a "
  24. + "daily use. In IT Mill toolkit shortcuts are binded to "
  25. + "Panel and its subclasses like Windows (most common place)."
  26. + "</p>"
  27. + "<p>Browsers reserve some keyboard combinations for their own"
  28. + " actions, so all combinations cannot be used in web "
  29. + "applications. (see our article on <a href=\"http://www.itmill"
  30. + ".com/articles/Keybindings_in_Web_Browsers.htm\">"
  31. + "www.itmill.com)</a></p>"
  32. + "Events are attached to Window in this application, so a "
  33. + "component inside window must be focused to fire event on"
  34. + " keyboard shortcut.</p>"
  35. + "<strong>Shortcuts used in this example:</strong> "
  36. + "<br/>ESC restarts program, ctrl-shift-a (Button A), "
  37. + "ctrl-shift-z (Button Z), ctrl-shift-x (Button X)",
  38. Label.CONTENT_XHTML);
  39. private final Action ACTION_A = new ShortcutAction("Button a action",
  40. ShortcutAction.KeyCode.A, new int[] {
  41. ShortcutAction.ModifierKey.CTRL,
  42. ShortcutAction.ModifierKey.SHIFT });
  43. private final Action ACTION_Z = new ShortcutAction("Button z action",
  44. ShortcutAction.KeyCode.Z, new int[] {
  45. ShortcutAction.ModifierKey.CTRL,
  46. ShortcutAction.ModifierKey.SHIFT });
  47. private final Action ACTION_X = new ShortcutAction("Button x action",
  48. ShortcutAction.KeyCode.X, new int[] {
  49. ShortcutAction.ModifierKey.CTRL,
  50. ShortcutAction.ModifierKey.SHIFT });
  51. private final Action ACTION_RESTART = new ShortcutAction("Restart ",
  52. ShortcutAction.KeyCode.ESCAPE, null);
  53. private final Action[] actions = new Action[] { ACTION_A, ACTION_Z,
  54. ACTION_X, ACTION_RESTART };
  55. private TextField f;
  56. public void init() {
  57. final Window w = new Window("Keyboard shortcuts demo");
  58. final ExpandLayout main = new ExpandLayout();
  59. main.setMargin(true);
  60. main.setSpacing(true);
  61. setMainWindow(w);
  62. w.setLayout(main);
  63. final Panel p = new Panel("Test application for shortcut actions");
  64. p.addComponent(instructions);
  65. final OrderedLayout buttons = new OrderedLayout(
  66. OrderedLayout.ORIENTATION_HORIZONTAL);
  67. // Restart button
  68. final Button close = new Button("restart", this, "close");
  69. final Button a = new Button("Button A", this, "actionAHandler");
  70. final Button z = new Button("Button Z", this, "actionZHandler");
  71. final Button x = new Button("Button X", this, "actionXHandler");
  72. f = new TextField();
  73. buttons.addComponent(close);
  74. buttons.addComponent(a);
  75. buttons.addComponent(z);
  76. buttons.addComponent(x);
  77. buttons.addComponent(f);
  78. p.addComponent(buttons);
  79. main.addComponent(p);
  80. loki = new OrderedLayout();
  81. main.addComponent(loki);
  82. main.expand(loki);
  83. w.addActionHandler(this);
  84. f.focus();
  85. }
  86. public Action[] getActions(Object target, Object sender) {
  87. return actions;
  88. }
  89. public void handleAction(Action action, Object sender, Object target) {
  90. if (action == ACTION_A) {
  91. actionAHandler();
  92. }
  93. if (action == ACTION_Z) {
  94. actionZHandler();
  95. }
  96. if (action == ACTION_X) {
  97. actionXHandler();
  98. }
  99. if (action == ACTION_RESTART) {
  100. actionRestartHandler();
  101. }
  102. }
  103. public void actionAHandler() {
  104. log("Button A handler fired");
  105. }
  106. public void actionZHandler() {
  107. log("Button Z handler fired");
  108. }
  109. public void actionXHandler() {
  110. log("Button X handler fired");
  111. }
  112. public void actionRestartHandler() {
  113. close();
  114. }
  115. public void log(String s) {
  116. loki.addComponentAsFirst(new Label(new Date() + " : " + s));
  117. }
  118. }