001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.core;
010:
011: import com.google.gwt.core.client.GWT;
012: import com.google.gwt.core.client.JavaScriptObject;
013: import com.gwtext.client.widgets.QuickTips;
014: import com.gwtext.client.widgets.form.Field;
015:
016: /**
017: * Base class for representing a native Javascript class. <b>Note</b> : For internal use only.
018: */
019: public abstract class JsObject {
020: // Ext global initialization code
021: static {
022: Ext
023: .setBlankImageUrl(GWT.getModuleBaseURL()
024: + "clear.cache.gif");
025:
026: // setup Ext Function prototypes for the GWT scope
027: // see http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/3412d58a3c7a5e0d/f5f7bbe5754513aa#f5f7bbe5754513aa
028: init();
029: QuickTips.init();
030: Field.setMsgTarget("side");
031: }
032:
033: private native static void initConstants() /*-{
034: @com.gwtext.client.core.EventObject::BACKSPACE = $wnd.Ext.EventObject.BACKSPACE;
035: @com.gwtext.client.core.EventObject::CONTROL = $wnd.Ext.EventObject.CONTROL;
036: @com.gwtext.client.core.EventObject::DELETE = $wnd.Ext.EventObject.DELETE;
037: @com.gwtext.client.core.EventObject::DOWN = $wnd.Ext.EventObject.DOWN;
038: @com.gwtext.client.core.EventObject::END = $wnd.Ext.EventObject.END;
039: @com.gwtext.client.core.EventObject::ENTER = $wnd.Ext.EventObject.ENTER;
040: @com.gwtext.client.core.EventObject::ESC = $wnd.Ext.EventObject.ESC;
041: @com.gwtext.client.core.EventObject::F5 = $wnd.Ext.EventObject.F5;
042: @com.gwtext.client.core.EventObject::HOME = $wnd.Ext.EventObject.HOME;
043: @com.gwtext.client.core.EventObject::LEFT = $wnd.Ext.EventObject.LEFT;
044: @com.gwtext.client.core.EventObject::PAGEDOWN = $wnd.Ext.EventObject.PAGEDOWN;
045: @com.gwtext.client.core.EventObject::PAGEUP = $wnd.Ext.EventObject.PAGEUP;
046: @com.gwtext.client.core.EventObject::RETURN = $wnd.Ext.EventObject.RETURN;
047: @com.gwtext.client.core.EventObject::RIGHT = $wnd.Ext.EventObject.RIGHT;
048: @com.gwtext.client.core.EventObject::SHIFT = $wnd.Ext.EventObject.SHIFT;
049: @com.gwtext.client.core.EventObject::SPACE = $wnd.Ext.EventObject.SPACE;
050: @com.gwtext.client.core.EventObject::TAB = $wnd.Ext.EventObject.TAB;
051: @com.gwtext.client.core.EventObject::UP = $wnd.Ext.EventObject.UP;
052: }-*/;
053:
054: private static native void init()/*-{
055: @com.gwtext.client.core.JsObject::initConstants()();
056:
057: Function.prototype.createCallback = function() {
058: // make args available, in function below
059: var args = arguments;
060: var method = this;
061: return function() {
062: return method.apply(window, args);
063: };
064: };
065:
066: Function.prototype.createDelegate = function(obj, args, appendArgs) {
067: var method = this;
068: return function() {
069: var callArgs = args || arguments;
070: if (appendArgs === true) {
071: callArgs = Array.prototype.slice.call(arguments, 0);
072: callArgs = callArgs.concat(args);
073: } else if (typeof appendArgs == "number") {
074: callArgs = Array.prototype.slice.call(arguments, 0);
075: // copy arguments first
076: var applyArgs = [appendArgs, 0].concat(args);
077: // create method call params
078: Array.prototype.splice.apply(callArgs, applyArgs);
079: // splice them in
080: }
081: return method.apply(obj || window, callArgs);
082: };
083: };
084:
085: Function.prototype.defer = function(millis, obj, args, appendArgs) {
086: var fn = this.createDelegate(obj, args, appendArgs);
087: if (millis) {
088: return setTimeout(fn, millis);
089: }
090: fn();
091: return 0;
092: };
093:
094: Function.prototype.createSequence = function(fcn, scope) {
095: if (typeof fcn != "function") {
096: return this;
097: }
098: var method = this;
099: return function() {
100: var retval = method.apply(this || window, arguments);
101: fcn.apply(scope || this || window, arguments);
102: return retval;
103: };
104: };
105:
106: Function.prototype.createInterceptor = function(fcn, scope) {
107: if (typeof fcn != "function") {
108: return this;
109: }
110: var method = this;
111: return function() {
112: fcn.target = this;
113: fcn.method = method;
114: if (fcn.apply(scope || this || window, arguments) === false) {
115: return;
116: }
117: return method.apply(this || window, arguments);
118: };
119: };
120:
121: $wnd.Ext.namespace("GwtExt");
122:
123: //convert javascript data types into corresponding Java wrapper types
124: //int -> Integer, float -> Float, boolean -> Boolean and date - > java.util.Date
125: $wnd.GwtExt.convertToJavaType = function(obj) {
126: if(obj == null || obj === undefined) return null;
127: if(typeof obj == 'string') {
128: return obj;
129: } else if (typeof obj == 'number') {
130: if(obj.toString().indexOf('.') == -1) {
131: if(obj <= @java.lang.Integer::MAX_VALUE) {
132: return @com.gwtext.client.util.JavaScriptObjectHelper::toInteger(I)(obj);
133: } else {
134: return @com.gwtext.client.util.JavaScriptObjectHelper::toLong(J)(obj);
135: }
136: } else {
137: if(obj <= @java.lang.Float::MAX_VALUE) {
138: return @com.gwtext.client.util.JavaScriptObjectHelper::toFloat(F)(obj);
139: } else {
140: return @com.gwtext.client.util.JavaScriptObjectHelper::toDouble(D)(obj);
141: }
142: }
143: } else if(typeof obj == 'boolean') {
144: return @com.gwtext.client.util.JavaScriptObjectHelper::toBoolean(Z)(obj);
145: } else if(obj instanceof $wnd.Date) {
146: return @com.gwtext.client.util.JavaScriptObjectHelper::toDate(J)(obj.getTime());
147: } else {
148: throw 'Unrecognized type ' + (typeof obj) + ' for value ' + obj.toString();
149: }
150: };
151: }-*/;
152:
153: protected JavaScriptObject jsObj;
154:
155: protected JsObject() {
156: }
157:
158: public JsObject(JavaScriptObject jsObj) {
159: this .jsObj = jsObj;
160: }
161:
162: protected boolean isCreated() {
163: return jsObj != null;
164: }
165:
166: public JavaScriptObject getJsObj() {
167: return jsObj;
168: }
169:
170: public void setJsObj(JavaScriptObject jsObj) {
171: this .jsObj = jsObj;
172: }
173:
174: public native String[] getProperties() /*-{
175: var jsObj = this.@com.gwtext.client.core.JsObject::getJsObj()();
176: return @com.gwtext.client.util.JavaScriptObjectHelper::getProperties(Lcom/google/gwt/core/client/JavaScriptObject;)(jsObj);
177: }-*/;
178:
179: }
|