1 package com.explosion.utilities.preferences.groups;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.util.Hashtable;
24 import java.util.Vector;
25
26 import org.apache.log4j.LogManager;
27 import org.apache.log4j.Logger;
28
29 import com.explosion.utilities.preferences.Preference;
30 import com.explosion.utilities.preferences.persist.PreferenceGroupPersister;
31 import com.explosion.utilities.preferences.persist.PreferenceGroupPersisterFactory;
32
33 /***
34 * @author Stephen Cowx Date created:@25-Jan-2003
35 */
36 public class PreferenceGroup
37 {
38 private static Logger log = LogManager.getLogger(PreferenceGroup.class);
39
40 private Hashtable preferencesHash;
41
42 private Vector preferencesVector;
43
44 private String identifier;
45
46 private PreferenceGroupPersister pgPersister;
47
48 private Object object;
49
50 private Object referenceToStore;
51
52 private boolean editable = true;
53
54 /***
55 * Constructor for InMemoryPreferenceGroup.
56 */
57 public PreferenceGroup(Object referenceToStore, String identifier) throws PreferencePersistenceException
58 {
59 this.identifier = identifier;
60 this.referenceToStore = referenceToStore;
61 preferencesHash = new Hashtable();
62 preferencesVector = new Vector();
63 pgPersister = PreferenceGroupPersisterFactory.createPreferenceGroupPersister(referenceToStore);
64 if (pgPersister == null)
65 {
66 throw new PreferencePersistenceException("No suitable persister found for preference " + identifier + " using store of type " + referenceToStore.getClass().getName());
67 }
68 }
69
70 /***
71 * Returns the identifier.
72 *
73 * @return String
74 */
75 public String getIdentifier()
76 {
77 return identifier;
78 }
79
80 /***
81 * Sets the identifier.
82 *
83 * @param identifier The identifier to set
84 */
85 public void setIdentifier(String identifier) throws Exception
86 {
87 if (!editable)
88 {
89 log.warn("Attempt to edit non editable preference group " + identifier);
90 return;
91 }
92
93 pgPersister.renameGroup(this, identifier);
94 this.identifier = identifier;
95 }
96
97 /***
98 * Returns the preferencesHash.
99 *
100 * @return Hashtable
101 */
102 public Hashtable getPreferencesHash()
103 {
104 return preferencesHash;
105 }
106
107 /***
108 * Sets the preferencesHash.
109 *
110 * @param preferencesHash The preferencesHash to set
111 */
112 public void setPreferencesHash(Hashtable preferencesHash)
113 {
114 if (!editable)
115 {
116 log.warn("Attempt to edit non editable preference group " + identifier);
117 return;
118 }
119
120 this.preferencesHash = preferencesHash;
121 }
122
123 /***
124 * Returns the preferencesVector.
125 *
126 * @return Vector
127 */
128 public Vector getPreferencesVector()
129 {
130 return preferencesVector;
131 }
132
133 /***
134 * Sets the preferencesVector.
135 *
136 * @param preferencesVector The preferencesVector to set
137 */
138 public void setPreferencesVector(Vector preferencesVector)
139 {
140 if (!editable)
141 {
142 log.warn("Attempt to edit non editable preference group " + identifier);
143 return;
144 }
145
146 this.preferencesVector = preferencesVector;
147 }
148
149 /***
150 * Sets the object.
151 *
152 * @param object The object to set
153 */
154 public void setObject(Object object)
155 {
156 this.object = object;
157 }
158
159 /***
160 * Returns the object.
161 *
162 * @return Object
163 */
164 public Object getObject()
165 {
166 return object;
167 }
168
169 /***
170 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#commit()
171 */
172 public void commit() throws Exception
173 {
174
175 pgPersister.saveGroup(this);
176 }
177
178 /***
179 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#delete()
180 */
181 public void delete() throws Exception
182 {
183 if (!editable)
184 {
185 log.warn("Attempt to edit non editable preference group " + identifier);
186 return;
187 }
188
189 pgPersister.deleteGroup(this);
190
191
192 preferencesVector = null;
193 preferencesHash = null;
194 referenceToStore = null;
195 identifier = null;
196
197 }
198
199 /***
200 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#getAttributeValue()
201 */
202 public Object getPreferenceValue(String preferenceName)
203 {
204 return ((Preference) preferencesHash.get(preferenceName)).getValue();
205 }
206
207 /***
208 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#getAttribute()
209 */
210 public Preference getPreference(String preferenceName)
211 {
212 return (Preference) preferencesHash.get(preferenceName);
213 }
214
215 /***
216 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#setPreferenceValue(String,
217 * Object)
218 */
219 public void setPreferenceValue(String preferenceName, Object preferenceValue) throws Exception
220 {
221 if (!editable)
222 {
223 log.warn("Attempt to edit non editable preference group " + identifier);
224 return;
225 }
226
227 ((Preference) preferencesHash.get(preferenceName)).setValue(preferenceValue);
228 }
229
230 /***
231 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#addPreference(String,
232 * Object)
233 */
234 public void addPreference(String preferenceName, Preference preference) throws Exception
235 {
236 if (!editable)
237 {
238 log.warn("Attempt to edit non editable preference group " + identifier);
239 return;
240 }
241
242 if (preferencesHash.get(preferenceName) != null)
243 throw new Exception("Attribute with the name " + preferenceName + " already exists.");
244 else
245 {
246 preferencesHash.put(preferenceName, preference);
247 preferencesVector.addElement(preference);
248 }
249 }
250
251 /***
252 * @see com.explosion.utilities.preferences.groups.PreferenceGroup#removePreference(String)
253 */
254 public void removePreference(String preferenceName) throws Exception
255 {
256 if (!editable)
257 {
258 log.warn("Attempt to edit non editable preference group " + identifier);
259 return;
260 }
261
262 if (preferencesHash.get(preferenceName) != null)
263 {
264 preferencesVector.remove(preferencesHash.get(preferenceName));
265 preferencesHash.remove(preferenceName);
266 }
267 }
268
269 public boolean isEditable()
270 {
271 return editable;
272 }
273
274 public void setEditable(boolean editable)
275 {
276 this.editable = editable;
277
278
279 for (int i = 0; i < preferencesVector.size(); i++)
280 {
281 Preference pref = (Preference) preferencesVector.elementAt(i);
282 pref.setEditable(false);
283 }
284 }
285
286 /***
287 * @return Returns the referenceToStore.
288 */
289 public Object getReferenceToStore()
290 {
291 return referenceToStore;
292 }
293
294 /***
295 * @param referenceToStore The referenceToStore to set.
296 */
297 public void setReferenceToStore(Object referenceToStore)
298 {
299 this.referenceToStore = referenceToStore;
300 pgPersister = PreferenceGroupPersisterFactory.createPreferenceGroupPersister(referenceToStore);
301
302 }
303 }
304