Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 246b8aa0e4838a97a36ebd7f0b6b2d7f5ec0783a (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
/*******************************************************************************
 * Copyright (c) 2000, 2016 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
 *     QNX Software System
 *     Anton Leherbauer (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.ui;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider;
import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
import org.eclipse.cdt.internal.ui.viewsupport.CUILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * The label provider for the c model elements.
 */
public class CElementLabelProvider extends LabelProvider {
	/**
	 * Flag (bit mask) indicating that methods labels include the method return type. (appended)
	 */
	public final static int SHOW_RETURN_TYPE = 0x001;

	/**
	 * Flag (bit mask) indicating that method label include method parameter types.
	 */
	public final static int SHOW_PARAMETERS = 0x002;

	/**
	 * Flag (bit mask) indicating that method label include thrown exception.
	 */
	public final static int SHOW_EXCEPTION = 0x004;

	/**
	 * Flag (bit mask) indicating that the label should show the icons with no space
	 * reserved for overlays.
	 */
	public final static int SHOW_SMALL_ICONS = 0x100;

	/**
	 * Flag (bit mask) indicating that the label should include overlay icons
	 * for element type and modifiers.
	 */
	public final static int SHOW_OVERLAY_ICONS = 0x010;

	/**
	 * Flag (bit mask) indicating that Complation Units, Class Files, Types, Declarations and Members
	 * should be rendered qualified.
	 * Examples: java.lang.String, java.util.Vector.size()
	 *
	 * @since 2.0
	 */
	public final static int SHOW_QUALIFIED = 0x400;

	/**
	 * Flag (bit mask) indicating that Compilation Units, Class Files, Types, Declarations and Members
	 * should be rendered qualified. The qualification is appended
	 * Examples: String - java.lang, size() - java.util.Vector
	 *
	 * @since 2.0
	 */
	public final static int SHOW_POST_QUALIFIED = 0x800;

	/**
	 * Constant (value <code>0</code>) indicating that the label should show
	 * the basic images only.
	 */
	public final static int SHOW_BASICS = 0x000;

	public final static int SHOW_DEFAULT = Integer.valueOf(SHOW_PARAMETERS | SHOW_OVERLAY_ICONS).intValue();

	private volatile WorkbenchLabelProvider fWorkbenchLabelProvider;
	protected CElementImageProvider fImageLabelProvider;
	private CUILabelProvider fCElementLabelProvider;

	private int fFlags;
	private int fImageFlags;
	private int fTextFlags;

	public CElementLabelProvider() {
		this(SHOW_DEFAULT);
	}

	public CElementLabelProvider(int flags) {
		// WorkbenchLabelProvider may only be initialized on the UI thread
		// http://bugs.eclipse.org/247274
		if (Display.getCurrent() != null) {
			fWorkbenchLabelProvider = new WorkbenchLabelProvider();
		} else {
			// Delay initialization
			CUIPlugin.getStandardDisplay().asyncExec(() -> {
				if (fCElementLabelProvider != null) {
					fWorkbenchLabelProvider = new WorkbenchLabelProvider();
				}
			});
		}
		fImageLabelProvider = new CElementImageProvider();

		fFlags = flags;
		fCElementLabelProvider = new CUILabelProvider(getTextFlags() | CElementLabels.TEMPLATE_PARAMETERS,
				getImageFlags());
	}

	@Override
	public String getText(Object element) {
		if (element instanceof ICElement) {
			return fCElementLabelProvider.getText(element);
		}
		if (fWorkbenchLabelProvider != null) {
			return fWorkbenchLabelProvider.getText(element);
		}
		return super.getText(element);
	}

	@Override
	public Image getImage(Object element) {
		return fImageLabelProvider.getImageLabel(element, getImageFlags());
	}

	@Override
	public void dispose() {
		if (fCElementLabelProvider != null) {
			fCElementLabelProvider.dispose();
			fCElementLabelProvider = null;
		}
		if (fWorkbenchLabelProvider != null) {
			fWorkbenchLabelProvider.dispose();
			fWorkbenchLabelProvider = null;
		}
		if (fImageLabelProvider != null) {
			fImageLabelProvider.dispose();
		}
	}

	private boolean getFlag(int flag) {
		return (fFlags & flag) != 0;
	}

	/**
	 * Gets the image flags.
	 * Can be overwritten by super classes.
	 * @return Returns a int
	 */
	public int getImageFlags() {
		fImageFlags = 0;
		if (getFlag(SHOW_OVERLAY_ICONS)) {
			fImageFlags |= CElementImageProvider.OVERLAY_ICONS;
		}
		if (getFlag(SHOW_SMALL_ICONS)) {
			fImageFlags |= CElementImageProvider.SMALL_ICONS;
		}
		return fImageFlags;
	}

	/**
	 * Gets the text flags. Can be overwritten by super classes.
	 * @return Returns a int
	 */
	public int getTextFlags() {
		fTextFlags = 0;
		if (getFlag(SHOW_RETURN_TYPE)) {
			fTextFlags |= CElementLabels.M_APP_RETURNTYPE;
		}
		if (getFlag(SHOW_PARAMETERS)) {
			fTextFlags |= CElementLabels.M_PARAMETER_TYPES;
		}
		if (getFlag(SHOW_EXCEPTION)) {
			fTextFlags |= CElementLabels.M_EXCEPTIONS;
		}
		if (getFlag(SHOW_POST_QUALIFIED)) {
			fTextFlags |= CElementLabels.M_POST_QUALIFIED;
		}
		return fTextFlags;
	}
}

Back to the top