Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f1211bba5607ffcf84cd17cd45cfd1bea406b5ca (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*******************************************************************************
 * Copyright (c) 2004, 2012 QNX Software Systems 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:
 *     QNX Software Systems - initial API and implementation
 *     IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.wizards;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IPluginContribution;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.activities.WorkbenchActivityHelper;

/**
 * Convenience class for drop-in C/C++ Wizard contributions.
 */
public class CWizardRegistry {

	private final static String TAG_WIZARD = "wizard"; //$NON-NLS-1$
	private final static String ATT_CATEGORY = "category";//$NON-NLS-1$
	private final static String ATT_PROJECT = "project";//$NON-NLS-1$
	private final static String TAG_PARAMETER = "parameter";//$NON-NLS-1$
	private final static String TAG_NAME = "name";//$NON-NLS-1$
	private final static String TAG_VALUE = "value";//$NON-NLS-1$
	private final static String ATT_CTYPE = "ctype";//$NON-NLS-1$
	private final static String ATT_CFILE = "cfile";//$NON-NLS-1$
	private final static String ATT_CFOLDER = "cfolder";//$NON-NLS-1$
	private final static String ATT_CPROJECT = "cproject";//$NON-NLS-1$
	private final static String ATT_CCPROJECT = "ccproject";//$NON-NLS-1$
	private final static String TAG_CLASS = "class"; //$NON-NLS-1$
	private final static String TAG_ID = "id"; //$NON-NLS-1$
	private final static String PL_NEW = "newWizards"; //$NON-NLS-1$

	/**
	 * Checks if wizard supports C projects.
	 *
	 * @param element the wizard element
	 *
	 * @return <code>true</code> if the given wizard element applies to a C Project
	 */
	public static boolean isCProjectWizard(IConfigurationElement element) {
		String category = element.getAttribute(ATT_CATEGORY);
		return (category != null && category.equals(CUIPlugin.CWIZARD_CATEGORY_ID));
	}

	/**
	 * Checks if wizard supports C++ project.
	 *
	 * @param element the wizard element
	 *
	 * @return <code>true</code> if the given wizard element applies to a C++ Project
	 */
	public static boolean isCCProjectWizard(IConfigurationElement element) {
		String category = element.getAttribute(ATT_CATEGORY);
		return (category != null && category.equals(CUIPlugin.CCWIZARD_CATEGORY_ID));
	}

	/**
	 * Returns IDs of all C/C++ project wizards contributed to the workbench.
	 *
	 * @return an array of wizard ids
	 */
	public static String[] getProjectWizardIDs() {
		return getWizardIDs(getProjectWizardElements());
	}

	/**
	 * Returns extension data for all the C/C++ project wizards contributed to the workbench.
	 *     <wizard
	 *         name="My C Wizard"
	 *         icon="icons/cwiz.gif"
	 *         category="org.eclipse.cdt.ui.newCWizards"
	 *         id="xx.MyCWizard"
	 *         class="org.xx.MyCWizard"
	 *         project="true">
	 *         <description>
	 *             My C Wizard
	 *         </description>
	 *      </wizard>
	 *
	 *
	 * @return an array of IConfigurationElement
	 */
	public static IConfigurationElement[] getProjectWizardElements() {
		List<IConfigurationElement> elemList = new ArrayList<>();
		IConfigurationElement[] elements = getAllWizardElements();
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isProjectWizard(element)) {
				elemList.add(element);
			}
		}
		return elemList.toArray(new IConfigurationElement[elemList.size()]);
	}

	private static boolean isProjectWizard(IConfigurationElement element) {
		String project = element.getAttribute(ATT_PROJECT);
		if (project != null) {
			return Boolean.valueOf(project).booleanValue();
		}

		IConfigurationElement[] classElements = element.getChildren(TAG_CLASS);
		if (classElements.length > 0) {
			for (IConfigurationElement classElement : classElements) {
				IConfigurationElement[] paramElements = classElement.getChildren(TAG_PARAMETER);
				for (IConfigurationElement curr : paramElements) {
					String name = curr.getAttribute(TAG_NAME);
					if (name != null && (name.equals(ATT_CPROJECT) || name.equals(ATT_CCPROJECT))) {
						String value = curr.getAttribute(TAG_VALUE);
						if (value != null)
							return Boolean.valueOf(value).booleanValue();
					}
				}
			}
			return false;
		}
		// fall back, if no <class> element found then assume it's a project wizard
		return true;
	}

	public static IAction[] getProjectWizardActions() {
		return createActions(getProjectWizardElements());
	}

	/**
	 * Returns IDs of all C/C++ type wizards contributed to the workbench.
	 *
	 * @return an array of wizard ids
	 */
	public static String[] getTypeWizardIDs() {
		return getWizardIDs(getTypeWizardElements());
	}

	/**
	 * Returns extension data for all the C/C++ type wizards contributed to the workbench.
	 *     <wizard
	 *         name="My C Wizard"
	 *         icon="icons/cwiz.gif"
	 *         category="org.eclipse.cdt.ui.newCWizards"
	 *         id="xx.MyCWizard">
	 *         <class class="org.xx.MyCWizard">
	 *             <parameter name="ctype" value="true" />
	 *         </class>
	 *         <description>
	 *             My C Wizard
	 *         </description>
	 *      </wizard>
	 *
	 * @return an array of IConfigurationElement
	 */
	public static IConfigurationElement[] getTypeWizardElements() {
		List<IConfigurationElement> elemList = new ArrayList<>();
		IConfigurationElement[] elements = getAllWizardElements();
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isTypeWizard(element)) {
				elemList.add(element);
			}
		}
		return elemList.toArray(new IConfigurationElement[elemList.size()]);
	}

	private static boolean isTypeWizard(IConfigurationElement element) {
		IConfigurationElement[] classElements = element.getChildren(TAG_CLASS);
		if (classElements.length > 0) {
			for (IConfigurationElement classElement : classElements) {
				IConfigurationElement[] paramElements = classElement.getChildren(TAG_PARAMETER);
				for (IConfigurationElement curr : paramElements) {
					String name = curr.getAttribute(TAG_NAME);
					if (name != null && name.equals(ATT_CTYPE)) {
						String value = curr.getAttribute(TAG_VALUE);
						if (value != null)
							return Boolean.valueOf(value).booleanValue();
					}
				}
			}
		}
		return false;
	}

	public static IAction[] getTypeWizardActions() {
		return createActions(getTypeWizardElements());
	}

	/**
	 * Returns IDs of all C/C++ file wizards contributed to the workbench.
	 *
	 * @return an array of wizard ids
	 */
	public static String[] getFileWizardIDs() {
		return getWizardIDs(getFileWizardElements());
	}

	/**
	 * Returns extension data for all the C/C++ file wizards contributed to the workbench.
	 *     <wizard
	 *         name="My C File Wizard"
	 *         icon="icons/cwiz.gif"
	 *         category="org.eclipse.cdt.ui.newCWizards"
	 *         id="xx.MyCWizard">
	 *         <class class="org.xx.MyCFileWizard">
	 *             <parameter name="cfile" value="true" />
	 *         </class>
	 *         <description>
	 *             My C File Wizard
	 *         </description>
	 *      </wizard>
	 *
	 * @return an array of IConfigurationElement
	 */
	public static IConfigurationElement[] getFileWizardElements() {
		List<IConfigurationElement> elemList = new ArrayList<>();
		IConfigurationElement[] elements = getAllWizardElements();
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isFileWizard(element)) {
				elemList.add(element);
			}
		}
		return elemList.toArray(new IConfigurationElement[elemList.size()]);
	}

	private static boolean isFileWizard(IConfigurationElement element) {
		IConfigurationElement[] classElements = element.getChildren(TAG_CLASS);
		if (classElements.length > 0) {
			for (IConfigurationElement classElement : classElements) {
				IConfigurationElement[] paramElements = classElement.getChildren(TAG_PARAMETER);
				for (IConfigurationElement curr : paramElements) {
					String name = curr.getAttribute(TAG_NAME);
					if (name != null && name.equals(ATT_CFILE)) {
						String value = curr.getAttribute(TAG_VALUE);
						if (value != null)
							return Boolean.valueOf(value).booleanValue();
					}
				}
			}
		}
		return false;
	}

	public static IAction[] getFolderWizardActions() {
		return createActions(getFolderWizardElements());
	}

	/**
	 * Returns IDs of all C/C++ folder wizards contributed to the workbench.
	 *
	 * @return an array of wizard ids
	 */
	public static String[] getFolderWizardIDs() {
		return getWizardIDs(getFolderWizardElements());
	}

	/**
	 * Returns extension data for all the C/C++ folder wizards contributed to the workbench.
	 *     <wizard
	 *         name="My C Folder Wizard"
	 *         icon="icons/cwiz.gif"
	 *         category="org.eclipse.cdt.ui.newCWizards"
	 *         id="xx.MyCWizard">
	 *         <class class="org.xx.MyCFolderWizard">
	 *             <parameter name="cfolder" value="true" />
	 *         </class>
	 *         <description>
	 *             My C Folder Wizard
	 *         </description>
	 *      </wizard>
	 *
	 * @return an array of IConfigurationElement
	 */
	public static IConfigurationElement[] getFolderWizardElements() {
		List<IConfigurationElement> elemList = new ArrayList<>();
		IConfigurationElement[] elements = getAllWizardElements();
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isFolderWizard(element)) {
				elemList.add(element);
			}
		}
		return elemList.toArray(new IConfigurationElement[elemList.size()]);
	}

	private static boolean isFolderWizard(IConfigurationElement element) {
		IConfigurationElement[] classElements = element.getChildren(TAG_CLASS);
		if (classElements.length > 0) {
			for (IConfigurationElement classElement : classElements) {
				IConfigurationElement[] paramElements = classElement.getChildren(TAG_PARAMETER);
				for (IConfigurationElement curr : paramElements) {
					String name = curr.getAttribute(TAG_NAME);
					if (name != null && name.equals(ATT_CFOLDER)) {
						String value = curr.getAttribute(TAG_VALUE);
						if (value != null)
							return Boolean.valueOf(value).booleanValue();
					}
				}
			}
		}
		return false;
	}

	public static IAction[] getFileWizardActions() {
		return createActions(getFileWizardElements());
	}

	private static String[] getWizardIDs(IConfigurationElement[] elements) {
		List<String> idList = new ArrayList<>();

		// add C wizards first
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isCProjectWizard(element)) {
				String id = element.getAttribute(TAG_ID);
				if (id != null && !idList.contains(id)) {
					idList.add(id);
				}
			}
		}
		// now add C++ wizards
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isCCProjectWizard(element)) {
				String id = element.getAttribute(TAG_ID);
				if (id != null && !idList.contains(id)) {
					idList.add(id);
				}
			}
		}

		return idList.toArray(new String[idList.size()]);
	}

	private static IAction[] createActions(IConfigurationElement[] elements) {
		List<String> idList = new ArrayList<>();
		List<IAction> actionList = new ArrayList<>();

		// add C wizards first
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isCProjectWizard(element)) {
				String id = element.getAttribute(TAG_ID);
				if (id != null && !idList.contains(id)) {
					idList.add(id);
					IAction action = new OpenNewWizardAction(element);
					actionList.add(action);
				}
			}
		}
		// now add C++ wizards
		for (int i = 0; i < elements.length; ++i) {
			IConfigurationElement element = elements[i];
			if (isCCProjectWizard(element)) {
				String id = element.getAttribute(TAG_ID);
				if (id != null && !idList.contains(id)) {
					idList.add(id);
					IAction action = new OpenNewWizardAction(element);
					actionList.add(action);
				}
			}
		}

		return actionList.toArray(new IAction[actionList.size()]);
	}

	private static class WizardConfig implements IPluginContribution {

		private IConfigurationElement fElement;

		public WizardConfig(IConfigurationElement element) {
			fElement = element;
		}

		@Override
		public String getLocalId() {
			return fElement.getAttribute("id"); //$NON-NLS-1$
		}

		@Override
		public String getPluginId() {
			return fElement.getContributor().getName();
		}

	}

	/**
	 * Returns extension data for all the C/C++ wizards contributed to the workbench.
	 *     <wizard
	 *         name="My C Wizard"
	 *         icon="icons/cwiz.gif"
	 *         category="org.eclipse.cdt.ui.newCWizards"
	 *         id="xx.MyCWizard">
	 *         <description>
	 *             My C Wizard
	 *         </description>
	 *      </wizard>
	 *
	 * @return an array of IConfigurationElement
	 */
	public static IConfigurationElement[] getAllWizardElements() {
		List<IConfigurationElement> elemList = new ArrayList<>();
		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(PlatformUI.PLUGIN_ID,
				PL_NEW);
		if (extensionPoint != null) {
			IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (element.getName().equals(TAG_WIZARD)) {
					if (!WorkbenchActivityHelper.filterItem(new WizardConfig(element))) {
						String category = element.getAttribute(ATT_CATEGORY);
						if (category != null && (category.equals(CUIPlugin.CCWIZARD_CATEGORY_ID)
								|| category.equals(CUIPlugin.CWIZARD_CATEGORY_ID))) {
							elemList.add(element);
						}
					}
				}
			}
		}
		return elemList.toArray(new IConfigurationElement[elemList.size()]);
	}

}

Back to the top