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.

WindowShouldRemoveActionHandler.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.vaadin.tests.components.window;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vaadin.event.Action;
  5. import com.vaadin.event.Action.Handler;
  6. import com.vaadin.event.ShortcutAction;
  7. import com.vaadin.event.ShortcutAction.ModifierKey;
  8. import com.vaadin.server.VaadinRequest;
  9. import com.vaadin.shared.ui.MarginInfo;
  10. import com.vaadin.tests.components.AbstractReindeerTestUI;
  11. import com.vaadin.ui.Button;
  12. import com.vaadin.ui.Button.ClickEvent;
  13. import com.vaadin.ui.Label;
  14. import com.vaadin.ui.Notification;
  15. import com.vaadin.v7.ui.TextField;
  16. public class WindowShouldRemoveActionHandler extends AbstractReindeerTestUI {
  17. @Override
  18. protected String getTestDescription() {
  19. return "Adding action handlers to the window should make them appear on the client side. Removing the action handlers should remove them also from the client side, also if all action handlers are removed.";
  20. }
  21. @Override
  22. protected Integer getTicketNumber() {
  23. return 2941;
  24. }
  25. private Label state;
  26. @Override
  27. protected void setup(VaadinRequest request) {
  28. getLayout().setMargin(new MarginInfo(true, false, false, false));
  29. state = new Label("An UI with no action handlers.");
  30. state.setId("state");
  31. addComponents(state, new TextField());
  32. addButton("Add an action handler", new Button.ClickListener() {
  33. @Override
  34. public void buttonClick(ClickEvent event) {
  35. add();
  36. }
  37. });
  38. addButton("Add another action handler", new Button.ClickListener() {
  39. @Override
  40. public void buttonClick(ClickEvent event) {
  41. addAnother();
  42. }
  43. });
  44. addButton("Remove an action handler", new Button.ClickListener() {
  45. @Override
  46. public void buttonClick(ClickEvent event) {
  47. remove();
  48. }
  49. });
  50. }
  51. public void remove() {
  52. state.setValue(state.getValue() + " - Removed handler");
  53. removeActionHandler(actionHandlers.remove(actionHandlers.size() - 1));
  54. }
  55. private List<Handler> actionHandlers = new ArrayList<>();
  56. public void add() {
  57. Handler actionHandler = new Handler() {
  58. @Override
  59. public Action[] getActions(Object target, Object sender) {
  60. return new Action[] { new ShortcutAction("Ctrl+Left",
  61. ShortcutAction.KeyCode.ARROW_LEFT,
  62. new int[] { ModifierKey.CTRL }) };
  63. }
  64. @Override
  65. public void handleAction(Action action, Object sender,
  66. Object target) {
  67. Notification.show("Handling action " + action.getCaption());
  68. }
  69. };
  70. addHandler(actionHandler);
  71. }
  72. public void addAnother() {
  73. Handler actionHandler = new Handler() {
  74. @Override
  75. public Action[] getActions(Object target, Object sender) {
  76. return new Action[] { new ShortcutAction("Ctrl+Right",
  77. ShortcutAction.KeyCode.ARROW_RIGHT,
  78. new int[] { ModifierKey.CTRL }) };
  79. }
  80. @Override
  81. public void handleAction(Action action, Object sender,
  82. Object target) {
  83. Notification.show("Handling action " + action.getCaption());
  84. }
  85. };
  86. addHandler(actionHandler);
  87. }
  88. private void addHandler(Handler actionHandler) {
  89. actionHandlers.add(actionHandler);
  90. addActionHandler(actionHandler);
  91. state.setValue(
  92. "An UI with " + actionHandlers.size() + " action handlers");
  93. }
  94. }