| 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 | |
| 49 | // ------------------------------------------------------------------------ |
| 50 | // Attributes |
| 51 | // ------------------------------------------------------------------------ |
| 52 | |
| 53 | private final Image fTraceFolderIcon = fFolderIcon; |
| 54 | private final Image fExperimentFolderIcon = fFolderIcon; |
| 55 | |
| 56 | private final Image fDefaultTraceIcon; |
| 57 | private final Image fUnknownTraceIcon; |
| 58 | private final Image fExperimentIcon; |
| 59 | |
| 60 | // ------------------------------------------------------------------------ |
| 61 | // Constructors |
| 62 | // ------------------------------------------------------------------------ |
| 63 | |
| 64 | /** |
| 65 | * Constructor. |
| 66 | * |
| 67 | * Creates the TMF navigator content provider. |
| 68 | */ |
| 69 | public TmfNavigatorLabelProvider() { |
| 70 | Bundle bundle = Activator.getDefault().getBundle(); |
| 71 | fDefaultTraceIcon = loadIcon(bundle, fTraceIconFile); |
| 72 | fUnknownTraceIcon = loadIcon(bundle, fUnknownIconFile); |
| 73 | fExperimentIcon = loadIcon(bundle, fExperimentIconFile); |
| 74 | } |
| 75 | |
| 76 | private static Image loadIcon(Bundle bundle, String url) { |
| 77 | Activator plugin = Activator.getDefault(); |
| 78 | String key = bundle.getSymbolicName() + "/" + url; //$NON-NLS-1$ |
| 79 | Image icon = plugin.getImageRegistry().get(key); |
| 80 | if (icon == null) { |
| 81 | URL imageURL = bundle.getResource(url); |
| 82 | ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL); |
| 83 | icon = descriptor.createImage(); |
| 84 | plugin.getImageRegistry().put(key, icon); |
| 85 | } |
| 86 | return icon; |
| 87 | } |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // ICommonLabelProvider |
| 91 | // ------------------------------------------------------------------------ |
| 92 | /* |
| 93 | * (non-Javadoc) |
| 94 | * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object) |
| 95 | */ |
| 96 | @Override |
| 97 | public Image getImage(Object element) { |
| 98 | |
| 99 | if (element instanceof TmfTraceElement) { |
| 100 | TmfTraceElement trace = (TmfTraceElement) element; |
| 101 | try { |
| 102 | if (trace.getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE) == null) { |
| 103 | return fUnknownTraceIcon; |
| 104 | } |
| 105 | String name = trace.getResource().getPersistentProperty(TmfCommonConstants.TRACEBUNDLE); |
| 106 | String icon = trace.getResource().getPersistentProperty(TmfCommonConstants.TRACEICON); |
| 107 | if (name != null && icon != null) { |
| 108 | Bundle bundle = Platform.getBundle(name); |
| 109 | return loadIcon(bundle, icon); |
| 110 | } |
| 111 | } catch (CoreException e) { |
| 112 | } |
| 113 | return fDefaultTraceIcon; |
| 114 | } |
| 115 | |
| 116 | if (element instanceof TmfExperimentElement) { |
| 117 | return fExperimentIcon; |
| 118 | } |
| 119 | |
| 120 | if (element instanceof TmfExperimentFolder) { |
| 121 | return fExperimentFolderIcon; |
| 122 | } |
| 123 | |
| 124 | if (element instanceof TmfTraceFolder) { |
| 125 | return fTraceFolderIcon; |
| 126 | } |
| 127 | |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * (non-Javadoc) |
| 133 | * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object) |
| 134 | */ |
| 135 | @Override |
| 136 | public String getText(Object element) { |
| 137 | |
| 138 | if (element instanceof TmfTraceFolder) { |
| 139 | TmfTraceFolder folder = (TmfTraceFolder) element; |
| 140 | return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$ |
| 141 | } |
| 142 | |
| 143 | if (element instanceof TmfExperimentElement) { |
| 144 | TmfExperimentElement folder = (TmfExperimentElement) element; |
| 145 | return folder.getName() + " [" + folder.getTraces().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$ |
| 146 | } |
| 147 | |
| 148 | if (element instanceof TmfExperimentFolder) { |
| 149 | TmfExperimentFolder folder = (TmfExperimentFolder) element; |
| 150 | return folder.getName() + " [" + folder.getChildren().size() + "]"; //$NON-NLS-1$//$NON-NLS-2$ |
| 151 | } |
| 152 | |
| 153 | // Catch all |
| 154 | if (element instanceof ITmfProjectModelElement) { |
| 155 | return ((ITmfProjectModelElement) element).getName(); |
| 156 | } |
| 157 | |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * (non-Javadoc) |
| 163 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| 164 | */ |
| 165 | @Override |
| 166 | public void addListener(ILabelProviderListener listener) { |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * (non-Javadoc) |
| 171 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose() |
| 172 | */ |
| 173 | @Override |
| 174 | public void dispose() { |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * (non-Javadoc) |
| 179 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(java.lang.Object, java.lang.String) |
| 180 | */ |
| 181 | @Override |
| 182 | public boolean isLabelProperty(Object element, String property) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * (non-Javadoc) |
| 188 | * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(org.eclipse.jface.viewers.ILabelProviderListener) |
| 189 | */ |
| 190 | @Override |
| 191 | public void removeListener(ILabelProviderListener listener) { |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * (non-Javadoc) |
| 196 | * @see org.eclipse.ui.navigator.IMementoAware#restoreState(org.eclipse.ui.IMemento) |
| 197 | */ |
| 198 | @Override |
| 199 | public void restoreState(IMemento aMemento) { |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * (non-Javadoc) |
| 204 | * @see org.eclipse.ui.navigator.IMementoAware#saveState(org.eclipse.ui.IMemento) |
| 205 | */ |
| 206 | @Override |
| 207 | public void saveState(IMemento aMemento) { |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * (non-Javadoc) |
| 212 | * @see org.eclipse.ui.navigator.IDescriptionProvider#getDescription(java.lang.Object) |
| 213 | */ |
| 214 | @Override |
| 215 | public String getDescription(Object anElement) { |
| 216 | return getText(anElement); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * (non-Javadoc) |
| 221 | * @see org.eclipse.ui.navigator.ICommonLabelProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite) |
| 222 | */ |
| 223 | @Override |
| 224 | public void init(ICommonContentExtensionSite aConfig) { |
| 225 | } |
| 226 | |
| 227 | } |