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.Comparator;
014: import java.util.TreeMap;
015:
016: import org.eclipse.core.commands.Category;
017: import org.eclipse.core.commands.Command;
018: import org.eclipse.core.commands.common.NotDefinedException;
019: import org.eclipse.jface.viewers.ITreeContentProvider;
020: import org.eclipse.jface.viewers.Viewer;
021: import org.eclipse.ui.commands.ICommandService;
022:
023: public class CommandTreeContentProvider implements ITreeContentProvider {
024:
025: protected final int F_CAT_CONTENT = 0; // category grouped content
026: protected final int F_CON_CONTENT = 1; // context grouped content
027:
028: private ICommandService fComServ;
029: private TreeMap fCatMap; // mapping of commands to category
030: private TreeMap fConMap; // mapping of commands to context
031: private Viewer fViewer;
032: private int fCurContent = F_CAT_CONTENT;
033:
034: public CommandTreeContentProvider(ICommandService comServ) {
035: fComServ = comServ;
036: init();
037: }
038:
039: private void init() {
040: fCatMap = new TreeMap(new Comparator() {
041: public int compare(Object arg0, Object arg1) {
042: String comA = CommandList.getText(arg0);
043: String comB = CommandList.getText(arg1);
044: if (comA != null)
045: return comA.compareTo(comB);
046: return +1; // undefined ids should go last
047: }
048: });
049: fConMap = new TreeMap();
050: Command[] commands = fComServ.getDefinedCommands();
051: for (int i = 0; i < commands.length; i++) {
052: /*
053: * IWorkbenchRegistryConstants.AUTOGENERATED_PREFIX = "AUTOGEN:::"
054: */
055: // skip commands with autogenerated id's
056: if (commands[i].getId().startsWith("AUTOGEN:::")) //$NON-NLS-1$
057: continue;
058: // populate category map
059: try {
060: Category cat = commands[i].getCategory();
061: ArrayList list = (ArrayList) fCatMap.get(cat);
062: if (list == null)
063: fCatMap.put(cat, list = new ArrayList());
064: list.add(commands[i]);
065: } catch (NotDefinedException e) {
066: continue;
067: }
068: // TODO: populate context map
069: // can we easily group commands by context?
070: }
071: }
072:
073: public Object getParent(Object element) {
074: if (element instanceof Command)
075: try {
076: return ((Command) element).getCategory();
077: } catch (NotDefinedException e) {
078: // undefined category - should never hit this as these commands
079: // will not be listed
080: }
081: return null;
082: }
083:
084: public void dispose() {
085: fCatMap.clear();
086: fConMap.clear();
087: }
088:
089: public Object[] getChildren(Object parentElement) {
090: if (parentElement instanceof Category) {
091: ArrayList list = (ArrayList) fCatMap.get(parentElement);
092: if (list != null)
093: return list.toArray(new Command[list.size()]);
094: }
095: return null;
096: }
097:
098: public boolean hasChildren(Object element) {
099: if (element instanceof Category) {
100: ArrayList list = (ArrayList) fCatMap.get(element);
101: if (list != null)
102: return list.size() > 0;
103: }
104: return false;
105: }
106:
107: public Object[] getElements(Object inputElement) {
108: switch (fCurContent) {
109: case F_CAT_CONTENT:
110: return fCatMap.keySet().toArray();
111: case F_CON_CONTENT:
112: return new Object[0];
113: }
114: return null;
115: }
116:
117: public void inputChanged(Viewer viewer, Object oldInput,
118: Object newInput) {
119: fViewer = viewer;
120: }
121:
122: public void refreshWithCategoryContent() {
123: fCurContent = F_CAT_CONTENT;
124: if (fViewer != null)
125: fViewer.refresh();
126: }
127:
128: public void refreshWithContextContent() {
129: fCurContent = F_CON_CONTENT;
130: if (fViewer != null)
131: fViewer.refresh();
132: }
133:
134: }
|