Tmf: Batch Trace Import
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
CommitLineData
12c155f5 1/*******************************************************************************
beb19106 2 * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal
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
b544077e 11 * Bernd Hufmann - Added supplementary files handling
beb19106
GB
12 * Geneviève Bastien - Moved supplementary files handling to parent class, added
13 * code to copy trace
12c155f5
FC
14 *******************************************************************************/
15
16package org.eclipse.linuxtools.tmf.ui.project.model;
17
c4c81d91
PT
18import java.io.ByteArrayInputStream;
19import java.io.InputStream;
5a5c2fc7 20import java.util.Arrays;
12c155f5
FC
21import java.util.HashMap;
22import java.util.Map;
23
c4c81d91 24import org.eclipse.core.resources.IFile;
5e4bf87d 25import org.eclipse.core.resources.IFolder;
12c155f5
FC
26import org.eclipse.core.resources.IResource;
27import org.eclipse.core.runtime.CoreException;
28import org.eclipse.core.runtime.IConfigurationElement;
29import org.eclipse.core.runtime.Platform;
8fd82db5 30import org.eclipse.linuxtools.internal.tmf.ui.Activator;
d34665f9
FC
31import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
32import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
33import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
34import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
35import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
36import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
e12ecd30 37import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
ce2388e0 38import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 39import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
c4c81d91 40import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
253d5be1 41import org.eclipse.linuxtools.tmf.core.util.ReadOnlyTextPropertyDescriptor;
4bf17f4a 42import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
12c155f5
FC
43import org.eclipse.ui.IActionFilter;
44import org.eclipse.ui.views.properties.IPropertyDescriptor;
45import org.eclipse.ui.views.properties.IPropertySource2;
12c155f5
FC
46
47/**
d04ec5a7
MK
48 * Implementation of trace model element representing a trace. It provides
49 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
50 * well as editor ID from the trace type extension definition.
abbdd66a 51 *
b544077e
BH
52 * @version 1.0
53 * @author Francois Chouinard
12c155f5 54 */
99504bb8 55public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
12c155f5
FC
56
57 // ------------------------------------------------------------------------
58 // Constants
59 // ------------------------------------------------------------------------
60
12c155f5 61 // Other attributes
b544077e
BH
62 /**
63 * Bundle attribute name
64 */
12c155f5 65 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
b544077e
BH
66 /**
67 * IsLinked attribute name.
68 */
12c155f5
FC
69 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
70
71 // Property View stuff
72 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
73 private static final String sfName = "name"; //$NON-NLS-1$
74 private static final String sfPath = "path"; //$NON-NLS-1$
75 private static final String sfLocation = "location"; //$NON-NLS-1$
76 private static final String sfEventType = "type"; //$NON-NLS-1$
77 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
78
253d5be1
BH
79 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
80 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
81 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation);
82 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfEventType, sfEventType);
83 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor = new ReadOnlyTextPropertyDescriptor(sfIsLinked, sfIsLinked);
12c155f5
FC
84
85 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
86 sfTypeDescriptor, sfIsLinkedDescriptor };
87
88 static {
89 sfNameDescriptor.setCategory(sfInfoCategory);
90 sfPathDescriptor.setCategory(sfInfoCategory);
91 sfLocationDescriptor.setCategory(sfInfoCategory);
92 sfTypeDescriptor.setCategory(sfInfoCategory);
93 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
94 }
6256d8ad 95
c4c81d91
PT
96 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
97
12c155f5
FC
98 // ------------------------------------------------------------------------
99 // Attributes
100 // ------------------------------------------------------------------------
101
102 // This trace type ID as defined in plugin.xml
103 private String fTraceTypeId = null;
104
105 // ------------------------------------------------------------------------
106 // Static initialization
107 // ------------------------------------------------------------------------
108
d04ec5a7
MK
109 // The mapping of available trace type IDs to their corresponding
110 // configuration element
12c155f5
FC
111 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
112 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
113
b544077e 114 /**
d04ec5a7
MK
115 * Initialize statically at startup by getting extensions from the platform
116 * extension registry.
b544077e 117 */
12c155f5 118 public static void init() {
4bf17f4a 119 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 120 for (IConfigurationElement ce : config) {
4bf17f4a 121 String elementName = ce.getName();
122 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
123 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 124 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 125 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
126 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
127 sfTraceCategories.put(categoryId, ce);
128 }
129 }
130 }
131
132 // ------------------------------------------------------------------------
133 // Constructors
134 // ------------------------------------------------------------------------
b544077e 135 /**
d04ec5a7
MK
136 * Constructor. Creates trace model element under the trace folder.
137 *
138 * @param name
139 * The name of trace
140 * @param trace
141 * The trace resource.
142 * @param parent
143 * The parent element (trace folder)
b544077e 144 */
12c155f5
FC
145 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
146 this(name, trace, (TmfProjectModelElement) parent);
147 }
d04ec5a7 148
b544077e 149 /**
d04ec5a7
MK
150 * Constructor. Creates trace model element under the experiment folder.
151 *
152 * @param name
153 * The name of trace
154 * @param trace
155 * The trace resource.
156 * @param parent
157 * The parent element (experiment folder)
b544077e 158 */
12c155f5
FC
159 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
160 this(name, trace, (TmfProjectModelElement) parent);
161 }
162
163 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
164 super(name, trace, parent);
165 parent.addChild(this);
166 refreshTraceType();
167 }
168
169 // ------------------------------------------------------------------------
170 // Operations
171 // ------------------------------------------------------------------------
b544077e
BH
172 /**
173 * Returns the trace type ID.
d04ec5a7 174 *
b544077e
BH
175 * @return trace type ID.
176 */
12c155f5
FC
177 public String getTraceType() {
178 return fTraceTypeId;
179 }
180
b544077e 181 /**
d04ec5a7
MK
182 * Refreshes the trace type filed by reading the trace type persistent
183 * property of the resource referenece.
b544077e 184 */
12c155f5
FC
185 public void refreshTraceType() {
186 try {
e12ecd30 187 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
12c155f5 188 } catch (CoreException e) {
8fd82db5 189 Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
190 }
191 }
192
b544077e 193 /**
d04ec5a7
MK
194 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
195 * the corresponding extension.
abbdd66a 196 *
b544077e
BH
197 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
198 */
6256d8ad 199 public ITmfTrace instantiateTrace() {
12c155f5 200 try {
e12ecd30
BH
201
202 // make sure that supplementary folder exists
203 refreshSupplementaryFolder();
204
12c155f5 205 if (fTraceTypeId != null) {
4bf17f4a 206 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
207 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
208 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
209 return new CustomTxtTrace(def);
210 }
211 }
212 }
213 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
214 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
215 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
216 return new CustomXmlTrace(def);
217 }
218 }
219 }
12c155f5 220 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
d04ec5a7
MK
221 if (ce == null) {
222 return null;
223 }
6256d8ad 224 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
225 return trace;
226 }
227 } catch (CoreException e) {
8fd82db5 228 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
229 }
230 return null;
231 }
232
b544077e 233 /**
d04ec5a7
MK
234 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
235 * the corresponding extension.
abbdd66a 236 *
b544077e
BH
237 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
238 */
ce2388e0 239 public ITmfEvent instantiateEvent() {
12c155f5
FC
240 try {
241 if (fTraceTypeId != null) {
4bf17f4a 242 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
243 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
244 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
245 return new CustomTxtEvent(def);
246 }
247 }
248 }
249 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
250 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
251 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
252 return new CustomXmlEvent(def);
253 }
254 }
255 }
12c155f5 256 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
d04ec5a7
MK
257 if (ce == null) {
258 return null;
259 }
ce2388e0 260 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
261 return event;
262 }
263 } catch (CoreException e) {
8fd82db5 264 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
265 }
266 return null;
267 }
268
b544077e
BH
269 /**
270 * Returns the optional editor ID from the trace type extension.
d04ec5a7 271 *
b544077e
BH
272 * @return the editor ID or <code>null</code> if not defined.
273 */
12c155f5
FC
274 public String getEditorId() {
275 if (fTraceTypeId != null) {
4bf17f4a 276 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
277 return TmfEventsEditor.ID;
278 }
279 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
280 return TmfEventsEditor.ID;
281 }
12c155f5 282 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 283 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 284 if (defaultEditorCE.length == 1) {
4bf17f4a 285 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
286 }
287 }
288 return null;
289 }
e12ecd30 290
c4c81d91 291 /**
d04ec5a7
MK
292 * Returns the file resource used to store bookmarks after creating it if
293 * necessary. If the trace resource is a file, it is returned directly. If
294 * the trace resource is a folder, a linked file is returned. The file will
295 * be created if it does not exist.
296 *
c4c81d91 297 * @return the bookmarks file
d04ec5a7
MK
298 * @throws CoreException
299 * if the bookmarks file cannot be created
c4c81d91
PT
300 * @since 2.0
301 */
81fe3479
PT
302 public IFile createBookmarksFile() throws CoreException {
303 IFile file = getBookmarksFile();
304 if (fResource instanceof IFolder) {
305 if (!file.exists()) {
306 final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
307 if (!bookmarksFile.exists()) {
308 final InputStream source = new ByteArrayInputStream(new byte[0]);
309 bookmarksFile.create(source, true, null);
310 }
311 bookmarksFile.setHidden(true);
312 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
313 file.setHidden(true);
314 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
315 }
316 }
317 return file;
318 }
319
320 /**
d04ec5a7
MK
321 * Returns the file resource used to store bookmarks. The file may not
322 * exist.
323 *
81fe3479
PT
324 * @return the bookmarks file
325 * @since 2.0
326 */
327 public IFile getBookmarksFile() {
c4c81d91
PT
328 IFile file = null;
329 if (fResource instanceof IFile) {
330 file = (IFile) fResource;
331 } else if (fResource instanceof IFolder) {
c4c81d91
PT
332 final IFolder folder = (IFolder) fResource;
333 file = folder.getFile(getName() + '_');
c4c81d91
PT
334 }
335 return file;
336 }
337
e12ecd30 338 /**
d04ec5a7
MK
339 * Returns the <code>TmfTraceElement</code> located under the
340 * <code>TmfTracesFolder</code>.
6256d8ad 341 *
d04ec5a7
MK
342 * @return <code>this</code> if this element is under the
343 * <code>TmfTracesFolder</code> else the corresponding
344 * <code>TmfTraceElement</code> if this element is under
e12ecd30
BH
345 * <code>TmfExperimentElement</code>.
346 */
5e4bf87d 347 public TmfTraceElement getElementUnderTraceFolder() {
e12ecd30 348
d04ec5a7
MK
349 // If trace is under an experiment, return original trace from the
350 // traces folder
5e4bf87d
BH
351 if (getParent() instanceof TmfExperimentElement) {
352 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
353 if (aTrace.getName().equals(getName())) {
354 return aTrace;
355 }
356 }
357 }
358 return this;
359 }
6256d8ad 360
12c155f5
FC
361 // ------------------------------------------------------------------------
362 // IActionFilter
363 // ------------------------------------------------------------------------
364
365 @Override
366 public boolean testAttribute(Object target, String name, String value) {
367 if (name.equals(IS_LINKED)) {
368 boolean isLinked = getResource().isLinked();
369 return Boolean.toString(isLinked).equals(value);
370 }
371 return false;
372 }
373
374 // ------------------------------------------------------------------------
375 // TmfTraceElement
376 // ------------------------------------------------------------------------
11252342 377
12c155f5
FC
378 @Override
379 public TmfProjectElement getProject() {
380 if (getParent() instanceof TmfTraceFolder) {
381 TmfTraceFolder folder = (TmfTraceFolder) getParent();
382 TmfProjectElement project = (TmfProjectElement) folder.getParent();
383 return project;
384 }
385 if (getParent() instanceof TmfExperimentElement) {
386 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
387 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
388 TmfProjectElement project = (TmfProjectElement) folder.getParent();
389 return project;
390 }
391 return null;
392 }
393
394 // ------------------------------------------------------------------------
395 // IPropertySource2
396 // ------------------------------------------------------------------------
397
398 @Override
399 public Object getEditableValue() {
400 return null;
401 }
402
403 @Override
404 public IPropertyDescriptor[] getPropertyDescriptors() {
77fdc5df 405 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
12c155f5
FC
406 }
407
408 @Override
409 public Object getPropertyValue(Object id) {
410
ce2388e0 411 if (sfName.equals(id)) {
12c155f5 412 return getName();
ce2388e0 413 }
12c155f5 414
ce2388e0 415 if (sfPath.equals(id)) {
12c155f5 416 return getPath().toString();
ce2388e0 417 }
12c155f5 418
ce2388e0 419 if (sfLocation.equals(id)) {
12c155f5 420 return getLocation().toString();
ce2388e0 421 }
12c155f5 422
ce2388e0 423 if (sfIsLinked.equals(id)) {
12c155f5 424 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 425 }
12c155f5
FC
426
427 if (sfEventType.equals(id)) {
428 if (fTraceTypeId != null) {
429 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 430 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
431 }
432 }
433
434 return null;
435 }
436
abbdd66a 437 private static String getCategory(IConfigurationElement ce) {
4bf17f4a 438 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
439 if (categoryId != null) {
440 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 441 if (category != null) {
442 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
443 }
444 }
445 return "[no category]"; //$NON-NLS-1$
446 }
447
448 @Override
449 public void resetPropertyValue(Object id) {
450 }
451
452 @Override
453 public void setPropertyValue(Object id, Object value) {
454 }
455
456 @Override
457 public boolean isPropertyResettable(Object id) {
458 return false;
459 }
460
461 @Override
462 public boolean isPropertySet(Object id) {
463 return false;
464 }
465
beb19106
GB
466 /**
467 * Copy this trace in the trace folder. No other parameters are mentioned so
468 * the trace is copied in this element's project trace folder
469 *
470 * @param string
471 * The new trace name
472 * @return the new Resource object
473 * @since 2.0
474 */
475 public TmfTraceElement copy(String string) {
476 TmfTraceFolder folder = this.getProject().getTracesFolder();
477 IResource res = super.copy(string, false);
478 return new TmfTraceElement(string, res, folder);
479 }
480
12c155f5 481}
This page took 0.063001 seconds and 5 git commands to generate.