001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.commands;
011:
012: import java.util.ArrayList;
013: import java.util.HashSet;
014: import java.util.Hashtable;
015: import java.util.Iterator;
016:
017: import org.eclipse.core.commands.Command;
018: import org.eclipse.core.runtime.ListenerList;
019:
020: public class TagManager {
021:
022: private Hashtable fCommandToTags = new Hashtable();
023: private ListenerList fListeners = new ListenerList();
024:
025: public void update(Command command, String tags) {
026: if ((tags == null) || ("".equals(tags))) { //$NON-NLS-1$
027: fCommandToTags.remove(command);
028: } else {
029: fCommandToTags.put(command, tags);
030: }
031: fireTagManagerChanged();
032: }
033:
034: private boolean hasTag(String tags, String tag) {
035: String[] tagArray = tags.split(","); //$NON-NLS-1$
036: for (int i = 0; i < tagArray.length; i++) {
037: String trimTag = tagArray[i].trim();
038: if (tag.equalsIgnoreCase(trimTag))
039: return true;
040: }
041:
042: return false;
043: }
044:
045: public String[] getTags() {
046: HashSet tagSet = new HashSet();
047: for (Iterator i = fCommandToTags.values().iterator(); i
048: .hasNext();) {
049: String tags = (String) i.next();
050:
051: String[] tagArray = tags.split(","); //$NON-NLS-1$
052: for (int j = 0; j < tagArray.length; j++) {
053: String trimTag = tagArray[j].trim();
054: tagSet.add(trimTag);
055: }
056: }
057:
058: return (String[]) tagSet.toArray(new String[tagSet.size()]);
059: }
060:
061: public String getTags(Command command) {
062: String tags = (String) fCommandToTags.get(command);
063: if (tags == null)
064: return ""; //$NON-NLS-1$
065: return tags;
066: }
067:
068: public Command[] getCommands(String tag) {
069: ArrayList list = new ArrayList();
070:
071: for (Iterator i = fCommandToTags.keySet().iterator(); i
072: .hasNext();) {
073: Command command = (Command) i.next();
074: String tags = (String) fCommandToTags.get(command);
075: if (hasTag(tags, tag)) {
076: list.add(command);
077: }
078: }
079:
080: return (Command[]) list.toArray(new Command[list.size()]);
081: }
082:
083: public interface Listener {
084: void tagManagerChanged();
085: }
086:
087: public void addListener(Listener listener) {
088: fListeners.add(listener);
089: }
090:
091: public void removeListener(Listener listener) {
092: fListeners.remove(listener);
093: }
094:
095: private void fireTagManagerChanged() {
096: Object[] listeners = fListeners.getListeners();
097: for (int i = 0; i < listeners.length; i++) {
098: Listener listener = (Listener) listeners[i];
099: listener.tagManagerChanged();
100: }
101: }
102:
103: }
|