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.

TableAndBrowserContextMenu.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.vaadin.tests.components.table;
  2. import com.vaadin.data.Item;
  3. import com.vaadin.data.Property.ValueChangeEvent;
  4. import com.vaadin.data.Property.ValueChangeListener;
  5. import com.vaadin.event.Action;
  6. import com.vaadin.event.ItemClickEvent;
  7. import com.vaadin.event.ItemClickEvent.ItemClickListener;
  8. import com.vaadin.server.ExternalResource;
  9. import com.vaadin.tests.components.TestBase;
  10. import com.vaadin.ui.CheckBox;
  11. import com.vaadin.ui.Component;
  12. import com.vaadin.ui.Link;
  13. import com.vaadin.ui.Table;
  14. public class TableAndBrowserContextMenu extends TestBase implements
  15. Action.Handler, ItemClickListener {
  16. private Table table;
  17. private boolean actionHandlerHasActions = false;
  18. @Override
  19. public void setup() {
  20. CheckBox cb = new CheckBox("Item click listener");
  21. cb.setImmediate(true);
  22. cb.addListener(new ValueChangeListener() {
  23. @Override
  24. public void valueChange(ValueChangeEvent event) {
  25. if (((Boolean) event.getProperty().getValue())) {
  26. table.addListener(TableAndBrowserContextMenu.this);
  27. } else {
  28. table.removeListener(TableAndBrowserContextMenu.this);
  29. }
  30. }
  31. });
  32. addComponent(cb);
  33. CheckBox cbActionHandler = new CheckBox("Action handler");
  34. cbActionHandler.setImmediate(true);
  35. cbActionHandler.addListener(new ValueChangeListener() {
  36. @Override
  37. public void valueChange(ValueChangeEvent event) {
  38. if (((Boolean) event.getProperty().getValue())) {
  39. table.addActionHandler(TableAndBrowserContextMenu.this);
  40. } else {
  41. table.removeActionHandler(TableAndBrowserContextMenu.this);
  42. }
  43. }
  44. });
  45. addComponent(cbActionHandler);
  46. CheckBox cbActionHasActions = new CheckBox("Action handler has actions");
  47. cbActionHasActions.setImmediate(true);
  48. cbActionHasActions.addListener(new ValueChangeListener() {
  49. @Override
  50. public void valueChange(ValueChangeEvent event) {
  51. actionHandlerHasActions = ((Boolean) event.getProperty()
  52. .getValue());
  53. // Workaround to ensure actions are repainted
  54. removeComponent(table);
  55. addComponent(table);
  56. }
  57. });
  58. addComponent(cbActionHasActions);
  59. createTable();
  60. addComponent(table);
  61. }
  62. private void createTable() {
  63. // Have a table with a numeric column
  64. table = new Table("A table");
  65. table.addContainerProperty("Name", String.class, null);
  66. table.addContainerProperty("Died At Age", Integer.class, null);
  67. // Add a generated column with a link to Google
  68. table.addGeneratedColumn("Search", new Table.ColumnGenerator() {
  69. @Override
  70. public Component generateCell(Table source, Object itemId,
  71. Object columnId) {
  72. Item item = source.getItem(itemId);
  73. String name = (String) item.getItemProperty("Name").getValue();
  74. return new Link("Google for " + name, new ExternalResource(
  75. "http://www.google.co.uk/search?q=" + name));
  76. }
  77. });
  78. // Insert some data
  79. Object people[][] = { { "Galileo", 77 }, { "Monnier", 83 },
  80. { "Vaisala", 79 }, { "Oterma", 86 } };
  81. for (int i = 0; i < people.length; i++) {
  82. table.addItem(people[i], i);
  83. }
  84. // Calculate the average of the numeric column
  85. double avgAge = 0;
  86. for (int i = 0; i < people.length; i++) {
  87. avgAge += (Integer) people[i][1];
  88. }
  89. avgAge /= people.length;
  90. // Set the footers
  91. table.setFooterVisible(true);
  92. table.setColumnFooter("Name", "Average");
  93. table.setColumnFooter("Died At Age", String.valueOf(avgAge));
  94. // Adjust the table height a bit
  95. table.setPageLength(table.size() + 2);
  96. for (int i = 0; i < people.length; i++) {
  97. Object[] person = people[i];
  98. String name = (String) person[0];
  99. addComponent(new Link("Google for " + name, new ExternalResource(
  100. "http://www.google.co.uk/search?q=" + name)));
  101. }
  102. }
  103. @Override
  104. protected String getDescription() {
  105. return "Table should only prevent the browser context menu when the right click is used for some Table specific operation. In practice these are either action handlers/context menu or item click listeners (right click). Note that item click listeners affects rows only, not the body.";
  106. }
  107. @Override
  108. protected Integer getTicketNumber() {
  109. return 5924;
  110. }
  111. @Override
  112. public void itemClick(ItemClickEvent event) {
  113. getMainWindow()
  114. .showNotification("Click using " + event.getButtonName());
  115. }
  116. @Override
  117. public Action[] getActions(Object target, Object sender) {
  118. if (!actionHandlerHasActions) {
  119. return null;
  120. }
  121. return new Action[] { new Action("test") };
  122. }
  123. @Override
  124. public void handleAction(Action action, Object sender, Object target) {
  125. getMainWindow().showNotification("Action: " + action.getCaption());
  126. }
  127. }