summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/demo/reservation/CalendarField.java
blob: f555c832281028c62db0b836f87772bae18cc853 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.demo.reservation;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.DateField;

// TODO send one month at a time, do lazyLoading
// TODO check date limit when updating variables
// TODO Allow item selection
@SuppressWarnings("serial")
public class CalendarField extends DateField implements Container.Viewer {

    private static final String TAGNAME = "calendarfield";

    private Date minDate;
    private Date maxDate;

    private Container dataSource;
    private Object itemStyleNamePropertyId;
    private Object itemStartPropertyId;
    private Object itemEndPropertyId;
    private Object itemTitlePropertyId;
    private Object itemDescriptionPropertyId;
    private Object itemNotimePropertyId;

    public CalendarField() {
        super();
        init();
    }

    public CalendarField(Property dataSource) throws IllegalArgumentException {
        super(dataSource);
        init();
    }

    public CalendarField(String caption, Date value) {
        super(caption, value);
        init();
    }

    public CalendarField(String caption, Property dataSource) {
        super(caption, dataSource);
        init();
    }

    public CalendarField(String caption) {
        super(caption);
        init();
    }

    /*
     * Gets the components UIDL tag string. Don't add a JavaDoc comment here, we
     * use the default documentation from implemented interface.
     */
    @Override
    public String getTag() {
        return TAGNAME;
    }

    public void init() {
        super.setResolution(RESOLUTION_HOUR);

    }

    /**
     * Sets the resolution of the CalendarField. Only RESOLUTION_DAY and
     * RESOLUTION_HOUR are supported.
     * 
     * @param resolution
     *            the resolution to set.
     * @see com.vaadin.ui.DateField#setResolution(int)
     */
    @Override
    public void setResolution(int resolution) {
        if (resolution != RESOLUTION_DAY && resolution != RESOLUTION_HOUR) {
            throw new IllegalArgumentException();
        }
        super.setResolution(resolution);
    }

    public void setMinimumDate(Date date) {
        minDate = date;
        requestRepaint();
    }

    public Date getMinimumDate() {
        return minDate;
    }

    public void setMaximumDate(Date date) {
        maxDate = date;
        requestRepaint();
    }

    public Date getMaximumDate() {
        return maxDate;
    }

    public Container getContainerDataSource() {
        return dataSource;
    }

    public void setContainerDataSource(Container newDataSource) {
        if (newDataSource == null || checkDataSource(newDataSource)) {
            dataSource = newDataSource;
        } else {
            // TODO error message
            throw new IllegalArgumentException();
        }
        requestRepaint();
    }

    private boolean checkDataSource(Container dataSource) {

        // Check old propertyIds
        if (itemEndPropertyId != null) {
            final Class c = dataSource.getType(itemEndPropertyId);
            if (!Date.class.isAssignableFrom(c)) {
                itemEndPropertyId = null;
            }
        }
        if (itemNotimePropertyId != null) {
            final Class c = dataSource.getType(itemNotimePropertyId);
            if (!Boolean.class.isAssignableFrom(c)) {
                itemNotimePropertyId = null;
            }
        }
        if (itemStartPropertyId != null) {
            final Class c = dataSource.getType(itemStartPropertyId);
            if (Date.class.isAssignableFrom(c)) {
                // All we _really_ need is one date
                return true;
            } else {
                itemStartPropertyId = null;
            }
        }
        // We need at least one Date
        final Collection ids = dataSource.getContainerPropertyIds();
        for (final Iterator it = ids.iterator(); it.hasNext();) {
            final Object id = it.next();
            final Class c = dataSource.getType(id);
            if (Date.class.isAssignableFrom(c)) {
                itemStartPropertyId = id;
                return true;
            }
        }

        return false;
    }

    public Object getItemStyleNamePropertyId() {
        return itemStyleNamePropertyId;
    }

    public void setItemStyleNamePropertyId(Object propertyId) {
        itemStyleNamePropertyId = propertyId;
    }

    public Object getItemStartPropertyId() {
        return itemStartPropertyId;
    }

    public void setItemStartPropertyId(Object propertyId) {
        // TODO nullcheck for property id
        if (dataSource != null
                && !Date.class.isAssignableFrom(dataSource.getType(propertyId))) {
            // TODO error message
            throw new IllegalArgumentException();
        }
        itemStartPropertyId = propertyId;
    }

