1 package org.columba.core.tagging; 2 3 import java.awt.Color ; 4 import java.io.File ; 5 import java.util.Hashtable ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.UUID ; 9 import java.util.logging.Logger ; 10 11 import javax.swing.event.EventListenerList ; 12 13 import org.columba.api.exception.StoreException; 14 import org.columba.core.config.DefaultConfigDirectory; 15 import org.columba.core.config.XmlConfig; 16 import org.columba.core.tagging.api.ITag; 17 import org.columba.core.tagging.api.ITagEvent; 18 import org.columba.core.tagging.api.ITagListener; 19 import org.columba.core.tagging.api.ITagManager; 20 import org.jdom.Document; 21 import org.jdom.Element; 22 23 29 public class TagManager implements ITagManager { 30 31 private static final String TAGS = "tags"; 32 33 private static final String TAG = "tag"; 34 35 private static final String DESCRIPTION = "description"; 36 37 private static final String COLOR = "color"; 38 39 private static final String NAME = "name"; 40 41 private static final String ID = "id"; 42 43 44 private static final Logger LOG = Logger 45 .getLogger("org.columba.core.tagging.TagManager"); 46 47 public static Hashtable <String , ITag> map = new Hashtable <String , ITag>(); 48 49 protected EventListenerList listenerList = new EventListenerList (); 50 51 static private TagManager instance; 52 53 private Document document; 54 55 private MyXmlConfig xmlConfig; 56 57 60 protected TagManager() { 61 62 File file = DefaultConfigDirectory.getInstance().getCurrentPath(); 63 File tagFile = new File (file, "tags.xml"); 64 65 xmlConfig = new MyXmlConfig(tagFile); 69 70 document = xmlConfig.load(); 71 72 loadModel(document); 74 } 75 76 public static TagManager getInstance() { 77 if (instance == null) { 78 synchronized (TagManager.class) { 79 if (instance == null) 80 instance = new TagManager(); 81 } 82 } 83 return instance; 84 } 85 86 89 private void loadModel(Document document) { 90 Element tagsElement = null; 91 if (document.hasRootElement()) 92 tagsElement = document.getRootElement(); 93 else { 94 tagsElement = new Element("tags"); 95 document.setRootElement(tagsElement); 96 } 97 98 List <Element> list = tagsElement.getChildren(); 99 Iterator <Element> it = list.listIterator(); 100 while (it.hasNext()) { 101 Element tagElement = it.next(); 102 Tag tag = new Tag(tagElement.getAttributeValue(TagManager.ID), 103 tagElement.getAttributeValue(TagManager.NAME)); 104 if (tagElement.getAttributeValue(TagManager.COLOR) != null) { 105 String colorString = tagElement 106 .getAttributeValue(TagManager.COLOR); 107 int rgb = Integer.parseInt(colorString); 108 tag.setColor(new Color (rgb)); 109 } 110 if (tagElement.getAttributeValue(TagManager.DESCRIPTION) != null) { 111 String description = tagElement 112 .getAttributeValue(TagManager.DESCRIPTION); 113 tag.setDescription(description); 114 } 115 116 map.put(tag.getId(), tag); 117 } 118 } 119 120 123 private void saveModel(Document document) { 124 Element tagsElement = document.getRootElement(); 125 126 if (tagsElement == null) { 128 tagsElement = new Element(TagManager.TAGS); 129 document.setRootElement(tagsElement); 130 } 131 132 tagsElement.removeContent(); 134 135 Iterator <ITag> it = getAllTags(); 136 while (it.hasNext()) { 137 ITag tag = it.next(); 138 Element tagElement = new Element(TagManager.TAG); 139 tagElement.setAttribute(TagManager.ID, tag.getId()); 140 tagElement.setAttribute(TagManager.NAME, tag.getName()); 141 if (tag.getColor() != null) { 142 int rgb = tag.getColor().getRGB(); 143 tagElement 144 .setAttribute(TagManager.COLOR, Integer.toString(rgb)); 145 } 146 147 if ( tag.getDescription() != null) { 148 tagElement.setAttribute("description", tag.getDescription()); 149 } 150 151 tagsElement.addContent(tagElement); 152 } 153 } 154 155 158 public ITag addTag(String name) throws StoreException { 159 if ((name == null) || (name.trim().length() == 0)) 161 throw new IllegalArgumentException ("name == null"); 162 163 String uuid = UUID.randomUUID().toString(); 165 ITag tag = new Tag(uuid, name); 166 167 if (map.containsKey(uuid)) 169 throw new IllegalArgumentException ("duplicate key " + uuid); 170 171 map.put(tag.getId(), tag); 172 173 fireTagAddedEvent(tag.getId()); 175 176 return tag; 177 } 178 179 182 public Iterator <ITag> getAllTags() { 183 return map.values().iterator(); 184 } 185 186 189 public ITag getTag(String id) { 190 return map.get(id); 191 } 192 193 196 public void removeTag(String id) throws StoreException { 197 map.remove(id); 198 199 fireTagDeletedEvent(id); 201 } 202 203 206 public void replaceTag(ITag tag) throws StoreException { 207 if (tag == null) 208 throw new IllegalArgumentException ("tag == null"); 209 210 map.put(tag.getId(), tag); 212 213 fireTagChangedEvent(tag.getId()); 215 } 216 217 218 219 222 public void addTagListener(ITagListener l) { 223 listenerList.add(ITagListener.class, l); 224 225 } 226 227 230 public void removeTagListener(ITagListener l) { 231 listenerList.remove(ITagListener.class, l); 232 } 233 234 237 protected void fireTagChangedEvent(String id) { 238 ITagEvent e = new TagEvent(this, id); 239 Object [] listeners = listenerList.getListenerList(); 241 242 for (int i = listeners.length - 2; i >= 0; i -= 2) { 245 if (listeners[i] == ITagListener.class) { 246 ((ITagListener) listeners[i + 1]).tagChanged(e); 247 } 248 } 249 } 250 251 254 protected void fireTagAddedEvent(String id) { 255 ITagEvent e = new TagEvent(this, id); 256 Object [] listeners = listenerList.getListenerList(); 258 259 for (int i = listeners.length - 2; i >= 0; i -= 2) { 262 if (listeners[i] == ITagListener.class) { 263 ((ITagListener) listeners[i + 1]).tagAdded(e); 264 } 265 } 266 } 267 268 271 protected void fireTagDeletedEvent(String id) { 272 ITagEvent e = new TagEvent(this, id); 273 274 Object [] listeners = listenerList.getListenerList(); 276 277 for (int i = listeners.length - 2; i >= 0; i -= 2) { 280 if (listeners[i] == ITagListener.class) { 281 ((ITagListener) listeners[i + 1]).tagDeleted(e); 282 } 283 } 284 } 285 286 class MyXmlConfig extends XmlConfig { 287 288 public MyXmlConfig(File xmlFile) { 289 super(xmlFile, null); 290 } 291 292 @Override 293 protected void transformModelToDocument(Document document) { 294 saveModel(document); 295 } 296 297 } 298 } 299 | Popular Tags |