001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client;
009:
010: import net.mygwt.ui.client.fx.FXStyle;
011: import net.mygwt.ui.client.messages.MyMessages;
012: import net.mygwt.ui.client.state.CookieProvider;
013: import net.mygwt.ui.client.state.StateManager;
014: import net.mygwt.ui.client.util.CSS;
015:
016: import com.google.gwt.core.client.GWT;
017: import com.google.gwt.user.client.DOM;
018: import com.google.gwt.user.client.Element;
019:
020: /**
021: * MyGWT core utilities and functions.
022: */
023: public class MyGWT {
024:
025: /**
026: * MyGWT messages.
027: */
028: public static MyMessages MESSAGES = (MyMessages) GWT
029: .create(MyMessages.class);
030:
031: /**
032: * defaultTheme specifies the default theme. Supported theme names are "gray"
033: * and "default". Default value is "default".
034: */
035: public static String defaultTheme = "default";
036:
037: /**
038: * <code>true</code> if the browser is safari.
039: */
040: public static boolean isSafari;
041:
042: /**
043: * <code>true</code> if the browser is opera.
044: */
045: public static boolean isOpera;
046:
047: /**
048: * <code>true</code> if the browser is ie.
049: */
050: public static boolean isIE;
051:
052: /**
053: * <code>true</code> if the browser is ie7.
054: */
055: public static boolean isIE7;
056:
057: /**
058: * <code>true</code> if the browser is gecko.
059: */
060: public static boolean isGecko;
061:
062: /**
063: * <code>true</code> if the browser is in strict mode.
064: */
065: public static boolean isStrict;
066:
067: /**
068: * <code>true</code> if using https.
069: */
070: public static boolean isSecure;
071:
072: /**
073: * <code>true</code> if mac os.
074: */
075: public static boolean isMac;
076:
077: /**
078: * <code>true</code> if linux os.
079: */
080: public static boolean isLinux;
081:
082: /**
083: * URL to a blank file used by MyGWT when in secure mode for iframe src to
084: * prevent the IE insecure content. Default value is 'blank.html'.
085: */
086: public static String SSL_SECURE_URL = GWT.getModuleBaseURL()
087: + "blank.html";
088:
089: /**
090: * URL to a 1x1 transparent gif image used by MyGWT to create inline icons
091: * with CSS background images. Default value is
092: * 'images/default/shared/clear.gif';
093: */
094: public static String BLANK_IMAGE_URL = GWT.getModuleBaseURL()
095: + "images/default/shared/clear.gif";
096:
097: private static boolean initialized;
098:
099: /**
100: * Returns the browser's user agent.
101: *
102: * @return the user agent
103: */
104: public native static String getUserAgent() /*-{
105: return $wnd.navigator.userAgent.toLowerCase();
106: }-*/;
107:
108: /**
109: * Sets whether the html used in MyDOM.setInnerHTML is escaped for cross-site
110: * scripting (XSS) exploits. Enabling will effect performance as all HTML must
111: * be parsed. Default value is <code>false</code>.
112: *
113: * @param escape <code>true</code> to escape html
114: */
115: public native static void enableEscapeHTML(boolean escape) /*-{
116: $wnd.escapeFlag = escape;
117: }-*/;
118:
119: /**
120: * Changes the theme. Supported theme names are "gray" and "default".
121: *
122: * @param theme the new theme name.
123: */
124: public static void switchTheme(String theme) {
125: if (theme.equals("gray")) {
126: CSS.addStyleSheet("mygwt-all-gray", "mygwt-all-gray.css");
127: } else {
128: CSS.removeStyleSheet("mygwt-all-gray");
129: }
130: StateManager.set("theme", theme.equals("gray") ? "gray"
131: : "default");
132: }
133:
134: /**
135: * Returns the current theme.
136: *
137: * @return the theme
138: */
139: public static String getTheme() {
140: return StateManager.getString("theme");
141: }
142:
143: /**
144: * Hides the loading panel.
145: *
146: * @param id the loading panel id
147: */
148: public static void hideLoadingPanel(String id) {
149: final Element loading = DOM.getElementById(id);
150: if (loading != null) {
151: FXStyle fx = new FXStyle(loading);
152: fx.duration = 300;
153: fx.hideOnComplete = true;
154: fx.fadeOut();
155: }
156: }
157:
158: /**
159: * Initializes MyGWT.
160: */
161: public static void init() {
162: if (initialized) {
163: return;
164: }
165: initialized = true;
166: String ua = getUserAgent();
167: isSafari = ua.indexOf("webkit") != -1;
168: isOpera = ua.indexOf("opera") != -1;
169: isIE = ua.indexOf("msie") != -1;
170: isIE7 = ua.indexOf("msie 7") != -1;
171: isGecko = ua.indexOf("gecko") != -1;
172: isMac = ua.indexOf("macintosh") != -1
173: || ua.indexOf("mac os x") != -1;
174: isLinux = ua.indexOf("linux") != -1;
175:
176: String mode = DOM.getElementProperty(MyDOM.getDocument(),
177: "compatMode");
178: isStrict = mode != null ? mode.equals("CSS1Compat") : false;
179:
180: isSecure = isSecure();
181:
182: String cls = "";
183: if (isIE) {
184: cls = "ext-ie";
185: } else if (isGecko) {
186: cls = "ext-gecko";
187: } else if (isOpera) {
188: cls = "ext-opera";
189: } else if (isSafari) {
190: cls = "ext-safari";
191: }
192:
193: if (isMac) {
194: cls += " ext-mac";
195: }
196:
197: if (isLinux) {
198: cls += " ext-linux";
199: }
200:
201: MyDOM.setStyleName(MyDOM.getBody(), cls);
202:
203: CookieProvider provider = new CookieProvider("/", null, null,
204: false);
205: StateManager.setProvider(provider);
206:
207: String theme = StateManager.getString("theme");
208: if (theme == null || theme.equals("")) {
209: theme = defaultTheme;
210: }
211:
212: initInternal(theme);
213: }
214:
215: private static native void initInternal(String theme) /*-{
216: var links = $doc.getElementsByTagName('link');
217: for (var i = 0; i < links.length; i++) {
218: var link = links[i];
219: var href = link.href;
220: href = href.substring(href.lastIndexOf('/') + 1, href.length);
221: if (href == 'mygwt-all.css') {
222: link.setAttribute('id','mygwt-all');
223: }
224: if (href == 'mygwt-all-gray.css') {
225: link.setAttribute('id','mygwt-all-gray');
226: if (theme != 'gray') {
227: link.setAttribute('disabled', true);
228: link.parentNode.removeChild(link);
229: }
230: }
231: }
232: }-*/;
233:
234: private static native boolean isSecure() /*-{
235: return $wnd.location.href.toLowerCase().indexOf("https") === 0;
236: }-*/;
237:
238: }
|