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.state;
009:
010: import java.util.Date;
011:
012: import net.mygwt.ui.client.Events;
013: import net.mygwt.ui.client.event.BaseEvent;
014: import net.mygwt.ui.client.util.Observable;
015:
016: /**
017: * Abstract base class for state provider implementations. This class provides
018: * methods for encoding and decoding objects.
019: */
020: public abstract class Provider extends Observable {
021:
022: /**
023: * Clears a value.
024: *
025: * @param name the key name
026: */
027: public void clear(String name) {
028: clearKey(name);
029: BaseEvent be = new BaseEvent();
030: be.name = name;
031: fireEvent(Events.StateChange, be);
032: }
033:
034: /**
035: * Returns the current value for a key.
036: *
037: * @param name the key name
038: * @return the value
039: */
040: public Object get(String name) {
041: String val = getValue(name);
042: if (val == null)
043: return null;
044: Object obj = decodeValue(val);
045: return obj;
046: }
047:
048: /**
049: * Returns the current value for a key.
050: *
051: * @param name the key name
052: * @return the value
053: */
054: public String getString(String name) {
055: String val = getValue(name);
056: if (val == null)
057: return null;
058: String obj = (String) decodeValue(val);
059: return obj;
060: }
061:
062: /**
063: * Returns the current value for a key.
064: *
065: * @param name the key name
066: * @return the value
067: */
068: public boolean getBoolean(String name) {
069: String val = getValue(name);
070: if (val == null)
071: return false;
072: Boolean bVal = (Boolean) decodeValue(val);
073: return bVal.booleanValue();
074: }
075:
076: /**
077: * Returns the current value for a key.
078: *
079: * @param name the key name
080: * @return the value
081: */
082: public Date getDate(String name) {
083: String val = getValue(name);
084: if (val == null)
085: return null;
086: Date date = (Date) decodeValue(val);
087: return date;
088: }
089:
090: /**
091: * Returns the current value for a key.
092: *
093: * @param name the key name
094: * @return the value or -1
095: */
096: public int getInteger(String name) {
097: String val = getValue(name);
098: Integer iVal = (Integer) decodeValue(val);
099: if (iVal == null) {
100: return -1;
101: }
102: return iVal.intValue();
103: }
104:
105: /**
106: * Sets a key.
107: *
108: * @param name the key name
109: * @param value the value
110: */
111: public void set(String name, Object value) {
112: String val = encodeValue(value);
113: setValue(name, val);
114: BaseEvent be = new BaseEvent();
115: be.name = name;
116: be.value = value;
117: fireEvent(Events.StateChange, be);
118: }
119:
120: protected abstract void clearKey(String name);
121:
122: protected Object decodeValue(String value) {
123: if (value == null)
124: return null;
125: String type = value.substring(0, 2);
126: String val = value.substring(2);
127: if (type.equals("i:")) {
128: return Integer.decode(val);
129: } else if (type.equals("d:")) {
130: long time = Long.parseLong(val);
131: return new Date(time);
132: } else if (type.equals("b:")) {
133: return new Boolean(val);
134: }
135: return val;
136: }
137:
138: protected String encodeValue(Object value) {
139: if (value instanceof Date) {
140: return "d:" + ((Date) value).getTime();
141: } else if (value instanceof Integer) {
142: return "i:" + value;
143: } else if (value instanceof Float) {
144: return "i:" + value;
145: } else if (value instanceof Boolean) {
146: return "b:" + value;
147: }
148: return "s:" + value.toString();
149: }
150:
151: protected abstract String getValue(String name);
152:
153: protected abstract void setValue(String name, String value);
154:
155: }
|