01: /*
02: * This is free software, licensed under the Gnu Public License (GPL)
03: * get a copy from <http://www.gnu.org/licenses/gpl.html>
04: * $Id: PropertyRegistry.java,v 1.5 2004/03/05 23:34:38 hzeller Exp $
05: * author: Henner Zeller <H.Zeller@acm.org>
06: */
07: package henplus;
08:
09: import java.util.SortedMap;
10: import java.util.TreeMap;
11:
12: import henplus.property.PropertyHolder;
13:
14: /**
15: * A Registry that binds names to Properties.
16: */
17: public class PropertyRegistry {
18: private final SortedMap/*<String,PropertyHolder>*/_namedProperties;
19:
20: public PropertyRegistry() {
21: _namedProperties = new TreeMap();
22: }
23:
24: /**
25: * Every command or subsystem that needs to be able to set
26: * Properties, needs to register its property here.
27: */
28: public void registerProperty(String name, PropertyHolder holder)
29: throws IllegalArgumentException {
30: if (_namedProperties.containsKey(name)) {
31: throw new IllegalArgumentException("Property named '"
32: + name + "' already exists");
33: }
34: _namedProperties.put(name, holder);
35: }
36:
37: public void unregisterProperty(String name) {
38: _namedProperties.remove(name);
39: }
40:
41: /**
42: * sets the Property to the given value. This throws an Exception, if
43: * the PropertyHolder vetoes this attempt or if there is simply no
44: * Property bound to the given name.
45: *
46: * @param name the name the property is bound to.
47: * @param value the new value of the property to be set.
48: * @throws Exception, if the property does not exist or throws an
49: * Exception to veto the new value.
50: */
51: public void setProperty(String name, String value) throws Exception {
52: PropertyHolder holder = (PropertyHolder) _namedProperties
53: .get(name);
54: if (holder == null) {
55: throw new IllegalArgumentException("unknown property '"
56: + name + "'");
57: }
58: holder.setValue(value);
59: }
60:
61: /**
62: * returns a Map view of property-Names to values. The returned Map
63: * must not be modified.
64: */
65: public SortedMap/*<String,PropertyHolder>*/getPropertyMap() {
66: return _namedProperties;
67: }
68: }
69:
70: /*
71: * Local variables:
72: * c-basic-offset: 4
73: * compile-command: "ant -emacs -find build.xml"
74: * End:
75: */
|