tmf: Display icons using the new tracetypeui extension point
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfNavigatorLabelProvider.java
CommitLineData
12c155f5 1/*******************************************************************************
c8422608 2 * Copyright (c) 2011, 2013 Ericsson
013a5f1c 3 *
12c155f5
FC
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
013a5f1c 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
ab37ff41 11 * Patrick Tasse - Add support for unknown trace type icon
12c155f5
FC
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.tmf.ui.project.model;
15
16import java.net.URL;
17
18import org.eclipse.core.runtime.CoreException;
efd33708 19import org.eclipse.core.runtime.IConfigurationElement;
12c155f5
FC
20import org.eclipse.core.runtime.Platform;
21import org.eclipse.jface.resource.ImageDescriptor;
22import org.eclipse.jface.viewers.ILabelProviderListener;
8fd82db5 23import org.eclipse.linuxtools.internal.tmf.ui.Activator;
e12ecd30 24import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
12c155f5
FC
25import org.eclipse.swt.graphics.Image;
26import org.eclipse.ui.IMemento;
27import org.eclipse.ui.ISharedImages;
28import org.eclipse.ui.PlatformUI;
29import org.eclipse.ui.navigator.ICommonContentExtensionSite;
30import org.eclipse.ui.navigator.ICommonLabelProvider;
31import org.osgi.framework.Bundle;
32
33/**
013a5f1c 34 * The TMF project label provider for the tree viewer in the project explorer view.
12c155f5 35 * <p>
b544077e
BH
36 * @version 1.0
37 * @author Francois Chouinard
12c155f5
FC
38 */
39public class TmfNavigatorLabelProvider implements ICommonLabelProvider {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44
5a5c2fc7
FC
45 private static final Image fFolderIcon = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
46 private static final String fTraceIconFile = "icons/elcl16/trace.gif"; //$NON-NLS-1$
ab37ff41 47 private static final String fUnknownIconFile = "icons/elcl16/unknown_parser.gif"; //$NON-NLS-1$
5a5c2fc7 48 private static final String fExperimentIconFile = "icons/elcl16/experiment.gif"; //$NON-NLS-1$
c068a752
GB
49 private static final String fAnalysisIconFile = "icons/ovr16/experiment_folder_ovr.png"; //$NON-NLS-1$
50 private static final String fViewIconFile = "icons/obj16/node_obj.gif"; //$NON-NLS-1$
12c155f5
FC
51
52 // ------------------------------------------------------------------------
53 // Attributes
54 // ------------------------------------------------------------------------
55
56 private final Image fTraceFolderIcon = fFolderIcon;
57 private final Image fExperimentFolderIcon = fFolderIcon;
58
59 private final Image fDefaultTraceIcon;
ab37ff41 60 private final Image fUnknownTraceIcon;
12c155f5 61 private final Image fExperimentIcon;
c068a752
GB
62 private final Image fDefaultAnalysisIcon;
63 private final Image fDefaultViewIcon;
12c155f5
FC
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
b544077e 69 /**
013a5f1c
AM
70 * Constructor.
71 *
b544077e
BH
72 * Creates the TMF navigator content provider.
73 */
12c155f5 74 public TmfNavigatorLabelProvider() {
8fd82db5 75 Bundle bundle = Activator.getDefault().getBundle();
12c155f5 76 fDefaultTraceIcon = loadIcon(bundle, fTraceIconFile);
ab37ff41 77 fUnknownTraceIcon = loadIcon(bundle, fUnknownIconFile);
12c155f5 78 fExperimentIcon = loadIcon(bundle, fExperimentIconFile);
c068a752
GB
79 fDefaultAnalysisIcon = loadIcon(bundle, fAnalysisIconFile);
80 fDefaultViewIcon = loadIcon(bundle, fViewIconFile);
12c155f5
FC
81 }
82
abbdd66a 83 private static Image loadIcon(Bundle bundle, String url) {
8fd82db5 84 Activator plugin = Activator.getDefault();
12c155f5
FC
85 String key = bundle.getSymbolicName() + "/" + url; //$NON-NLS-1$
86 Image icon = plugin.getImageRegistry().get(key);
87 if (icon == null) {
88 URL imageURL = bundle.getResource(url);
89 ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL);
25dc6000
PT
90 if (descriptor != null) {
91 icon = descriptor.createImage();
92 plugin.getImageRegistry().put(key, icon);
93 }
12c155f5
FC
94 }
95 return icon;
96 }
97
98 // ------------------------------------------------------------------------
99 // ICommonLabelProvider
100 // ------------------------------------------------------------------------
11252342 101
12c155f5
FC
102 @Override
103 public Image getImage(Object element) {
104
105 if (element instanceof TmfTraceElement) {
106 TmfTraceElement trace = (TmfTraceElement) element;
12c155f5 107 try {
efd33708
MAL
108 String traceType = trace.getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
109 if (traceType == null) {
ab37ff41
PT
110 return fUnknownTraceIcon;
111 }
efd33708
MAL
112
113 IConfigurationElement traceUIAttributes = TmfTraceTypeUIUtils.getTraceUIAttributes(traceType);
e12ecd30 114 String name = trace.getResource().getPersistentProperty(TmfCommonConstants.TRACEBUNDLE);
ab37ff41 115 String icon = trace.getResource().getPersistentProperty(TmfCommonConstants.TRACEICON);
efd33708
MAL
116 if (traceUIAttributes != null) {
117 String iconAttr = traceUIAttributes.getAttribute(TmfTraceTypeUIUtils.ICON_ATTR);
118 if (iconAttr != null) {
119 name = traceUIAttributes.getContributor().getName();
120 icon = iconAttr;
121 }
122 }
123
12c155f5
FC
124 if (name != null && icon != null) {
125 Bundle bundle = Platform.getBundle(name);
25dc6000
PT
126 if (bundle != null) {
127 Image image = loadIcon(bundle, icon);
128 if (image != null) {
129 return image;
130 }
131 }
132 return fUnknownTraceIcon;
12c155f5
FC
133 }
134 } catch (CoreException e) {
135 }
136 return fDefaultTraceIcon;
137 }
138
013a5f1c 139 if (element instanceof TmfExperimentElement) {
12c155f5 140 return fExperimentIcon;
013a5f1c 141 }
12c155f5 142
013a5f1c 143 if (element instanceof TmfExperimentFolder) {
12c155f5 144 return fExperimentFolderIcon;
013a5f1c 145 }
12c155f5 146
013a5f1c 147 if (element instanceof TmfTraceFolder) {
12c155f5 148 return fTraceFolderIcon;
013a5f1c 149 }
12c155f5 150
c068a752
GB
151 if (element instanceof TmfAnalysisOutputElement) {
152 TmfAnalysisOutputElement output = (TmfAnalysisOutputElement) element;
153 Image icon = output.getIcon();
154 if (icon == null) {
155 return fDefaultViewIcon;
156 }
157 return icon;
158 }
159
160 if (element instanceof TmfAnalysisElement) {
161 TmfAnalysisElement analysis = (TmfAnalysisElement) element;
162 String iconFile = analysis.getIconFile();
163 if (iconFile != null) {
164 Bundle bundle = analysis.getBundle();
165 if (bundle != null) {
166 Image icon = loadIcon(bundle, iconFile);
167 return icon;
168 }
169 }
170 return fDefaultAnalysisIcon;
171 }
172
12c155f5
FC
173 return null;
174 }
175
176 @Override
177 public String getText(Object element) {
178
179 if (element instanceof TmfTraceFolder) {
180 TmfTraceFolder folder = (TmfTraceFolder) element;
181 return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
182 }
183
184 if (element instanceof TmfExperimentElement) {
185 TmfExperimentElement folder = (TmfExperimentElement) element;
186 return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
187 }
188
7d3950f5
FC
189 if (element instanceof TmfExperimentFolder) {
190 TmfExperimentFolder folder = (TmfExperimentFolder) element;
191 return folder.getName() + " [" + folder.getChildren().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
192 }
193
12c155f5
FC
194 // Catch all
195 if (element instanceof ITmfProjectModelElement) {
196 return ((ITmfProjectModelElement) element).getName();
197 }
198
199 return null;
200 }
201
202 @Override
203 public void addListener(ILabelProviderListener listener) {
204 }
205
206 @Override
207 public void dispose() {
208 }
209
210 @Override
211 public boolean isLabelProperty(Object element, String property) {
212 return false;
213 }
214
215 @Override
216 public void removeListener(ILabelProviderListener listener) {
217 }
218
219 @Override
220 public void restoreState(IMemento aMemento) {
221 }
222
223 @Override
224 public void saveState(IMemento aMemento) {
225 }
226
227 @Override
228 public String getDescription(Object anElement) {
229 return getText(anElement);
230 }
231
232 @Override
233 public void init(ICommonContentExtensionSite aConfig) {
234 }
235
236}
This page took 0.057225 seconds and 5 git commands to generate.