KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > config > Options


1 /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1
3   (the "License"); you may not use this file except in compliance with the
4   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5   
6   Software distributed under the License is distributed on an "AS IS" basis,
7   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8   for the specific language governing rights and
9   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 JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
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
31     extends DefaultItem
32 {
33
34     public static final int DEFAULT_POOLING_INTERVAL = 5, INTERVAL_TIME_UNIT = 1000; // seconds
35

36     static final String JavaDoc POLLING_INTERVAL_KEY = "poll_interval", POLLING_ENABLED_KEY = "polling_enabled";
37
38     private static final String JavaDoc POLLING_INTERVAL_PATH = "/" + POLLING_INTERVAL_KEY, POLLING_ENABLED_PATH = "/" + POLLING_ENABLED_KEY;
39
40     private List JavaDoc observers = new ArrayList JavaDoc();
41
42     public Options(XmlElement aRoot)
43     {
44         super(aRoot);
45     }
46
47     public int getPollInterval()
48     {
49         // TODO: key can't be null
50
return getIntegerWithDefault(POLLING_INTERVAL_PATH, null, DEFAULT_POOLING_INTERVAL);
51     }
52
53     public boolean isPollingEnabled()
54     {
55         // TODO: key can't be null
56
return getBooleanWithDefault(POLLING_ENABLED_PATH, null, true);
57     }
58
59     public void setPollInterval(int interval)
60     {
61         int old = getPollInterval();
62
63         setInteger(POLLING_INTERVAL_PATH, null, interval);
64
65         if (old != interval)
66         {
67             for (Iterator JavaDoc it = observers.iterator(); it.hasNext();)
68                 ((OptionsObserver) it.next()).pollingIntervalChanged(interval);
69         }
70
71     }
72
73     public void setPollingEnabled(boolean poll)
74     {
75         boolean old = isPollingEnabled();
76         setBoolean(POLLING_ENABLED_PATH, null, poll);
77         if (old != poll)
78         {
79             for (Iterator JavaDoc it = observers.iterator(); it.hasNext();)
80                 ((OptionsObserver) it.next()).pollingStateChanged(poll);
81         }
82     }
83
84     public void addObserver(OptionsObserver observer)
85     {
86         if (!observers.contains(observer)) observers.add(observer);
87     }
88
89     public void removeObserver(OptionsObserver observer)
90     {
91         observers.remove(observer);
92     }
93
94     void setDefaultData()
95     {
96         setPollInterval(DEFAULT_POOLING_INTERVAL);
97         setPollingEnabled(true);
98     }
99
100     public int getInternalPollingInterval()
101     {
102         return getPollInterval() * INTERVAL_TIME_UNIT;
103     }
104 }
105
Popular Tags