Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: afd75be6e93cb1d2a747d97ea0d1038def1d282e (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
435
436
437
438
/*******************************************************************************
 * Copyright (c) 2004, 2016 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
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.dialogs.cpaths;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.model.IPathEntryContainer;
import org.eclipse.cdt.internal.ui.util.ImageDescriptorRegistry;
import org.eclipse.cdt.ui.CDTSharedImages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.model.IWorkbenchAdapter;

class CPElementLabelProvider extends LabelProvider implements IColorProvider {

	private Color inDirect = new Color(Display.getDefault(), new RGB(170, 170, 170));

	private String fNewLabel, fCreateLabel;
	private ImageDescriptor fIncludeIcon, fMacroIcon, fLibWSrcIcon, fLibIcon;
	private ImageDescriptor fQuoteIncludeIcon, fIncludeFileIcon, fMacrosFileIcon;
	private ImageDescriptor fFolderImage, fOutputImage, fProjectImage, fContainerImage;
	private boolean bShowExported;
	private boolean bShowParentInfo;
	private ImageDescriptorRegistry fRegistry;

	public CPElementLabelProvider() {
		this(true, false);
	}

	public CPElementLabelProvider(boolean showExported, boolean showParentInfo) {
		fNewLabel = CPathEntryMessages.CPElementLabelProvider_new;
		fCreateLabel = CPathEntryMessages.CPElementLabelProvider_willbecreated;
		fRegistry = CUIPlugin.getImageDescriptorRegistry();

		fLibIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_ARCHIVE);
		fLibWSrcIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_ARCHIVE);
		fIncludeIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_INCLUDES_FOLDER);
		fQuoteIncludeIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_QUOTE_INCLUDES_FOLDER);
		fIncludeFileIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT_HEADER);
		fMacroIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_MACRO);
		fMacrosFileIcon = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_TUNIT_HEADER);
		fFolderImage = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_SOURCE_ROOT);
		fOutputImage = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_CONTAINER);
		fContainerImage = CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_LIBRARY);

		IWorkbench workbench = CUIPlugin.getDefault().getWorkbench();

		fProjectImage = workbench.getSharedImages().getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT);
		bShowExported = showExported;
		bShowParentInfo = showParentInfo;
	}

	/*
	 * @see org.eclipse.jface.viewers.BaseLabelProvider#dispose()
	 */
	@Override
	public void dispose() {
		inDirect.dispose();
		inDirect = null;
		super.dispose();
	}

	@Override
	public String getText(Object element) {
		if (element instanceof CPElement) {
			return getCPElementText((CPElement) element);
		} else if (element instanceof CPElementAttribute) {
			return getCPElementAttributeText((CPElementAttribute) element);
		} else if (element instanceof IPathEntry) {
			return getCPElementText(CPElement.createFromExisting((IPathEntry) element, null));
		} else if (element instanceof CPElementGroup) {
			return (getCPContainerGroupText((CPElementGroup) element));
		}
		return super.getText(element);
	}

	private String getCPContainerGroupText(CPElementGroup group) {
		switch (group.getEntryKind()) {
		case IPathEntry.CDT_INCLUDE:
			return CPathEntryMessages.CPElementLabelProvider_Includes;
		case IPathEntry.CDT_INCLUDE_FILE:
			return CPathEntryMessages.CPElementLabelProvider_IncludeFiles;
		case IPathEntry.CDT_MACRO:
			return CPathEntryMessages.CPElementLabelProvider_PreprocessorSymbols;
		case IPathEntry.CDT_MACRO_FILE:
			return CPathEntryMessages.CPElementLabelProvider_MacrosFiles;
		case IPathEntry.CDT_LIBRARY:
			return CPathEntryMessages.CPElementLabelProvider_Libraries;
		case -1:
			if (group.getResource().getType() == IResource.PROJECT) {
				return group.getResource().getName();
			}
			StringBuilder label = new StringBuilder(group.getResource().getProjectRelativePath().toString());
			if (!group.getResource().exists()) {
				label.append(fCreateLabel);
			}
			return label.toString();
		}
		return ""; //$NON-NLS-1$
	}

	public String getCPElementAttributeText(CPElementAttribute attrib) {
		String notAvailable = CPathEntryMessages.CPElementLabelProvider_none;
		StringBuilder buf = new StringBuilder();
		String key = attrib.getKey();
		if (key.equals(CPElement.SOURCEATTACHMENT)) {
			buf.append(CPathEntryMessages.CPElementLabelProvider_source_attachment_label);
			IPath path = (IPath) attrib.getValue();
			if (path != null && !path.isEmpty()) {
				buf.append(getPathString(path, path.getDevice() != null));
			} else {
				buf.append(notAvailable);
			}
		} else if (key.equals(CPElement.SOURCEATTACHMENTROOT)) {
			buf.append(CPathEntryMessages.CPElementLabelProvider_source_attachment_root_label);
			IPath path = (IPath) attrib.getValue();
			if (path != null && !path.isEmpty()) {
				buf.append(path.toString());
			} else {
				buf.append(notAvailable);
			}
		}
		if (key.equals(CPElement.EXCLUSION)) {
			buf.append(CPathEntryMessages.CPElementLabelProvider_exclusion_filter_label);
			IPath[] patterns = (IPath[]) attrib.getValue();
			if (patterns != null && patterns.length > 0) {
				for (int i = 0; i < patterns.length; i++) {
					if (i > 0) {
						buf.append(CPathEntryMessages.CPElementLabelProvider_exclusion_filter_separator);
					}
					buf.append(patterns[i].toString());
				}
			} else {
				buf.append(notAvailable);
			}
		}
		return buf.toString();
	}

	public String getCPElementText(CPElement cpentry) {
		IPath path = cpentry.getPath();
		switch (cpentry.getEntryKind()) {
		case IPathEntry.CDT_LIBRARY: {
			IPath libPath = (IPath) cpentry.getAttribute(CPElement.LIBRARY);
			StringBuilder str = new StringBuilder();
			addBaseString(libPath, cpentry, str);
			addExport(cpentry, str);
			addParentInfo(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_PROJECT:
			return path.lastSegment();
		case IPathEntry.CDT_INCLUDE: {
			IPath incPath = ((IPath) cpentry.getAttribute(CPElement.INCLUDE));
			StringBuilder str = new StringBuilder();
			addBaseString(incPath, cpentry, str);
			addExport(cpentry, str);
			addParentInfo(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_INCLUDE_FILE: {
			IPath incFilePath = ((IPath) cpentry.getAttribute(CPElement.INCLUDE_FILE));
			StringBuilder str = new StringBuilder();
			addBaseString(incFilePath, cpentry, str);
			addExport(cpentry, str);
			addParentInfo(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_MACRO: {
			StringBuilder str = new StringBuilder((String) cpentry.getAttribute(CPElement.MACRO_NAME) + "=" //$NON-NLS-1$
					+ (String) cpentry.getAttribute(CPElement.MACRO_VALUE));
			addBaseString(null, cpentry, str);
			addExport(cpentry, str);
			addParentInfo(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_MACRO_FILE: {
			IPath macroFilePath = ((IPath) cpentry.getAttribute(CPElement.MACROS_FILE));
			StringBuilder str = new StringBuilder();
			addBaseString(macroFilePath, cpentry, str);
			addExport(cpentry, str);
			addParentInfo(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_CONTAINER: {
			StringBuilder str = new StringBuilder(path.toString());
			try {
				IPathEntryContainer container = CoreModel.getPathEntryContainer(cpentry.getPath(),
						cpentry.getCProject());
				if (container != null) {
					str.setLength(0);
					str.append(container.getDescription());
				}
			} catch (CModelException e) {
			}
			addExport(cpentry, str);
			return str.toString();
		}
		case IPathEntry.CDT_SOURCE:
		case IPathEntry.CDT_OUTPUT: {
			StringBuilder buf = new StringBuilder(path.makeRelative().toString());
			IResource resource = cpentry.getResource();
			if (resource != null && !resource.exists()) {
				buf.append(' ');
				if (cpentry.getStatus().getSeverity() != IStatus.OK) { // only valid error for src/output would missing path...
					buf.append(fCreateLabel);
				} else {
					buf.append(fNewLabel);
				}
			}
			return buf.toString();
		}
		default:
			// pass
		}
		return CPathEntryMessages.CPElementLabelProvider_unknown_element_label;
	}

	/**
	 * @param cpentry
	 * @param str
	 */
	private void addParentInfo(CPElement cpentry, StringBuilder str) {
		if (bShowParentInfo) {
			CPElement parent = cpentry.getParentContainer();
			if (parent != null) {
				str.append(" ["); //$NON-NLS-1$
				try {
					IPathEntryContainer container = CoreModel.getPathEntryContainer(cpentry.getPath(),
							cpentry.getCProject());
					if (container != null) {
						str.append(container.getDescription());
					}
				} catch (CModelException e) {
					str.append(parent.getPath());
				}
				str.append(']');
			}
		}
	}

	private void addExport(CPElement cpentry, StringBuilder str) {
		if (bShowExported && cpentry.isExported()) {
			str.append(' ');
			str.append(CPathEntryMessages.CPElementLabelProvider_export_label);
		}
	}

	private void addBaseString(IPath endPath, CPElement cpentry, StringBuilder str) {
		IPath baseRef = (IPath) cpentry.getAttribute(CPElement.BASE_REF);
		if (!baseRef.isEmpty()) {
			if (baseRef.isAbsolute()) {
				//				str.append("From project ");
				IPath path = baseRef;
				if (endPath != null) {
					path = path.append(endPath);
				}
				str.append(path.makeRelative().toOSString());
			} else {
				//				str.append("From contribution ");
				IPathEntryContainer container;
				if (endPath != null) {
					str.append(endPath.toOSString());
				}
				str.append(" - ("); //$NON-NLS-1$
				try {
					container = CoreModel.getPathEntryContainer(baseRef, cpentry.getCProject());
					if (container != null) {
						str.append(container.getDescription());
					}
				} catch (CModelException e1) {
				}
				str.append(')');
			}
		} else {
			IPath path = (IPath) cpentry.getAttribute(CPElement.BASE);
			if (!path.isEmpty()) {
				if (endPath != null) {
					path = path.append(endPath);
				}
				str.insert(0, path.toOSString());
			} else if (endPath != null) {
				str.insert(0, endPath.toOSString());
			}
		}

	}

	private String getPathString(IPath path, boolean isExternal) {
		//		if (ArchiveFileFilter.isArchivePath(path)) {
		//			IPath appendedPath = path.removeLastSegments(1);
		//			String appended = isExternal ? appendedPath.toOSString() :
		// appendedPath.makeRelative().toString();
		//			return
		// CPathEntryMessages.getFormattedString("CPListLabelProvider.twopart",
		// //$NON-NLS-1$
		//					new String[] { path.lastSegment(), appended});
		//		} else {
		return isExternal ? path.toOSString() : path.makeRelative().toString();
		//		}
	}

	private ImageDescriptor getCPElementBaseImage(CPElement cpentry) {
		switch (cpentry.getEntryKind()) {
		case IPathEntry.CDT_OUTPUT:
			if (cpentry.getPath().segmentCount() == 1) {
				return fProjectImage;
			}
			return fOutputImage;
		case IPathEntry.CDT_SOURCE:
			if (cpentry.getPath().segmentCount() == 1) {
				return fProjectImage;
			}
			return fFolderImage;
		case IPathEntry.CDT_LIBRARY:
			IPath path = (IPath) cpentry.getAttribute(CPElement.SOURCEATTACHMENT);
			if (path == null || path.isEmpty()) {
				return fLibIcon;
			}
			return fLibWSrcIcon;
		case IPathEntry.CDT_PROJECT:
			return fProjectImage;
		case IPathEntry.CDT_CONTAINER:
			return fContainerImage;
		case IPathEntry.CDT_INCLUDE:
			if (((Boolean) cpentry.getAttribute(CPElement.SYSTEM_INCLUDE)).booleanValue())
				return fIncludeIcon;
			return fQuoteIncludeIcon;
		case IPathEntry.CDT_INCLUDE_FILE:
			return fIncludeFileIcon;
		case IPathEntry.CDT_MACRO:
			return fMacroIcon;
		case IPathEntry.CDT_MACRO_FILE:
			return fMacrosFileIcon;
		default:
			return null;
		}
	}

	private static final Point SMALL_SIZE = new Point(16, 16);

	@Override
	public Image getImage(Object element) {
		if (element instanceof CPElement) {
			CPElement cpentry = (CPElement) element;
			ImageDescriptor imageDescriptor = getCPElementBaseImage(cpentry);
			if (imageDescriptor != null) {
				switch (cpentry.getStatus().getSeverity()) {
				case IStatus.WARNING:
					imageDescriptor = new CPListImageDescriptor(imageDescriptor, CPListImageDescriptor.WARNING,
							SMALL_SIZE);
					break;
				case IStatus.ERROR:
					imageDescriptor = new CPListImageDescriptor(imageDescriptor, CPListImageDescriptor.ERROR,
							SMALL_SIZE);
					break;
				}
				if (cpentry.getInherited() != null) {
					imageDescriptor = new CPListImageDescriptor(imageDescriptor, CPListImageDescriptor.PATH_INHERIT,
							SMALL_SIZE);
				}
				return fRegistry.get(imageDescriptor);
			}
		} else if (element instanceof CPElementAttribute) {
			String key = ((CPElementAttribute) element).getKey();
			if (key.equals(CPElement.SOURCEATTACHMENT)) {
				return fRegistry.get(CDTSharedImages.getImageDescriptor(CDTSharedImages.IMG_OBJS_SOURCE_ATTACH_ATTRIB));
			} else if (key.equals(CPElement.EXCLUSION)) {
				return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_EXCLUSION_FILTER_ATTRIB);
			}
		} else if (element instanceof IPathEntry) {
			return getImage(CPElement.createFromExisting((IPathEntry) element, null));
		} else if (element instanceof CPElementGroup) {
			switch (((CPElementGroup) element).getEntryKind()) {
			case IPathEntry.CDT_INCLUDE:
				return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_INCLUDES_CONTAINER);
			case IPathEntry.CDT_MACRO:
				return fRegistry.get(fMacroIcon);
			case IPathEntry.CDT_INCLUDE_FILE:
			case IPathEntry.CDT_MACRO_FILE:
				return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_INCLUDE);
			case IPathEntry.CDT_LIBRARY:
				return CDTSharedImages.getImage(CDTSharedImages.IMG_OBJS_LIBRARY);
			case -1:
				IResource res = ((CPElementGroup) element).getResource();
				IWorkbenchAdapter adapter = res.getAdapter(IWorkbenchAdapter.class);
				ImageDescriptor imageDescriptor = adapter.getImageDescriptor(res);
				if (!res.exists()) {
					imageDescriptor = new CPListImageDescriptor(imageDescriptor, CPListImageDescriptor.WARNING,
							SMALL_SIZE);
				}
				return fRegistry.get(imageDescriptor);
			}
		}
		return null;
	}

	@Override
	public Color getForeground(Object element) {
		if (element instanceof CPElement) {
			if (((CPElement) element).getInherited() != null) {
				return inDirect;
			}
		}
		return null;
	}

	@Override
	public Color getBackground(Object element) {
		// TODO Auto-generated method stub
		return null;
	}
}

Back to the top