    public Object getItemEndPropertyId() {
        return itemEndPropertyId;
    }

    public void setItemEndPropertyId(Object propertyId) {
        // TODO nullcheck for property id
        if (dataSource != null
                && !Date.class.isAssignableFrom(dataSource.getType(propertyId))) {
            // TODO error message
            throw new IllegalArgumentException();
        }
        itemEndPropertyId = propertyId;
    }

    public Object getItemTitlePropertyId() {
        return itemTitlePropertyId;
    }

    public void setItemTitlePropertyId(Object propertyId) {
        itemTitlePropertyId = propertyId;
    }

    public Object getItemDescriptionPropertyId() {
        return itemDescriptionPropertyId;
    }

    public void setItemDescriptionPropertyId(Object propertyId) {
        itemDescriptionPropertyId = propertyId;
    }

    public Object getitemNotimePropertyId() {
        return itemNotimePropertyId;
    }

    public void setItemNotimePropertyId(Object propertyId) {
        // TODO nullcheck for property id
        if (dataSource != null
                && !Boolean.class.isAssignableFrom(dataSource
                        .getType(propertyId))) {
            // TODO error message
            throw new IllegalArgumentException();
        }
        itemNotimePropertyId = propertyId;
    }

    /**
     * Paints the content of this component.
     * 
     * @param target
     *            the Paint Event.
     * @throws PaintException
     *             if the paint operation failed.
     */
    @SuppressWarnings("deprecation")
    @Override
    public void paintContent(PaintTarget target) throws PaintException {
        super.paintContent(target);

        if (minDate != null) {
            target.addAttribute("min", String.valueOf(minDate.getTime()));
        }
        if (maxDate != null) {
            target.addAttribute("max", String.valueOf(maxDate.getTime()));
        }

        if (dataSource != null) {
            target.startTag("items");

            // TODO send one month now, the rest via lazyloading
            int month = new Date().getMonth();
            final Object value = getValue();
            if (value != null && value instanceof Date) {
                month = ((Date) value).getMonth();
            }

            for (final Iterator it = dataSource.getItemIds().iterator(); it
                    .hasNext();) {
                final Object itemId = it.next();
                final Item item = dataSource.getItem(itemId);
                Property p = item.getItemProperty(itemStartPropertyId);
                Date start = (Date) p.getValue();
                Date end = start; // assume same day
                if (itemEndPropertyId != null) {
                    p = item.getItemProperty(itemEndPropertyId);
                    end = (Date) p.getValue();
                    if (end == null) {
                        end = start;
                    } else if (end.before(start)) {
                        final Date tmp = start;
                        start = end;
                        end = tmp;
                    }
                }

                // TODO half-done lazyloading logic (hence broken)

                if (start != null) {
                    if ((start.getMonth() <= month || end.getMonth() >= month)) {
                        target.startTag("item");
                        // TODO different id?
                        target.addAttribute("id", itemId.hashCode());
                        if (itemStyleNamePropertyId != null) {
                            p = item.getItemProperty(itemStyleNamePropertyId);
                            final String styleName = (String) p.getValue();
                            target.addAttribute("styleName", styleName);
                        }
                        SimpleDateFormat sdf = new SimpleDateFormat(
                                "d MM yyyy HH:mm:ss Z");

                        target.addAttribute("Z", start.getTimezoneOffset());

                        target.addAttribute("start", "" + sdf.format(start));

                        if (end != start) {
                            target.addAttribute("end", "" + sdf.format(end));
                        }
                        if (itemTitlePropertyId != null) {
                            p = item.getItemProperty(itemTitlePropertyId);
                            final Object val = p.getValue();
                            if (val != null) {
                                target.addAttribute("title", val.toString());
                            }
                        }
                        if (itemDescriptionPropertyId != null) {
                            p = item.getItemProperty(itemDescriptionPropertyId);
                            final Object val = p.getValue();
                            if (val != null) {
                                target.addAttribute("description", val
                                        .toString());
                            }
                        }
                        if (itemNotimePropertyId != null) {
                            p = item.getItemProperty(itemNotimePropertyId);
                            final Object val = p.getValue();
                            if (val != null) {
                                target.addAttribute("notime", ((Boolean) val)
                                        .booleanValue());
                            }
                        }

                        target.endTag("item");
                    }
                }
            }

            target.endTag("items");
        }
    }
}