summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/reservation/CalendarDemo.java
blob: 664fc6d5d945034492a83949c234ccbe1f9aecf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.demo.reservation;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.data.Property.ValueChangeListener;
import com.itmill.toolkit.data.util.QueryContainer;
import com.itmill.toolkit.demo.util.SampleCalendarDatabase;
import com.itmill.toolkit.ui.DateField;
import com.itmill.toolkit.ui.OrderedLayout;
import com.itmill.toolkit.ui.Window;

/**
 * This example shows how the CalendarField can use Containers. A QueryContainer
 * is used to bind SQL table rows to the calendar. Demonstrates: how to create
 * <code>com.itmill.toolkit.data.Container</code> and set it as datasource for
 * <code>com.itmill.toolkit.ui.Component.CalendarField</code>
 * 
 * @author IT Mill Ltd.
 * @since 4.0.0
 * 
 */
public class CalendarDemo extends com.itmill.toolkit.Application {

    // Database provided with sample data
    private SampleCalendarDatabase sampleDatabase;

    // The calendar UI component
    private CalendarField from;
    private CalendarField to;

    /**
     * Initialize Application. Demo components are added to main window.
     */
    @Override
    public void init() {
        final Window main = new Window("Calendar demo");
        setMainWindow(main);

        main.setLayout(new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL));

        // create the calendar component and add to layout
        from = new CalendarField();
        main.addComponent(from);
        from.setResolution(DateField.RESOLUTION_HOUR);
        from.setImmediate(true);

        to = new CalendarField();
        main.addComponent(to);
        to.setResolution(DateField.RESOLUTION_HOUR);
        to.setEnabled(false);
        to.setImmediate(true);

        from.addListener(new ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                final Date fd = (Date) from.getValue();
                final Date td = (Date) to.getValue();
                if (fd == null) {
                    to.setValue(null);
                    to.setEnabled(false);
                    return;
                } else {
                    to.setEnabled(true);
                }
                to.setMinimumDate(fd);
                if (td == null || td.before(fd)) {
                    to.setValue(fd);
                }
            }
        });

        // initialize the sample database and set as calendar datasource
        sampleDatabase = new SampleCalendarDatabase();

        initCalendars();

        // Don't allow dates before today
        from.setMinimumDate(Calendar.getInstance().getTime());

    }

    /**
     * Populates table component with all rows from calendar table.
     */
    private void initCalendars() {
        try {
            final QueryContainer qc = new QueryContainer("SELECT * FROM "
                    + SampleCalendarDatabase.DB_TABLE_NAME, sampleDatabase
                    .getConnection());
            from.setContainerDataSource(qc);
            to.setContainerDataSource(qc);
        } catch (final SQLException e) {
            e.printStackTrace();
        }
        /*
         * // Calendar will use the first date property as start if you do not
         * // explicitly specify the property id. Our start -property will be
         * the // first one, so it's intentionally left out. // Start is the
         * only mandatory property, but you'll probably want to // specify title
         * as well.
         * from.setItemEndPropertyId(SampleCalendarDatabase.PROPERTY_ID_END);
         * from
         * .setItemTitlePropertyId(SampleCalendarDatabase.PROPERTY_ID_TITLE);
         * from
         * .setItemNotimePropertyId(SampleCalendarDatabase.PROPERTY_ID_NOTIME);
         * 
         * to.setItemEndPropertyId(SampleCalendarDatabase.PROPERTY_ID_END);
         * to.setItemTitlePropertyId(SampleCalendarDatabase.PROPERTY_ID_TITLE);
         * to
         * .setItemNotimePropertyId(SampleCalendarDatabase.PROPERTY_ID_NOTIME);
         */
    }

}