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