KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > commands > TagManager


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.commands;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.commands.Command;
19 import org.eclipse.core.runtime.ListenerList;
20
21 public class TagManager {
22
23     private Hashtable JavaDoc fCommandToTags = new Hashtable JavaDoc();
24     private ListenerList fListeners = new ListenerList();
25     
26     public void update(Command command, String JavaDoc tags) {
27         if ((tags == null) || ("".equals(tags))) { //$NON-NLS-1$
28
fCommandToTags.remove(command);
29         }
30         else {
31             fCommandToTags.put(command, tags);
32         }
33         fireTagManagerChanged();
34     }
35     
36     private boolean hasTag(String JavaDoc tags, String JavaDoc tag) {
37         String JavaDoc[] tagArray = tags.split(","); //$NON-NLS-1$
38
for (int i = 0; i < tagArray.length; i++) {
39             String JavaDoc trimTag = tagArray[i].trim();
40             if (tag.equalsIgnoreCase(trimTag)) return true;
41         }
42         
43         return false;
44     }
45     
46     public String JavaDoc[] getTags() {
47         HashSet JavaDoc tagSet = new HashSet JavaDoc();
48         for (Iterator JavaDoc i = fCommandToTags.values().iterator(); i.hasNext(); ) {
49             String JavaDoc tags = (String JavaDoc)i.next();
50             
51             String JavaDoc[] tagArray = tags.split(","); //$NON-NLS-1$
52
for (int j = 0; j < tagArray.length; j++) {
53                 String JavaDoc trimTag = tagArray[j].trim();
54                 tagSet.add(trimTag);
55             }
56         }
57         
58         return (String JavaDoc[]) tagSet.toArray(new String JavaDoc[tagSet.size()]);
59     }
60     
61     public String JavaDoc getTags(Command command) {
62         String JavaDoc tags = (String JavaDoc)fCommandToTags.get(command);
63         if (tags == null) return ""; //$NON-NLS-1$
64
return tags;
65     }
66     
67     public Command[] getCommands(String JavaDoc tag) {
68         ArrayList JavaDoc list = new ArrayList JavaDoc();
69         
70         for (Iterator JavaDoc i = fCommandToTags.keySet().iterator(); i.hasNext(); ) {
71             Command command = (Command)i.next();
72             String JavaDoc tags = (String JavaDoc)fCommandToTags.get(command);
73             if (hasTag(tags, tag)) {
74                 list.add(command);
75             }
76         }
77         
78         return (Command[]) list.toArray(new Command[list.size()]);
79     }
80     
81     public interface Listener {
82         void tagManagerChanged();
83     }
84     
85     public void addListener(Listener listener) {
86         fListeners.add(listener);
87     }
88     
89     public void removeListener(Listener listener) {
90         fListeners.remove(listener);
91     }
92     
93     private void fireTagManagerChanged() {
94         Object JavaDoc[] listeners = fListeners.getListeners();
95         for (int i = 0; i < listeners.length; i++) {
96             Listener listener = (Listener)listeners[i];
97             listener.tagManagerChanged();
98         }
99     }
100     
101 }
102
Popular Tags