]> source.dussan.org Git - vaadin-framework.git/commitdiff
Enhanced browser detection system.
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 11 Jun 2008 08:30:29 +0000 (08:30 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 11 Jun 2008 08:30:29 +0000 (08:30 +0000)
svn changeset:4853/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/BrowserInfo.java [new file with mode: 0644]
src/com/itmill/toolkit/terminal/gwt/client/Util.java

diff --git a/src/com/itmill/toolkit/terminal/gwt/client/BrowserInfo.java b/src/com/itmill/toolkit/terminal/gwt/client/BrowserInfo.java
new file mode 100644 (file)
index 0000000..e47708b
--- /dev/null
@@ -0,0 +1,129 @@
+/* 
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.terminal.gwt.client;
+
+/**
+ * Class used to query information about web browser.
+ * 
+ * Browser details are detected only once and those are stored in this singleton
+ * class.
+ * 
+ */
+public class BrowserInfo {
+
+    private static BrowserInfo instance;
+
+    /**
+     * Singleton method to get BrowserInfo object.
+     * 
+     * @return instance of BrowserInfo object
+     */
+    public static BrowserInfo get() {
+        if (instance == null) {
+            instance = new BrowserInfo();
+        }
+        return instance;
+    }
+
+    private boolean isGecko;
+    private boolean isAppleWebKit;
+    private boolean isSafari;
+    private boolean isOpera;
+    private boolean isIE;
+    private float ieVersion = -1;
+    private float geckoVersion = -1;
+    private float appleWebKitVersion = -1;
+
+    private BrowserInfo() {
+        try {
+            String ua = getBrowserString().toLowerCase();
+            // browser engine name
+            isGecko = ua.indexOf("gecko") != -1 && ua.indexOf("safari") == -1;
+            isAppleWebKit = ua.indexOf("applewebkit") != -1;
+
+            // browser name
+            isSafari = ua.indexOf("safari") != -1;
+            isOpera = ua.indexOf("opera") != -1;
+            isIE = ua.indexOf("msie") != -1 && !isOpera
+                    && (ua.indexOf("webtv") == -1);
+
+            if (isGecko) {
+                String tmp = ua.substring(ua.indexOf("rv:") + 3);
+                tmp = tmp.replaceFirst("(\\.[0-9]+).+", "$1");
+                geckoVersion = Float.parseFloat(tmp);
+            }
+            if (isAppleWebKit) {
+                String tmp = ua.substring(ua.indexOf("webkit/") + 7);
+                tmp = tmp.replaceFirst("([0-9]+)[^0-9].+", "$1");
+                appleWebKitVersion = Float.parseFloat(tmp);
+
+            }
+
+            if (isIE) {
+                String ieVersionString = ua.substring(ua.indexOf("msie ") + 5);
+                ieVersionString = ieVersionString.substring(0, ieVersionString
+                        .indexOf(";"));
+                ieVersion = Float.parseFloat(ieVersionString);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            ApplicationConnection.getConsole().error(e.getMessage());
+        }
+    }
+
+    public boolean isIE() {
+        return isIE;
+    }
+
+    public boolean isSafari() {
+        return isSafari;
+    }
+
+    public boolean isIE6() {
+        return isIE && ieVersion == 6;
+    }
+
+    public boolean isIE7() {
+        return isIE && ieVersion == 7;
+    }
+
+    public boolean isFF2() {
+        return isGecko && geckoVersion == 1.8;
+    }
+
+    public float getGeckoVersion() {
+        return geckoVersion;
+    }
+
+    public float getWebkitVersion() {
+        return appleWebKitVersion;
+    }
+
+    public float getIEVersion() {
+        return ieVersion;
+    }
+
+    public native static String getBrowserString()
+    /*-{
+        return $wnd.navigator.userAgent;
+    }-*/;
+
+    public static void test() {
+        Console c = ApplicationConnection.getConsole();
+
+        c.log("getBrowserString() " + get().getBrowserString());
+        c.log("isIE() " + get().isIE());
+        c.log("isIE6() " + get().isIE6());
+        c.log("isIE7() " + get().isIE7());
+        c.log("isFF2() " + get().isFF2());
+        c.log("isSafari() " + get().isSafari());
+        c.log("getGeckoVersion() " + get().getGeckoVersion());
+        c.log("getWebkitVersion() " + get().getWebkitVersion());
+        c.log("getIEVersion() " + get().getIEVersion());
+        c.log("isIE() " + get().isIE());
+        c.log("isIE() " + get().isIE());
+    }
+
+}
index 20e591ff9f4899439ec1af7601005ce86e822fa9..e1f70c4099cfeb5f1f1e03d5064c9b11a62b0229 100644 (file)
@@ -25,38 +25,6 @@ public class Util {
             debugger;
     }-*/;
 
-    /**
-     * Detects if current browser is IE.
-     * 
-     * @return true if IE
-     */
-    public static native boolean isIE()
-    /*-{
-       var browser=$wnd.navigator.appName;
-       if (browser=="Microsoft Internet Explorer") {
-           return true;
-       }
-       return false;
-    }-*/;
-
-    /**
-     * Detects if current browser is IE6.
-     * 
-     * @return true if IE6
-     */
-    public static native boolean isIE6()
-    /*-{
-    var browser=$wnd.navigator.appName;
-    if (browser=="Microsoft Internet Explorer") {
-       var ua = navigator.userAgent;
-               var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
-               if (re.exec(ua) != null)
-                       rv = parseFloat(RegExp.$1);
-                       if(rv == 6) return true;
-    }
-    return false;
-    }-*/;
-
     /**
      * Nulls oncontextmenu function on given element. We need to manually clear
      * context menu events due bad browsers memory leaks, since GWT don't
@@ -106,23 +74,43 @@ public class Util {
         return null;
     }
 
+    /**
+     * Detects if current browser is IE.
+     * 
+     * @deprecated use BrowserInfo class instead
+     * 
+     * @return true if IE
+     */
+    public static boolean isIE() {
+        return BrowserInfo.get().isIE();
+    }
+
+    /**
+     * Detects if current browser is IE6.
+     * 
+     * @deprecated use BrowserInfo class instead
+     * 
+     * @return true if IE6
+     */
+    public static boolean isIE6() {
+        return BrowserInfo.get().isIE6();
+    }
+
+    /**
+     * @deprecated use BrowserInfo class instead
+     * @return
+     */
     public static boolean isIE7() {
-        return isIE() && !isIE6();
+        return BrowserInfo.get().isIE7();
     }
 
-    public static native boolean isFF2()
-    /*-{
-    var browser=$wnd.navigator.appName;
-    if (browser=="Netscape") {
-        var ua = navigator.userAgent;
-        var re  = new RegExp("Firefox/([0-9]+)");
-        if (re.exec(ua) != null)
-            var rv = parseInt(RegExp.$1);
-            if(rv && rv == 2) 
-                return true;
+    /**
+     * @deprecated use BrowserInfo class instead
+     * @return
+     */
+    public static boolean isFF2() {
+        return BrowserInfo.get().isFF2();
     }
-    return false;
-    }-*/;
 
     private static final Element escapeHtmlHelper = DOM.createDiv();