tmf: Move TmfTraceType and custom parsers to tmf.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 *******************************************************************************/
18
19 package org.eclipse.linuxtools.tmf.ui.project.model;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IFolder;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.resources.ResourcesPlugin;
34 import org.eclipse.core.runtime.CoreException;
35 import org.eclipse.core.runtime.IConfigurationElement;
36 import org.eclipse.core.runtime.IPath;
37 import org.eclipse.core.runtime.IProgressMonitor;
38 import org.eclipse.core.runtime.Platform;
39 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
40 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
41 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
42 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
43 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
44 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtEvent;
45 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTrace;
46 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomTxtTraceDefinition;
47 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlEvent;
48 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTrace;
49 import org.eclipse.linuxtools.tmf.core.parsers.custom.CustomXmlTraceDefinition;
50 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
51 import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
52 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
53 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
54 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
55 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
56 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
57 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
58 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
59 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
60 import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
61 import org.eclipse.ui.IActionFilter;
62 import org.eclipse.ui.IEditorReference;
63 import org.eclipse.ui.IWorkbench;
64 import org.eclipse.ui.IWorkbenchPage;
65 import org.eclipse.ui.IWorkbenchWindow;
66 import org.eclipse.ui.PartInitException;
67 import org.eclipse.ui.PlatformUI;
68 import org.eclipse.ui.part.FileEditorInput;
69 import org.eclipse.ui.views.properties.IPropertyDescriptor;
70 import org.eclipse.ui.views.properties.IPropertySource2;
71
72 /**
73 * Implementation of trace model element representing a trace. It provides
74 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
75 * well as editor ID from the trace type extension definition.
76 *
77 * @version 1.0
78 * @author Francois Chouinard
79 */
80 public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
81
82 // ------------------------------------------------------------------------
83 // Constants
84 // ------------------------------------------------------------------------
85
86 // Other attributes
87 /**
88 * Bundle attribute name
89 */
90 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
91 /**
92 * IsLinked attribute name.
93 */
94 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
95
96 // Property View stuff
97 private static final String sfResourcePropertiesCategory = Messages.TmfTraceElement_ResourceProperties;
98 private static final String sfName = Messages.TmfTraceElement_Name;
99 private static final String sfPath = Messages.TmfTraceElement_Path;
100 private static final String sfLocation = Messages.TmfTraceElement_Location;
101 private static final String sfEventType = Messages.TmfTraceElement_EventType;
102 private static final String sfIsLinked = Messages.TmfTraceElement_IsLinked;
103 private static final String sfTracePropertiesCategory = Messages.TmfTraceElement_TraceProperties;
104
105 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
106 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
107 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation);
108 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfEventType, sfEventType);
109 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor = new ReadOnlyTextPropertyDescriptor(sfIsLinked, sfIsLinked);
110
111 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
112 sfTypeDescriptor, sfIsLinkedDescriptor };
113
114 static {
115 sfNameDescriptor.setCategory(sfResourcePropertiesCategory);
116 sfPathDescriptor.setCategory(sfResourcePropertiesCategory);
117 sfLocationDescriptor.setCategory(sfResourcePropertiesCategory);
118 sfTypeDescriptor.setCategory(sfResourcePropertiesCategory);
119 sfIsLinkedDescriptor.setCategory(sfResourcePropertiesCategory);
120 }
121
122 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
123
124 // ------------------------------------------------------------------------
125 // Attributes
126 // ------------------------------------------------------------------------
127
128 // This trace type ID as defined in plugin.xml
129 private String fTraceTypeId = null;
130
131 // ------------------------------------------------------------------------
132 // Static initialization
133 // ------------------------------------------------------------------------
134
135 // The mapping of available trace type IDs to their corresponding
136 // configuration element
137 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<>();
138 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<>();
139
140 /**
141 * Initialize statically at startup by getting extensions from the platform
142 * extension registry.
143 */
144 public static void init() {
145 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
146 for (IConfigurationElement ce : config) {
147 String elementName = ce.getName();
148 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
149 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
150 sfTraceTypeAttributes.put(traceTypeId, ce);
151 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
152 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
153 sfTraceCategories.put(categoryId, ce);
154 }
155 }
156 }
157
158 // ------------------------------------------------------------------------
159 // Constructors
160 // ------------------------------------------------------------------------
161 /**
162 * Constructor. Creates trace model element under the trace folder.
163 *
164 * @param name
165 * The name of trace
166 * @param trace
167 * The trace resource.
168 * @param parent
169 * The parent element (trace folder)
170 */
171 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
172 this(name, trace, (TmfProjectModelElement) parent);
173 }
174
175 /**
176 * Constructor. Creates trace model element under the experiment folder.
177 *
178 * @param name
179 * The name of trace
180 * @param trace
181 * The trace resource.
182 * @param parent
183 * The parent element (experiment folder)
184 */
185 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
186 this(name, trace, (TmfProjectModelElement) parent);
187 }
188
189 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
190 super(name, trace, parent);
191 parent.addChild(this);
192 refreshTraceType();
193 TmfSignalManager.register(this);
194 }
195
196 // ------------------------------------------------------------------------
197 // TmfProjectModelElement
198 // ------------------------------------------------------------------------
199
200 @Override
201 void refreshChildren() {
202
203 /* Refreshes the analysis under this trace */
204 Map<String, TmfAnalysisElement> childrenMap = new HashMap<>();
205 for (TmfAnalysisElement analysis : getAvailableAnalysis()) {
206 childrenMap.put(analysis.getAnalysisId(), analysis);
207 }
208
209 TraceTypeHelper helper = TmfTraceType.getInstance().getTraceType(getTraceType());
210
211 Class<? extends ITmfTrace> traceClass = null;
212
213 if (helper == null && fTraceTypeId != null) {
214 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
215 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
216 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
217 traceClass = CustomTxtTrace.class;
218 }
219 }
220 }
221 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
222 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
223 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
224 traceClass = CustomTxtTrace.class;
225 }
226 }
227 }
228 } else if (helper != null) {
229 traceClass = helper.getTraceClass();
230 }
231
232 /* Remove all analysis and return */
233 if (traceClass == null) {
234 for (TmfAnalysisElement analysis : childrenMap.values()) {
235 removeChild(analysis);
236 }
237 return;
238 }
239
240 /** Get the base path to put the resource to */
241 IPath path = fResource.getFullPath();
242
243 /* Add all new analysis modules or refresh outputs of existing ones */
244 for (IAnalysisModuleHelper module : TmfAnalysisManager.getAnalysisModules(traceClass).values()) {
245
246 /* If the analysis is not a child of the trace, create it */
247 TmfAnalysisElement analysis = childrenMap.remove(module.getId());
248 if (analysis == null) {
249 /**
250 * No need for the resource to exist, nothing will be done with
251 * it
252 */
253 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(module.getId()));
254 analysis = new TmfAnalysisElement(module.getName(), newresource, this, module.getId());
255 }
256 analysis.refreshChildren();
257 }
258
259 /* Remove analysis that are not children of this trace anymore */
260 for (TmfAnalysisElement analysis : childrenMap.values()) {
261 removeChild(analysis);
262 }
263 }
264
265 // ------------------------------------------------------------------------
266 // Operations
267 // ------------------------------------------------------------------------
268 /**
269 * Returns the trace type ID.
270 *
271 * @return trace type ID.
272 */
273 public String getTraceType() {
274 return fTraceTypeId;
275 }
276
277 /**
278 * Refreshes the trace type filed by reading the trace type persistent
279 * property of the resource referenece.
280 */
281 public void refreshTraceType() {
282 try {
283 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
284 } catch (CoreException e) {
285 Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
286 }
287 }
288
289 /**
290 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
291 * the corresponding extension.
292 *
293 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
294 */
295 public ITmfTrace instantiateTrace() {
296 try {
297
298 // make sure that supplementary folder exists
299 refreshSupplementaryFolder();
300
301 if (fTraceTypeId != null) {
302 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
303 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
304 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
305 return new CustomTxtTrace(def);
306 }
307 }
308 }
309 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
310 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
311 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
312 return new CustomXmlTrace(def);
313 }
314 }
315 }
316 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
317 if (ce == null) {
318 return null;
319 }
320 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
321 return trace;
322 }
323 } catch (CoreException e) {
324 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
325 }
326 return null;
327 }
328
329 /**
330 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
331 * the corresponding extension.
332 *
333 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
334 */
335 public ITmfEvent instantiateEvent() {
336 try {
337 if (fTraceTypeId != null) {
338 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
339 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
340 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
341 return new CustomTxtEvent(def);
342 }
343 }
344 }
345 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
346 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
347 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
348 return new CustomXmlEvent(def);
349 }
350 }
351 }
352 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
353 if (ce == null) {
354 return null;
355 }
356 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
357 return event;
358 }
359 } catch (CoreException e) {
360 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
361 }
362 return null;
363 }
364
365 /**
366 * Returns the optional editor ID from the trace type extension.
367 *
368 * @return the editor ID or <code>null</code> if not defined.
369 */
370 public String getEditorId() {
371 if (fTraceTypeId != null) {
372 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
373 return TmfEventsEditor.ID;
374 }
375 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
376 return TmfEventsEditor.ID;
377 }
378 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
379 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
380 if (defaultEditorCE.length == 1) {
381 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
382 }
383 }
384 return null;
385 }
386
387 /**
388 * Returns the file resource used to store bookmarks after creating it if
389 * necessary. If the trace resource is a file, it is returned directly. If
390 * the trace resource is a folder, a linked file is returned. The file will
391 * be created if it does not exist.
392 *
393 * @return the bookmarks file
394 * @throws CoreException
395 * if the bookmarks file cannot be created
396 * @since 2.0
397 */
398 public IFile createBookmarksFile() throws CoreException {
399 IFile file = getBookmarksFile();
400 if (fResource instanceof IFolder) {
401 if (!file.exists()) {
402 final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
403 if (!bookmarksFile.exists()) {
404 final InputStream source = new ByteArrayInputStream(new byte[0]);
405 bookmarksFile.create(source, true, null);
406 }
407 bookmarksFile.setHidden(true);
408 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
409 file.setHidden(true);
410 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
411 }
412 }
413 return file;
414 }
415
416 /**
417 * Returns the file resource used to store bookmarks. The file may not
418 * exist.
419 *
420 * @return the bookmarks file
421 * @since 2.0
422 */
423 public IFile getBookmarksFile() {
424 IFile file = null;
425 if (fResource instanceof IFile) {
426 file = (IFile) fResource;
427 } else if (fResource instanceof IFolder) {
428 final IFolder folder = (IFolder) fResource;
429 file = folder.getFile(getName() + '_');
430 }
431 return file;
432 }
433
434 /**
435 * Returns the <code>TmfTraceElement</code> located under the
436 * <code>TmfTracesFolder</code>.
437 *
438 * @return <code>this</code> if this element is under the
439 * <code>TmfTracesFolder</code> else the corresponding
440 * <code>TmfTraceElement</code> if this element is under
441 * <code>TmfExperimentElement</code>.
442 */
443 public TmfTraceElement getElementUnderTraceFolder() {
444
445 // If trace is under an experiment, return original trace from the
446 // traces folder
447 if (getParent() instanceof TmfExperimentElement) {
448 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
449 if (aTrace.getName().equals(getName())) {
450 return aTrace;
451 }
452 }
453 }
454 return this;
455 }
456
457 // ------------------------------------------------------------------------
458 // IActionFilter
459 // ------------------------------------------------------------------------
460
461 @Override
462 public boolean testAttribute(Object target, String name, String value) {
463 if (name.equals(IS_LINKED)) {
464 boolean isLinked = getResource().isLinked();
465 return Boolean.toString(isLinked).equals(value);
466 }
467 return false;
468 }
469
470 // ------------------------------------------------------------------------
471 // TmfTraceElement
472 // ------------------------------------------------------------------------
473
474 @Override
475 public TmfProjectElement getProject() {
476 if (getParent() instanceof TmfTraceFolder) {
477 TmfTraceFolder folder = (TmfTraceFolder) getParent();
478 TmfProjectElement project = (TmfProjectElement) folder.getParent();
479 return project;
480 }
481 if (getParent() instanceof TmfExperimentElement) {
482 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
483 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
484 TmfProjectElement project = (TmfProjectElement) folder.getParent();
485 return project;
486 }
487 return null;
488 }
489
490 // ------------------------------------------------------------------------
491 // IPropertySource2
492 // ------------------------------------------------------------------------
493
494 @Override
495 public Object getEditableValue() {
496 return null;
497 }
498
499 /**
500 * Get the trace properties of this traceElement if the corresponding trace
501 * is opened in an editor
502 *
503 * @return a map with the names and values of the trace properties
504 * respectively as keys and values
505 */
506 private Map<String, String> getTraceProperties() {
507 for (ITmfTrace openedTrace : TmfTraceManager.getInstance().getOpenedTraces()) {
508 for (ITmfTrace singleTrace : TmfTraceManager.getTraceSet(openedTrace)) {
509 if (this.getLocation().toString().endsWith(singleTrace.getPath())) {
510 if (singleTrace instanceof ITmfTraceProperties) {
511 ITmfTraceProperties traceProperties = (ITmfTraceProperties) singleTrace;
512 return traceProperties.getTraceProperties();
513 }
514 }
515 }
516 }
517 return new HashMap<>();
518 }
519
520 @Override
521 public IPropertyDescriptor[] getPropertyDescriptors() {
522 Map<String, String> traceProperties = getTraceProperties();
523 if (!traceProperties.isEmpty()) {
524 IPropertyDescriptor[] propertyDescriptorArray = new IPropertyDescriptor[traceProperties.size() + sfDescriptors.length];
525 int index = 0;
526 for (Map.Entry<String, String> varName : traceProperties.entrySet()) {
527 ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + "_" + varName.getKey(), varName.getKey()); //$NON-NLS-1$
528 descriptor.setCategory(sfTracePropertiesCategory);
529 propertyDescriptorArray[index] = descriptor;
530 index++;
531 }
532 for (int i = 0; i < sfDescriptors.length; i++) {
533 propertyDescriptorArray[index] = sfDescriptors[i];
534 index++;
535 }
536 return propertyDescriptorArray;
537 }
538 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
539 }
540
541 @Override
542 public Object getPropertyValue(Object id) {
543
544 if (sfName.equals(id)) {
545 return getName();
546 }
547
548 if (sfPath.equals(id)) {
549 return getPath().toString();
550 }
551
552 if (sfLocation.equals(id)) {
553 return getLocation().toString();
554 }
555
556 if (sfIsLinked.equals(id)) {
557 return Boolean.valueOf(getResource().isLinked()).toString();
558 }
559
560 if (sfEventType.equals(id)) {
561 if (fTraceTypeId != null) {
562 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
563 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
564 }
565 }
566
567 Map<String, String> traceProperties = getTraceProperties();
568 if (id != null && !traceProperties.isEmpty()) {
569 String key = (String) id;
570 key = key.replaceFirst(this.getName() + "_", ""); //$NON-NLS-1$ //$NON-NLS-2$
571 String value = traceProperties.get(key);
572 return value;
573 }
574
575 return null;
576 }
577
578 private static String getCategory(IConfigurationElement ce) {
579 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
580 if (categoryId != null) {
581 IConfigurationElement category = sfTraceCategories.get(categoryId);
582 if (category != null) {
583 return category.getAttribute(TmfTraceType.NAME_ATTR);
584 }
585 }
586 return "[no category]"; //$NON-NLS-1$
587 }
588
589 @Override
590 public void resetPropertyValue(Object id) {
591 }
592
593 @Override
594 public void setPropertyValue(Object id, Object value) {
595 }
596
597 @Override
598 public boolean isPropertyResettable(Object id) {
599 return false;
600 }
601
602 @Override
603 public boolean isPropertySet(Object id) {
604 return false;
605 }
606
607 /**
608 * Copy this trace in the trace folder. No other parameters are mentioned so
609 * the trace is copied in this element's project trace folder
610 *
611 * @param string
612 * The new trace name
613 * @return the new Resource object
614 * @since 2.0
615 */
616 public TmfTraceElement copy(String string) {
617 TmfTraceFolder folder = this.getProject().getTracesFolder();
618 IResource res = super.copy(string, false);
619 return new TmfTraceElement(string, res, folder);
620 }
621
622 /**
623 * Close opened editors associated with this trace.
624 *
625 * @since 2.0
626 */
627 public void closeEditors() {
628 // Close the trace if open
629 IFile file = getBookmarksFile();
630 FileEditorInput input = new FileEditorInput(file);
631 IWorkbench wb = PlatformUI.getWorkbench();
632 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
633 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
634 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
635 try {
636 if (editorReference.getEditorInput().equals(input)) {
637 wbPage.closeEditor(editorReference.getEditor(false), false);
638 }
639 } catch (PartInitException e) {
640 Activator.getDefault().logError("Error closing editor for trace " + getName(), e); //$NON-NLS-1$
641 }
642 }
643 }
644 }
645
646 // Close experiments that contain the trace if open
647 if (getParent() instanceof TmfTraceFolder) {
648 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
649 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
650 for (ITmfProjectModelElement child : experiment.getChildren()) {
651 if (child.getName().equals(getName())) {
652 ((TmfExperimentElement) experiment).closeEditors();
653 break;
654 }
655 }
656 }
657 } else if (getParent() instanceof TmfExperimentElement) {
658 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
659 experiment.closeEditors();
660 }
661 }
662
663 /**
664 * Delete the trace resource, remove it from experiments and delete its
665 * supplementary files
666 *
667 * @param progressMonitor
668 * a progress monitor, or null if progress reporting is not
669 * desired
670 *
671 * @throws CoreException
672 * thrown when IResource.delete fails
673 * @since 2.2
674 */
675 public void delete(IProgressMonitor progressMonitor) throws CoreException {
676 closeEditors();
677
678 IPath path = fResource.getLocation();
679 if (path != null && (getParent() instanceof TmfTraceFolder)) {
680 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
681
682 // Propagate the removal to traces
683 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
684 List<ITmfProjectModelElement> toRemove = new LinkedList<>();
685 for (ITmfProjectModelElement child : experiment.getChildren()) {
686 if (child.getName().equals(getName())) {
687 toRemove.add(child);
688 }
689 }
690 for (ITmfProjectModelElement child : toRemove) {
691 ((TmfExperimentElement) experiment).removeTrace((TmfTraceElement) child);
692 }
693 }
694
695 // Delete supplementary files
696 deleteSupplementaryFolder();
697 }
698
699 // Finally, delete the trace
700 fResource.delete(true, progressMonitor);
701 }
702
703 /**
704 * Get the instantiated trace associated with this element.
705 *
706 * @return The instantiated trace or null if trace is not (yet) available
707 * @since 2.1
708 */
709 public ITmfTrace getTrace() {
710 for (ITmfTrace trace : TmfTraceManager.getInstance().getOpenedTraces()) {
711 if (trace.getResource().equals(getResource())) {
712 return trace;
713 }
714 }
715 return null;
716 }
717
718 /**
719 * Get the list of analysis model elements under this trace
720 *
721 * @return Array of analysis elements
722 * @since 3.0
723 */
724 public List<TmfAnalysisElement> getAvailableAnalysis() {
725 List<ITmfProjectModelElement> children = getChildren();
726 List<TmfAnalysisElement> analysis = new ArrayList<>();
727 for (ITmfProjectModelElement child : children) {
728 if (child instanceof TmfAnalysisElement) {
729 analysis.add((TmfAnalysisElement) child);
730 }
731 }
732 return analysis;
733 }
734
735 /**
736 * Handler for the Trace Opened signal
737 *
738 * @param signal
739 * The incoming signal
740 * @since 3.0
741 */
742 @TmfSignalHandler
743 public void traceOpened(TmfTraceOpenedSignal signal) {
744 IResource resource = signal.getTrace().getResource();
745 if ((resource == null) || !resource.equals(getResource())) {
746 return;
747 }
748
749 getParent().refresh();
750 }
751 }
This page took 0.058079 seconds and 5 git commands to generate.