| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2010, 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.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | |
| 18 | import org.eclipse.core.resources.IResource; |
| 19 | import org.eclipse.core.runtime.CoreException; |
| 20 | import org.eclipse.core.runtime.IConfigurationElement; |
| 21 | import org.eclipse.core.runtime.Platform; |
| 22 | import org.eclipse.core.runtime.QualifiedName; |
| 23 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 25 | import org.eclipse.linuxtools.tmf.core.util.TmfTraceType; |
| 26 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; |
| 27 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtEvent; |
| 28 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTrace; |
| 29 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomTxtTraceDefinition; |
| 30 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlEvent; |
| 31 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTrace; |
| 32 | import org.eclipse.linuxtools.tmf.ui.parsers.custom.CustomXmlTraceDefinition; |
| 33 | import org.eclipse.ui.IActionFilter; |
| 34 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
| 35 | import org.eclipse.ui.views.properties.IPropertySource2; |
| 36 | import org.eclipse.ui.views.properties.TextPropertyDescriptor; |
| 37 | |
| 38 | /** |
| 39 | * <b><u>TmfTraceElement</u></b> |
| 40 | * <p> |
| 41 | */ |
| 42 | public class TmfTraceElement extends TmfProjectModelElement implements IActionFilter, IPropertySource2 { |
| 43 | |
| 44 | // ------------------------------------------------------------------------ |
| 45 | // Constants |
| 46 | // ------------------------------------------------------------------------ |
| 47 | |
| 48 | // Property keys |
| 49 | public static final QualifiedName TRACEBUNDLE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.bundle"); //$NON-NLS-1$//$NON-NLS-2$ |
| 50 | public static final QualifiedName TRACETYPE = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.id"); //$NON-NLS-1$//$NON-NLS-2$ |
| 51 | public static final QualifiedName TRACEICON = new QualifiedName("org.eclipse.linuxtools.tmf", "tracetype.icon"); //$NON-NLS-1$//$NON-NLS-2$ |
| 52 | |
| 53 | // Other attributes |
| 54 | public static final String BUNDLE = "bundle"; //$NON-NLS-1$ |
| 55 | public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$ |
| 56 | |
| 57 | // Property View stuff |
| 58 | private static final String sfInfoCategory = "Info"; //$NON-NLS-1$ |
| 59 | private static final String sfName = "name"; //$NON-NLS-1$ |
| 60 | private static final String sfPath = "path"; //$NON-NLS-1$ |
| 61 | private static final String sfLocation = "location"; //$NON-NLS-1$ |
| 62 | private static final String sfEventType = "type"; //$NON-NLS-1$ |
| 63 | private static final String sfIsLinked = "linked"; //$NON-NLS-1$ |
| 64 | |
| 65 | private static final TextPropertyDescriptor sfNameDescriptor = new TextPropertyDescriptor(sfName, sfName); |
| 66 | private static final TextPropertyDescriptor sfPathDescriptor = new TextPropertyDescriptor(sfPath, sfPath); |
| 67 | private static final TextPropertyDescriptor sfLocationDescriptor = new TextPropertyDescriptor(sfLocation, sfLocation); |
| 68 | private static final TextPropertyDescriptor sfTypeDescriptor = new TextPropertyDescriptor(sfEventType, sfEventType); |
| 69 | private static final TextPropertyDescriptor sfIsLinkedDescriptor = new TextPropertyDescriptor(sfIsLinked, sfIsLinked); |
| 70 | |
| 71 | private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor, |
| 72 | sfTypeDescriptor, sfIsLinkedDescriptor }; |
| 73 | |
| 74 | static { |
| 75 | sfNameDescriptor.setCategory(sfInfoCategory); |
| 76 | sfPathDescriptor.setCategory(sfInfoCategory); |
| 77 | sfLocationDescriptor.setCategory(sfInfoCategory); |
| 78 | sfTypeDescriptor.setCategory(sfInfoCategory); |
| 79 | sfIsLinkedDescriptor.setCategory(sfInfoCategory); |
| 80 | } |
| 81 | |
| 82 | // ------------------------------------------------------------------------ |
| 83 | // Attributes |
| 84 | // ------------------------------------------------------------------------ |
| 85 | |
| 86 | // This trace type ID as defined in plugin.xml |
| 87 | private String fTraceTypeId = null; |
| 88 | |
| 89 | // ------------------------------------------------------------------------ |
| 90 | // Static initialization |
| 91 | // ------------------------------------------------------------------------ |
| 92 | |
| 93 | // The mapping of available trace type IDs to their corresponding configuration element |
| 94 | private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>(); |
| 95 | private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>(); |
| 96 | |
| 97 | // Initialize statically at startup |
| 98 | public static void init() { |
| 99 | IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID); |
| 100 | for (IConfigurationElement ce : config) { |
| 101 | String elementName = ce.getName(); |
| 102 | if (elementName.equals(TmfTraceType.TYPE_ELEM)) { |
| 103 | String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR); |
| 104 | sfTraceTypeAttributes.put(traceTypeId, ce); |
| 105 | } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) { |
| 106 | String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR); |
| 107 | sfTraceCategories.put(categoryId, ce); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // ------------------------------------------------------------------------ |
| 113 | // Constructors |
| 114 | // ------------------------------------------------------------------------ |
| 115 | |
| 116 | public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) { |
| 117 | this(name, trace, (TmfProjectModelElement) parent); |
| 118 | } |
| 119 | |
| 120 | public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) { |
| 121 | this(name, trace, (TmfProjectModelElement) parent); |
| 122 | } |
| 123 | |
| 124 | private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) { |
| 125 | super(name, trace, parent); |
| 126 | parent.addChild(this); |
| 127 | refreshTraceType(); |
| 128 | } |
| 129 | |
| 130 | // ------------------------------------------------------------------------ |
| 131 | // Operations |
| 132 | // ------------------------------------------------------------------------ |
| 133 | |
| 134 | public String getTraceType() { |
| 135 | return fTraceTypeId; |
| 136 | } |
| 137 | |
| 138 | public void refreshTraceType() { |
| 139 | try { |
| 140 | fTraceTypeId = getResource().getPersistentProperty(TRACETYPE); |
| 141 | } catch (CoreException e) { |
| 142 | e.printStackTrace(); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | public ITmfTrace<?> instantiateTrace() { |
| 147 | try { |
| 148 | if (fTraceTypeId != null) { |
| 149 | if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) { |
| 150 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { |
| 151 | if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$ |
| 152 | return new CustomTxtTrace(def); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) { |
| 157 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { |
| 158 | if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$ |
| 159 | return new CustomXmlTrace(def); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId); |
| 164 | ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR); |
| 165 | return trace; |
| 166 | } |
| 167 | } catch (CoreException e) { |
| 168 | e.printStackTrace(); |
| 169 | } |
| 170 | return null; |
| 171 | } |
| 172 | |
| 173 | public TmfEvent instantiateEvent() { |
| 174 | try { |
| 175 | if (fTraceTypeId != null) { |
| 176 | if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) { |
| 177 | for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) { |
| 178 | if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$ |
| 179 | return new CustomTxtEvent(def); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) { |
| 184 | for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) { |
| 185 | if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$ |
| 186 | return new CustomXmlEvent(def); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId); |
| 191 | TmfEvent event = (TmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR); |
| 192 | return event; |
| 193 | } |
| 194 | } catch (CoreException e) { |
| 195 | e.printStackTrace(); |
| 196 | } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | public String getEditorId() { |
| 201 | if (fTraceTypeId != null) { |
| 202 | if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) { |
| 203 | return TmfEventsEditor.ID; |
| 204 | } |
| 205 | if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) { |
| 206 | return TmfEventsEditor.ID; |
| 207 | } |
| 208 | IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId); |
| 209 | IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM); |
| 210 | if (defaultEditorCE.length == 1) { |
| 211 | return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR); |
| 212 | } |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
| 217 | // ------------------------------------------------------------------------ |
| 218 | // IActionFilter |
| 219 | // ------------------------------------------------------------------------ |
| 220 | |
| 221 | @Override |
| 222 | public boolean testAttribute(Object target, String name, String value) { |
| 223 | if (name.equals(IS_LINKED)) { |
| 224 | boolean isLinked = getResource().isLinked(); |
| 225 | return Boolean.toString(isLinked).equals(value); |
| 226 | } |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | // ------------------------------------------------------------------------ |
| 231 | // TmfTraceElement |
| 232 | // ------------------------------------------------------------------------ |
| 233 | |
| 234 | @Override |
| 235 | public TmfProjectElement getProject() { |
| 236 | if (getParent() instanceof TmfTraceFolder) { |
| 237 | TmfTraceFolder folder = (TmfTraceFolder) getParent(); |
| 238 | TmfProjectElement project = (TmfProjectElement) folder.getParent(); |
| 239 | return project; |
| 240 | } |
| 241 | if (getParent() instanceof TmfExperimentElement) { |
| 242 | TmfExperimentElement experiment = (TmfExperimentElement) getParent(); |
| 243 | TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent(); |
| 244 | TmfProjectElement project = (TmfProjectElement) folder.getParent(); |
| 245 | return project; |
| 246 | } |
| 247 | return null; |
| 248 | } |
| 249 | |
| 250 | // ------------------------------------------------------------------------ |
| 251 | // IPropertySource2 |
| 252 | // ------------------------------------------------------------------------ |
| 253 | |
| 254 | @Override |
| 255 | public Object getEditableValue() { |
| 256 | return null; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public IPropertyDescriptor[] getPropertyDescriptors() { |
| 261 | return sfDescriptors; |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public Object getPropertyValue(Object id) { |
| 266 | |
| 267 | if (sfName.equals(id)) |
| 268 | return getName(); |
| 269 | |
| 270 | if (sfPath.equals(id)) |
| 271 | return getPath().toString(); |
| 272 | |
| 273 | if (sfLocation.equals(id)) |
| 274 | return getLocation().toString(); |
| 275 | |
| 276 | if (sfIsLinked.equals(id)) |
| 277 | return Boolean.valueOf(getResource().isLinked()).toString(); |
| 278 | |
| 279 | if (sfEventType.equals(id)) { |
| 280 | if (fTraceTypeId != null) { |
| 281 | IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId); |
| 282 | return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$ |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return null; |
| 287 | } |
| 288 | |
| 289 | private String getCategory(IConfigurationElement ce) { |
| 290 | String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR); |
| 291 | if (categoryId != null) { |
| 292 | IConfigurationElement category = sfTraceCategories.get(categoryId); |
| 293 | if (category != null) { |
| 294 | return category.getAttribute(TmfTraceType.NAME_ATTR); |
| 295 | } |
| 296 | } |
| 297 | return "[no category]"; //$NON-NLS-1$ |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void resetPropertyValue(Object id) { |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public void setPropertyValue(Object id, Object value) { |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public boolean isPropertyResettable(Object id) { |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public boolean isPropertySet(Object id) { |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | } |