Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1453ac56d4569dd97644c2ae8fc0718f9f5cfc78 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 QNX Software Systems 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:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.dialogs.cpaths;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IContainerEntry;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.internal.ui.util.CoreUtility;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.wizards.ICPathContainerPage;
import org.eclipse.cdt.ui.wizards.IPathEntryContainerPage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.osgi.framework.Bundle;

public class CPathContainerDescriptor implements IContainerDescriptor {


	/**
	 * Adapter class to adapter deprecated ICPathContainerPage to new IPathEntryContainerPage
	 * @author Dave
	 * @deprecated
	 */
	public class PathEntryContainerPageAdapter implements IWizardPage, IPathEntryContainerPage {
		private final ICPathContainerPage fPage;
		/**
		 * @param pageName
		 */
		protected PathEntryContainerPageAdapter(ICPathContainerPage page) {
			fPage = page;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.ui.wizards.IPathEntryContainerPage#initialize(org.eclipse.cdt.core.model.ICProject, org.eclipse.cdt.core.model.IPathEntry[])
		 */
		public void initialize(ICProject project, IPathEntry[] currentEntries) {
			fPage.initialize(project, currentEntries);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.ui.wizards.IPathEntryContainerPage#finish()
		 */
		public boolean finish() {
			return fPage.finish();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.ui.wizards.IPathEntryContainerPage#getNewContainers()
		 */
		public IContainerEntry[] getNewContainers() {
			IPathEntry[] entries = fPage.getContainerEntries();
			IContainerEntry[] containers = new IContainerEntry[entries.length];
			System.arraycopy(entries, 0, containers, 0, entries.length);
			return containers;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.ui.wizards.IPathEntryContainerPage#setSelection(org.eclipse.cdt.core.model.IContainerEntry)
		 */
		public void setSelection(IContainerEntry containerEntry) {
			fPage.setSelection(containerEntry);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
		 */
		public void createControl(Composite parent) {
			fPage.createControl(parent);

		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#canFlipToNextPage()
		 */
		public boolean canFlipToNextPage() {
			return fPage.canFlipToNextPage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#getName()
		 */
		public String getName() {
			return fPage.getName();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#getNextPage()
		 */
		public IWizardPage getNextPage() {
			return fPage.getNextPage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#getPreviousPage()
		 */
		public IWizardPage getPreviousPage() {
			return fPage.getPreviousPage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#getWizard()
		 */
		public IWizard getWizard() {
			return fPage.getWizard();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#isPageComplete()
		 */
		public boolean isPageComplete() {
			return fPage.isPageComplete();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#setPreviousPage(org.eclipse.jface.wizard.IWizardPage)
		 */
		public void setPreviousPage(IWizardPage page) {
			fPage.setPreviousPage(page);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.wizard.IWizardPage#setWizard(org.eclipse.jface.wizard.IWizard)
		 */
		public void setWizard(IWizard newWizard) {
			fPage.setWizard(newWizard);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
		 */
		public void dispose() {
			fPage.dispose();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getControl()
		 */
		public Control getControl() {
			return fPage.getControl();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getDescription()
		 */
		public String getDescription() {
			return fPage.getDescription();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getErrorMessage()
		 */
		public String getErrorMessage() {
			return fPage.getErrorMessage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getImage()
		 */
		public Image getImage() {
			return fPage.getImage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getMessage()
		 */
		public String getMessage() {
			return fPage.getMessage();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#getTitle()
		 */
		public String getTitle() {
			return fPage.getTitle();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#performHelp()
		 */
		public void performHelp() {
			fPage.performHelp();
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#setDescription(java.lang.String)
		 */
		public void setDescription(String description) {
			fPage.setDescription(description);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#setImageDescriptor(org.eclipse.jface.resource.ImageDescriptor)
		 */
		public void setImageDescriptor(ImageDescriptor image) {
			fPage.setImageDescriptor(image);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#setTitle(java.lang.String)
		 */
		public void setTitle(String title) {
			fPage.setTitle(title);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
		 */
		public void setVisible(boolean visible) {
			fPage.setVisible(visible);
		}

	}
	private IConfigurationElement fConfigElement;

	private static final String ATT_EXTENSION = "PathContainerPage"; //$NON-NLS-1$

	private static final String ATT_ID = "id"; //$NON-NLS-1$
	private static final String ATT_NAME = "name"; //$NON-NLS-1$
	private static final String ATT_ICON = "icon"; //$NON-NLS-1$
	private static final String ATT_PAGE_CLASS = "class"; //$NON-NLS-1$	

	private Image pageImage;

	public CPathContainerDescriptor(IConfigurationElement configElement) throws CoreException {
		super();
		fConfigElement = configElement;

		String id = fConfigElement.getAttribute(ATT_ID);
		String name = configElement.getAttribute(ATT_NAME);
		String pageClassName = configElement.getAttribute(ATT_PAGE_CLASS);

		if (name == null) {
			throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0, "Invalid extension (missing name): " + id, //$NON-NLS-1$
					null));
		}
		if (pageClassName == null) {
			throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0,
					"Invalid extension (missing page class name): " + id, null)); //$NON-NLS-1$
		}
	}

	public IPathEntryContainerPage createPage() throws CoreException {
		Object elem = CoreUtility.createExtension(fConfigElement, ATT_PAGE_CLASS);
		if (elem instanceof IPathEntryContainerPage) {
			return (IPathEntryContainerPage) elem;
		} else if (elem instanceof ICPathContainerPage) {
			return new PathEntryContainerPageAdapter((ICPathContainerPage)elem);
		}
		String id = fConfigElement.getAttribute(ATT_ID);
		throw new CoreException(new Status(IStatus.ERROR, CUIPlugin.PLUGIN_ID, 0,
				"Invalid extension (page not of type IClasspathContainerPage): " + id, null)); //$NON-NLS-1$
	}

	public String getName() {
		return fConfigElement.getAttribute(ATT_NAME);
	}

	public Image getImage() {
		if (pageImage == null) {
			String imageName = fConfigElement.getAttribute(ATT_ICON);
			if (imageName != null) {
				IExtension extension = fConfigElement.getDeclaringExtension();
				String plugin = extension.getNamespace();
				Image image = getImageFromPlugin(plugin, imageName);
				pageImage = image;
			}
		}
		return pageImage;
	}

	public Image getImageFromPlugin(String plugin, String subdirectoryAndFilename) {
		Bundle bundle = Platform.getBundle(plugin);
		URL iconURL = bundle.getEntry("/"); //$NON-NLS-1$
		return getImageFromURL(iconURL, subdirectoryAndFilename);
	}

	public Image getImageFromURL(URL installURL, String subdirectoryAndFilename) {
		Image image = null;
		try {
			URL newURL = new URL(installURL, subdirectoryAndFilename);
			ImageDescriptor desc = ImageDescriptor.createFromURL(newURL);
			image = desc.createImage();
		} catch (MalformedURLException e) {
		} catch (SWTException e) {
		}
		return image;
	}

	public String getPageClass() {
		return fConfigElement.getAttribute(ATT_PAGE_CLASS);
	}

	public boolean canEdit(IPathEntry entry) {
		String id = fConfigElement.getAttribute(ATT_ID);
		if (entry.getEntryKind() == IPathEntry.CDT_CONTAINER) {
			String type = entry.getPath().segment(0);
			return id.equals(type);
		}
		return false;
	}

	public static IContainerDescriptor[] getDescriptors() {
		ArrayList containers = new ArrayList();

		IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(CUIPlugin.PLUGIN_ID, ATT_EXTENSION);
		if (extensionPoint != null) {
			IContainerDescriptor defaultPage = null;
			String defaultPageName = CPathContainerDefaultPage.class.getName();

			IConfigurationElement[] elements = extensionPoint.getConfigurationElements();
			for (int i = 0; i < elements.length; i++) {
				try {
					CPathContainerDescriptor curr = new CPathContainerDescriptor(elements[i]);
					if (defaultPageName.equals(curr.getPageClass())) {
						defaultPage = curr;
					} else {
						containers.add(curr);
					}
				} catch (CoreException e) {
					CUIPlugin.getDefault().log(e);
				}
			}
			if (defaultPageName != null && containers.isEmpty()) {
				// default page only added if no other extensions found
				containers.add(defaultPage);
			}
		}
		return (CPathContainerDescriptor[]) containers.toArray(new CPathContainerDescriptor[containers.size()]);
	}

}

Back to the top