001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.plaf;
014:
015: import java.beans.PropertyChangeListener;
016: import java.beans.PropertyChangeSupport;
017: import java.util.HashMap;
018: import java.util.Map;
019: import java.util.Properties;
020:
021: /**
022: * A Property table that stores default. This table overrides the
023: * mappings of its <code>parent</code> table.
024: */
025: public class ResourceDefaults {
026: private PropertyChangeSupport changeSupport;
027: private final ResourceDefaults parent;
028: private final ResourceFactory factory;
029: private final Map<Object, Object> storage;
030:
031: /**
032: * @param parent the parent defaults table that backs this defaults table
033: */
034: public ResourceDefaults(ResourceDefaults parent,
035: Properties properties) {
036: this .parent = parent;
037: this .factory = new ResourceFactory(properties);
038: this .storage = new HashMap<Object, Object>();
039: }
040:
041: /**
042: * Set the value of <code>key</code> to <code>value</code>.
043: * If <code>key</code> is a string and the new value isn't
044: * equal to the old one, fire a PropertyChangeEvent. If value
045: * is null, the key is removed from the table.
046: *
047: * @param key the unique Object who's value will be used to
048: * retreive the data value associated with it
049: * @param value the new Object to store as data under that key
050: * @return the previous Object value, or null
051: * @see java.util.Map#put
052: */
053: public Object put(Object key, Object value) {
054: Object oldValue = (value == null) ? storage.remove(key)
055: : storage.put(key, value);
056: if (key instanceof String) {
057: firePropertyChange((String) key, oldValue, value);
058: }
059: return oldValue;
060: }
061:
062: /**
063: * Get a value from the defaults table.
064: * If the <code>key</code> is not associated with a value,
065: * the request is delegated to the parent defaults table
066: *
067: * @param key the key
068: * @param type the class of the value in question
069: * @return the associated value or <code>null</code>
070: */
071: public Object get(Object key, Class type) {
072: Object value = storage.get(key);
073: if (value == null) {
074: // cached with a null value
075: if (storage.containsKey(key))
076: return null;
077:
078: // ask the parent
079: if (parent != null)
080: value = parent.get(key, type);
081:
082: // ask the factory
083: if (value == null && factory != null)
084: value = factory.get(key, type);
085: }
086:
087: storage.put(key, value);
088: return value;
089: }
090:
091: /**
092: * Add a PropertyChangeListener to the listener list.
093: * The listener is registered for all properties.
094: * <p/>
095: * A PropertyChangeEvent will get fired whenever a default
096: * is changed.
097: *
098: * @param listener The PropertyChangeListener to be added
099: * @see java.beans.PropertyChangeSupport
100: */
101: public synchronized void addPropertyChangeListener(
102: PropertyChangeListener listener) {
103: if (changeSupport == null) {
104: changeSupport = new PropertyChangeSupport(this );
105: }
106: changeSupport.addPropertyChangeListener(listener);
107: }
108:
109: /**
110: * Remove a PropertyChangeListener from the listener list.
111: * This removes a PropertyChangeListener that was registered
112: * for all properties.
113: *
114: * @param listener The PropertyChangeListener to be removed
115: * @see java.beans.PropertyChangeSupport
116: */
117: public synchronized void removePropertyChangeListener(
118: PropertyChangeListener listener) {
119: if (changeSupport != null) {
120: changeSupport.removePropertyChangeListener(listener);
121: }
122: }
123:
124: /**
125: * Support for reporting bound property changes. If oldValue and
126: * newValue are not equal and the PropertyChangeEvent listener list
127: * isn't empty, then fire a PropertyChange event to each listener.
128: *
129: * @param propertyName The programmatic name of the property that was changed.
130: * @param oldValue The old value of the property.
131: * @param newValue The new value of the property.
132: * @see java.beans.PropertyChangeSupport
133: */
134: protected void firePropertyChange(String propertyName,
135: Object oldValue, Object newValue) {
136: if (changeSupport != null) {
137: changeSupport.firePropertyChange(propertyName, oldValue,
138: newValue);
139: }
140: }
141: }
|