KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > tagging > TagManager


1 package org.columba.core.tagging;
2
3 import java.awt.Color JavaDoc;
4 import java.io.File JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.UUID JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.swing.event.EventListenerList JavaDoc;
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 /**
24  *
25  *
26  * @author mhub
27  * @author frd (added eventing, replaced old configuration infrastructure)
28  */

29 public class TagManager implements ITagManager {
30
31     private static final String JavaDoc TAGS = "tags";
32
33     private static final String JavaDoc TAG = "tag";
34
35     private static final String JavaDoc DESCRIPTION = "description";
36
37     private static final String JavaDoc COLOR = "color";
38
39     private static final String JavaDoc NAME = "name";
40
41     private static final String JavaDoc ID = "id";
42
43     /** JDK 1.4+ logging framework logger, used for logging. */
44     private static final Logger JavaDoc LOG = Logger
45             .getLogger("org.columba.core.tagging.TagManager");
46
47     public static Hashtable JavaDoc<String JavaDoc, ITag> map = new Hashtable JavaDoc<String JavaDoc, ITag>();
48
49     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
50
51     static private TagManager instance;
52
53     private Document document;
54
55     private MyXmlConfig xmlConfig;
56
57     /**
58      * protected singleton constructor
59      */

60     protected TagManager() {
61
62         File JavaDoc file = DefaultConfigDirectory.getInstance().getCurrentPath();
63         File JavaDoc tagFile = new File JavaDoc(file, "tags.xml");
64
65         // load xml configuration
66
// -> will be automatically saved every couple of minutes and on
67
// shutdown
68
xmlConfig = new MyXmlConfig(tagFile);
69
70         document = xmlConfig.load();
71
72         // create tag vector
73
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     /**
87      * @param document
88      */

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 JavaDoc<Element> list = tagsElement.getChildren();
99         Iterator JavaDoc<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 JavaDoc colorString = tagElement
106                         .getAttributeValue(TagManager.COLOR);
107                 int rgb = Integer.parseInt(colorString);
108                 tag.setColor(new Color JavaDoc(rgb));
109             }
110             if (tagElement.getAttributeValue(TagManager.DESCRIPTION) != null) {
111                 String JavaDoc description = tagElement
112                         .getAttributeValue(TagManager.DESCRIPTION);
113                 tag.setDescription(description);
114             }
115
116             map.put(tag.getId(), tag);
117         }
118     }
119
120     /**
121      * @param document
122      */

123     private void saveModel(Document document) {
124         Element tagsElement = document.getRootElement();
125
126         // ensure root exists
127
if (tagsElement == null) {
128             tagsElement = new Element(TagManager.TAGS);
129             document.setRootElement(tagsElement);
130         }
131
132         // clear
133
tagsElement.removeContent();
134
135         Iterator JavaDoc<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     /**
156      * @see org.columba.core.tagging.api.ITagManager#addTag(java.lang.String)
157      */

158     public ITag addTag(String JavaDoc name) throws StoreException {
159         // Do not allow an empty name
160
if ((name == null) || (name.trim().length() == 0))
161             throw new IllegalArgumentException JavaDoc("name == null");
162
163         // create uuid
164
String JavaDoc uuid = UUID.randomUUID().toString();
165         ITag tag = new Tag(uuid, name);
166
167         // very unlikely to happen
168
if (map.containsKey(uuid))
169             throw new IllegalArgumentException JavaDoc("duplicate key " + uuid);
170
171         map.put(tag.getId(), tag);
172
173         // notify listeners
174
fireTagAddedEvent(tag.getId());
175
176         return tag;
177     }
178
179     /**
180      * @see org.columba.core.tagging.api.ITagManager#getAllTags()
181      */

182     public Iterator JavaDoc<ITag> getAllTags() {
183         return map.values().iterator();
184     }
185
186     /**
187      * @see org.columba.core.tagging.api.ITagManager#getTag(java.lang.String)
188      */

189     public ITag getTag(String JavaDoc id) {
190         return map.get(id);
191     }
192
193     /**
194      * @see org.columba.core.tagging.api.ITagManager#removeTag(java.lang.String)
195      */

196     public void removeTag(String JavaDoc id) throws StoreException {
197         map.remove(id);
198
199         // notify listeners
200
fireTagDeletedEvent(id);
201     }
202
203     /**
204      * @see org.columba.core.tagging.api.ITagManager#replaceTag(org.columba.core.tagging.api.ITag)
205      */

206     public void replaceTag(ITag tag) throws StoreException {
207         if (tag == null)
208             throw new IllegalArgumentException JavaDoc("tag == null");
209
210         // this will overwrite the "old" tag with same id
211
map.put(tag.getId(), tag);
212
213         // notify listeners
214
fireTagChangedEvent(tag.getId());
215     }
216
217     /** ************** eventing ***************************** */
218
219     /**
220      * @see org.columba.core.tagging.api.ITagManager#addTagListener(org.columba.core.tagging.api.ITagListener)
221      */

222     public void addTagListener(ITagListener l) {
223         listenerList.add(ITagListener.class, l);
224
225     }
226
227     /**
228      * @see org.columba.core.tagging.api.ITagManager#removeTagListener(org.columba.core.tagging.api.ITagListener)
229      */

230     public void removeTagListener(ITagListener l) {
231         listenerList.remove(ITagListener.class, l);
232     }
233
234     /**
235      * @param id
236      */

237     protected void fireTagChangedEvent(String JavaDoc id) {
238         ITagEvent e = new TagEvent(this, id);
239         // Guaranteed to return a non-null array
240
Object JavaDoc[] listeners = listenerList.getListenerList();
241
242         // Process the listeners last to first, notifying
243
// those that are interested in this event
244
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     /**
252      * @param id
253      */

254     protected void fireTagAddedEvent(String JavaDoc id) {
255         ITagEvent e = new TagEvent(this, id);
256         // Guaranteed to return a non-null array
257
Object JavaDoc[] listeners = listenerList.getListenerList();
258
259         // Process the listeners last to first, notifying
260
// those that are interested in this event
261
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     /**
269      * @param id
270      */

271     protected void fireTagDeletedEvent(String JavaDoc id) {
272         ITagEvent e = new TagEvent(this, id);
273
274         // Guaranteed to return a non-null array
275
Object JavaDoc[] listeners = listenerList.getListenerList();
276
277         // Process the listeners last to first, notifying
278
// those that are interested in this event
279
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 JavaDoc xmlFile) {
289             super(xmlFile, null);
290         }
291
292         @Override JavaDoc
293         protected void transformModelToDocument(Document document) {
294             saveModel(document);
295         }
296
297     }
298 }
299
Popular Tags