01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: * See the GNU Lesser General Public License for more details at gnu.org.
11: */
12:
13: package com.eviware.soapui.impl.settings;
14:
15: import java.util.HashSet;
16: import java.util.Set;
17:
18: import com.eviware.soapui.model.settings.Settings;
19: import com.eviware.soapui.model.settings.SettingsListener;
20: import com.eviware.soapui.support.types.StringToStringMap;
21:
22: /**
23: * Default Settings implementation
24: *
25: * @author Ole.Matzura
26: */
27:
28: public class SettingsImpl implements Settings {
29: private final Settings parent;
30: private final StringToStringMap values = new StringToStringMap();
31: private final Set<SettingsListener> listeners = new HashSet<SettingsListener>();
32:
33: public SettingsImpl(Settings parent) {
34: this .parent = parent;
35: }
36:
37: public boolean isSet(String id) {
38: return values.containsKey(id);
39: }
40:
41: public String getString(String id, String defaultValue) {
42: if (values.containsKey(id))
43: return values.get(id);
44: return parent == null ? defaultValue : parent.getString(id,
45: defaultValue);
46: }
47:
48: public void setString(String id, String value) {
49: String oldValue = getString(id, null);
50: values.put(id, value);
51:
52: for (SettingsListener listener : listeners) {
53: listener.settingChanged(id, oldValue, value);
54: }
55: }
56:
57: public boolean getBoolean(String id) {
58: if (values.containsKey(id))
59: return Boolean.getBoolean(values.get(id));
60: return parent == null ? false : parent.getBoolean(id);
61: }
62:
63: public void setBoolean(String id, boolean value) {
64: String oldValue = getString(id, null);
65: values.put(id, Boolean.toString(value));
66:
67: for (SettingsListener listener : listeners) {
68: listener.settingChanged(id, oldValue, Boolean
69: .toString(value));
70: }
71: }
72:
73: public long getLong(String id, long defaultValue) {
74: if (values.containsKey(id)) {
75: try {
76: return Long.parseLong(values.get(id));
77: } catch (NumberFormatException e) {
78: }
79: }
80:
81: return defaultValue;
82: }
83:
84: public void addSettingsListener(SettingsListener listener) {
85: listeners.add(listener);
86: }
87:
88: public void removeSettingsListener(SettingsListener listener) {
89: listeners.remove(listener);
90: }
91:
92: public void clearSetting(String id) {
93: values.remove(id);
94: }
95:
96: public void setLong(String id, long value) {
97: values.put(id, Long.toString(value));
98: }
99: }
|