]> source.dussan.org Git - vaadin-framework.git/commitdiff
Splitted Widget and Paintable in VDateField and VDateFieldCalendar
authorJens Jansson <peppe@vaadin.com>
Mon, 30 Jan 2012 12:52:17 +0000 (14:52 +0200)
committerJens Jansson <peppe@vaadin.com>
Mon, 30 Jan 2012 12:52:17 +0000 (14:52 +0200)
src/com/vaadin/terminal/gwt/client/ui/VDateField.java
src/com/vaadin/terminal/gwt/client/ui/VDateFieldCalendar.java
src/com/vaadin/terminal/gwt/client/ui/VDateFieldCalendarPaintable.java [new file with mode: 0644]
src/com/vaadin/terminal/gwt/client/ui/VDateFieldPaintable.java [new file with mode: 0644]

index d21a20229e31f73740ad3452cb925cef7d792833..14c3b99210000f03574e6b9ad2bd2f02dcdb9878 100644 (file)
@@ -8,22 +8,17 @@ import java.util.Date;
 
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.FlowPanel;
-import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.terminal.gwt.client.ApplicationConnection;
 import com.vaadin.terminal.gwt.client.DateTimeService;
-import com.vaadin.terminal.gwt.client.LocaleNotLoadedException;
-import com.vaadin.terminal.gwt.client.VPaintableWidget;
-import com.vaadin.terminal.gwt.client.UIDL;
-import com.vaadin.terminal.gwt.client.VConsole;
 import com.vaadin.terminal.gwt.client.VTooltip;
 
