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.

CalendarDemo.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.itmill.toolkit.demo;
  2. import java.sql.SQLException;
  3. import java.util.Date;
  4. import com.itmill.toolkit.data.Property.ValueChangeEvent;
  5. import com.itmill.toolkit.data.Property.ValueChangeListener;
  6. import com.itmill.toolkit.data.util.QueryContainer;
  7. import com.itmill.toolkit.demo.util.SampleCalendarDatabase;
  8. import com.itmill.toolkit.ui.CalendarField;
  9. import com.itmill.toolkit.ui.OrderedLayout;
  10. import com.itmill.toolkit.ui.Window;
  11. /**
  12. * This example shows how the CalendarField can use Containers. A QueryContainer
  13. * is used to bind SQL table rows to the calendar. Demonstrates: how to create
  14. * <code>com.itmill.toolkit.data.Container</code> and set it as datasource for
  15. * <code>com.itmill.toolkit.ui.Component.CalendarField</code>
  16. *
  17. * @author IT Mill Ltd.
  18. * @since 4.0.0
  19. *
  20. */
  21. public class CalendarDemo extends com.itmill.toolkit.Application {
  22. // Database provided with sample data
  23. private SampleCalendarDatabase sampleDatabase;
  24. // The calendar UI component
  25. private CalendarField from;
  26. private CalendarField to;
  27. /**
  28. * Initialize Application. Demo components are added to main window.
  29. */
  30. public void init() {
  31. Window main = new Window("Calendar demo");
  32. setMainWindow(main);
  33. main.setLayout(new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL));
  34. // set the application to use Corporate -theme
  35. setTheme("corporate");
  36. // create the calendar component and add to layout
  37. from = new CalendarField();
  38. main.addComponent(from);
  39. from.setResolution(CalendarField.RESOLUTION_HOUR);
  40. from.setImmediate(true);
  41. to = new CalendarField();
  42. main.addComponent(to);
  43. to.setResolution(CalendarField.RESOLUTION_HOUR);
  44. to.setEnabled(false);
  45. to.setImmediate(true);
  46. from.addListener(new ValueChangeListener() {
  47. public void valueChange(ValueChangeEvent event) {
  48. Date fd = (Date) from.getValue();
  49. Date td = (Date) to.getValue();
  50. if (fd == null) {
  51. to.setValue(null);
  52. to.setEnabled(false);
  53. return;
  54. } else {
  55. to.setEnabled(true);
  56. }
  57. to.setMinimumDate(fd);
  58. if (td == null || td.before(fd)) {
  59. to.setValue(fd);
  60. }
  61. }
  62. });
  63. // initialize the sample database and set as calendar datasource
  64. sampleDatabase = new SampleCalendarDatabase();
  65. initCalendars();
  66. // Don't allow dates before today
  67. from.setMinimumDate(new Date());
  68. }
  69. /**
  70. * Populates table component with all rows from calendar table.
  71. */
  72. private void initCalendars() {
  73. try {
  74. QueryContainer qc = new QueryContainer("SELECT * FROM "
  75. + SampleCalendarDatabase.DB_TABLE_NAME, sampleDatabase
  76. .getConnection());
  77. from.setContainerDataSource(qc);
  78. to.setContainerDataSource(qc);
  79. } catch (SQLException e) {
  80. e.printStackTrace();
  81. }
  82. // Calendar will use the first date property as start if you do not
  83. // explicitly specify the property id. Our start -property will be the
  84. // first one, so it's intentionally left out.
  85. // Start is the only mandatory property, but you'll probably want to
  86. // specify title as well.
  87. from.setItemEndPropertyId(SampleCalendarDatabase.PROPERTY_ID_END);
  88. from.setItemTitlePropertyId(SampleCalendarDatabase.PROPERTY_ID_TITLE);
  89. from.setItemNotimePropertyId(SampleCalendarDatabase.PROPERTY_ID_NOTIME);
  90. to.setItemEndPropertyId(SampleCalendarDatabase.PROPERTY_ID_END);
  91. to.setItemTitlePropertyId(SampleCalendarDatabase.PROPERTY_ID_TITLE);
  92. to.setItemNotimePropertyId(SampleCalendarDatabase.PROPERTY_ID_NOTIME);
  93. }
  94. }