KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > ItemManager


1 package net.suberic.util;
2
3 import javax.mail.*;
4 import java.util.*;
5
6 /**
7  * This class manages a list of Items.
8  */

9
10 public class ItemManager implements ValueChangeListener {
11
12   // an ordered list of item id's
13
private List itemIdList;
14
15   // the mapping from the item id's to the items themselves
16
private HashMap itemIdMap = new HashMap();
17
18   // for convenience, the itemList itself.
19
private List itemList;
20
21   //the ItemListChange listeners
22
private List listenerList = new LinkedList();
23
24   // the resource which defines this ItemList
25
private String JavaDoc resourceString;
26
27   // the current value.
28
private String JavaDoc currentValue;
29
30   // the VariableBundle which contains the Item information.
31
private VariableBundle sourceBundle;
32
33   // the ItemCreator which is used to create new items.
34
private ItemCreator itemCreator;
35
36   /**
37    * Create an ItemManager which loads information from the given
38    * VariableBundle using the given newResourceString, and creates new
39    * Items using the given ItemCreator.
40    */

41   public ItemManager(String JavaDoc newResourceString, VariableBundle newSourceBundle, ItemCreator newItemCreator) {
42     resourceString = newResourceString;
43     sourceBundle = newSourceBundle;
44     itemCreator = newItemCreator;
45
46     createItemList();
47     sourceBundle.addValueChangeListener(this, resourceString);
48   }
49
50   //-----------------------
51
// public interface.
52

53   /**
54    * This returns a Vector with all the currently registered Item
55    * objects.
56    */

57   public synchronized java.util.Vector JavaDoc getItems() {
58     return new Vector(itemList);
59   }
60
61   /**
62    * This returns the Item with the given itemName if it exists; otherwise,
63    * returns null.
64    */

65   public synchronized Item getItem(String JavaDoc itemID) {
66     if (itemID != null && itemIdList.contains(itemID))
67       return (Item)itemIdMap.get(itemID);
68     else
69       return null;
70   }
71
72   /**
73    * This adds the item with the given name to the item list.
74    */

75   public synchronized void addItem(String JavaDoc itemName) {
76     addItem(new String JavaDoc[] { itemName });
77   }
78
79   /**
80    * This adds the items with the given itemNames to the items list.
81    */

82   public synchronized void addItem(String JavaDoc[] itemName) {
83     if (itemName != null && itemName.length > 0) {
84       StringBuffer JavaDoc itemString = new StringBuffer JavaDoc();
85       for (int i = 0 ; i < itemName.length; i++) {
86   if (! itemIdList.contains(itemName[i]))
87     itemString.append(itemName[i] + ":");
88       }
89       if (itemString.length() > 0)
90   appendToItemString(new String JavaDoc(itemString.deleteCharAt(itemString.length() -1)));
91     }
92   }
93
94   /**
95    * This adds the given item to the items list.
96    */

97   public synchronized void addItem(Item newItem) {
98     addItem(new Item[] { newItem });
99   }
100
101   /**
102    * This adds the given items to the items list.
103    */

104   public synchronized void addItem(Item[] newItem) {
105     if (newItem != null) {
106       String JavaDoc[] itemNames = new String JavaDoc[newItem.length];
107       for (int i = 0; i < itemNames.length; i++) {
108   itemNames[i] = newItem[i].getItemID();
109   // we'll go ahead and add this here. this will make it so, later
110
// on, when we add the item to the main list, we get this Item,
111
// rather than creating a new one.
112
if (getItem(itemNames[i]) == null)
113     itemIdMap.put(itemNames[i], newItem[i]);
114       }
115
116       addItem(itemNames);
117     }
118   }
119
120   /**
121    * This removes the item with the given itemName.
122    */

123   public synchronized void removeItem(String JavaDoc itemName) {
124     removeFromItemString(new String JavaDoc[] { itemName });
125   }
126
127   /**
128    * This removes the items with the given itemNames.
129    */

130   public synchronized void removeItem(String JavaDoc[] itemNames) {
131     // this is probably not necessary at all, but what the hell?
132

133     if (itemNames == null || itemNames.length < 1)
134       return;
135
136     Vector matches = new Vector();
137     for ( int i = 0; i < itemNames.length; i++) {
138       if (itemIdList.contains(itemNames[i]))
139   matches.add(itemNames[i]);
140
141     }
142
143     if (matches.size() < 1)
144       return;
145
146     String JavaDoc[] removedItems = new String JavaDoc[matches.size()];
147
148     matches.toArray(removedItems);
149
150     removeFromItemString(removedItems);
151   }
152
153   /**
154    * This removes the given Item.
155    */

156   public synchronized void removeItem(Item item) {
157     if (item != null)
158       removeItem(item.getItemID());
159   }
160
161   /**
162    * This removes the given Items.
163    */

164   public synchronized void removeItem(Item[] item) {
165     if (item != null && item.length > 0) {
166       String JavaDoc[] itemNames = new String JavaDoc[item.length];
167       for (int i = 0; i < item.length; i++) {
168   if (item[i] != null)
169     itemNames[i] = item[i].getItemID();
170       }
171
172       removeItem(itemNames);
173     }
174   }
175
176   /**
177    * This compares the itemList object with the resourceString property, and
178    * updates the itemList appropriately.
179    *
180    * This method is called from valueChanged() when the underlying resource
181    * changes. It actually goes through and updates the objects on the
182    * ItemManager, and then notifies its ItemListChangedListeners by calling
183    * fireItemListChangeEvent().
184    */

185   public synchronized void refreshItems() {
186     if (! sourceBundle.getProperty(resourceString, "").equals(currentValue)) {
187       currentValue = sourceBundle.getProperty(resourceString, "");
188
189       LinkedList newIdList = new LinkedList();
190       LinkedList newItemList = new LinkedList();
191
192       Vector addedItemList = new Vector();
193       Vector removedIdList = new Vector(itemIdList);
194
195       StringTokenizer tokens = new StringTokenizer(sourceBundle.getProperty(resourceString, ""), ":");
196
197       String JavaDoc itemID;
198
199       // at the end of this loop, we should end up with a newIdList which is a
200
// list of the currently valid item id's, a newItemList which is a list
201
// of the currently valid items, an addedItemList which is a list of added
202
// items, and a removedIdList which is a list of removed id's.
203
while (tokens.hasMoreTokens()) {
204   itemID = tokens.nextToken();
205   newIdList.add(itemID);
206
207   if (itemIdList.contains(itemID)) {
208     removedIdList.remove(itemID);
209   } else {
210     // this is being added.
211
Item currentItem = (Item) itemIdMap.get(itemID);
212     if (currentItem == null) {
213       itemIdMap.put(itemID, itemCreator.createItem(sourceBundle, resourceString, itemID));
214     }
215     addedItemList.add(itemIdMap.get(itemID));
216   }
217   newItemList.add(itemIdMap.get(itemID));
218       }
219
220       Item[] removedItems = new Item[removedIdList.size()];
221       for (int i = 0 ; i < removedIdList.size(); i++) {
222   Item currentItem = (Item) itemIdMap.get(removedIdList.get(i));
223   if (currentItem != null) {
224     itemIdMap.remove(removedIdList.get(i));
225     removedItems[i] = currentItem;
226   }
227       }
228
229       Item[] addedItems = new Item[addedItemList.size()];
230       addedItemList.toArray(addedItems);
231
232       itemList = newItemList;
233       itemIdList = newIdList;
234
235       fireItemListChangeEvent(new ItemListChangeEvent(this, addedItems, removedItems));
236     }
237   }
238
239   /**
240    * As defined in net.suberic.util.ValueChangeListener.
241    *
242    * This listens for changes to the source property and calls
243    * refreshItems() when it gets one.
244    */

245   public void valueChanged(String JavaDoc changedValue) {
246     if (changedValue.equals(resourceString))
247       refreshItems();
248   }
249
250   /**
251    * This adds a ItemListChangeListener to the local listener list.
252    */

253   public void addItemListChangeListener(ItemListChangeListener ilcl) {
254     if (! listenerList.contains(ilcl))
255       listenerList.add(ilcl);
256   }
257
258   /**
259    * This removes a ItemListChangeListener from the local listener list.
260    */

261   public void removeItemListChangeListener(ItemListChangeListener ilcl) {
262     listenerList.remove(ilcl);
263   }
264
265   /**
266    * This notifies all listeners that the ItemList has changed.
267    */

268   public void fireItemListChangeEvent(ItemListChangeEvent e) {
269     for (int i = 0; i < listenerList.size(); i++)
270       ((ItemListChangeListener)listenerList.get(i)).itemListChanged(e);
271   }
272
273
274     //---------------------------
275
// the background stuff.
276

277   /**
278    * This loads and creates all the Items using the resourceString property
279    * of the sourceBundle.
280    */

281   private void createItemList() {
282     itemList = new LinkedList();
283     itemIdList = new LinkedList();
284     String JavaDoc itemID = null;
285
286     currentValue = sourceBundle.getProperty(resourceString, "");
287     StringTokenizer tokens = new StringTokenizer(currentValue, ":");
288
289     while (tokens.hasMoreTokens()) {
290       itemID=(String JavaDoc)tokens.nextToken();
291       Item newItem = itemCreator.createItem(sourceBundle, resourceString, itemID);
292       itemList.add(newItem);
293       itemIdList.add(itemID);
294       itemIdMap.put(itemID, newItem);
295     }
296
297   }
298
299   /**
300    * This appends the newItemString to the "Item" property.
301    */

302   private void appendToItemString(String JavaDoc newItemString) {
303     String JavaDoc oldValue = sourceBundle.getProperty("Item", "");
304     String JavaDoc newValue;
305     if (oldValue.length() > 0 && oldValue.charAt(oldValue.length() -1) != ':') {
306       newValue = oldValue + ":" + newItemString;
307     } else {
308       newValue = oldValue + newItemString;
309     }
310
311     sourceBundle.setProperty(resourceString, newValue);
312   }
313
314   /**
315    * This removes the item names in the itemNames array from the
316    * "Item" property.
317    */

318   private void removeFromItemString(String JavaDoc[] itemNames) {
319     StringTokenizer tokens = new StringTokenizer(sourceBundle.getProperty(resourceString, ""), ":");
320
321     boolean first = true;
322     StringBuffer JavaDoc newValue = new StringBuffer JavaDoc();
323     String JavaDoc itemID;
324
325     while (tokens.hasMoreTokens()) {
326       itemID=tokens.nextToken();
327       boolean keep=true;
328
329       for (int i = 0; keep == true && i < itemNames.length; i++) {
330   if (itemID.equals(itemNames[i]))
331     keep = false;
332       }
333       if (keep) {
334   if (!first)
335     newValue.append(":");
336
337   newValue.append(itemID);
338   first = false;
339       }
340
341     }
342
343     sourceBundle.setProperty(resourceString, newValue.toString());
344   }
345
346
347 }
348
349
Popular Tags