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 4.0KB

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