]> source.dussan.org Git - vaadin-framework.git/commitdiff
#6066 Support for time zones in DateField
authorLeif Åstrand <leif@vaadin.com>
Mon, 22 Aug 2011 11:13:29 +0000 (11:13 +0000)
committerLeif Åstrand <leif@vaadin.com>
Mon, 22 Aug 2011 11:13:29 +0000 (11:13 +0000)
svn changeset:20536/svn branch:6.7

src/com/vaadin/ui/DateField.java
tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.html [new file with mode: 0644]
tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.java [new file with mode: 0644]

index eee26555f73bbee5879ae71113397497e3b0e873..1ecf616832eb42760a7f13d4cb96197c2dfcc059 100644 (file)
@@ -11,6 +11,7 @@ import java.util.Collection;
 import java.util.Date;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 import com.vaadin.data.Property;
 import com.vaadin.data.Validator;
@@ -126,6 +127,8 @@ public class DateField extends AbstractField implements
 
     private String defaultParseErrorMessage = "Date format not recognized";
 
+    private TimeZone timeZone = null;
+
     /* Constructors */
 
     /**
@@ -290,7 +293,7 @@ public class DateField extends AbstractField implements
                         || variables.containsKey("min")
                         || variables.containsKey("sec")
                         || variables.containsKey("msec") || variables
-                        .containsKey("dateString"))) {
+                            .containsKey("dateString"))) {
 
             // Old and new dates
             final Date oldDate = (Date) getValue();
@@ -511,6 +514,10 @@ public class DateField extends AbstractField implements
             // Try to parse the given string value to Date
             try {
                 final SimpleDateFormat parser = new SimpleDateFormat();
+                final TimeZone currentTimeZone = getTimeZone();
+                if (currentTimeZone != null) {
+                    parser.setTimeZone(currentTimeZone);
+                }
                 final Date val = parser.parse(newValue.toString());
                 super.setValue(val, repaintIsNotNeeded);
             } catch (final ParseException e) {
@@ -635,6 +642,11 @@ public class DateField extends AbstractField implements
             newCal.setTime(currentDate);
         }
 
+        final TimeZone currentTimeZone = getTimeZone();
+        if (currentTimeZone != null) {
+            newCal.setTimeZone(currentTimeZone);
+        }
+
         return newCal;
     }
 
@@ -791,6 +803,37 @@ public class DateField extends AbstractField implements
         defaultParseErrorMessage = parsingErrorMessage;
     }
 
+    /**
+     * Sets the time zone used by this date field. The time zone is used to
+     * convert the absolute time in a Date object to a logical time displayed in
+     * the selector and to convert the select time back to a Date object.
+     * 
+     * If no time zone has been set, the current default time zone returned by
+     * {@code TimeZone.getDefault()} is used.
+     * 
+     * @see #getTimeZone()
+     * @param timeZone
+     *            the time zone to use for time calculations.
+     */
+    public void setTimeZone(TimeZone timeZone) {
+        this.timeZone = timeZone;
+        requestRepaint();
+    }
+
+    /**
+     * Gets the time zone used by this field. The time zone is used to convert
+     * the absolute time in a Date object to a logical time displayed in the
+     * selector and to convert the select time back to a Date object.
+     * 
+     * If {@code null} is returned, the current default time zone returned by
+     * {@code TimeZone.getDefault()} is used.
+     * 
+     * @return the current time zone
+     */
+    public TimeZone getTimeZone() {
+        return timeZone;
+    }
+
     public static class UnparsableDateString extends
             Validator.InvalidValueException {
 
diff --git a/tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.html b/tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.html
new file mode 100644 (file)
index 0000000..c20a711
--- /dev/null
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="http://localhost:8888/" />
+<title>DateFieldTimezone</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">DateFieldTimezone</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.datefield.DateFieldTimezone?restartApplication</td>
+       <td></td>
+</tr>
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VFilterSelect[0]/domChild[1]</td>
+       <td>6,10</td>
+</tr>
+<tr>
+       <td>type</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VFilterSelect[0]/domChild[0]</td>
+       <td>Europe/Helsinki (Eastern European Time)</td>
+</tr>
+<tr>
+       <td>pressSpecialKey</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[0]/VFilterSelect[0]/domChild[0]</td>
+       <td>enter</td>
+</tr>
+<tr>
+       <td>assertValue</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VPopupCalendar[0]#field</td>
+       <td>1/1/10 02:00:00.000 AM</td>
+</tr>
+<tr>
+       <td>type</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::/VVerticalLayout[0]/ChildComponentContainer[1]/VVerticalLayout[0]/ChildComponentContainer[2]/VPopupCalendar[0]#field</td>
+       <td>1/1/10 01:00:00.000 AM</td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>vaadin=runcomvaadintestscomponentsdatefieldDateFieldTimezone::PID_SLog_row_0</td>
+       <td>2. Date changed to 12/31/09 11:00:00 PM UTC</td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
diff --git a/tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.java b/tests/src/com/vaadin/tests/components/datefield/DateFieldTimezone.java
new file mode 100644 (file)
index 0000000..6562e54
--- /dev/null
@@ -0,0 +1,99 @@
+package com.vaadin.tests.components.datefield;
+
+import java.text.DateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import com.vaadin.data.Property;
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.tests.components.TestBase;
+import com.vaadin.tests.util.Log;
+import com.vaadin.ui.ComboBox;
+import com.vaadin.ui.DateField;
+
+public class DateFieldTimezone extends TestBase {
+
+    private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
+    private static final Locale EN = Locale.ENGLISH;
+    private final Log log = new Log(5);
+    private final DateField dateField = new DateField();
+    private static final String nullValue = "";
+
+    @Override
+    protected void setup() {
+        ArrayList<String> timeZoneCodes = new ArrayList<String>();
+        timeZoneCodes.add(nullValue);
+        timeZoneCodes.addAll(Arrays.asList(TimeZone.getAvailableIDs()));
+        ComboBox timezoneSelector = new ComboBox("Select time zone",
+                timeZoneCodes) {
+            @Override
+            public String getItemCaption(Object itemId) {
+                if (itemId == nullValue || itemId == null) {
+                    TimeZone timeZone = TimeZone.getDefault();
+                    return "Default time zone (" + timeZone.getDisplayName()
+                            + ")";
+                } else {
+                    TimeZone timeZone = TimeZone.getTimeZone((String) itemId);
+                    return itemId + " (" + timeZone.getDisplayName() + ")";
+                }
+            }
+        };
+        timezoneSelector.setValue("UTC");
+        timezoneSelector.setImmediate(true);
+        timezoneSelector.setNullSelectionAllowed(true);
+        timezoneSelector.setNullSelectionItemId(nullValue);
+        timezoneSelector.setFilteringMode(ComboBox.FILTERINGMODE_CONTAINS);
+        timezoneSelector.addListener(new Property.ValueChangeListener() {
+            public void valueChange(ValueChangeEvent event) {
+                Object value = event.getProperty().getValue();
+                TimeZone timeZone;
+                if (value == nullValue || value == null) {
+                    timeZone = null;
+                    log.log("Change to default time zone "
+                            + TimeZone.getDefault().getID());
+                } else {
+                    timeZone = TimeZone.getTimeZone((String) value);
+                    log.log("Changed to time zone " + timeZone.getID());
+                }
+                dateField.setTimeZone(timeZone);
+            }
+        });
+
+        Calendar cal = Calendar.getInstance(UTC);
+        cal.set(2010, 0, 1, 0, 0, 0);
+        cal.set(Calendar.MILLISECOND, 0);
+
+        dateField.setValue(cal.getTime());
+        dateField.setImmediate(true);
+        dateField.setTimeZone(cal.getTimeZone());
+        dateField.setLocale(EN);
+        dateField.addListener(new Property.ValueChangeListener() {
+            public void valueChange(ValueChangeEvent event) {
+                Date date = (Date) dateField.getValue();
+                DateFormat format = DateFormat.getDateTimeInstance(
+                        DateFormat.SHORT, DateFormat.LONG, EN);
+                format.setTimeZone(UTC);
+                log.log("Date changed to " + format.format(date));
+            }
+        });
+
+        addComponent(timezoneSelector);
+        addComponent(log);
+        addComponent(dateField);
+    }
+
+    @Override
+    protected String getDescription() {
+        return "Tests the operation of the date field with different time zones";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return Integer.valueOf(6066);
+    }
+
+}