Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c64097f87b10a03ffe99769e843f0a4081c7f08e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************************
 *  Copyright (c) 2006, 2012 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.ui.commands;

import java.util.ArrayList;

import java.util.*;
import org.eclipse.core.commands.Category;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.ui.commands.ICommandService;

public class CommandTreeContentProvider implements ITreeContentProvider {

	protected final int F_CAT_CONTENT = 0; // category grouped content
	protected final int F_CON_CONTENT = 1; // context grouped content

	private ICommandService fComServ;
	private TreeMap<Category, ArrayList<Command>> fCatMap; // mapping of commands to category
	private TreeMap<?, ?> fConMap; // mapping of commands to context
	private Viewer fViewer;
	private int fCurContent = F_CAT_CONTENT;

	public CommandTreeContentProvider(ICommandService comServ) {
		fComServ = comServ;
		init();
	}

	private void init() {
		fCatMap = new TreeMap<Category, ArrayList<Command>>(new Comparator<Object>() {
			public int compare(Object arg0, Object arg1) {
				String comA = CommandList.getText(arg0);
				String comB = CommandList.getText(arg1);
				if (comA != null)
					return comA.compareTo(comB);
				return +1; // undefined ids should go last
			}
		});
		fConMap = new TreeMap<Object, Object>();
		Command[] commands = fComServ.getDefinedCommands();
		for (int i = 0; i < commands.length; i++) {
			/*
			 * IWorkbenchRegistryConstants.AUTOGENERATED_PREFIX = "AUTOGEN:::"
			 */
			// skip commands with autogenerated id's
			if (commands[i].getId().startsWith("AUTOGEN:::")) //$NON-NLS-1$
				continue;
			// populate category map
			try {
				Category cat = commands[i].getCategory();
				ArrayList<Command> list = fCatMap.get(cat);
				if (list == null)
					fCatMap.put(cat, list = new ArrayList<Command>());
				list.add(commands[i]);
			} catch (NotDefinedException e) {
				continue;
			}
			// TODO: populate context map
			// can we easily group commands by context?
		}
	}

	public Object getParent(Object element) {
		if (element instanceof Command)
			try {
				return ((Command) element).getCategory();
			} catch (NotDefinedException e) {
				// undefined category - should never hit this as these commands
				// will not be listed
			}
		return null;
	}

	public void dispose() {
		fCatMap.clear();
		fConMap.clear();
	}

	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof Category) {
			ArrayList<?> list = fCatMap.get(parentElement);
			if (list != null)
				return list.toArray(new Command[list.size()]);
		}
		return null;
	}

	public boolean hasChildren(Object element) {
		if (element instanceof Category) {
			ArrayList<?> list = fCatMap.get(element);
			if (list != null)
				return list.size() > 0;
		}
		return false;
	}

	public Object[] getElements(Object inputElement) {
		switch (fCurContent) {
			case F_CAT_CONTENT :
				return fCatMap.keySet().toArray();
			case F_CON_CONTENT :
				return new Object[0];
		}
		return null;
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		fViewer = viewer;
	}

	public void refreshWithCategoryContent() {
		fCurContent = F_CAT_CONTENT;
		if (fViewer != null)
			fViewer.refresh();
	}

	public void refreshWithContextContent() {
		fCurContent = F_CON_CONTENT;
		if (fViewer != null)
			fViewer.refresh();
	}

}

Back to the top