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.util;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.google.gwt.user.client.Element;
013: import com.gwtext.client.core.Function;
014: import com.gwtext.client.core.JsObject;
015: import com.gwtext.client.widgets.Component;
016: import com.gwtext.client.widgets.ComponentFactory;
017:
018: import java.util.Date;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Map;
022:
023: /**
024: * Internal helper class.
025: */
026: public class JavaScriptObjectHelper {
027:
028: private JavaScriptObjectHelper() {
029: }
030:
031: public static native String getAttribute(JavaScriptObject elem,
032: String attr) /*-{
033: var ret = elem[attr];
034: return (ret === undefined) ? null : String(ret);
035: }-*/;
036:
037: public static native void setAttribute(JavaScriptObject elem,
038: String attr, String value) /*-{
039: elem[attr] = value;
040: }-*/;
041:
042: public static native JavaScriptObject getAttributeAsJavaScriptObject(
043: JavaScriptObject elem, String attr) /*-{
044: var ret = elem[attr];
045: return (ret === undefined) ? null : ret;
046: }-*/;
047:
048: public static native JavaScriptObject[] getAttributeAsJavaScriptObjectArray(
049: JavaScriptObject elem, String attr) /*-{
050: var arrayJS = elem[attr];
051: return (arrayJS === undefined) ? null : @com.gwtext.client.util.JavaScriptObjectHelper::toArray(Lcom/google/gwt/core/client/JavaScriptObject;)(arrayJS);
052: }-*/;
053:
054: public static JavaScriptObject[] toArray(JavaScriptObject array) {
055: int length = getJavaScriptObjectArraySize(array);
056: JavaScriptObject[] recs = new JavaScriptObject[length];
057: for (int i = 0; i < length; i++) {
058: recs[i] = getValueFromJavaScriptObjectArray(array, i);
059: }
060: return recs;
061: }
062:
063: public static Element[] toElementArray(JavaScriptObject array) {
064: int length = getJavaScriptObjectArraySize(array);
065: Element[] recs = new Element[length];
066: for (int i = 0; i < length; i++) {
067: recs[i] = getElementValueFromJavaScriptObjectArray(array, i);
068: }
069: return recs;
070: }
071:
072: public static native void setAttribute(JavaScriptObject elem,
073: String attr, JavaScriptObject[] value) /*-{
074: elem[attr] = value;
075: }-*/;
076:
077: public static void setAttribute(JavaScriptObject elem, String attr,
078: int[] values) {
079: setAttribute(elem, attr, JavaScriptObjectHelper
080: .convertToJavaScriptArray(values));
081: }
082:
083: public static void setAttribute(JavaScriptObject elem, String attr,
084: String[] values) {
085: setAttribute(elem, attr, JavaScriptObjectHelper
086: .convertToJavaScriptArray(values));
087: }
088:
089: public static void setAttribute(JavaScriptObject elem, String attr,
090: Date[] values) {
091: setAttribute(elem, attr, JavaScriptObjectHelper
092: .convertToJavaScriptArray(values));
093: }
094:
095: public static native void setAttribute(JavaScriptObject elem,
096: String attr, Object value) /*-{
097: elem[attr] = value;
098: }-*/;
099:
100: public static native void setAttribute(JavaScriptObject elem,
101: String attr, JavaScriptObject value) /*-{
102: elem[attr] = value;
103: }-*/;
104:
105: public static native void setAttribute(JavaScriptObject elem,
106: String attr, int value) /*-{
107: elem[attr] = value;
108: }-*/;
109:
110: public static native void setAttribute(JavaScriptObject elem,
111: String attr, boolean value) /*-{
112: elem[attr] = value;
113: }-*/;
114:
115: public static native void setAttribute(JavaScriptObject elem,
116: String attr, float value) /*-{
117: elem[attr] = value;
118: }-*/;
119:
120: public static native void setAttribute(JavaScriptObject elem,
121: String attr, double value) /*-{
122: elem[attr] = value;
123: }-*/;
124:
125: public static native void setAttribute(JavaScriptObject elem,
126: String attr, Function handler) /*-{
127: elem[attr] = function() {
128: handler.@com.gwtext.client.core.Function::execute()();
129: };
130: }-*/;
131:
132: public static void setAttribute(JavaScriptObject elem, String attr,
133: Date value) {
134: if (value == null) {
135: setAttribute(elem, attr, (String) null);
136: } else {
137: setDateAttribute(elem, attr, value.getTime());
138: }
139: }
140:
141: private static native void setDateAttribute(JavaScriptObject elem,
142: String attr, long time) /*-{
143: elem[attr] = new $wnd.Date(time);
144: }-*/;
145:
146: public static native void setObjectAttribute(JavaScriptObject elem,
147: String attr, Object object) /*-{
148: elem[attr] = object;
149: }-*/;
150:
151: public static native Element getAttributeAsElement(
152: JavaScriptObject elem, String attr) /*-{
153: var ret = elem[attr];
154: return (ret === undefined) ? null : ret;
155: }-*/;
156:
157: public static native int getAttributeAsInt(JavaScriptObject elem,
158: String attr) /*-{
159: var ret = elem[attr];
160: return (ret === undefined) ? 0 : ret;
161: }-*/;
162:
163: public static native Date getAttributeAsDate(JavaScriptObject elem,
164: String attr) /*-{
165: var ret = elem[attr];
166: return (ret === undefined || ret == null) ? null: @com.gwtext.client.util.JavaScriptObjectHelper::toDate(J)(ret.getTime());
167: }-*/;
168:
169: public static native float getAttributeAsFloat(
170: JavaScriptObject elem, String attr) /*-{
171: var ret = elem[attr];
172: return (ret === undefined) ? 0 : ret;
173: }-*/;
174:
175: public static int[] getAttributeAsIntArray(JavaScriptObject elem,
176: String attr) {
177: int[] rtn = null;
178: JavaScriptObject hold = getAttributeAsJavaScriptObject(elem,
179: attr);
180:
181: if (hold != null) {
182: rtn = new int[getJavaScriptObjectArraySize(hold)];
183:
184: for (int i = 0; i < rtn.length; i++) {
185: rtn[i] = getIntValueFromJavaScriptObjectArray(hold, i);
186: }
187: }
188: return rtn;
189: }
190:
191: public static String[] getAttributeAsStringArray(
192: JavaScriptObject elem, String attr) {
193: String[] rtn = null;
194: JavaScriptObject hold = getAttributeAsJavaScriptObject(elem,
195: attr);
196:
197: if (hold != null) {
198: rtn = new String[getJavaScriptObjectArraySize(hold)];
199:
200: for (int i = 0; i < rtn.length; i++) {
201: rtn[i] = getStringValueFromJavaScriptObjectArray(hold,
202: i);
203: }
204: }
205: return rtn;
206: }
207:
208: public static native int getJavaScriptObjectArraySize(
209: JavaScriptObject elem) /*-{
210: if (elem) return elem.length;
211: return 0;
212: }-*/;
213:
214: public static native int getIntValueFromJavaScriptObjectArray(
215: JavaScriptObject elem, int i) /*-{
216: return elem[i];
217: }-*/;
218:
219: public static native String getStringValueFromJavaScriptObjectArray(
220: JavaScriptObject elem, int i) /*-{
221: return elem[i];
222: }-*/;
223:
224: public static native JavaScriptObject getValueFromJavaScriptObjectArray(
225: JavaScriptObject elem, int i) /*-{
226: return elem[i];
227: }-*/;
228:
229: public static native boolean getAttributeAsBoolean(
230: JavaScriptObject elem, String attr) /*-{
231: var ret = elem[attr];
232: return (ret === undefined) ? false : ret;
233: }-*/;
234:
235: public static native Object getAttributeAsObject(
236: JavaScriptObject elem, String attr) /*-{
237: var ret = elem[attr];
238: return (ret === undefined) ? null : ret;
239: }-*/;
240:
241: public static JavaScriptObject[] listToArray(List list) {
242: JavaScriptObject[] array = new JavaScriptObject[list.size()];
243:
244: for (int i = 0; i < array.length; i++) {
245: array[i] = (JavaScriptObject) list.get(i);
246: }
247: return array;
248: }
249:
250: public static JavaScriptObject arrayConvert(Object[] array) {
251: JavaScriptObject result = newJSArray(array.length);
252:
253: for (int i = 0; i < array.length; i++) {
254: arraySet(result, i, array[i]);
255: }
256: return result;
257: }
258:
259: public static JavaScriptObject arrayConvert(JavaScriptObject[] array) {
260: JavaScriptObject result = newJSArray(array.length);
261:
262: for (int i = 0; i < array.length; i++) {
263: arraySet(result, i, array[i]);
264: }
265: return result;
266: }
267:
268: private static native JavaScriptObject newJSArray(int length) /*-{
269: if (length < 0) {
270: return new Array();
271: } else {
272: return new Array(length);
273: }
274: }-*/;
275:
276: public static native int arrayLength(JavaScriptObject array) /*-{
277: return array.length;
278: }-*/;
279:
280: public static native Object arrayGetObject(JavaScriptObject array,
281: int index) /*-{
282: return array[index];
283: }-*/;
284:
285: public static native void arraySet(JavaScriptObject array,
286: int index, Object value) /*-{
287: array[index] = value;
288: }-*/;
289:
290: public static native void arraySet(JavaScriptObject array,
291: int index, JavaScriptObject value) /*-{
292: array[index] = value;
293: }-*/;
294:
295: /**
296: * This is used to access Element array as JavaScriptObject
297: */
298: public static native Element getElementValueFromJavaScriptObjectArray(
299: JavaScriptObject elem, int i) /*-{
300: return elem[i];
301: }-*/;
302:
303: public static native JavaScriptObject createObject() /*-{
304: return new Object;
305: }-*/;
306:
307: public static JavaScriptObject convertToJavaScriptArray(int[] array) {
308: JavaScriptObject jsArray = createJavaScriptArray();
309: for (int i = 0; i < array.length; i++) {
310: JavaScriptObjectHelper.setArrayValue(jsArray, i, array[i]);
311: }
312: return jsArray;
313: }
314:
315: public static JavaScriptObject convertToJavaScriptConfigArray(
316: Component[] components) {
317: JavaScriptObject jsArray = createJavaScriptArray();
318: for (int i = 0; i < components.length; i++) {
319: Component component = components[i];
320: JavaScriptObjectHelper.setArrayValue(jsArray, i, component
321: .getConfig());
322: }
323: return jsArray;
324: }
325:
326: public static JavaScriptObject convertToJavaScriptArray(
327: Object[] array) {
328: JavaScriptObject jsArray = createJavaScriptArray();
329: for (int i = 0; i < array.length; i++) {
330: Object val = array[i];
331: if (val instanceof String) {
332: JavaScriptObjectHelper.setArrayValue(jsArray, i,
333: (String) val);
334: } else if (val instanceof Integer) {
335: JavaScriptObjectHelper.setArrayValue(jsArray, i,
336: ((Integer) val).intValue());
337: } else if (val instanceof Float) {
338: JavaScriptObjectHelper.setArrayValue(jsArray, i,
339: ((Float) val).floatValue());
340: } else if (val instanceof Double) {
341: JavaScriptObjectHelper.setArrayValue(jsArray, i,
342: ((Double) val).doubleValue());
343: } else if (val instanceof Boolean) {
344: JavaScriptObjectHelper.setArrayValue(jsArray, i,
345: ((Boolean) val).booleanValue());
346: } else if (val instanceof Date) {
347: JavaScriptObjectHelper.setArrayValue(jsArray, i,
348: (Date) val);
349: } else if (val instanceof JavaScriptObject) {
350: JavaScriptObjectHelper.setArrayValue(jsArray, i,
351: ((JavaScriptObject) val));
352: } else if (val instanceof JsObject) {
353: JavaScriptObjectHelper.setArrayValue(jsArray, i,
354: ((JsObject) val).getJsObj());
355: } else if (val instanceof Object[]) {
356: JavaScriptObjectHelper.setArrayValue(jsArray, i,
357: convertToJavaScriptArray((Object[]) val));
358: } else if (val instanceof Object) {
359: JavaScriptObjectHelper.setArrayValue(jsArray, i,
360: ((Object) val));
361: }
362: }
363: return jsArray;
364: }
365:
366: public static Integer toInteger(int value) {
367: return new Integer(value);
368: }
369:
370: public static Long toLong(long value) {
371: return new Long(value);
372: }
373:
374: public static Float toFloat(float value) {
375: return new Float(value);
376: }
377:
378: public static Double toDouble(double value) {
379: return new Double(value);
380: }
381:
382: public static Date toDate(long millis) {
383: return new Date(millis);
384: }
385:
386: public static Boolean toBoolean(boolean value) {
387: return Boolean.valueOf(value);
388: }
389:
390: public static native JavaScriptObject createJavaScriptArray() /*-{
391: //Important : constructing an from JSNI array using [] or new Array() results in a
392: //corrupted array object in the final javascript. The array ends up havign the correct elements
393: //but the test (myarr instaneof Array) fails because the jsni created array constructor is different.
394: //Need to construct array within the scope of the applications iframe by using new $wnd.Array
395: //return new $wnd.Array();
396: //to work around safari bug, call funtion in main window that returns new Array
397: //see http://bugs.webkit.org/show_bug.cgi?id=17250
398: //http://extjs.com/forum/showthread.php?t=25874
399: return $wnd.newArray();
400: }-*/;
401:
402: public static void setArrayValue(JavaScriptObject array, int index,
403: Date value) {
404: setArrayDateValue(array, index, value.getTime());
405: }
406:
407: private static native void setArrayDateValue(
408: JavaScriptObject array, int index, long time) /*-{
409: array[index] = new $wnd.Date(time);
410: }-*/;
411:
412: public static native void setArrayValue(JavaScriptObject array,
413: int index, String value) /*-{
414: array[index] = value;
415: }-*/;
416:
417: public static native void setArrayValue(JavaScriptObject array,
418: int index, double value) /*-{
419: array[index] = value;
420: }-*/;
421:
422: public static native void setArrayValue(JavaScriptObject array,
423: int index, float value) /*-{
424: array[index] = value;
425: }-*/;
426:
427: public static native void setArrayValue(JavaScriptObject array,
428: int index, boolean value) /*-{
429: array[index] = value;
430: }-*/;
431:
432: public static native void setArrayValue(JavaScriptObject array,
433: int index, JavaScriptObject value) /*-{
434: array[index] = value;
435: }-*/;
436:
437: public static native void setArrayValue(JavaScriptObject array,
438: int index, Object value) /*-{
439: array[index] = value;
440: }-*/;
441:
442: public static native String getArrayValue(JavaScriptObject array,
443: int index) /*-{
444: var result = array[index];
445: return (result == null || result === undefined) ? null : result;
446: }-*/;
447:
448: public static native int getIntArrayValue(JavaScriptObject array,
449: int index) /*-{
450: return array[index];
451: }-*/;
452:
453: public static native int getArrayLength(JavaScriptObject array) /*-{
454: return array.length;
455: }-*/;
456:
457: public static int[] convertToJavaIntArray(JavaScriptObject array) {
458: int length = getArrayLength(array);
459: int[] arr = new int[length];
460: for (int i = 0; i < length; i++) {
461: arr[i] = getIntArrayValue(array, i);
462: }
463: return arr;
464: }
465:
466: public static String[] convertToJavaStringArray(
467: JavaScriptObject array) {
468: if (array == null)
469: return new String[] {};
470: int length = getArrayLength(array);
471: String[] arr = new String[length];
472: for (int i = 0; i < length; i++) {
473: arr[i] = getArrayValue(array, i);
474: }
475: return arr;
476: }
477:
478: public static Component[] convertToJavaComponentArray(
479: JavaScriptObject nativeArray) {
480: if (nativeArray == null) {
481: return new Component[] {};
482: }
483: JavaScriptObject[] componentsj = JavaScriptObjectHelper
484: .toArray(nativeArray);
485: Component[] components = new Component[componentsj.length];
486: for (int i = 0; i < componentsj.length; i++) {
487: JavaScriptObject componentJS = componentsj[i];
488: components[i] = ComponentFactory.getComponent(componentJS);
489: }
490: return components;
491: }
492:
493: public static native void apply(JavaScriptObject config,
494: JavaScriptObject jsObj) /*-{
495: for(var k in config) {
496: jsObj[k] = config[k];
497: }
498: }-*/;
499:
500: public static void setAttribute(JavaScriptObject jsObj,
501: String attr, Map valueMap) {
502: JavaScriptObject valueJS = convertMapToJavascriptObject(valueMap);
503: setAttribute(jsObj, attr, valueJS);
504: }
505:
506: public static JavaScriptObject convertMapToJavascriptObject(
507: Map valueMap) {
508: JavaScriptObject valueJS = JavaScriptObjectHelper
509: .createObject();
510: for (Iterator iterator = valueMap.keySet().iterator(); iterator
511: .hasNext();) {
512: String key = (String) iterator.next();
513: Object value = valueMap.get(key);
514: if (value instanceof JsObject) {
515: setAttribute(valueJS, key, ((JsObject) value)
516: .getJsObj());
517: } else if (value instanceof Date) {
518: setAttribute(valueJS, key, ((Date) value));
519: } else if (value instanceof Number) {
520: setAttribute(valueJS, key, ((Number) value)
521: .doubleValue());
522: } else if (value instanceof String) {
523: setAttribute(valueJS, key, ((String) value));
524: } else if (value instanceof Boolean) {
525: setAttribute(valueJS, key, ((Boolean) value)
526: .booleanValue());
527: } else {
528: throw new IllegalArgumentException(
529: "Unsupported type for attribute " + key + " : "
530: + value);
531: }
532: }
533: return valueJS;
534: }
535:
536: public static native String[] getProperties(JavaScriptObject jsObj) /*-{
537: var props = @com.gwtext.client.util.JavaScriptObjectHelper::createJavaScriptArray()();
538: for(var k in jsObj) {
539: props.push(k);
540: }
541: return @com.gwtext.client.util.JavaScriptObjectHelper::convertToJavaStringArray(Lcom/google/gwt/core/client/JavaScriptObject;)(props);
542: }-*/;
543:
544: public static native String getPropertiesAsString(
545: JavaScriptObject jsObj) /*-{
546: var props = '{';
547: for(var k in jsObj) {
548: props += '\n' + k;
549: }
550: return props + '}';
551: }-*/;
552:
553: }
|