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.Collections;
24 import java.util.Enumeration;
25 import java.util.Hashtable;
26 import java.util.Vector;
27
28 import com.explosion.utilities.preferences.Preference;
29
30 /***
31 * Author: Stephen Cowx Date: Jan 25, 2003 Time: 14:29:26 PM
32 * <p>
33 * This class provides a useful mechanism for maintaining a list of
34 * PersistentObjects.
35 *
36 * The only method which needs overriding is the loadusing method. Which will
37 * load the objects from whatever persistent store has been used.
38 * </p>
39 */
40 public abstract class PreferenceGroupManager
41 {
42
43 private Hashtable groups = new Hashtable(0);
44 private Vector identifiers = new Vector();
45 private Object referenceToStore;
46
47 public PreferenceGroupManager(Object referenceToStore) throws Exception
48 {
49 this.referenceToStore = referenceToStore;
50 }
51
52 /***
53 * Method getGroup.
54 *
55 * @param identifier
56 * @return PreferenceGroup
57 */
58 public PreferenceGroup getGroup(String identifier)
59 {
60 return (PreferenceGroup) groups.get(identifier);
61 }
62
63 /***
64 * Method setGroup.
65 *
66 * @param identifier
67 * @param instance
68 */
69 public void setGroup(String identifier, PreferenceGroup instance) throws Exception
70 {
71 if (groups.get(identifier) == null)
72 addGroup(identifier, instance);
73 else
74 groups.put(identifier, instance);
75 }
76
77 /***
78 * Method removeGroup.
79 *
80 * @param identifier
81 */
82 public void deleteGroup(String identifier) throws Exception
83 {
84 if (groups.get(identifier) != null) ((PreferenceGroup) groups.get(identifier)).delete();
85
86 groups.remove(identifier);
87 identifiers.remove(identifier);
88 }
89
90 /***
91 * Method addGroup.
92 *
93 * @param instance
94 */
95 public void addGroup(String identifier, PreferenceGroup instance) throws Exception
96 {
97 if (groups.get(identifier) != null)
98 throw new Exception("Cannot add '" + identifier + "' because this name is already being used.");
99 else
100 {
101 groups.put(identifier, instance);
102 identifiers.addElement(identifier);
103 instance.setReferenceToStore(getReferenceForGroup(identifier));
104 instance.commit();
105 }
106 }
107
108 /***
109 * Method loadUsing. Loads all of these persistent objects from their
110 * persistent store.
111 *
112 * @param uri
113 */
114 public abstract void loadUsing(Object referenceToStore) throws Exception;
115
116 /***
117 * Returns an empty PreferenceGroup. IMplementors must provide this method
118 * for creating new groups
119 *
120 * @param uri
121 */
122 public abstract PreferenceGroup createGroup(String identifier) throws Exception;
123
124 /***
125 * Return the correct node for the group with the given identifier
126 * @param identifier
127 * @return
128 */
129 public abstract Object getReferenceForGroup(String identifier);
130
131 /***
132 * Method saveAll. Saves all of the PersistentObjects to their persistent
133 * store
134 */
135 public void saveAll() throws Exception
136 {
137 Enumeration en = groups.elements();
138 while (en.hasMoreElements())
139 {
140 PreferenceGroup object = (PreferenceGroup) en.nextElement();
141 object.commit();
142 }
143 }
144
145 /***
146 * Method removeAll. Removes all of these Persistent Objects from the
147 * Manager as well as from their persistent store. Non-reversible
148 */
149 public void removeAll() throws Exception
150 {
151 Enumeration en = groups.elements();
152 while (en.hasMoreElements())
153 {
154 PreferenceGroup object = (PreferenceGroup) en.nextElement();
155 object.delete();
156 }
157 groups = new Hashtable();
158 identifiers = new Vector();
159 }
160
161 /***
162 * Method sort.
163 */
164 public void sort()
165 {
166 Collections.sort(identifiers);
167 }
168
169 /***
170 * Returns the groups.
171 *
172 * @return Hashtable
173 */
174 public Hashtable getGroups()
175 {
176 return groups;
177 }
178
179 /***
180 * Returns the identifiers.
181 *
182 * @return Vector
183 */
184 public Vector getIdentifiers()
185 {
186 return identifiers;
187 }
188
189 /***
190 * Sets the groups.
191 *
192 * @param groups The groups to set
193 */
194 public void setGroups(Hashtable objectsHash)
195 {
196 this.groups = objectsHash;
197 }
198
199 /***
200 * Sets the identifiers.
201 *
202 * @param identifiers The identifiers to set
203 */
204 public void setObjectsVector(Vector objectsVector)
205 {
206 this.identifiers = objectsVector;
207 }
208
209 /***
210 * Renames the instance and makes it available from it's new location
211 *
212 * @param oldName The old name ofthe instance
213 * @param newName The new name ofthe instance
214 */
215 public void renameGroup(String oldName, String newName) throws Exception
216 {
217 if (groups.get(oldName) != null)
218 {
219 PreferenceGroup object = ((PreferenceGroup) groups.get(oldName));
220 object.setIdentifier(newName);
221
222 groups.put(newName, object);
223 identifiers.addElement(newName);
224
225 groups.remove(oldName);
226 identifiers.remove(oldName);
227 }
228
229 }
230
231 public void duplicateGroup(String nameOfOriginal, String nameOfCopy) throws Exception
232 {
233
234 if (nameOfOriginal.equalsIgnoreCase(nameOfCopy)) throw new IllegalArgumentException("Name of original may not be the same as the name of the copy.");
235
236 PreferenceGroup group = (PreferenceGroup) groups.get(nameOfOriginal);
237 if ( group != null)
238 {
239 PreferenceGroup original = ((PreferenceGroup) groups.get(nameOfOriginal));
240 PreferenceGroup copy = new PreferenceGroup(original.getReferenceToStore(), nameOfCopy);
241 Enumeration en = original.getPreferencesHash().keys();
242 while (en.hasMoreElements())
243 {
244 String preferenceName = (String) en.nextElement();
245 Preference pref = original.getPreference(preferenceName);
246 Preference duplicate = (Preference) pref.clone();
247 copy.addPreference(duplicate.getUniqueIdentifier(), duplicate);
248 }
249
250 addGroup(nameOfCopy, group);
251 }
252 }
253
254 /***
255 * @return Returns the referenceToStore.
256 */
257 public Object getReferenceToStore()
258 {
259 return referenceToStore;
260 }
261
262 /***
263 * @param referenceToStore The referenceToStore to set.
264 */
265 public void setReferenceToStore(Object referenceToStore)
266 {
267 this.referenceToStore = referenceToStore;
268 }
269 }
270