Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49f360566d3e8dba67ccd8256c5deb515dee4821 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.cheatsheets.registry;

import java.util.ArrayList;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPluginContribution;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.internal.cheatsheets.Messages;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Category provides for hierarchical grouping of elements registered in the
 * registry. One extension normally defines a category, and other reference it
 * via its ID.
 * <p>
 * A category may specify its parent category in order to achieve hierarchy.
 * </p>
 */
public class Category implements IWorkbenchAdapter, IPluginContribution,
		IAdaptable {
	/**
	 * Name of the miscellaneous category
	 */
	public final static String MISC_NAME = Messages.CATEGORY_OTHER;

	/**
	 */
	public final static String MISC_ID = "org.eclipse.ui.cheatsheets.otherCategory"; //$NON-NLS-1$

	private String id;

	private String name;

	private String[] parentPath;

	private ArrayList elements;

	private IConfigurationElement configurationElement;

	private String pluginId;

	/**
	 * Creates an instance of <code>Category</code> as a miscellaneous
	 * category.
	 */
	public Category() {
		this.id = MISC_ID;
		this.name = MISC_NAME;
		this.pluginId = MISC_ID; // TODO: remove hack for bug 55172
	}

	/**
	 * Creates an instance of <code>Category</code> with an ID and label.
	 *
	 * @param id
	 *            the unique identifier for the category
	 * @param label
	 *            the presentation label for this category
	 */
	public Category(String id, String label) {
		this.id = id;
		this.name = label;
	}

	/**
	 * Creates an instance of <code>Category</code> using the information from
	 * the specified configuration element.
	 *
	 * @param configElement
	 *            the <code>IConfigurationElement<code> containing
	 * 		the ID, label, and optional parent category path.
	 * @throws WorkbenchException if the ID or label is <code>null</code
	 */
	public Category(IConfigurationElement configElement)
			throws WorkbenchException {
		id = configElement.getAttribute("id"); //$NON-NLS-1$

		configurationElement = configElement;
		if (id == null || getLabel() == null)
			throw new WorkbenchException("Invalid category: " + id); //$NON-NLS-1$
	}

	/**
	 * Add an element to this category.
	 *
	 * @param element
	 *            the element to add
	 */
	public void addElement(Object element) {
		if (elements == null)
			elements = new ArrayList(5);
		elements.add(element);
	}

	@Override
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter == IWorkbenchAdapter.class)
			return adapter.cast(this);
		else if (adapter == IConfigurationElement.class)
			return adapter.cast(configurationElement);
		else
			return null;
	}

	@Override
	public Object[] getChildren(Object o) {
		return getElements().toArray();
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		return null;
	}

	@Override
	public String getLabel(Object o) {
		return getLabel();
	}

	/**
	 * Return the id for this category.
	 *
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * Return the label for this category.
	 *
	 * @return the label
	 */
	public String getLabel() {
		return configurationElement == null ? name : configurationElement
				.getAttribute("name"); //$NON-NLS-1$
	}

	/**
	 * Return the parent path for this category.
	 *
	 * @return the parent path
	 */
	public String[] getParentPath() {
		if (parentPath != null)
			return parentPath;

		String unparsedPath = getRawParentPath();
		if (unparsedPath != null) {
			StringTokenizer stok = new StringTokenizer(unparsedPath, "/"); //$NON-NLS-1$
			parentPath = new String[stok.countTokens()];
			for (int i = 0; stok.hasMoreTokens(); i++) {
				parentPath[i] = stok.nextToken();
			}
		}

		return parentPath;
	}

	/**
	 * Return the unparsed parent path. May be <code>null</code>.
	 *
	 * @return the unparsed parent path or <code>null</code>
	 */
	public String getRawParentPath() {
		return configurationElement == null ? null : configurationElement
				.getAttribute("parentCategory"); //$NON-NLS-1$
	}

	/**
	 * Return the root path for this category.
	 *
	 * @return the root path
	 */
	public String getRootPath() {
		String[] path = getParentPath();
		if (path != null && path.length > 0)
			return path[0];

		return id;
	}

	/**
	 * Return the elements contained in this category.
	 *
	 * @return the elements
	 */
	public ArrayList getElements() {
		return elements;
	}

	/**
	 * Return whether a given object exists in this category.
	 *
	 * @param o
	 *            the object to search for
	 * @return whether the object is in this category
	 */
	public boolean hasElement(Object o) {
		if (elements == null)
			return false;
		if (elements.isEmpty())
			return false;
		return elements.contains(o);
	}

	/**
	 * Return whether this category has child elements.
	 *
	 * @return whether this category has child elements
	 */
	public boolean hasElements() {
		if (elements != null)
			return !elements.isEmpty();

		return false;
	}

	@Override
	public Object getParent(Object o) {
		return null;
	}

	@Override
	public String getLocalId() {
		return id;
	}

	@Override
	public String getPluginId() {
		return configurationElement == null ? pluginId : configurationElement
				.getContributor().getName();
	}

	/**
	 * Clear all elements from this category.
	 *
	 * @since 3.1
	 */
	public void clear() {
		if (elements != null) {
			elements.clear();
		}
	}
}

Back to the top