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.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013:
014: import java.util.Date;
015:
016: /**
017: * A generic class used a various places that represents a name / value pair.
018: */
019: public class NameValuePair extends JsObject {
020:
021: public static final int STRING = 0;
022: public static final int BOOLEAN = 1;
023: public static final int FLOAT = 2;
024: public static final int INT = 3;
025: public static final int DATE = 4;
026:
027: private int type = STRING;
028: private static final String NAME = "name";
029: private static final String VALUE = "value";
030:
031: public NameValuePair(JavaScriptObject jsObj) {
032: super (jsObj);
033: }
034:
035: public NameValuePair(String name, String value) {
036: jsObj = JavaScriptObjectHelper.createObject();
037: JavaScriptObjectHelper.setAttribute(jsObj, NAME, name);
038: JavaScriptObjectHelper.setAttribute(jsObj, VALUE, value);
039: type = STRING;
040: }
041:
042: public NameValuePair(String name, Boolean value) {
043: this (name, value.booleanValue());
044: }
045:
046: public NameValuePair(String name, boolean value) {
047: jsObj = JavaScriptObjectHelper.createObject();
048: JavaScriptObjectHelper.setAttribute(jsObj, NAME, name);
049: JavaScriptObjectHelper.setAttribute(jsObj, VALUE, value);
050: type = BOOLEAN;
051: }
052:
053: public NameValuePair(String name, Float value) {
054: this (name, value.floatValue());
055:
056: }
057:
058: public NameValuePair(String name, float value) {
059: jsObj = JavaScriptObjectHelper.createObject();
060: JavaScriptObjectHelper.setAttribute(jsObj, NAME, name);
061: JavaScriptObjectHelper.setAttribute(jsObj, VALUE, value);
062: type = FLOAT;
063: }
064:
065: public NameValuePair(String name, Integer value) {
066: this (name, value.intValue());
067: }
068:
069: public NameValuePair(String name, int value) {
070: jsObj = JavaScriptObjectHelper.createObject();
071: JavaScriptObjectHelper.setAttribute(jsObj, NAME, name);
072: JavaScriptObjectHelper.setAttribute(jsObj, VALUE, value);
073: type = INT;
074: }
075:
076: public NameValuePair(String name, Date value) {
077: jsObj = JavaScriptObjectHelper.createObject();
078: JavaScriptObjectHelper.setAttribute(jsObj, NAME, name);
079: JavaScriptObjectHelper.setAttribute(jsObj, VALUE, value);
080: type = DATE;
081: }
082:
083: private static NameValuePair instance(String name, String value) {
084: return new NameValuePair(name, value);
085: }
086:
087: public String getName() {
088: return JavaScriptObjectHelper.getAttribute(jsObj, NAME);
089: }
090:
091: public String getValue() {
092: return JavaScriptObjectHelper.getAttribute(jsObj, VALUE);
093: }
094:
095: public boolean getValueAsBoolean() {
096: return JavaScriptObjectHelper.getAttributeAsBoolean(jsObj,
097: VALUE);
098: }
099:
100: public float getValueAsFloat() {
101: return JavaScriptObjectHelper.getAttributeAsFloat(jsObj, VALUE);
102: }
103:
104: public int getValueAsInt() {
105: return JavaScriptObjectHelper.getAttributeAsInt(jsObj, VALUE);
106: }
107:
108: public Date getValueAsDate() {
109: return JavaScriptObjectHelper.getAttributeAsDate(jsObj, VALUE);
110: }
111:
112: public int getType() {
113: return type;
114: }
115:
116: public static JavaScriptObject getJsObj(
117: NameValuePair[] nameValuePairs) {
118: JavaScriptObject paramObj = JavaScriptObjectHelper
119: .createObject();
120: if (nameValuePairs == null)
121: return paramObj;
122:
123: for (int i = 0; i < nameValuePairs.length; i++) {
124: NameValuePair param = nameValuePairs[i];
125: switch (param.getType()) {
126: case STRING: {
127: JavaScriptObjectHelper.setAttribute(paramObj, param
128: .getName(), param.getValue());
129: break;
130: }
131: case BOOLEAN: {
132: JavaScriptObjectHelper.setAttribute(paramObj, param
133: .getName(), param.getValueAsBoolean());
134: break;
135: }
136: case FLOAT: {
137: JavaScriptObjectHelper.setAttribute(paramObj, param
138: .getName(), param.getValueAsFloat());
139: break;
140: }
141: case INT: {
142: JavaScriptObjectHelper.setAttribute(paramObj, param
143: .getName(), param.getValueAsInt());
144: break;
145: }
146: case DATE: {
147: JavaScriptObjectHelper.setAttribute(paramObj, param
148: .getName(), param.getValueAsDate());
149: }
150: default: {
151: JavaScriptObjectHelper.setAttribute(paramObj, param
152: .getName(), param.getValue());
153: }
154: }
155: }
156: return paramObj;
157: }
158: }
|