/*
* MyGWT Widget Library
* Copyright(c) 2007, MyGWT.
* licensing@mygwt.net
*
* http://mygwt.net/license
*/
package net.mygwt.ui.client;
import net.mygwt.ui.client.fx.FXStyle;
import net.mygwt.ui.client.messages.MyMessages;
import net.mygwt.ui.client.state.CookieProvider;
import net.mygwt.ui.client.state.StateManager;
import net.mygwt.ui.client.util.CSS;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
/**
* MyGWT core utilities and functions.
*/
public class MyGWT {
/**
* MyGWT messages.
*/
public static MyMessages MESSAGES = (MyMessages) GWT.create(MyMessages.class);
/**
* <code>true</code> if the browser is safari.
*/
public static boolean isSafari;
/**
* <code>true</code> if the browser is opera.
*/
public static boolean isOpera;
/**
* <code>true</code> if the browser is ie.
*/
public static boolean isIE;
/**
* <code>true</code> if the browser is ie7.
*/
public static boolean isIE7;
/**
* <code>true</code> if the browser is gecko.
*/
public static boolean isGecko;
/**
* <code>true</code> if the browser is in strict mode.
*/
public static boolean isStrict;
/**
* <code>true</code> if using https.
*/
public static boolean isSecure;
/**
* <code>true</code> if mac os.
*/
public static boolean isMac;
/**
* <code>true</code> if linux os.
*/
public static boolean isLinux;
private static boolean initialized;
/**
* Returns the browser's user agent.
*
* @return the user agent
*/
public native static String getUserAgent() /*-{
return $wnd.navigator.userAgent.toLowerCase();
}-*/;
/**
* Changes the theme. Supported theme names are "gray" and "default".
*
* @param theme the new theme name.
*/
public static void switchTheme(String theme) {
if (theme.equals("gray")) {
CSS.addStyleSheet("mygwt-all-gray", "mygwt-all-gray.css");
} else {
CSS.removeStyleSheet("mygwt-all-gray");
}
StateManager.set("theme", theme.equals("gray") ? "gray" : "default");
}
/**
* Returns the current theme.
*
* @return the theme
*/
public static String getTheme() {
return StateManager.getString("theme");
}
/**
* Hides the loading panel.
*
* @param id the loading panel id
*/
public static void hideLoadingPanel(String id) {
final Element loading = DOM.getElementById(id);
if (loading != null) {
FXStyle fx = new FXStyle(loading);
fx.duration = 300;
fx.hideOnComplete = true;
fx.fadeOut();
}
}
/**
* Initializes MyGWT.
*/
public static void init() {
if (initialized) {
return;
}
initialized = true;
String ua = getUserAgent();
isSafari = ua.indexOf("webkit") != -1;
isOpera = ua.indexOf("opera") != -1;
isIE = ua.indexOf("msie") != -1;
isIE7 = ua.indexOf("msie 7") != -1;
isGecko = ua.indexOf("gecko") != -1;
isMac = ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1;
isLinux = ua.indexOf("linux") != -1;
String mode = DOM.getElementProperty(MyDOM.getDocument(), "compatMode");
isStrict = mode != null ? mode.equals("CSS1Compat") : false;
isSecure = isSecure();
String cls = "";
if (isIE) {
cls = "ext-ie";
} else if (isGecko) {
cls = "ext-gecko";
} else if (isOpera) {
cls = "ext-opera";
} else if (isSafari) {
cls = "ext-safari";
}
if (isMac) {
cls += " ext-mac";
}
if (isLinux) {
cls += " ext-linux";
}
MyDOM.setStyleName(MyDOM.getBody(), cls);
CookieProvider provider = new CookieProvider("/", null, null, false);
StateManager.setProvider(provider);
String theme = StateManager.getString("theme");
if (theme == null) {
theme = "default";
}
initInternal(theme);
}
private static native void initInternal(String theme) /*-{
var links = $doc.getElementsByTagName('link');
for (var i = 0; i < links.length; i++) {
var link = links[i];
var href = link.href;
href = href.substring(href.lastIndexOf('/') + 1, href.length);
if (href == 'mygwt-all.css') {
link.id = 'mygwt-all';
}
if (href == 'mygwt-all-gray.css') {
link.id = 'mygwt-all-gray';
if (theme != 'gray') {
link.parentNode.removeChild(link);
}
}
}
}-*/;
private static native boolean isSecure() /*-{
return $wnd.location.href.toLowerCase().indexOf("https") === 0;
}-*/;
}