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.

QueryContainerDemo.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Label;
  10. import com.itmill.toolkit.ui.Select;
  11. import com.itmill.toolkit.ui.Table;
  12. import com.itmill.toolkit.ui.Tree;
  13. import com.itmill.toolkit.ui.Window;
  14. /**
  15. * This example shows how Table, Select and Tree UI components can use
  16. * Containers. QueryContainer is used to bind SQL table rows into Toolkit UI
  17. * components. Table has few example actions added. Also embedding XHTML through
  18. * Label components is used. Demonstrates: how to create
  19. * <code>com.itmill.toolkit.data.Container</code> and set it as datasource to
  20. * UI components <code>com.itmill.toolkit.ui.Component.Tree</code>, how to
  21. * receive ExpandEvent and implement
  22. * <code>com.itmill.toolkit.ui.Tree.ExpandListener</code>, how to use
  23. * <code>com.itmill.toolkit.event.Action</code>.
  24. *
  25. * @author IT Mill Ltd.
  26. * @since 4.0.0
  27. *
  28. */
  29. public class QueryContainerDemo extends com.itmill.toolkit.Application
  30. implements Action.Handler {
  31. private static final String ACTION_DESCRIPTION = "Try right mouse button to initiate "
  32. + "actions menu.<br />Note: on Opera you use meta key "
  33. + "and left mouse button.";
  34. private static final String TABLE_CAPTION = SampleDatabase.ROWCOUNT
  35. + " dynamically loaded rows from example SQL table";
  36. // Table component where SQL rows are attached (using QueryContainer)
  37. private final Table table = new Table();
  38. private final Label tableLastAction = new Label(
  39. "No action selected for table.");
  40. // Select component where SQL rows are attached (using QueryContainer)
  41. private final Select select = new Select();
  42. // Tree component that uses select as datasource
  43. private final Tree tree = new Tree();
  44. private final Label treeLastAction = new Label(
  45. "No action selected for tree.");
  46. // Database provided with sample data
  47. private SampleDatabase sampleDatabase;
  48. // Example Actions for table
  49. private final Action ACTION1 = new Action("Upload");
  50. private final Action ACTION2 = new Action("Download");
  51. private final Action ACTION3 = new Action("Show history");
  52. private final Action[] actions = new Action[] { ACTION1, ACTION2, ACTION3 };
  53. /**
  54. * Initialize Application. Demo components are added to main window.
  55. */
  56. public void init() {
  57. final Window main = new Window("QueryContainer demo");
  58. setMainWindow(main);
  59. // Main window contains heading, table, select and tree
  60. main
  61. .addComponent(new Label(
  62. "<h2>QueryContainer demo</h2>"
  63. + "<b>Rows are loaded from the server as they are needed.<br />"
  64. + "Try scrolling the table to see it in action.</b><br />"
  65. + ACTION_DESCRIPTION, Label.CONTENT_XHTML));
  66. main.addComponent(table);
  67. main.addComponent(tableLastAction);
  68. main.addComponent(new Label("<hr />", Label.CONTENT_XHTML));
  69. main.addComponent(select);
  70. main.addComponent(new Label("<hr />", Label.CONTENT_XHTML));
  71. main.addComponent(tree);
  72. main.addComponent(treeLastAction);
  73. // create demo database
  74. sampleDatabase = new SampleDatabase();
  75. // initialize demo components
  76. initTable();
  77. initSelect();
  78. initTree();
  79. }
  80. /**
  81. * Populates table component with all rows from employee table.
  82. *
  83. */
  84. private void initTable() {
  85. // init table
  86. table.setCaption(TABLE_CAPTION);
  87. table.setPageLength(10);
  88. table.setSelectable(true);
  89. table.setRowHeaderMode(Table.ROW_HEADER_MODE_INDEX);
  90. table.setColumnCollapsingAllowed(true);
  91. table.setColumnReorderingAllowed(true);
  92. table.setSelectable(true);
  93. // this class handles table actions (see handleActions method below)
  94. table.addActionHandler(this);
  95. table.setDescription(ACTION_DESCRIPTION);
  96. // populate Toolkit table component with test SQL table rows
  97. try {
  98. final QueryContainer qc = new QueryContainer(
  99. "SELECT * FROM employee", sampleDatabase.getConnection());
  100. table.setContainerDataSource(qc);
  101. } catch (final SQLException e) {
  102. e.printStackTrace();
  103. }
  104. // define which columns should be visible on Table component
  105. table.setVisibleColumns(new Object[] { "FIRSTNAME", "LASTNAME",
  106. "TITLE", "UNIT" });
  107. table.setItemCaptionPropertyId("ID");
  108. }
  109. /**
  110. * Populates select component with distinct unit values from employee table.
  111. *
  112. */
  113. private void initSelect() {
  114. // init select
  115. select.setCaption("All distinct units from employee table.");
  116. select.setItemCaptionPropertyId("UNIT");
  117. // populate Toolkit select component with test SQL table rows
  118. try {
  119. final QueryContainer qc = new QueryContainer(
  120. "SELECT DISTINCT UNIT FROM employee", sampleDatabase
  121. .getConnection());
  122. select.setContainerDataSource(qc);
  123. } catch (final SQLException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. /**
  128. * Populates tree component using select component as data source for root
  129. * nodes, child nodes are queried from database. Implementation is done for
  130. * example purposes only.
  131. *
  132. */
  133. private void initTree() {
  134. // init tree
  135. tree.setCaption("All distinct units from employee table.");
  136. tree.setItemCaptionPropertyId("UNIT");
  137. tree.setSelectable(true);
  138. // this class handles tree actions (see handleActions method below)
  139. tree.addActionHandler(this);
  140. tree.setDescription("Try right mouse button to initiate "
  141. + "actions menu. Note: on Opera you use meta key "
  142. + "and left mouse button.");
  143. // Populate Toolkit Tree using select component as data source
  144. tree.setContainerDataSource(select.getContainerDataSource());
  145. }
  146. /**
  147. * Return example actions
  148. */
  149. public Action[] getActions(Object target, Object sender) {
  150. return actions;
  151. }
  152. /**
  153. * Executed by right mouse button on table or tree component.
  154. */
  155. public void handleAction(Action action, Object sender, Object target) {
  156. if (sender == table) {
  157. tableLastAction.setValue("Last action clicked was '"
  158. + action.getCaption() + "' on item " + target);
  159. } else if (sender == tree) {
  160. treeLastAction.setValue("Last action clicked was '"
  161. + action.getCaption() + "' on item " + target);
  162. }
  163. }
  164. }