001: /* $Id: ConfigScheme.java,v 1.1.1.1 2002/10/02 18:41:44 wastl Exp $ */
002: package net.wastl.webmail.config;
003:
004: import java.util.*;
005:
006: /*
007: * ConfigScheme.java
008: *
009: * Created: 31.08.99
010: *
011: * Copyright (C) 1999-2000 Sebastian Schaffert
012: *
013: * This program is free software; you can redistribute it and/or
014: * modify it under the terms of the GNU General Public License
015: * as published by the Free Software Foundation; either version 2
016: * of the License, or (at your option) any later version.
017: *
018: * This program is distributed in the hope that it will be useful,
019: * but WITHOUT ANY WARRANTY; without even the implied warranty of
020: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: * GNU General Public License for more details.
022: *
023: * You should have received a copy of the GNU General Public License
024: * along with this program; if not, write to the Free Software
025: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
026: */
027: /**
028: * This class contains a scheme for WebMail configuration data.
029: *
030: * It is mainly a container for the ConfigParameter objects and a wrapper to access
031: * the main functions in them to ease access to the scheme.
032: *
033: * Created: 31.08.99
034: *
035: * @author Sebastian Schaffert
036: * @version $Revision: 1.1.1.1 $
037: */
038: public class ConfigScheme {
039:
040: protected Hashtable config_scheme;
041:
042: public ConfigScheme() {
043: System.err.print("- Configuration Scheme Handler ... ");
044: config_scheme = new Hashtable();
045: System.err.println("done!");
046: }
047:
048: /**
049: * Check whether a key/value pair is valid in this configuration scheme
050: * @param key Name of the parameter
051: * @param value value to check for
052: */
053: public boolean isValid(String key, Object value) {
054: ConfigParameter scheme = getConfigParameter(key);
055: if (scheme == null) {
056: return false;
057: } else {
058: return scheme.isPossibleValue(value);
059: }
060: }
061:
062: public String filter(String key, String value) {
063: ConfigParameter c = (ConfigParameter) config_scheme.get(key);
064: if (c != null) {
065: return c.filter(value);
066: } else {
067: return value;
068: }
069: }
070:
071: /**
072: * Register a configuration key that can take String values
073: * @param key Name of the configuration key
074: * @param default Default value for this key
075: * @param desc Description for this key
076: */
077: public void configRegisterStringKey(String key, String def,
078: String desc) {
079: StringConfigParameter parm = new StringConfigParameter(key,
080: def, desc);
081: registerConfig(parm);
082: }
083:
084: public void configRegisterStringKey(ConfigurationListener l,
085: String key, String def, String desc) {
086: StringConfigParameter parm = new StringConfigParameter(key,
087: def, desc);
088: registerConfig(parm);
089: parm.addConfigurationListener(l);
090: }
091:
092: /**
093: * Register a configuration key that can take String values
094: * @param key Name of the configuration key
095: * @param default Default value for this key
096: * @param desc Description for this key
097: */
098: public void configRegisterIntegerKey(String key, String def,
099: String desc) {
100: IntegerConfigParameter parm = new IntegerConfigParameter(key,
101: def, desc);
102: registerConfig(parm);
103: }
104:
105: public void configRegisterIntegerKey(ConfigurationListener l,
106: String key, String def, String desc) {
107: IntegerConfigParameter parm = new IntegerConfigParameter(key,
108: def, desc);
109: registerConfig(parm);
110: parm.addConfigurationListener(l);
111: }
112:
113: /**
114: * Register a configuration key that can take String values and crypts them
115: * @param key Name of the configuration key
116: * @param default Default value for this key
117: * @param desc Description for this key
118: */
119: public void configRegisterCryptedStringKey(String key, String def,
120: String desc) {
121: CryptedStringConfigParameter parm = new CryptedStringConfigParameter(
122: key, def, desc);
123: registerConfig(parm);
124: }
125:
126: public void configRegisterCryptedStringKey(ConfigurationListener l,
127: String key, String def, String desc) {
128: CryptedStringConfigParameter parm = new CryptedStringConfigParameter(
129: key, def, desc);
130: registerConfig(parm);
131: parm.addConfigurationListener(l);
132: }
133:
134: /**
135: * Register a configuration key that can take one of a choice of possible values
136: * @param key Name of the configuration key
137: * @param desc Description for this key
138: * @see configAddChoice
139: */
140: public void configRegisterChoiceKey(String key, String desc) {
141: ChoiceConfigParameter parm = new ChoiceConfigParameter(key,
142: desc);
143: registerConfig(parm);
144: }
145:
146: public void configRegisterChoiceKey(ConfigurationListener l,
147: String key, String desc) {
148: ChoiceConfigParameter parm = new ChoiceConfigParameter(key,
149: desc);
150: registerConfig(parm);
151: parm.addConfigurationListener(l);
152: }
153:
154: public void configRegisterYesNoKey(String key, String desc) {
155: ChoiceConfigParameter parm = new ConfigYesNoParameter(key, desc);
156: registerConfig(parm);
157: }
158:
159: public void configRegisterYesNoKey(ConfigurationListener l,
160: String key, String desc) {
161: ChoiceConfigParameter parm = new ConfigYesNoParameter(key, desc);
162: registerConfig(parm);
163: parm.addConfigurationListener(l);
164: }
165:
166: /**
167: * Add a choice to an already-existing Choice-key
168: * @param key Name of the configuration key where a choice is to be added
169: * @param choice Name of the new choice
170: * @param desc Description for this choice
171: */
172: public void configAddChoice(String key, String choice, String desc) {
173: if (config_scheme != null) {
174: ConfigParameter parm = (ConfigParameter) config_scheme
175: .get(key);
176: if (parm instanceof ChoiceConfigParameter) {
177: ((ChoiceConfigParameter) parm).addChoice(choice, desc);
178: }
179: }
180: }
181:
182: /**
183: * Add a configuration listener for a key.
184: * There may be any amount of Listeners for a parameter.
185: */
186: public void addConfigurationListener(String key,
187: ConfigurationListener l) {
188: ConfigParameter parm = getConfigParameter(key);
189: parm.addConfigurationListener(l);
190: }
191:
192: public ConfigParameter getConfigParameter(String key) {
193: return (ConfigParameter) config_scheme.get(key);
194: }
195:
196: public String getConfigParameterType(String key) {
197: return getConfigParameter(key).getType();
198: }
199:
200: public String getConfigParameterGroup(String key) {
201: return getConfigParameter(key).getGroup();
202: }
203:
204: public Object getDefaultValue(String key) {
205: ConfigParameter cp = (ConfigParameter) config_scheme.get(key);
206: if (cp != null) {
207: return cp.getDefault();
208: } else {
209: return null;
210: }
211: }
212:
213: public void setDefaultValue(String key, Object default_value) {
214: ConfigParameter cp = (ConfigParameter) config_scheme.get(key);
215: if (cp != null) {
216: cp.setDefault(default_value);
217: }
218: }
219:
220: public String getDescription(String key) {
221: ConfigParameter cp = (ConfigParameter) config_scheme.get(key);
222: if (cp != null) {
223: return cp.getDescription();
224: } else {
225: return null;
226: }
227: }
228:
229: public Enumeration getPossibleKeys() {
230: return config_scheme.keys();
231: }
232:
233: public void notifyConfigurationChange(String key) {
234:// System.err.println("NOTIFY: "+key);
235: ConfigParameter parm=getConfigParameter(key);
236: if(parm!=null) {
237: Enumeration enum=parm.getConfigurationListeners();
238:
239: while(enum.hasMoreElements()) {
240: ConfigurationListener l=(ConfigurationListener)enum.nextElement();
241:// System.err.println(l);
242: try {
243: l.notifyConfigurationChange(key);
244: } catch(Exception e) {
245: e.printStackTrace();
246: }
247: }
248: }
249: }
250:
251: public void registerConfig(ConfigParameter parm) {
252: if (config_scheme == null) {
253: config_scheme = new Hashtable();
254: }
255: Enumeration e = config_scheme.keys();
256: boolean flag = false;
257: while (e.hasMoreElements()) {
258: if (e.nextElement().equals(parm.getKey())) {
259: flag = true;
260: break;
261: }
262: }
263: if (!flag) {
264: config_scheme.put(parm.getKey(), parm);
265: }
266: }
267: }
|