Fix another batch of FindBugs warnings
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfNavigatorLabelProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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 *******************************************************************************/
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;
21 import org.eclipse.linuxtools.tmf.ui.TmfUiPlugin;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.ISharedImages;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.navigator.ICommonContentExtensionSite;
27 import org.eclipse.ui.navigator.ICommonLabelProvider;
28 import org.osgi.framework.Bundle;
29
30 /**
31 * <b><u>TmfNavigatorLabelProvider</u></b>
32 * <p>
33 */
34 public class TmfNavigatorLabelProvider implements ICommonLabelProvider {
35
36 // ------------------------------------------------------------------------
37 // Constants
38 // ------------------------------------------------------------------------
39
40 private static final Image fFolderIcon = PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
41 private static final String fTraceIconFile = "icons/elcl16/trace.gif"; //$NON-NLS-1$
42 private static final String fExperimentIconFile = "icons/elcl16/experiment.gif"; //$NON-NLS-1$
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
48 private final Image fTraceFolderIcon = fFolderIcon;
49 private final Image fExperimentFolderIcon = fFolderIcon;
50
51 private final Image fDefaultTraceIcon;
52 private final Image fExperimentIcon;
53
54 // ------------------------------------------------------------------------
55 // Constructors
56 // ------------------------------------------------------------------------
57
58 public TmfNavigatorLabelProvider() {
59 Bundle bundle = TmfUiPlugin.getDefault().getBundle();
60 fDefaultTraceIcon = loadIcon(bundle, fTraceIconFile);
61 fExperimentIcon = loadIcon(bundle, fExperimentIconFile);
62 }
63
64 private Image loadIcon(Bundle bundle, String url) {
65 TmfUiPlugin plugin = TmfUiPlugin.getDefault();
66 String key = bundle.getSymbolicName() + "/" + url; //$NON-NLS-1$
67 Image icon = plugin.getImageRegistry().get(key);
68 if (icon == null) {
69 URL imageURL = bundle.getResource(url);
70 ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL);
71 icon = descriptor.createImage();
72 plugin.getImageRegistry().put(key, icon);
73 }
74 return icon;
75 }
76
77 // ------------------------------------------------------------------------
78 // ICommonLabelProvider
79 // ------------------------------------------------------------------------
80
81 @Override
82 public Image getImage(Object element) {
83
84 if (element instanceof TmfTraceElement) {
85 TmfTraceElement trace = (TmfTraceElement) element;
86 String icon = null;
87 try {
88 String name = trace.getResource().getPersistentProperty(TmfTraceElement.TRACEBUNDLE);
89 icon = trace.getResource().getPersistentProperty(TmfTraceElement.TRACEICON);
90 if (name != null && icon != null) {
91 Bundle bundle = Platform.getBundle(name);
92 return loadIcon(bundle, icon);
93 }
94 } catch (CoreException e) {
95 }
96 return fDefaultTraceIcon;
97 }
98
99 if (element instanceof TmfExperimentElement)
100 return fExperimentIcon;
101
102 if (element instanceof TmfExperimentFolder)
103 return fExperimentFolderIcon;
104
105 if (element instanceof TmfTraceFolder)
106 return fTraceFolderIcon;
107
108 return null;
109 }
110
111 @Override
112 public String getText(Object element) {
113
114 if (element instanceof TmfTraceFolder) {
115 TmfTraceFolder folder = (TmfTraceFolder) element;
116 return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
117 }
118
119 if (element instanceof TmfExperimentElement) {
120 TmfExperimentElement folder = (TmfExperimentElement) element;
121 return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
122 }
123
124 if (element instanceof TmfExperimentFolder) {
125 TmfExperimentFolder folder = (TmfExperimentFolder) element;
126 return folder.getName() + " [" + folder.getChildren().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$
127 }
128
129 // Catch all
130 if (element instanceof ITmfProjectModelElement) {
131 return ((ITmfProjectModelElement) element).getName();
132 }
133
134 return null;
135 }
136
137 @Override
138 public void addListener(ILabelProviderListener listener) {
139 }
140
141 @Override
142 public void dispose() {
143 }
144
145 @Override
146 public boolean isLabelProperty(Object element, String property) {
147 return false;
148 }
149
150 @Override
151 public void removeListener(ILabelProviderListener listener) {
152 }
153
154 @Override
155 public void restoreState(IMemento aMemento) {
156 }
157
158 @Override
159 public void saveState(IMemento aMemento) {
160 }
161
162 @Override
163 public String getDescription(Object anElement) {
164 return getText(anElement);
165 }
166
167 @Override
168 public void init(ICommonContentExtensionSite aConfig) {
169 }
170
171 }
This page took 0.037127 seconds and 6 git commands to generate.