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