001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.model;
023:
024: import java.io.Serializable;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Set;
028:
029: import org.beryl.gui.GUIException;
030: import org.beryl.gui.View;
031:
032: /**
033: * Key-Value based data model
034: */
035:
036: public class MapDataModel extends AbstractDataModel implements
037: Serializable {
038: private HashMap values = new HashMap();
039:
040: public void setValue(View source, String key, Object newValue)
041: throws GUIException {
042: MapChangeEvent event = new MapChangeEvent(source, this , key,
043: values.get(key), newValue);
044: values.put(key, newValue);
045: fireModelEvent(event);
046: }
047:
048: public Object getValue(String key) {
049: return values.get(key);
050: }
051:
052: public Object removeValueByKey(View source, String key)
053: throws GUIException {
054: MapChangeEvent event = new MapChangeEvent(source, this , key,
055: values.get(key), null);
056: Object value = values.remove(key);
057: fireModelEvent(event);
058: return value;
059: }
060:
061: public void clear(View source) throws GUIException {
062: MapChangeEvent event = new MapChangeEvent(source, this , null,
063: null, null);
064: values.clear();
065: fireModelEvent(event);
066: }
067:
068: public void copy(View source, MapDataModel model)
069: throws GUIException {
070: MapChangeEvent event = new MapChangeEvent(source, this , null,
071: null, null);
072: if (getClass().equals(MapDataModel.class)) {
073: /* Faster */
074: values.putAll(model.values);
075: } else {
076: /* There might be dynamic content or content which requires validation .. */
077: for (Iterator i = model.getKeys().iterator(); i.hasNext();) {
078: String key = (String) i.next();
079: setValue(source, key, model.getValue(key));
080: }
081: }
082: fireModelEvent(event);
083: }
084:
085: public Set getKeys() {
086: return values.keySet();
087: }
088:
089: public void replace(View view, MapDataModel model)
090: throws GUIException {
091: values.clear();
092: copy(view, model);
093: }
094:
095: public Object clone() {
096: MapDataModel model = new MapDataModel();
097: try {
098: model.copy(null, this );
099: } catch (GUIException e) {
100: /* Will never occur since there are no listeners attached yet */
101: }
102: return model;
103: }
104:
105: public String toString() {
106: StringBuffer buf = new StringBuffer();
107: buf.append("org.beryl.gui.MapDataModel [");
108: for (Iterator i = values.keySet().iterator(); i.hasNext();) {
109: String key = (String) i.next();
110: Object value = values.get(key);
111:
112: buf.append(key).append("=").append(value);
113: if (i.hasNext())
114: buf.append(", ");
115: }
116: buf.append("]");
117: return buf.toString();
118: }
119:
120: /* ========== Convenience methods ========== */
121:
122: public final void setValue(String key, Object newValue)
123: throws GUIException {
124: setValue(null, key, newValue);
125: }
126:
127: public final Object removeValueByKey(String key)
128: throws GUIException {
129: return removeValueByKey(null, key);
130: }
131:
132: public final void clear() throws GUIException {
133: clear(null);
134: }
135:
136: public final void replace(MapDataModel model) throws GUIException {
137: replace(null, model);
138: }
139: }
|