1 package net.suberic.util; 2 3 import javax.mail.*; 4 import java.util.*; 5 6 9 10 public class ItemManager implements ValueChangeListener { 11 12 private List itemIdList; 14 15 private HashMap itemIdMap = new HashMap(); 17 18 private List itemList; 20 21 private List listenerList = new LinkedList(); 23 24 private String resourceString; 26 27 private String currentValue; 29 30 private VariableBundle sourceBundle; 32 33 private ItemCreator itemCreator; 35 36 41 public ItemManager(String 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 53 57 public synchronized java.util.Vector getItems() { 58 return new Vector(itemList); 59 } 60 61 65 public synchronized Item getItem(String itemID) { 66 if (itemID != null && itemIdList.contains(itemID)) 67 return (Item)itemIdMap.get(itemID); 68 else 69 return null; 70 } 71 72 75 public synchronized void addItem(String itemName) { 76 addItem(new String [] { itemName }); 77 } 78 79 82 public synchronized void addItem(String [] itemName) { 83 if (itemName != null && itemName.length > 0) { 84 StringBuffer itemString = new StringBuffer (); 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 (itemString.deleteCharAt(itemString.length() -1))); 91 } 92 } 93 94 97 public synchronized void addItem(Item newItem) { 98 addItem(new Item[] { newItem }); 99 } 100 101 104 public synchronized void addItem(Item[] newItem) { 105 if (newItem != null) { 106 String [] itemNames = new String [newItem.length]; 107 for (int i = 0; i < itemNames.length; i++) { 108 itemNames[i] = newItem[i].getItemID(); 109 if (getItem(itemNames[i]) == null) 113 itemIdMap.put(itemNames[i], newItem[i]); 114 } 115 116 addItem(itemNames); 117 } 118 } 119 120 123 public synchronized void removeItem(String itemName) { 124 removeFromItemString(new String [] { itemName }); 125 } 126 127 130 public synchronized void removeItem(String [] itemNames) { 131 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 [] removedItems = new String [matches.size()]; 147 148 matches.toArray(removedItems); 149 150 removeFromItemString(removedItems); 151 } 152 153 156 public synchronized void removeItem(Item item) { 157 if (item != null) 158 removeItem(item.getItemID()); 159 } 160 161 164 public synchronized void removeItem(Item[] item) { 165 if (item != null && item.length > 0) { 166 String [] itemNames = new String [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 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 itemID; 198 199 while (tokens.hasMoreTokens()) { 204 itemID = tokens.nextToken(); 205 newIdList.add(itemID); 206 207 if (itemIdList.contains(itemID)) { 208 removedIdList.remove(itemID); 209 } else { 210 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 245 public void valueChanged(String changedValue) { 246 if (changedValue.equals(resourceString)) 247 refreshItems(); 248 } 249 250 253 public void addItemListChangeListener(ItemListChangeListener ilcl) { 254 if (! listenerList.contains(ilcl)) 255 listenerList.add(ilcl); 256 } 257 258 261 public void removeItemListChangeListener(ItemListChangeListener ilcl) { 262 listenerList.remove(ilcl); 263 } 264 265 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 277 281 private void createItemList() { 282 itemList = new LinkedList(); 283 itemIdList = new LinkedList(); 284 String itemID = null; 285 286 currentValue = sourceBundle.getProperty(resourceString, ""); 287 StringTokenizer tokens = new StringTokenizer(currentValue, ":"); 288 289 while (tokens.hasMoreTokens()) { 290 itemID=(String )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 302 private void appendToItemString(String newItemString) { 303 String oldValue = sourceBundle.getProperty("Item", ""); 304 String 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 318 private void removeFromItemString(String [] itemNames) { 319 StringTokenizer tokens = new StringTokenizer(sourceBundle.getProperty(resourceString, ""), ":"); 320 321 boolean first = true; 322 StringBuffer newValue = new StringBuffer (); 323 String 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 |