1 11 package org.eclipse.pde.internal.ui.commands; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 import java.util.Hashtable ; 16 import java.util.Iterator ; 17 18 import org.eclipse.core.commands.Command; 19 import org.eclipse.core.runtime.ListenerList; 20 21 public class TagManager { 22 23 private Hashtable fCommandToTags = new Hashtable (); 24 private ListenerList fListeners = new ListenerList(); 25 26 public void update(Command command, String tags) { 27 if ((tags == null) || ("".equals(tags))) { fCommandToTags.remove(command); 29 } 30 else { 31 fCommandToTags.put(command, tags); 32 } 33 fireTagManagerChanged(); 34 } 35 36 private boolean hasTag(String tags, String tag) { 37 String [] tagArray = tags.split(","); for (int i = 0; i < tagArray.length; i++) { 39 String trimTag = tagArray[i].trim(); 40 if (tag.equalsIgnoreCase(trimTag)) return true; 41 } 42 43 return false; 44 } 45 46 public String [] getTags() { 47 HashSet tagSet = new HashSet (); 48 for (Iterator i = fCommandToTags.values().iterator(); i.hasNext(); ) { 49 String tags = (String )i.next(); 50 51 String [] tagArray = tags.split(","); for (int j = 0; j < tagArray.length; j++) { 53 String trimTag = tagArray[j].trim(); 54 tagSet.add(trimTag); 55 } 56 } 57 58 return (String []) tagSet.toArray(new String [tagSet.size()]); 59 } 60 61 public String getTags(Command command) { 62 String tags = (String )fCommandToTags.get(command); 63 if (tags == null) return ""; return tags; 65 } 66 67 public Command[] getCommands(String tag) { 68 ArrayList list = new ArrayList (); 69 70 for (Iterator i = fCommandToTags.keySet().iterator(); i.hasNext(); ) { 71 Command command = (Command)i.next(); 72 String tags = (String )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 [] 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 |