001: package jaxx.runtime.css;
002:
003: import java.util.*;
004:
005: import jaxx.runtime.*;
006:
007: public class Pseudoclasses {
008: public static final String NO_PSEUDOCLASS = "no pseudoclass";
009:
010: private static Map/*<Object, Map<String, List<PropertyValue>>>*/properties = new WeakHashMap/*<Object, Map<String, List<PropertyValue>>>*/();
011:
012: private static class PropertyValue implements Comparable {
013: private Object value;
014: private int id;
015:
016: public PropertyValue(Object value, int id) {
017: this .value = value;
018: this .id = id;
019: }
020:
021: public Object getValue() {
022: return value;
023: }
024:
025: public int getId() {
026: return id;
027: }
028:
029: public int compareTo(Object o) {
030: return getId() - ((PropertyValue) o).getId();
031: }
032:
033: public boolean equals(Object o) {
034: if (!(o instanceof PropertyValue))
035: return false;
036: PropertyValue value = (PropertyValue) o;
037: if (value.getId() != getId())
038: return false;
039: if (value.getValue() == null)
040: return getValue() == null;
041: else
042: return value.getValue().equals(getValue());
043: }
044:
045: public int hashCode() {
046: return (value != null ? value.hashCode() : 0) ^ id;
047: }
048:
049: public String toString() {
050: return "PropertyValue[" + value + ", " + id + "]";
051: }
052: }
053:
054: private static List/*<PropertyValue>*/getPropertyList(
055: Object object, String property) {
056: Map/*<String, List<PropertyValue>>*/propertyMap = (Map) properties
057: .get(object);
058: if (propertyMap == null) {
059: propertyMap = new HashMap/*<String, List<PropertyValue>>*/();
060: properties.put(object, propertyMap);
061: }
062:
063: List/*<PropertyValue>*/propertyList = (List) propertyMap
064: .get(property);
065: if (propertyList == null) {
066: propertyList = new ArrayList/*<PropertyValue>*/();
067: propertyMap.put(property, propertyList);
068: }
069:
070: return propertyList;
071: }
072:
073: public static boolean isPropertyApplied(Object object,
074: String property, int id) {
075: List/*<PropertyValue>*/propertyList = getPropertyList(object,
076: property);
077: for (int i = 0; i < propertyList.size(); i++)
078: if (((PropertyValue) propertyList.get(i)).getId() == id)
079: return true;
080: return false;
081: }
082:
083: public static void propertyApplied(Object object, String property,
084: Object value, int id) {
085: List/*<PropertyValue>*/propertyList = getPropertyList(object,
086: property);
087: propertyList.add(new PropertyValue(value, id));
088: Collections.sort(propertyList);
089: }
090:
091: public static void propertyRemoved(Object object, String property,
092: Object value, int id) {
093: List/*<PropertyValue>*/propertyList = getPropertyList(object,
094: property);
095: propertyList.remove(new PropertyValue(value, id));
096: }
097:
098: public static Object getCurrentValue(Object object, String property) {
099: List/*<PropertyValue>*/propertyList = getPropertyList(object,
100: property);
101: if (propertyList.size() > 0)
102: return ((PropertyValue) propertyList.get(propertyList
103: .size() - 1)).getValue();
104: return NO_PSEUDOCLASS;
105: }
106:
107: public static Object applyProperty(JAXXObject parent,
108: Object object, String property, Object newValue,
109: Object currentValue, int id) {
110: if (!isPropertyApplied(object, property, id)) {
111: Object value = getCurrentValue(object, property);
112: if (value == NO_PSEUDOCLASS)
113: propertyApplied(object, property, wrap(currentValue),
114: -1);
115: propertyApplied(object, property, wrap(newValue), id);
116: value = getCurrentValue(object, property);
117: if (value instanceof DataBinding)
118: parent.applyDataBinding(((DataBinding) value).getId());
119: return value;
120: } else
121: return currentValue;
122: }
123:
124: public static Object removeProperty(JAXXObject parent,
125: Object object, String property, Object oldValue,
126: Object currentValue, int id) {
127: if (isPropertyApplied(object, property, id)) {
128: Object value = getCurrentValue(object, property);
129: if (value == NO_PSEUDOCLASS)
130: throw new java.lang.IllegalStateException(
131: "found NO_PSEUDOCLASS value for a property which does not have a default value");
132: if (value instanceof DataBinding)
133: parent.removeDataBinding(((DataBinding) value).getId());
134: propertyRemoved(object, property, wrap(oldValue), id);
135: value = getCurrentValue(object, property);
136: return value;
137: } else
138: return currentValue;
139: }
140:
141: public static Object wrap(boolean value) {
142: return new Boolean(value);
143: }
144:
145: public static Object wrap(byte value) {
146: return new Byte(value);
147: }
148:
149: public static Object wrap(short value) {
150: return new Short(value);
151: }
152:
153: public static Object wrap(int value) {
154: return new Integer(value);
155: }
156:
157: public static Object wrap(long value) {
158: return new Long(value);
159: }
160:
161: public static Object wrap(float value) {
162: return new Float(value);
163: }
164:
165: public static Object wrap(double value) {
166: return new Double(value);
167: }
168:
169: public static Object wrap(char value) {
170: return new Character(value);
171: }
172:
173: public static Object wrap(Object value) {
174: return value;
175: }
176: }
|