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.

HeaderPositionWhenSorting.java 5.4KB

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