01: /*
02: The contents of this file are subject to the Mozilla Public License Version 1.1
03: (the "License"); you may not use this file except in compliance with the
04: License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
05:
06: Software distributed under the License is distributed on an "AS IS" basis,
07: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
08: for the specific language governing rights and
09: limitations under the License.
10:
11: The Original Code is "The Columba Project"
12:
13: The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
14: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15:
16: All Rights Reserved.
17: */
18: package org.columba.core.scripting.config;
19:
20: import java.util.ArrayList;
21: import java.util.Iterator;
22: import java.util.List;
23:
24: import org.columba.core.config.DefaultItem;
25: import org.columba.core.xml.XmlElement;
26:
27: /**
28: @author Celso Pinto (cpinto@yimports.com)
29: */
30: public class Options extends DefaultItem {
31:
32: public static final int DEFAULT_POOLING_INTERVAL = 5,
33: INTERVAL_TIME_UNIT = 1000; // seconds
34:
35: static final String POLLING_INTERVAL_KEY = "poll_interval",
36: POLLING_ENABLED_KEY = "polling_enabled";
37:
38: private static final String POLLING_INTERVAL_PATH = "/"
39: + POLLING_INTERVAL_KEY, POLLING_ENABLED_PATH = "/"
40: + POLLING_ENABLED_KEY;
41:
42: private List observers = new ArrayList();
43:
44: public Options(XmlElement aRoot) {
45: super (aRoot);
46: }
47:
48: public int getPollInterval() {
49: // TODO: key can't be null
50: return getIntegerWithDefault(POLLING_INTERVAL_PATH, null,
51: DEFAULT_POOLING_INTERVAL);
52: }
53:
54: public boolean isPollingEnabled() {
55: // TODO: key can't be null
56: return getBooleanWithDefault(POLLING_ENABLED_PATH, null, true);
57: }
58:
59: public void setPollInterval(int interval) {
60: int old = getPollInterval();
61:
62: setInteger(POLLING_INTERVAL_PATH, null, interval);
63:
64: if (old != interval) {
65: for (Iterator it = observers.iterator(); it.hasNext();)
66: ((OptionsObserver) it.next())
67: .pollingIntervalChanged(interval);
68: }
69:
70: }
71:
72: public void setPollingEnabled(boolean poll) {
73: boolean old = isPollingEnabled();
74: setBoolean(POLLING_ENABLED_PATH, null, poll);
75: if (old != poll) {
76: for (Iterator it = observers.iterator(); it.hasNext();)
77: ((OptionsObserver) it.next()).pollingStateChanged(poll);
78: }
79: }
80:
81: public void addObserver(OptionsObserver observer) {
82: if (!observers.contains(observer))
83: observers.add(observer);
84: }
85:
86: public void removeObserver(OptionsObserver observer) {
87: observers.remove(observer);
88: }
89:
90: void setDefaultData() {
91: setPollInterval(DEFAULT_POOLING_INTERVAL);
92: setPollingEnabled(true);
93: }
94:
95: public int getInternalPollingInterval() {
96: return getPollInterval() * INTERVAL_TIME_UNIT;
97: }
98: }
|