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.

TableDemo.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo;
  5. import java.sql.SQLException;
  6. import com.itmill.toolkit.data.util.QueryContainer;
  7. import com.itmill.toolkit.demo.util.SampleDatabase;
  8. import com.itmill.toolkit.event.Action;
  9. import com.itmill.toolkit.ui.Button;
  10. import com.itmill.toolkit.ui.Label;
  11. import com.itmill.toolkit.ui.OrderedLayout;
  12. import com.itmill.toolkit.ui.Table;
  13. import com.itmill.toolkit.ui.Window;
  14. /**
  15. * Similar to QueryContainerDemo
  16. *
  17. * @author IT Mill Ltd.
  18. * @since 4.0.0
  19. *
  20. */
  21. public class TableDemo extends com.itmill.toolkit.Application implements
  22. Action.Handler {
  23. private static final String ACTION_DESCRIPTION = "Use right mouse button to initiate "
  24. + "actions menu.<br />Note: on Opera use meta key "
  25. + "and left mouse button.";
  26. private static final String TABLE_CAPTION = SampleDatabase.ROWCOUNT
  27. + " dynamically loaded rows from example SQL table";
  28. // Table component where SQL rows are attached (using QueryContainer)
  29. private final Table table = new Table();
  30. // Label which displays last performed action against table row
  31. private final Label tableLastAction = new Label(
  32. "No action selected for table.");
  33. // Database provided with sample data
  34. private SampleDatabase sampleDatabase;
  35. // Example Actions for table
  36. private final Action ACTION1 = new Action("Upload");
  37. private final Action ACTION2 = new Action("Download");
  38. private final Action ACTION3 = new Action("Show history");
  39. private final Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };
  40. // Button which is used to disable or enable table
  41. // note: when button click event occurs, tableEnabler() method is called
  42. private final Button tableEnabler = new Button("Disable table", this,
  43. "tableEnabler");
  44. // Button which is used to hide or show table
  45. // note: when button click event occurs, tableVisibility() method is called
  46. private final Button tableVisibility = new Button("Hide table", this,
  47. "tableVisibility");
  48. // Button which is used to hide or show table
  49. // note: when button click event occurs, tableVisibility() method is called
  50. private final Button tableCaption = new Button("Hide caption", this,
  51. "tableCaption");
  52. /**
  53. * Initialize Application. Demo components are added to main window.
  54. */
  55. public void init() {
  56. final Window main = new Window("Table demo");
  57. setMainWindow(main);
  58. // create demo database
  59. sampleDatabase = new SampleDatabase();
  60. // Main window contains heading, two buttons, table and label
  61. main
  62. .addComponent(new Label(
  63. "<h2>Table demo</h2>"
  64. + "<b>Rows are loaded from the server as they are needed.<br />"
  65. + "Try scrolling the table to see it in action.</b><br />"
  66. + ACTION_DESCRIPTION, Label.CONTENT_XHTML));
  67. final OrderedLayout layout = new OrderedLayout(
  68. OrderedLayout.ORIENTATION_HORIZONTAL);
  69. // TODO: disabled until #655 fixed
  70. // layout.addComponent(tableVisibility);
  71. layout.addComponent(tableEnabler);
  72. layout.addComponent(tableCaption);
  73. main.addComponent(layout);
  74. main.addComponent(table);
  75. main.addComponent(tableLastAction);
  76. // initialize demo components
  77. initTable();
  78. }
  79. /**
  80. * Populates table component with all rows from employee table.
  81. *
  82. */
  83. private void initTable() {
  84. // init table
  85. table.setCaption(TABLE_CAPTION);
  86. table.setPageLength(10);
  87. table.setSelectable(true);
  88. table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
  89. table.setColumnCollapsingAllowed(true);
  90. table.setColumnReorderingAllowed(true);
  91. table.setSelectable(true);
  92. // this class handles table actions (see handleActions method below)
  93. table.addActionHandler(this);
  94. table.setDescription(ACTION_DESCRIPTION);
  95. // populate Toolkit table component with test SQL table rows
  96. try {
  97. final QueryContainer qc = new QueryContainer(
  98. "SELECT * FROM employee", sampleDatabase.getConnection());
  99. table.setContainerDataSource(qc);
  100. } catch (final SQLException e) {
  101. e.printStackTrace();
  102. }
  103. // define which columns should be visible on Table component
  104. table.setVisibleColumns(new Object[] { "FIRSTNAME", "LASTNAME",
  105. "TITLE", "UNIT" });
  106. table.setItemCaptionPropertyId("ID");
  107. }
  108. public void tableVisibility() {
  109. if (table.isVisible()) {
  110. tableVisibility.setCaption("Show table");
  111. table.setVisible(false);
  112. tableEnabler.setEnabled(false);
  113. tableCaption.setEnabled(false);
  114. } else {
  115. tableVisibility.setCaption("Hide table");
  116. table.setVisible(true);
  117. tableEnabler.setEnabled(true);
  118. tableCaption.setEnabled(true);
  119. }
  120. }
  121. public void tableEnabler() {
  122. if (table.isEnabled()) {
  123. tableEnabler.setCaption("Enable table");
  124. table.setEnabled(false);
  125. } else {
  126. tableEnabler.setCaption("Disable table");
  127. table.setEnabled(true);
  128. }
  129. }
  130. public void tableCaption() {
  131. if (table.getCaption().equals("")) {
  132. table.setCaption(TABLE_CAPTION);
  133. tableCaption.setCaption("Hide caption");
  134. } else {
  135. table.setCaption("");
  136. tableCaption.setCaption("Show caption");
  137. }
  138. }
  139. /**
  140. * URIHandler Return example actions
  141. */
  142. public Action[] getActions(Object target, Object sender) {
  143. return actions;
  144. }
  145. /**
  146. * Executed by right mouse button on table or tree component.
  147. */
  148. public void handleAction(Action action, Object sender, Object target) {
  149. if (sender == table) {
  150. tableLastAction.setValue("Last action clicked was '"
  151. + action.getCaption() + "' on item " + target);
  152. }
  153. }
  154. }