KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > plaf > CGDefaults


1 /*
2  * $Id: CGDefaults.java,v 1.4 2004/12/01 07:54:09 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.plaf;
15
16 import java.beans.PropertyChangeListener JavaDoc;
17 import java.beans.PropertyChangeSupport JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 /**
21  * A Property table that stores default. This table overrides the
22  * mappings of its <code>parent</code> table.
23  */

24 public class CGDefaults
25         extends HashMap JavaDoc {
26     private PropertyChangeSupport JavaDoc changeSupport;
27     private CGDefaults parent;
28
29     /**
30      * @param parent the parent defaults table that backs this defaults table
31      */

32     public CGDefaults(CGDefaults parent) {
33         this.parent = parent;
34     }
35
36     /**
37      * Set the value of <code>key</code> to <code>value</code>.
38      * If <code>key</code> is a string and the new value isn't
39      * equal to the old one, fire a PropertyChangeEvent. If value
40      * is null, the key is removed from the table.
41      *
42      * @param key the unique Object who's value will be used to
43      * retreive the data value associated with it
44      * @param value the new Object to store as data under that key
45      * @return the previous Object value, or null
46      * @see java.util.Map#put
47      */

48     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
49         Object JavaDoc oldValue = (value == null) ? super.remove(key) : super.put(key, value);
50         if (key instanceof String JavaDoc) {
51             firePropertyChange((String JavaDoc) key, oldValue, value);
52         }
53         return oldValue;
54     }
55
56     /**
57      * Get a value from the defaults table.
58      * If the <code>key</code> is not associated with a value,
59      * the request is delegated to the parent defaults table
60      *
61      * @param key the key
62      * @param type the class of the value in question
63      * @return the associated value or <code>null</code>
64      */

65     public Object JavaDoc get(Object JavaDoc key, Class JavaDoc type) {
66         Object JavaDoc value = super.get(key);
67         if (value != null)
68             return value;
69
70         if (parent != null) {
71             return parent.get(key, type);
72         }
73         return null;
74     }
75
76
77     /**
78      * Add a PropertyChangeListener to the listener list.
79      * The listener is registered for all properties.
80      * <p/>
81      * A PropertyChangeEvent will get fired whenever a default
82      * is changed.
83      *
84      * @param listener The PropertyChangeListener to be added
85      * @see java.beans.PropertyChangeSupport
86      */

87     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
88         if (changeSupport == null) {
89             changeSupport = new PropertyChangeSupport JavaDoc(this);
90         }
91         changeSupport.addPropertyChangeListener(listener);
92     }
93
94
95     /**
96      * Remove a PropertyChangeListener from the listener list.
97      * This removes a PropertyChangeListener that was registered
98      * for all properties.
99      *
100      * @param listener The PropertyChangeListener to be removed
101      * @see java.beans.PropertyChangeSupport
102      */

103     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
104         if (changeSupport != null) {
105             changeSupport.removePropertyChangeListener(listener);
106         }
107     }
108
109
110     /**
111      * Support for reporting bound property changes. If oldValue and
112      * newValue are not equal and the PropertyChangeEvent listener list
113      * isn't empty, then fire a PropertyChange event to each listener.
114      *
115      * @param propertyName The programmatic name of the property that was changed.
116      * @param oldValue The old value of the property.
117      * @param newValue The new value of the property.
118      * @see java.beans.PropertyChangeSupport
119      */

120     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
121         if (changeSupport != null) {
122             changeSupport.firePropertyChange(propertyName, oldValue, newValue);
123         }
124     }
125 }
126
127
128
Popular Tags