001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal;
010:
011: import java.util.HashMap;
012: import java.util.Map;
013: import java.util.concurrent.CopyOnWriteArraySet;
014:
015: import net.refractions.udig.project.BlackboardEvent;
016: import net.refractions.udig.project.IBlackboard;
017: import net.refractions.udig.project.IBlackboardListener;
018:
019: /**
020: * A simple wrapper on a Map, used for tempoary collaboration.
021: *
022: * @author jgarnett
023: * @since 0.6.0
024: */
025: public class SimpleBlackboard implements IBlackboard {
026: /**
027: * @uml.property name="map"
028: * @uml.associationEnd qualifier="key:java.lang.Object java.lang.Object"
029: */
030: Map<String, Object> map;
031:
032: /**
033: * Construct <code>SimpleBlackboard</code>.
034: */
035: public SimpleBlackboard() {
036: map = new HashMap<String, Object>();
037: }
038:
039: /**
040: * Construct <code>SimpleBlackboard</code>.
041: */
042: public SimpleBlackboard(Map<String, Object> map) {
043: this .map = map;
044: }
045:
046: /**
047: * @see net.refractions.udig.project.IBlackboard#get(java.lang.String)
048: */
049: public Object get(String key) {
050: return map.get(key);
051: }
052:
053: /**
054: * @see net.refractions.udig.project.IBlackboard#put(java.lang.String, java.lang.Object)
055: */
056: public void put(String key, Object value) {
057: Object oldValue = map.put(key, value);
058: BlackboardEvent event = new BlackboardEvent(this , key,
059: oldValue, value);
060: for (IBlackboardListener l : listeners) {
061: try {
062: l.blackBoardChanged(event);
063: } catch (Exception e) {
064: ProjectPlugin.log("", e); //$NON-NLS-1$
065: }
066: }
067: }
068:
069: /**
070: * @see net.refractions.udig.project.IBlackboard#getFloat(java.lang.String)
071: */
072: public Float getFloat(String key) {
073: if (map.containsKey(key)) {
074: Object value = map.get(key);
075: if (value instanceof Float) {
076: return (Float) value;
077: }
078: }
079: return null;
080: }
081:
082: /**
083: * @see net.refractions.udig.project.IBlackboard#getInteger(java.lang.String)
084: */
085: public Integer getInteger(String key) {
086: if (map.containsKey(key)) {
087: Object value = map.get(key);
088: if (value instanceof Integer) {
089: return (Integer) value;
090: }
091: }
092: return null;
093: }
094:
095: /**
096: * @see net.refractions.udig.project.IBlackboard#getString(java.lang.String)
097: */
098: public String getString(String key) {
099: if (map.containsKey(key)) {
100: Object value = map.get(key);
101: if (value instanceof String) {
102: return (String) value;
103: }
104: }
105: return null;
106: }
107:
108: /**
109: * @see net.refractions.udig.project.IBlackboard#putFloat(java.lang.String, float)
110: */
111: public void putFloat(String key, float value) {
112: put(key, value);
113: }
114:
115: /**
116: * @see net.refractions.udig.project.IBlackboard#putInteger(java.lang.String, int)
117: */
118: public void putInteger(String key, int value) {
119: put(key, value);
120: }
121:
122: /**
123: * @see net.refractions.udig.project.IBlackboard#putString(java.lang.String, java.lang.String)
124: */
125: public void putString(String key, String value) {
126: put(key, value);
127: }
128:
129: /**
130: * @see net.refractions.udig.project.IBlackboard#clear()
131: */
132: public void clear() {
133: map.clear();
134:
135: for (IBlackboardListener l : listeners) {
136: try {
137: l.blackBoardCleared(this );
138: } catch (Exception e) {
139: ProjectPlugin.log("", e); //$NON-NLS-1$
140: }
141: }
142: }
143:
144: public void flush() {
145: // do nothing, does not support persistance
146: }
147:
148: /*
149: * @see net.refractions.udig.project.Blackboard#contains(java.lang.String)
150: */
151: public boolean contains(String key) {
152: return map.containsKey(key);
153: }
154:
155: CopyOnWriteArraySet<IBlackboardListener> listeners = new CopyOnWriteArraySet<IBlackboardListener>();
156:
157: public boolean addListener(IBlackboardListener listener) {
158: return listeners.add(listener);
159: }
160:
161: public boolean removeListener(IBlackboardListener listener) {
162: return listeners.remove(listener);
163: }
164:
165: }
|