diff options
author | Marc Englund <marc.englund@itmill.com> | 2011-03-29 07:31:44 +0000 |
---|---|---|
committer | Marc Englund <marc.englund@itmill.com> | 2011-03-29 07:31:44 +0000 |
commit | f39f819b4d90c8e988c5c04eed92e980095dc2b3 (patch) | |
tree | 51e643e82dab21356124e9c8efb5d72e8491d113 /src | |
parent | f36607b3e64a0539490944df668eef3ae784d773 (diff) | |
download | vaadin-framework-f39f819b4d90c8e988c5c04eed92e980095dc2b3.tar.gz vaadin-framework-f39f819b4d90c8e988c5c04eed92e980095dc2b3.zip |
Implements #6691 get timezone offset from client.
svn changeset:18000/svn branch:6.6
Diffstat (limited to 'src')
5 files changed, 179 insertions, 79 deletions
diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index a8b1909dab..df8fd11ca3 100755 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -191,30 +191,31 @@ public class ApplicationConnection { private native void initializeTestbenchHooks( ComponentLocator componentLocator, String TTAppId) /*-{ - var ap = this; - var client = {}; - client.isActive = function() { - return ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::hasActiveRequest()() || ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::isExecutingDeferredCommands()(); - } - var vi = ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::getVersionInfo()(); - if (vi) { - client.getVersionInfo = function() { - return vi; - } - } - - client.getElementByPath = function(id) { - return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getElementByPath(Ljava/lang/String;)(id); - } - client.getPathForElement = function(element) { - return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getPathForElement(Lcom/google/gwt/user/client/Element;)(element); - } - - if(!$wnd.vaadin.clients) { - $wnd.vaadin.clients = {}; - } - - $wnd.vaadin.clients[TTAppId] = client; + var ap = this; + var client = {}; + client.isActive = function() { + return ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::hasActiveRequest()() + || ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::isExecutingDeferredCommands()(); + } + var vi = ap.@com.vaadin.terminal.gwt.client.ApplicationConnection::getVersionInfo()(); + if (vi) { + client.getVersionInfo = function() { + return vi; + } + } + + client.getElementByPath = function(id) { + return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getElementByPath(Ljava/lang/String;)(id); + } + client.getPathForElement = function(element) { + return componentLocator.@com.vaadin.terminal.gwt.client.ComponentLocator::getPathForElement(Lcom/google/gwt/user/client/Element;)(element); + } + + if (!$wnd.vaadin.clients) { + $wnd.vaadin.clients = {}; + } + + $wnd.vaadin.clients[TTAppId] = client; }-*/; /** @@ -249,27 +250,27 @@ public class ApplicationConnection { */ private native void initializeClientHooks() /*-{ - var app = this; - var oldSync; - if($wnd.vaadin.forceSync) { - oldSync = $wnd.vaadin.forceSync; - } - $wnd.vaadin.forceSync = function() { - if(oldSync) { - oldSync(); - } - app.@com.vaadin.terminal.gwt.client.ApplicationConnection::sendPendingVariableChanges()(); - } - var oldForceLayout; - if($wnd.vaadin.forceLayout) { - oldForceLayout = $wnd.vaadin.forceLayout; - } - $wnd.vaadin.forceLayout = function() { - if(oldForceLayout) { - oldForceLayout(); - } - app.@com.vaadin.terminal.gwt.client.ApplicationConnection::forceLayout()(); - } + var app = this; + var oldSync; + if ($wnd.vaadin.forceSync) { + oldSync = $wnd.vaadin.forceSync; + } + $wnd.vaadin.forceSync = function() { + if (oldSync) { + oldSync(); + } + app.@com.vaadin.terminal.gwt.client.ApplicationConnection::sendPendingVariableChanges()(); + } + var oldForceLayout; + if ($wnd.vaadin.forceLayout) { + oldForceLayout = $wnd.vaadin.forceLayout; + } + $wnd.vaadin.forceLayout = function() { + if (oldForceLayout) { + oldForceLayout(); + } + app.@com.vaadin.terminal.gwt.client.ApplicationConnection::forceLayout()(); + } }-*/; /** @@ -280,15 +281,16 @@ public class ApplicationConnection { */ private static native void runPostRequestHooks(String appId) /*-{ - if($wnd.vaadin.postRequestHooks) { - for(var hook in $wnd.vaadin.postRequestHooks) { - if(typeof($wnd.vaadin.postRequestHooks[hook]) == "function") { - try { - $wnd.vaadin.postRequestHooks[hook](appId); - } catch(e) {} - } - } - } + if ($wnd.vaadin.postRequestHooks) { + for ( var hook in $wnd.vaadin.postRequestHooks) { + if (typeof ($wnd.vaadin.postRequestHooks[hook]) == "function") { + try { + $wnd.vaadin.postRequestHooks[hook](appId); + } catch (e) { + } + } + } + } }-*/; /** @@ -348,6 +350,8 @@ public class ApplicationConnection { int offsetWidth = pe.getOffsetWidth(); int screenWidth = BrowserInfo.get().getScreenWidth(); int screenHeight = BrowserInfo.get().getScreenHeight(); + int tzOffset = BrowserInfo.get().getTimezoneOffset(); + int rtzOffset = BrowserInfo.get().getRawTimezoneOffset(); String token = History.getToken(); @@ -356,7 +360,8 @@ public class ApplicationConnection { // values currently only via transaction listener. String parameters = "repaintAll=1&" + "sh=" + screenHeight + "&sw=" + screenWidth + "&cw=" + clientWidth + "&ch=" + clientHeight - + "&vw=" + offsetWidth + "&vh=" + offsetHeight + "&fr=" + token; + + "&vw=" + offsetWidth + "&vh=" + offsetHeight + "&fr=" + token + + "&tzo=" + tzOffset + "&rtzo=" + rtzOffset; return parameters; } @@ -771,11 +776,11 @@ public class ApplicationConnection { private static native ValueMap parseJSONResponse(String jsonText) /*-{ - try { - return JSON.parse(jsonText); - } catch(ignored) { - return eval('(' + jsonText + ')'); - } + try { + return JSON.parse(jsonText); + } catch (ignored) { + return eval('(' + jsonText + ')'); + } }-*/; private void handleReceivedJSONMessage(Date start, String jsonText, @@ -1023,11 +1028,11 @@ public class ApplicationConnection { // Redirect browser, null reloads current page private static native void redirect(String url) /*-{ - if (url) { - $wnd.location = url; - } else { - $wnd.location.reload(false); - } + if (url) { + $wnd.location = url; + } else { + $wnd.location.reload(false); + } }-*/; public void registerPaintable(String pid, Paintable paintable) { @@ -1039,7 +1044,7 @@ public class ApplicationConnection { private native void setPid(Element el, String pid) /*-{ - el.tkPid = pid; + el.tkPid = pid; }-*/; /** @@ -1069,7 +1074,7 @@ public class ApplicationConnection { */ public native String getPid(Element el) /*-{ - return el.tkPid; + return el.tkPid; }-*/; /** diff --git a/src/com/vaadin/terminal/gwt/client/BrowserInfo.java b/src/com/vaadin/terminal/gwt/client/BrowserInfo.java index 5981c2cfed..e4dd61b0b9 100644 --- a/src/com/vaadin/terminal/gwt/client/BrowserInfo.java +++ b/src/com/vaadin/terminal/gwt/client/BrowserInfo.java @@ -65,10 +65,10 @@ public class BrowserInfo { private native boolean isIE8InIE7CompatibilityMode() /*-{ - var mode = $wnd.document.documentMode; - if (!mode) - return false; - return (mode == 7); + var mode = $wnd.document.documentMode; + if (!mode) + return false; + return (mode == 7); }-*/; /** @@ -282,17 +282,53 @@ public class BrowserInfo { public native static String getBrowserString() /*-{ - return $wnd.navigator.userAgent; + return $wnd.navigator.userAgent; }-*/; public native int getScreenWidth() - /*-{ - return $wnd.screen.width; + /*-{ + return $wnd.screen.width; }-*/; public native int getScreenHeight() - /*-{ - return $wnd.screen.height; + /*-{ + return $wnd.screen.height; + }-*/; + + /** + * Get's the timezone offset from GMT in minutes, as reported by the + * browser. DST affects this value. + * + * @return offset to GMT in minutes + */ + public native int getTimezoneOffset() + /*-{ + return new Date().getTimezoneOffset(); + }-*/; + + /** + * Get's the timezone offset from GMT in minutes, as reported by the browser + * AND adjusted to ignore daylight savings time. DST does not affect this + * value. + * + * @return offset to GMT in minutes + */ + public native int getRawTimezoneOffset() + /*-{ + var d = new Date(); + var tzo1 = d.getTimezoneOffset(); // current offset + + for (var m=12;m>0;m--) { + d.setUTCMonth(m); + var tzo2 = d.getTimezoneOffset(); + if (tzo1 != tzo2) { + // NOTE js indicates this 'backwards' (e.g -180) + return (tzo1 > tzo2 ? tzo1 : tzo2); // offset w/o DST + } + } + + return tzo1; // no DST + }-*/; } diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java index e64cd6f243..6750b1faa8 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationPortlet.java @@ -545,7 +545,9 @@ public abstract class AbstractApplicationPortlet extends GenericPortlet browser.updateBrowserProperties(request.getLocale(), null, request.isSecure(), userAgent, getHTTPRequestParameter(request, "sw"), - getHTTPRequestParameter(request, "sh")); + getHTTPRequestParameter(request, "sh"), + getHTTPRequestParameter(request, "tzo"), + getHTTPRequestParameter(request, "rtzo")); } @Override diff --git a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java index 30eaf4e921..668f3892a0 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractApplicationServlet.java @@ -574,7 +574,7 @@ public abstract class AbstractApplicationServlet extends HttpServlet implements browser.updateBrowserProperties(request.getLocale(), request.getRemoteAddr(), request.isSecure(), request.getHeader("user-agent"), request.getParameter("sw"), - request.getParameter("sh")); + request.getParameter("sh"), request.getParameter("tzo"),request.getParameter("rtzo")); } protected ClassLoader getClassLoader() throws ServletException { diff --git a/src/com/vaadin/terminal/gwt/server/WebBrowser.java b/src/com/vaadin/terminal/gwt/server/WebBrowser.java index 1d6881a836..59cb6e6903 100644 --- a/src/com/vaadin/terminal/gwt/server/WebBrowser.java +++ b/src/com/vaadin/terminal/gwt/server/WebBrowser.java @@ -25,6 +25,8 @@ public class WebBrowser implements Terminal { private Locale locale; private String address; private boolean secureConnection; + private int timezoneOffset = 0; + private int rawTimezoneOffset = 0; private VBrowserDetails browserDetails; @@ -81,9 +83,14 @@ public class WebBrowser implements Terminal { * Screen width * @param sh * Screen height + * @param tzo + * TimeZone offset in minutes from GMT + * @param rtzo + * raw TimeZone offset in minutes from GMT (w/o DST adjustment) */ void updateBrowserProperties(Locale locale, String address, - boolean secureConnection, String agent, String sw, String sh) { + boolean secureConnection, String agent, String sw, String sh, + String tzo, String rtzo) { this.locale = locale; this.address = address; this.secureConnection = secureConnection; @@ -100,6 +107,22 @@ public class WebBrowser implements Terminal { screenHeight = screenWidth = 0; } } + if (tzo != null) { + try { + // browser->java conversion: min->ms, reverse sign + timezoneOffset = -Integer.parseInt(tzo) * 60 * 1000; + } catch (final NumberFormatException e) { + timezoneOffset = 0; // default gmt+0 + } + } + if (rtzo != null) { + try { + // browser->java conversion: min->ms, reverse sign + rawTimezoneOffset = -Integer.parseInt(rtzo) * 60 * 1000; + } catch (final NumberFormatException e) { + rawTimezoneOffset = 0; // default gmt+0 + } + } } /** @@ -256,4 +279,38 @@ public class WebBrowser implements Terminal { return browserDetails.isWindows(); } + /** + * Returns the browser-reported TimeZone offset in milliseconds from GMT. + * This includes possible daylight saving adjustments, to figure out which + * TimeZone the user actually might be in, see + * {@link #getRawTimezoneOffset()}. + * + * @see WebBrowser#getRawTimezoneOffset() + * @return timezone offset in milliseconds, 0 if not available + */ + public Integer getTimezoneOffset() { + return timezoneOffset; + } + + /** + * Returns the browser-reported TimeZone offset in milliseconds from GMT + * ignoring possible daylight saving adjustments that may be in effect in + * the browser. + * <p> + * You can use this to figure out which TimeZones the user could actually be + * in by calling {@link TimeZone#getAvailableIDs(int)}. + * </p> + * <p> + * If {@link #getRawTimezoneOffset()} and {@link #getTimezoneOffset()} + * returns the same value, the browser is either in a zone that does not + * currently have daylight saving time, or in a zone that never has daylight + * saving time. + * </p> + * + * @return timezone offset in milliseconds excluding DST, 0 if not available + */ + public Integer getRawTimezoneOffset() { + return rawTimezoneOffset; + } + } |