2f654eb5505e78d9abba2ad90b7cfcd219d99903
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson, École Polytechnique de Montréal
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 * Bernd Hufmann - Added supplementary files handling
12 * Geneviève Bastien - Moved supplementary files handling to parent class,
13 * added code to copy trace
14 * Patrick Tasse - Close editors to release resources
15 * Jean-Christian Kouame - added trace properties to be shown into
16 * the properties view
17 * Geneviève Bastien - Moved trace type related methods to parent class
18 *******************************************************************************/
19
20 package org.eclipse.linuxtools.tmf.ui.project.model;
21
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.core.resources.IFile;
29 import org.eclipse.core.resources.IFolder;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IConfigurationElement;
33 import org.eclipse.core.runtime.IPath;
34 import org.eclipse.core.runtime.IProgressMonitor;
35 import org.eclipse.core.runtime.Platform;
36 import org.eclipse.core.runtime.URIUtil;
37 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
38 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
39 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
40 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtEvent;
41 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace;
42 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition;
43 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlEvent;
44 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace;
45 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition;
46 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
47 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
48 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
49 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
50 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
51 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
52 import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
53 import org.eclipse.ui.IActionFilter;
54 import org.eclipse.ui.views.properties.IPropertyDescriptor;
55 import org.eclipse.ui.views.properties.IPropertySource2;
56
57 /**
58 * Implementation of trace model element representing a trace. It provides
59 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
60 * well as editor ID from the trace type extension definition.
61 *
62 * @version 1.0
63 * @author Francois Chouinard
64 */
65 public class TmfTraceElement extends TmfCommonProjectElement implements IActionFilter, IPropertySource2 {
66
67 // ------------------------------------------------------------------------
68 // Constants
69 // ------------------------------------------------------------------------
70
71 // Other attributes
72 /**
73 * Bundle attribute name
74 */
75 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
76 /**
77 * IsLinked attribute name.
78 */
79 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
80
81 // Property View stuff
82 private static final String sfResourcePropertiesCategory = Messages.TmfTraceElement_ResourceProperties;
83 private static final String sfName = Messages.TmfTraceElement_Name;
84 private static final String sfPath = Messages.TmfTraceElement_Path;
85 private static final String sfLocation = Messages.TmfTraceElement_Location;
86 private static final String sfEventType = Messages.TmfTraceElement_EventType;
87 private static final String sfIsLinked = Messages.TmfTraceElement_IsLinked;
88 private static final String sfSourceLocation = Messages.TmfTraceElement_SourceLocation;
89 private static final String sfTracePropertiesCategory = Messages.TmfTraceElement_TraceProperties;
90
91 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
92 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
93 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation);
94 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfEventType, sfEventType);
95 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor = new ReadOnlyTextPropertyDescriptor(sfIsLinked, sfIsLinked);
96 private static final ReadOnlyTextPropertyDescriptor sfSourceLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfSourceLocation, sfSourceLocation);
97
98 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
99 sfTypeDescriptor, sfIsLinkedDescriptor, sfSourceLocationDescriptor };
100
101 static {
102 sfNameDescriptor.setCategory(sfResourcePropertiesCategory);
103 sfPathDescriptor.setCategory(sfResourcePropertiesCategory);
104 sfLocationDescriptor.setCategory(sfResourcePropertiesCategory);
105 sfTypeDescriptor.setCategory(sfResourcePropertiesCategory);
106 sfIsLinkedDescriptor.setCategory(sfResourcePropertiesCategory);
107 sfSourceLocationDescriptor.setCategory(sfResourcePropertiesCategory);
108 }
109
110 // ------------------------------------------------------------------------
111 // Static initialization
112 // ------------------------------------------------------------------------
113
114 // The mapping of available trace type IDs to their corresponding
115 // configuration element
116 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<>();
117 private static final Map<String, IConfigurationElement> sfTraceTypeUIAttributes = new HashMap<>();
118 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<>();
119
120 /**
121 * Initialize statically at startup by getting extensions from the platform
122 * extension registry.
123 */
124 public static void init() {
125 /* Read the tmf.core "tracetype" extension point */
126 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
127 for (IConfigurationElement ce : config) {
128 switch (ce.getName()) {
129 case TmfTraceType.TYPE_ELEM:
130 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
131 sfTraceTypeAttributes.put(traceTypeId, ce);
132 break;
133 case TmfTraceType.CATEGORY_ELEM:
134 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
135 sfTraceCategories.put(categoryId, ce);
136 break;
137 default:
138 }
139 }
140
141 /*
142 * Read the corresponding tmf.ui "tracetypeui" extension point for this
143 * trace type, if it exists.
144 */
145 config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils.TMF_TRACE_TYPE_UI_ID);
146 for (IConfigurationElement ce : config) {
147 String elemName = ce.getName();
148 if (TmfTraceTypeUIUtils.TYPE_ELEM.equals(elemName)) {
149 String traceType = ce.getAttribute(TmfTraceTypeUIUtils.TRACETYPE_ATTR);
150 sfTraceTypeUIAttributes.put(traceType, ce);
151 }
152 }
153 }
154
155 // ------------------------------------------------------------------------
156 // Constructors
157 // ------------------------------------------------------------------------
158 /**
159 * Constructor. Creates trace model element under the trace folder.
160 *
161 * @param name
162 * The name of trace
163 * @param trace
164 * The trace resource.
165 * @param parent
166 * The parent element (trace folder)
167 */
168 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
169 this(name, trace, (TmfProjectModelElement) parent);
170 }
171
172 /**
173 * Constructor. Creates trace model element under the experiment folder.
174 *
175 * @param name
176 * The name of trace
177 * @param trace
178 * The trace resource.
179 * @param parent
180 * The parent element (experiment folder)
181 */
182 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
183 this(name, trace, (TmfProjectModelElement) parent);
184 }
185
186 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
187 super(name, trace, parent);
188 }
189
190 // ------------------------------------------------------------------------
191 // Operations
192 // ------------------------------------------------------------------------
193
194 /**
195 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
196 * the corresponding extension.
197 *
198 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
199 */
200 @Override
201 public ITmfTrace instantiateTrace() {
202 try {
203
204 // make sure that supplementary folder exists
205 refreshSupplementaryFolder();
206
207 if (getTraceType() != null) {
208 if (getTraceType().startsWith(CustomTxtTrace.class.getCanonicalName())) {
209 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
210 if (getTraceType().equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
211 return new CustomTxtTrace(def);
212 }
213 }
214 }
215 if (getTraceType().startsWith(CustomXmlTrace.class.getCanonicalName())) {
216 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
217 if (getTraceType().equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
218 return new CustomXmlTrace(def);
219 }
220 }
221 }
222 IConfigurationElement ce = sfTraceTypeAttributes.get(getTraceType());
223 if (ce == null) {
224 return null;
225 }
226 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
227 return trace;
228 }
229 } catch (CoreException e) {
230 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
231 }
232 return null;
233 }
234
235 /**
236 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
237 * the corresponding extension.
238 *
239 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
240 */
241 public ITmfEvent instantiateEvent() {
242 try {
243 if (getTraceType() != null) {
244 if (getTraceType().startsWith(CustomTxtTrace.class.getCanonicalName())) {
245 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
246 if (getTraceType().equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
247 return new CustomTxtEvent(def);
248 }
249 }
250 }
251 if (getTraceType().startsWith(CustomXmlTrace.class.getCanonicalName())) {
252 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
253 if (getTraceType().equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
254 return new CustomXmlEvent(def);
255 }
256 }
257 }
258 IConfigurationElement ce = sfTraceTypeAttributes.get(getTraceType());
259 if (ce == null) {
260 return null;
261 }
262 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
263 return event;
264 }
265 } catch (CoreException e) {
266 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
267 }
268 return null;
269 }
270
271 @Override
272 public String getEditorId() {
273 if (getTraceType() != null) {
274 if (getTraceType().startsWith(CustomTxtTrace.class.getCanonicalName())) {
275 return TmfEventsEditor.ID;
276 }
277 if (getTraceType().startsWith(CustomXmlTrace.class.getCanonicalName())) {
278 return TmfEventsEditor.ID;
279 }
280 IConfigurationElement ce = sfTraceTypeUIAttributes.get(getTraceType());
281 if (ce == null) {
282 /* This trace type does not define UI attributes */
283 return null;
284 }
285 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceTypeUIUtils.DEFAULT_EDITOR_ELEM);
286 if (defaultEditorCE.length == 1) {
287 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
288 }
289 }
290 return null;
291 }
292
293 /**
294 * Returns the file resource used to store bookmarks after creating it if
295 * necessary. If the trace resource is a file, it is returned directly. If
296 * the trace resource is a folder, a linked file is returned. The file will
297 * be created if it does not exist.
298 *
299 * @return the bookmarks file
300 * @throws CoreException
301 * if the bookmarks file cannot be created
302 * @since 2.0
303 */
304 @Override
305 public IFile createBookmarksFile() throws CoreException {
306 IFile file = getBookmarksFile();
307 if (fResource instanceof IFolder) {
308 return createBookmarksFile(getProject().getTracesFolder().getResource(), TmfTrace.class.getCanonicalName());
309 }
310 return file;
311 }
312
313 /**
314 * Returns the file resource used to store bookmarks. The file may not
315 * exist.
316 *
317 * @return the bookmarks file
318 * @since 2.0
319 */
320 @Override
321 public IFile getBookmarksFile() {
322 IFile file = null;
323 if (fResource instanceof IFile) {
324 file = (IFile) fResource;
325 } else if (fResource instanceof IFolder) {
326 final IFolder folder = (IFolder) fResource;
327 file = folder.getFile(getName() + '_');
328 }
329 return file;
330 }
331
332 /**
333 * Returns the <code>TmfTraceElement</code> located under the
334 * <code>TmfTracesFolder</code>.
335 *
336 * @return <code>this</code> if this element is under the
337 * <code>TmfTracesFolder</code> else the corresponding
338 * <code>TmfTraceElement</code> if this element is under
339 * <code>TmfExperimentElement</code>.
340 */
341 public TmfTraceElement getElementUnderTraceFolder() {
342
343 // If trace is under an experiment, return original trace from the
344 // traces folder
345 if (getParent() instanceof TmfExperimentElement) {
346 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
347 if (aTrace.getName().equals(getName())) {
348 return aTrace;
349 }
350 }
351 }
352 return this;
353 }
354
355 @Override
356 public String getTypeName() {
357 return Messages.TmfTraceElement_TypeName;
358 }
359
360 // ------------------------------------------------------------------------
361 // IActionFilter
362 // ------------------------------------------------------------------------
363
364 @Override
365 public boolean testAttribute(Object target, String name, String value) {
366 if (name.equals(IS_LINKED)) {
367 boolean isLinked = getResource().isLinked();
368 return Boolean.toString(isLinked).equals(value);
369 }
370 return false;
371 }
372
373 // ------------------------------------------------------------------------
374 // TmfTraceElement
375 // ------------------------------------------------------------------------
376
377 @Override
378 public TmfProjectElement getProject() {
379 if (getParent() instanceof TmfTraceFolder) {
380 TmfTraceFolder folder = (TmfTraceFolder) getParent();
381 TmfProjectElement project = (TmfProjectElement) folder.getParent();
382 return project;
383 }
384 if (getParent() instanceof TmfExperimentElement) {
385 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
386 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
387 TmfProjectElement project = (TmfProjectElement) folder.getParent();
388 return project;
389 }
390 return null;
391 }
392
393 // ------------------------------------------------------------------------
394 // IPropertySource2
395 // ------------------------------------------------------------------------
396
397 @Override
398 public Object getEditableValue() {
399 return null;
400 }
401
402 /**
403 * Get the trace properties of this traceElement if the corresponding trace
404 * is opened in an editor
405 *
406 * @return a map with the names and values of the trace properties
407 * respectively as keys and values
408 */
409 private Map<String, String> getTraceProperties() {
410 for (ITmfTrace openedTrace : TmfTraceManager.getInstance().getOpenedTraces()) {
411 for (ITmfTrace singleTrace : TmfTraceManager.getTraceSet(openedTrace)) {
412 if (this.getLocation().getPath().endsWith(singleTrace.getPath())) {
413 if (singleTrace instanceof ITmfTraceProperties) {
414 ITmfTraceProperties traceProperties = (ITmfTraceProperties) singleTrace;
415 return traceProperties.getTraceProperties();
416 }
417 }
418 }
419 }
420 return new HashMap<>();
421 }
422
423 @Override
424 public IPropertyDescriptor[] getPropertyDescriptors() {
425 Map<String, String> traceProperties = getTraceProperties();
426 if (!traceProperties.isEmpty()) {
427 IPropertyDescriptor[] propertyDescriptorArray = new IPropertyDescriptor[traceProperties.size() + sfDescriptors.length];
428 int index = 0;
429 for (Map.Entry<String, String> varName : traceProperties.entrySet()) {
430 ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + "_" + varName.getKey(), varName.getKey()); //$NON-NLS-1$
431 descriptor.setCategory(sfTracePropertiesCategory);
432 propertyDescriptorArray[index] = descriptor;
433 index++;
434 }
435 for (int i = 0; i < sfDescriptors.length; i++) {
436 propertyDescriptorArray[index] = sfDescriptors[i];
437 index++;
438 }
439 return propertyDescriptorArray;
440 }
441 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
442 }
443
444 @Override
445 public Object getPropertyValue(Object id) {
446
447 if (sfName.equals(id)) {
448 return getName();
449 }
450
451 if (sfPath.equals(id)) {
452 return getPath().toString();
453 }
454
455 if (sfLocation.equals(id)) {
456 return URIUtil.toUnencodedString(getLocation());
457 }
458
459 if (sfIsLinked.equals(id)) {
460 return Boolean.valueOf(getResource().isLinked()).toString();
461 }
462
463 if (sfSourceLocation.equals(id)) {
464 try {
465 String sourceLocation = getElementUnderTraceFolder().getResource().getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
466 if (sourceLocation != null) {
467 return sourceLocation;
468 }
469 } catch (CoreException e) {
470 }
471 return ""; //$NON-NLS-1$
472 }
473
474 if (sfEventType.equals(id)) {
475 if (getTraceType() != null) {
476 IConfigurationElement ce = sfTraceTypeAttributes.get(getTraceType());
477 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
478 }
479 }
480
481 Map<String, String> traceProperties = getTraceProperties();
482 if (id != null && !traceProperties.isEmpty()) {
483 String key = (String) id;
484 key = key.substring(this.getName().length() + 1); // remove name_
485 String value = traceProperties.get(key);
486 return value;
487 }
488
489 return null;
490 }
491
492 private static String getCategory(IConfigurationElement ce) {
493 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
494 if (categoryId != null) {
495 IConfigurationElement category = sfTraceCategories.get(categoryId);
496 if (category != null) {
497 return category.getAttribute(TmfTraceType.NAME_ATTR);
498 }
499 }
500 return "[no category]"; //$NON-NLS-1$
501 }
502
503 @Override
504 public void resetPropertyValue(Object id) {
505 }
506
507 @Override
508 public void setPropertyValue(Object id, Object value) {
509 }
510
511 @Override
512 public boolean isPropertyResettable(Object id) {
513 return false;
514 }
515
516 @Override
517 public boolean isPropertySet(Object id) {
518 return false;
519 }
520
521 /**
522 * Copy this trace in the trace folder. No other parameters are mentioned so
523 * the trace is copied in this element's project trace folder
524 *
525 * @param string
526 * The new trace name
527 * @return the new Resource object
528 * @since 2.0
529 */
530 public TmfTraceElement copy(String string) {
531 TmfTraceFolder folder = this.getProject().getTracesFolder();
532 IResource res = super.copy(string, false);
533 return new TmfTraceElement(string, res, folder);
534 }
535
536 /**
537 * Close opened editors associated with this trace.
538 *
539 * @since 2.0
540 */
541 @Override
542 public void closeEditors() {
543 super.closeEditors();
544
545 // Close experiments that contain the trace if open
546 if (getParent() instanceof TmfTraceFolder) {
547 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
548 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
549 for (ITmfProjectModelElement child : experiment.getChildren()) {
550 if (child.getName().equals(getName())) {
551 ((TmfExperimentElement) experiment).closeEditors();
552 break;
553 }
554 }
555 }
556 } else if (getParent() instanceof TmfExperimentElement) {
557 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
558 experiment.closeEditors();
559 }
560
561 /*
562 * We will be closing a trace shortly. Invoke GC to release
563 * MappedByteBuffer objects, which some trace types, like CTF, use.
564 * (see Java bug JDK-4724038)
565 */
566 System.gc();
567 }
568
569 /**
570 * Delete the trace resource, remove it from experiments and delete its
571 * supplementary files
572 *
573 * @param progressMonitor
574 * a progress monitor, or null if progress reporting is not
575 * desired
576 *
577 * @throws CoreException
578 * thrown when IResource.delete fails
579 * @since 2.2
580 */
581 public void delete(IProgressMonitor progressMonitor) throws CoreException {
582 closeEditors();
583
584 IPath path = fResource.getLocation();
585 if (path != null) {
586 if (getParent() instanceof TmfTraceFolder) {
587 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
588
589 // Propagate the removal to traces
590 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
591 List<ITmfProjectModelElement> toRemove = new LinkedList<>();
592 for (ITmfProjectModelElement child : experiment.getChildren()) {
593 if (child.getName().equals(getName())) {
594 toRemove.add(child);
595 }
596 }
597 for (ITmfProjectModelElement child : toRemove) {
598 ((TmfExperimentElement) experiment).removeTrace((TmfTraceElement) child);
599 }
600 }
601
602 // Delete supplementary files
603 deleteSupplementaryFolder();
604
605 } else if (getParent() instanceof TmfExperimentElement) {
606 TmfExperimentElement experimentElement = (TmfExperimentElement) getParent();
607 experimentElement.removeTrace(this);
608 }
609 }
610
611 // Finally, delete the trace
612 fResource.delete(true, progressMonitor);
613 }
614
615 }
This page took 0.045348 seconds and 5 git commands to generate.