Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b5470dad1db47590a57ba8a654baaa1ace82eba (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
/*******************************************************************************
 * Copyright (c) 2008, 2013 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *    IBM Corporation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.viewsupport;

import org.eclipse.cdt.core.CCProjectNature;
import org.eclipse.cdt.core.CProjectNature;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ResourceLocator;
import org.eclipse.jface.viewers.IDecoration;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ILightweightLabelDecorator;

/**
 * A label provider that marks all translation units that are currently part of the index.
 */
public class IndexedFilesLabelProvider implements ILightweightLabelDecorator {
	private static final ImageDescriptor INDEXED = ResourceLocator
			.imageDescriptorFromBundle(CUIPlugin.PLUGIN_ID, "$nl$/icons/ovr16/indexedFile.gif").get(); //$NON-NLS-1$

	public IndexedFilesLabelProvider() {
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	/**
	 * Adds the linked resource overlay if the given element is a linked
	 * resource.
	 *
	 * @param element element to decorate
	 * @param decoration  The decoration we are adding to
	 * @see org.eclipse.jface.viewers.ILightweightLabelDecorator#decorate(Object, IDecoration)
	 */
	@Override
	public void decorate(Object element, IDecoration decoration) {
		IIndexFileLocation ifl = null;
		IProject project = null;
		if (element instanceof IFile) {
			final IFile file = (IFile) element;
			project = file.getProject();

			try {
				if (project.hasNature(CProjectNature.C_NATURE_ID) || project.hasNature(CCProjectNature.CC_NATURE_ID)) {
					ifl = IndexLocationFactory.getWorkspaceIFL(file);
				}
			} catch (CoreException e) {
				CUIPlugin.log(e);
			}

		} else if (element instanceof ITranslationUnit) {
			final ITranslationUnit tu = (ITranslationUnit) element;
			ifl = IndexLocationFactory.getIFL(tu);
			project = tu.getCProject().getProject();
		}
		if (ifl != null && isIndexed(project, ifl)) {
			decoration.addOverlay(INDEXED, IDecoration.TOP_LEFT);
		}
	}

	private boolean isIndexed(IProject project, IIndexFileLocation ifl) {
		if (project == null || ifl == null) {
			return false;
		}

		return IndexedFilesCache.getInstance().isIndexed(project, ifl);
	}
}

Back to the top