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.

TableScrollingWithSQLContainer.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.vaadin.tests.components.table;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import com.vaadin.data.util.sqlcontainer.SQLContainer;
  6. import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
  7. import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
  8. import com.vaadin.data.util.sqlcontainer.query.QueryDelegate;
  9. import com.vaadin.data.util.sqlcontainer.query.TableQuery;
  10. import com.vaadin.data.util.sqlcontainer.query.generator.DefaultSQLGenerator;
  11. import com.vaadin.server.VaadinRequest;
  12. import com.vaadin.ui.Button;
  13. import com.vaadin.ui.Button.ClickEvent;
  14. import com.vaadin.ui.Button.ClickListener;
  15. import com.vaadin.ui.Table;
  16. import com.vaadin.ui.UI;
  17. import com.vaadin.ui.VerticalLayout;
  18. @SuppressWarnings("serial")
  19. public class TableScrollingWithSQLContainer extends UI {
  20. /** Table should never end up calling indexOfId in this case */
  21. private class LimitedSQLContainer extends SQLContainer {
  22. public LimitedSQLContainer(QueryDelegate delegate) throws SQLException {
  23. super(delegate);
  24. }
  25. @Override
  26. public int indexOfId(Object itemId) {
  27. throw new RuntimeException("This function should not be called");
  28. }
  29. }
  30. private void generateTestData(JDBCConnectionPool connectionPool)
  31. throws SQLException {
  32. Connection conn = connectionPool.reserveConnection();
  33. Statement statement = conn.createStatement();
  34. try {
  35. statement.execute("drop table PEOPLE");
  36. } catch (SQLException e) {
  37. // Will fail if table doesn't exist, which is OK.
  38. conn.rollback();
  39. }
  40. statement
  41. .execute("create table people (id integer generated always as identity,"
  42. + " name varchar(32), AGE INTEGER)");
  43. statement.execute("alter table people add primary key (id)");
  44. for (int i = 0; i < 5000; i++) {
  45. statement
  46. .executeUpdate("insert into people values(default, 'Person "
  47. + i + "', '" + i % 99 + "')");
  48. }
  49. statement.close();
  50. conn.commit();
  51. connectionPool.releaseConnection(conn);
  52. }
  53. static final String TABLE = "table";
  54. @Override
  55. public void init(VaadinRequest request) {
  56. try {
  57. SimpleJDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool(
  58. "org.hsqldb.jdbc.JDBCDriver",
  59. "jdbc:hsqldb:mem:sqlcontainer", "SA", "", 2, 20);
  60. generateTestData(connectionPool);
  61. TableQuery query = new TableQuery("people", connectionPool,
  62. new DefaultSQLGenerator());
  63. SQLContainer container = new LimitedSQLContainer(query);
  64. final VerticalLayout rootLayout = new VerticalLayout();
  65. final Table table = new Table();
  66. table.setContainerDataSource(container);
  67. table.setCurrentPageFirstItemIndex(300);
  68. rootLayout.addComponent(table);
  69. table.setImmediate(true);
  70. rootLayout.addComponent(new Button("GOTO 200", new ClickListener() {
  71. @Override
  72. public void buttonClick(ClickEvent event) {
  73. table.setCurrentPageFirstItemIndex(200);
  74. }
  75. }));
  76. setContent(rootLayout);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. }