-public class VDateField extends FlowPanel implements VPaintableWidget, Field {
+public class VDateField extends FlowPanel implements Field {
 
     public static final String CLASSNAME = "v-datefield";
 
-    private String id;
+    protected String paintableId;
 
-    private ApplicationConnection client;
+    protected ApplicationConnection client;
 
     protected boolean immediate;
 
@@ -65,7 +60,7 @@ public class VDateField extends FlowPanel implements VPaintableWidget, Field {
 
     protected DateTimeService dts;
 
-    private boolean showISOWeekNumbers = false;
+    protected boolean showISOWeekNumbers = false;
 
     public VDateField() {
         setStyleName(CLASSNAME);
@@ -77,80 +72,7 @@ public class VDateField extends FlowPanel implements VPaintableWidget, Field {
     public void onBrowserEvent(Event event) {
         super.onBrowserEvent(event);
         if (client != null) {
-            client.handleTooltipEvent(event, this);
-        }
-    }
-
-    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-        // Ensure correct implementation and let layout manage caption
-        if (client.updateComponent(this, uidl, true)) {
-            return;
-        }
-
-        // Save details
-        this.client = client;
-        id = uidl.getId();
-        immediate = uidl.getBooleanAttribute("immediate");
-
-        readonly = uidl.getBooleanAttribute("readonly");
-        enabled = !uidl.getBooleanAttribute("disabled");
-
-        if (uidl.hasAttribute("locale")) {
-            final String locale = uidl.getStringAttribute("locale");
-            try {
-                dts.setLocale(locale);
-                currentLocale = locale;
-            } catch (final LocaleNotLoadedException e) {
-                currentLocale = dts.getLocale();
-                VConsole.error("Tried to use an unloaded locale \"" + locale
-                        + "\". Using default locale (" + currentLocale + ").");
-                VConsole.error(e);
-            }
-        }
-
-        // We show week numbers only if the week starts with Monday, as ISO 8601
-        // specifies
-        showISOWeekNumbers = uidl.getBooleanAttribute(WEEK_NUMBERS)
-                && dts.getFirstDayOfWeek() == 1;
-
-        int newResolution;
-        if (uidl.hasVariable("sec")) {
-            newResolution = RESOLUTION_SEC;
-        } else if (uidl.hasVariable("min")) {
-            newResolution = RESOLUTION_MIN;
-        } else if (uidl.hasVariable("hour")) {
-            newResolution = RESOLUTION_HOUR;
-        } else if (uidl.hasVariable("day")) {
-            newResolution = RESOLUTION_DAY;
-        } else if (uidl.hasVariable("month")) {
-            newResolution = RESOLUTION_MONTH;
-        } else {
-            newResolution = RESOLUTION_YEAR;
-        }
-
-        currentResolution = newResolution;
-
-        // Add stylename that indicates current resolution
-        addStyleName(CLASSNAME + "-" + resolutionToString(currentResolution));
-
-        final int year = uidl.getIntVariable("year");
-        final int month = (currentResolution >= RESOLUTION_MONTH) ? uidl
-                .getIntVariable("month") : -1;
-        final int day = (currentResolution >= RESOLUTION_DAY) ? uidl
-                .getIntVariable("day") : -1;
-        final int hour = (currentResolution >= RESOLUTION_HOUR) ? uidl
-                .getIntVariable("hour") : 0;
-        final int min = (currentResolution >= RESOLUTION_MIN) ? uidl
-                .getIntVariable("min") : 0;
-        final int sec = (currentResolution >= RESOLUTION_SEC) ? uidl
-                .getIntVariable("sec") : 0;
-
-        // Construct new date for this datefield (only if not null)
-        if (year > -1) {
-            setCurrentDate(new Date((long) getTime(year, month, day, hour, min,
-                    sec, 0)));
-        } else {
-            setCurrentDate(null);
+            client.handleWidgetTooltipEvent(event, this);
         }
     }
 
@@ -158,7 +80,7 @@ public class VDateField extends FlowPanel implements VPaintableWidget, Field {
      * We need this redundant native function because Java's Date object doesn't
      * have a setMilliseconds method.
      */
-    private static native double getTime(int y, int m, int d, int h, int mi,
+    protected static native double getTime(int y, int m, int d, int h, int mi,
             int s, int ms)
     /*-{
        try {
@@ -227,7 +149,7 @@ public class VDateField extends FlowPanel implements VPaintableWidget, Field {
     }
 
     public String getId() {
-        return id;
+        return paintableId;
     }
 
     public ApplicationConnection getClient() {
@@ -270,8 +192,4 @@ public class VDateField extends FlowPanel implements VPaintableWidget, Field {
     protected void setDate(Date date) {
         this.date = date;
     }
-
-    public Widget getWidgetForPaintable() {
-        return this;
-    }
 }
index 73bfe27700e32be39f8083f4b282fb26768c96f6..6fb845625837e12d4af7b86f3968bea3d1bb39ad 100644 (file)
@@ -7,20 +7,16 @@ package com.vaadin.terminal.gwt.client.ui;
 import java.util.Date;\r
 \r
 import com.google.gwt.event.dom.client.DomEvent;\r
-import com.vaadin.terminal.gwt.client.ApplicationConnection;\r
 import com.vaadin.terminal.gwt.client.DateTimeService;\r
-import com.vaadin.terminal.gwt.client.UIDL;\r
-import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.FocusChangeListener;\r
 import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.FocusOutListener;\r
 import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.SubmitListener;\r
-import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.TimeChangeListener;\r
 \r
 /**\r
  * A client side implementation for InlineDateField\r
  */\r
 public class VDateFieldCalendar extends VDateField {\r
 \r
-    private final VCalendarPanel calendarPanel;\r
+    protected final VCalendarPanel calendarPanel;\r
 \r
     public VDateFieldCalendar() {\r
         super();\r
@@ -44,73 +40,11 @@ public class VDateFieldCalendar extends VDateField {
         });\r
     }\r
 \r
-    @Override\r
-    @SuppressWarnings("deprecation")\r
-    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {\r
-        super.updateFromUIDL(uidl, client);\r
-        calendarPanel.setShowISOWeekNumbers(isShowISOWeekNumbers());\r
-        calendarPanel.setDateTimeService(getDateTimeService());\r
-        calendarPanel.setResolution(getCurrentResolution());\r
-        Date currentDate = getCurrentDate();\r
-        if (currentDate != null) {\r
-            calendarPanel.setDate(new Date(currentDate.getTime()));\r
-        } else {\r
-            calendarPanel.setDate(null);\r
-        }\r
-\r
-        if (currentResolution > RESOLUTION_DAY) {\r
-            calendarPanel.setTimeChangeListener(new TimeChangeListener() {\r
-                public void changed(int hour, int min, int sec, int msec) {\r
-                    Date d = getDate();\r
-                    if (d == null) {\r
-                        // date currently null, use the value from calendarPanel\r
-                        // (~ client time at the init of the widget)\r
-                        d = (Date) calendarPanel.getDate().clone();\r
-                    }\r
-                    d.setHours(hour);\r
-                    d.setMinutes(min);\r
-                    d.setSeconds(sec);\r
-                    DateTimeService.setMilliseconds(d, msec);\r
-\r
-                    // Always update time changes to the server\r
-                    calendarPanel.setDate(d);\r
-                    updateValueFromPanel();\r
-                }\r
-            });\r
-        }\r
-\r
-        if (currentResolution <= RESOLUTION_MONTH) {\r
-            calendarPanel.setFocusChangeListener(new FocusChangeListener() {\r
-                public void focusChanged(Date date) {\r
-                    Date date2 = new Date();\r
-                    if (calendarPanel.getDate() != null) {\r
-                        date2.setTime(calendarPanel.getDate().getTime());\r
-                    }\r
-                    /*\r
-                     * Update the value of calendarPanel\r
-                     */\r
-                    date2.setYear(date.getYear());\r
-                    date2.setMonth(date.getMonth());\r
-                    calendarPanel.setDate(date2);\r
-                    /*\r
-                     * Then update the value from panel to server\r
-                     */\r
-                    updateValueFromPanel();\r
-                }\r
-            });\r
-        } else {\r
-            calendarPanel.setFocusChangeListener(null);\r
-        }\r
-\r
-        // Update possible changes\r
-        calendarPanel.renderCalendar();\r
-    }\r
-\r
     /**\r
      * TODO refactor: almost same method as in VPopupCalendar.updateValue\r
      */\r
     @SuppressWarnings("deprecation")\r
-    private void updateValueFromPanel() {\r
+    protected void updateValueFromPanel() {\r
         Date date2 = calendarPanel.getDate();\r
         Date currentDate = getCurrentDate();\r
         if (currentDate == null || date2.getTime() != currentDate.getTime()) {\r
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VDateFieldCalendarPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VDateFieldCalendarPaintable.java
new file mode 100644 (file)
index 0000000..8dced06
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+@VaadinApache2LicenseForJavaFiles@
+ */
+package com.vaadin.terminal.gwt.client.ui;
+
+import java.util.Date;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.terminal.gwt.client.ApplicationConnection;
+import com.vaadin.terminal.gwt.client.DateTimeService;
+import com.vaadin.terminal.gwt.client.UIDL;
+import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.FocusChangeListener;
+import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.TimeChangeListener;
+
+public class VDateFieldCalendarPaintable extends VDateFieldPaintable {
+
+    @Override
+    @SuppressWarnings("deprecation")
+    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
+        super.updateFromUIDL(uidl, client);
+        getWidgetForPaintable().calendarPanel
+                .setShowISOWeekNumbers(getWidgetForPaintable()
+                        .isShowISOWeekNumbers());
+        getWidgetForPaintable().calendarPanel
+                .setDateTimeService(getWidgetForPaintable()
+                        .getDateTimeService());
+        getWidgetForPaintable().calendarPanel
+                .setResolution(getWidgetForPaintable().getCurrentResolution());
+        Date currentDate = getWidgetForPaintable().getCurrentDate();
+        if (currentDate != null) {
+            getWidgetForPaintable().calendarPanel.setDate(new Date(currentDate
+                    .getTime()));
+        } else {
+            getWidgetForPaintable().calendarPanel.setDate(null);
+        }
+
+        if (getWidgetForPaintable().currentResolution > VDateField.RESOLUTION_DAY) {
+            getWidgetForPaintable().calendarPanel
+                    .setTimeChangeListener(new TimeChangeListener() {
+                        public void changed(int hour, int min, int sec, int msec) {
+                            Date d = getWidgetForPaintable().getDate();
+                            if (d == null) {
+                                // date currently null, use the value from
+                                // calendarPanel
+                                // (~ client time at the init of the widget)
+                                d = (Date) getWidgetForPaintable().calendarPanel
+                                        .getDate().clone();
+                            }
+                            d.setHours(hour);
+                            d.setMinutes(min);
+                            d.setSeconds(sec);
+                            DateTimeService.setMilliseconds(d, msec);
+
+                            // Always update time changes to the server
+                            getWidgetForPaintable().calendarPanel.setDate(d);
+                            getWidgetForPaintable().updateValueFromPanel();
+                        }
+                    });
+        }
+
+        if (getWidgetForPaintable().currentResolution <= VDateField.RESOLUTION_MONTH) {
+            getWidgetForPaintable().calendarPanel
+                    .setFocusChangeListener(new FocusChangeListener() {
+                        public void focusChanged(Date date) {
+                            Date date2 = new Date();
+                            if (getWidgetForPaintable().calendarPanel.getDate() != null) {
+                                date2.setTime(getWidgetForPaintable().calendarPanel
+                                        .getDate().getTime());
+                            }
+                            /*
+                             * Update the value of calendarPanel
+                             */
+                            date2.setYear(date.getYear());
+                            date2.setMonth(date.getMonth());
+                            getWidgetForPaintable().calendarPanel
+                                    .setDate(date2);
+                            /*
+                             * Then update the value from panel to server
+                             */
+                            getWidgetForPaintable().updateValueFromPanel();
+                        }
+                    });
+        } else {
+            getWidgetForPaintable().calendarPanel.setFocusChangeListener(null);
+        }
+
+        // Update possible changes
+        getWidgetForPaintable().calendarPanel.renderCalendar();
+    }
+
+    @Override
+    protected Widget createWidget() {
+        return GWT.create(VDateFieldCalendar.class);
+    }
+
+    @Override
+    public VDateFieldCalendar getWidgetForPaintable() {
+        return (VDateFieldCalendar) super.getWidgetForPaintable();
+    }
+}
diff --git a/src/com/vaadin/terminal/gwt/client/ui/VDateFieldPaintable.java b/src/com/vaadin/terminal/gwt/client/ui/VDateFieldPaintable.java
new file mode 100644 (file)
index 0000000..4aaf1c2
--- /dev/null
@@ -0,0 +1,106 @@
+package com.vaadin.terminal.gwt.client.ui;
+
+import java.util.Date;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.terminal.gwt.client.ApplicationConnection;
+import com.vaadin.terminal.gwt.client.LocaleNotLoadedException;
+import com.vaadin.terminal.gwt.client.UIDL;
+import com.vaadin.terminal.gwt.client.VConsole;
+
+public class VDateFieldPaintable extends VAbstractPaintableWidget {
+
+    public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
+        // Ensure correct implementation and let layout manage caption
+        if (client.updateComponent(this, uidl, true)) {
+            return;
+        }
+
+        // Save details
+        getWidgetForPaintable().client = client;
+        getWidgetForPaintable().paintableId = uidl.getId();
+        getWidgetForPaintable().immediate = uidl
+                .getBooleanAttribute("immediate");
+
+        getWidgetForPaintable().readonly = uidl.getBooleanAttribute("readonly");
+        getWidgetForPaintable().enabled = !uidl.getBooleanAttribute("disabled");
+
+        if (uidl.hasAttribute("locale")) {
+            final String locale = uidl.getStringAttribute("locale");
+            try {
+                getWidgetForPaintable().dts.setLocale(locale);
+                getWidgetForPaintable().currentLocale = locale;
+            } catch (final LocaleNotLoadedException e) {
+                getWidgetForPaintable().currentLocale = getWidgetForPaintable().dts
+                        .getLocale();
+                VConsole.error("Tried to use an unloaded locale \"" + locale
+                        + "\". Using default locale ("
+                        + getWidgetForPaintable().currentLocale + ").");
+                VConsole.error(e);
+            }
+        }
+
+        // We show week numbers only if the week starts with Monday, as ISO 8601
+        // specifies
+        getWidgetForPaintable().showISOWeekNumbers = uidl
+                .getBooleanAttribute(VDateField.WEEK_NUMBERS)
+                && getWidgetForPaintable().dts.getFirstDayOfWeek() == 1;
+
+        int newResolution;
+        if (uidl.hasVariable("sec")) {
+            newResolution = VDateField.RESOLUTION_SEC;
+        } else if (uidl.hasVariable("min")) {
+            newResolution = VDateField.RESOLUTION_MIN;
+        } else if (uidl.hasVariable("hour")) {
+            newResolution = VDateField.RESOLUTION_HOUR;
+        } else if (uidl.hasVariable("day")) {
+            newResolution = VDateField.RESOLUTION_DAY;
+        } else if (uidl.hasVariable("month")) {
+            newResolution = VDateField.RESOLUTION_MONTH;
+        } else {
+            newResolution = VDateField.RESOLUTION_YEAR;
+        }
+
+        getWidgetForPaintable().currentResolution = newResolution;
+
+        // Add stylename that indicates current resolution
+        getWidgetForPaintable()
+                .addStyleName(
+                        VDateField.CLASSNAME
+                                + "-"
+                                + VDateField
+                                        .resolutionToString(getWidgetForPaintable().currentResolution));
+
+        final int year = uidl.getIntVariable("year");
+        final int month = (getWidgetForPaintable().currentResolution >= VDateField.RESOLUTION_MONTH) ? uidl
+                .getIntVariable("month") : -1;
+        final int day = (getWidgetForPaintable().currentResolution >= VDateField.RESOLUTION_DAY) ? uidl
+                .getIntVariable("day") : -1;
+        final int hour = (getWidgetForPaintable().currentResolution >= VDateField.RESOLUTION_HOUR) ? uidl
+                .getIntVariable("hour") : 0;
+        final int min = (getWidgetForPaintable().currentResolution >= VDateField.RESOLUTION_MIN) ? uidl
+                .getIntVariable("min") : 0;
+        final int sec = (getWidgetForPaintable().currentResolution >= VDateField.RESOLUTION_SEC) ? uidl
+                .getIntVariable("sec") : 0;
+
+        // Construct new date for this datefield (only if not null)
+        if (year > -1) {
+            getWidgetForPaintable().setCurrentDate(
+                    new Date((long) getWidgetForPaintable().getTime(year,
+                            month, day, hour, min, sec, 0)));
+        } else {
+            getWidgetForPaintable().setCurrentDate(null);
+        }
+    }
+
+    @Override
+    protected Widget createWidget() {
+        return GWT.create(VDateField.class);
+    }
+
+    @Override
+    public VDateField getWidgetForPaintable() {
+        return (VDateField) super.getWidgetForPaintable();
+    }
+}