Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
CommitLineData
12c155f5
FC
1/*******************************************************************************
2 * Copyright (c) 2010, 2011 Ericsson
ce2388e0 3 *
12c155f5
FC
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
ce2388e0 8 *
12c155f5
FC
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.ui.project.model;
14
5a5c2fc7 15import java.util.Arrays;
12c155f5
FC
16import java.util.HashMap;
17import java.util.Map;
18
19import org.eclipse.core.resources.IResource;
20import org.eclipse.core.runtime.CoreException;
21import org.eclipse.core.runtime.IConfigurationElement;
22import org.eclipse.core.runtime.Platform;
23import org.eclipse.core.runtime.QualifiedName;
d34665f9
FC
24import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
25import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
26import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
27import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
28import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
29import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
ce2388e0 30import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 31import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
4bf17f4a 32import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5
FC
33import org.eclipse.ui.IActionFilter;
34import org.eclipse.ui.views.properties.IPropertyDescriptor;
35import org.eclipse.ui.views.properties.IPropertySource2;
36import org.eclipse.ui.views.properties.TextPropertyDescriptor;
37
38/**
39 * <b><u>TmfTraceElement</u></b>
40 * <p>
41 */
42public 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
12c155f5
FC
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() {
4bf17f4a 99 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 100 for (IConfigurationElement ce : config) {
4bf17f4a 101 String elementName = ce.getName();
102 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
103 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 104 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 105 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
106 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
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) {
4bf17f4a 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 }
12c155f5 163 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 164 ITmfTrace<?> trace = (ITmfTrace<?>) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
165 return trace;
166 }
167 } catch (CoreException e) {
168 e.printStackTrace();
169 }
170 return null;
171 }
172
ce2388e0 173 public ITmfEvent instantiateEvent() {
12c155f5
FC
174 try {
175 if (fTraceTypeId != null) {
4bf17f4a 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 }
12c155f5 190 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
ce2388e0 191 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
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) {
4bf17f4a 202 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
203 return TmfEventsEditor.ID;
204 }
205 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
206 return TmfEventsEditor.ID;
207 }
12c155f5 208 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 209 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 210 if (defaultEditorCE.length == 1) {
4bf17f4a 211 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
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() {
5a5c2fc7 261 return (sfDescriptors != null) ? Arrays.copyOf(sfDescriptors, sfDescriptors.length) : null;
12c155f5
FC
262 }
263
264 @Override
265 public Object getPropertyValue(Object id) {
266
ce2388e0 267 if (sfName.equals(id)) {
12c155f5 268 return getName();
ce2388e0 269 }
12c155f5 270
ce2388e0 271 if (sfPath.equals(id)) {
12c155f5 272 return getPath().toString();
ce2388e0 273 }
12c155f5 274
ce2388e0 275 if (sfLocation.equals(id)) {
12c155f5 276 return getLocation().toString();
ce2388e0 277 }
12c155f5 278
ce2388e0 279 if (sfIsLinked.equals(id)) {
12c155f5 280 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 281 }
12c155f5
FC
282
283 if (sfEventType.equals(id)) {
284 if (fTraceTypeId != null) {
285 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 286 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
287 }
288 }
289
290 return null;
291 }
292
293 private String getCategory(IConfigurationElement ce) {
4bf17f4a 294 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
295 if (categoryId != null) {
296 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 297 if (category != null) {
298 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
299 }
300 }
301 return "[no category]"; //$NON-NLS-1$
302 }
303
304 @Override
305 public void resetPropertyValue(Object id) {
306 }
307
308 @Override
309 public void setPropertyValue(Object id, Object value) {
310 }
311
312 @Override
313 public boolean isPropertyResettable(Object id) {
314 return false;
315 }
316
317 @Override
318 public boolean isPropertySet(Object id) {
319 return false;
320 }
321
322}
This page took 0.072536 seconds and 5 git commands to generate.