tmf: change ITmfTraceProperties to a more generic ITmfPropertiesProvider
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TmfTraceElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2015 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.tracecompass.tmf.ui.project.model;
21
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.eclipse.core.filesystem.EFS;
30 import org.eclipse.core.filesystem.IFileInfo;
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IFolder;
33 import org.eclipse.core.resources.IResource;
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.core.runtime.URIUtil;
40 import org.eclipse.osgi.util.NLS;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
43 import org.eclipse.tracecompass.internal.tmf.ui.editors.ITmfEventsEditorConstants;
44 import org.eclipse.tracecompass.tmf.core.TmfCommonConstants;
45 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
46 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtEvent;
47 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTrace;
48 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomTxtTraceDefinition;
49 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent;
50 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
51 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
52 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
53 import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
54 import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
55 import org.eclipse.tracecompass.tmf.core.synchronization.TimestampTransformFactory;
56 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
57 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
58 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
59 import org.eclipse.tracecompass.tmf.ui.editors.TmfEventsEditor;
60 import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
61 import org.eclipse.ui.IActionFilter;
62 import org.eclipse.ui.views.properties.IPropertyDescriptor;
63 import org.eclipse.ui.views.properties.IPropertySource2;
64
65 import com.ibm.icu.text.DateFormat;
66 import com.ibm.icu.text.NumberFormat;
67
68 /**
69 * Implementation of trace model element representing a trace. It provides
70 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
71 * well as editor ID from the trace type extension definition.
72 *
73 * @version 1.0
74 * @author Francois Chouinard
75 */
76 public class TmfTraceElement extends TmfCommonProjectElement implements IActionFilter, IPropertySource2 {
77
78 // ------------------------------------------------------------------------
79 // Constants
80 // ------------------------------------------------------------------------
81
82 // Other attributes
83 /**
84 * Bundle attribute name
85 */
86 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
87 /**
88 * IsLinked attribute name.
89 */
90 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
91
92 // Property View stuff
93 private static final String RESOURCE_PROPERTIES_CATEGORY = Messages.TmfTraceElement_ResourceProperties;
94 private static final String NAME = Messages.TmfTraceElement_Name;
95 private static final String PATH = Messages.TmfTraceElement_Path;
96 private static final String LOCATION = Messages.TmfTraceElement_Location;
97 private static final String TRACE_TYPE = Messages.TmfTraceElement_EventType;
98 private static final String IS_LINKED_PROPERTY = Messages.TmfTraceElement_IsLinked;
99 private static final String SOURCE_LOCATION = Messages.TmfTraceElement_SourceLocation;
100 private static final String TIME_OFFSET = Messages.TmfTraceElement_TimeOffset;
101 private static final String LAST_MODIFIED = Messages.TmfTraceElement_LastModified;
102 private static final String SIZE = Messages.TmfTraceElement_Size;
103 private static final String TRACE_PROPERTIES_CATEGORY = Messages.TmfTraceElement_TraceProperties;
104
105 private static final ReadOnlyTextPropertyDescriptor NAME_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(NAME, NAME);
106 private static final ReadOnlyTextPropertyDescriptor PATH_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(PATH, PATH);
107 private static final ReadOnlyTextPropertyDescriptor LOCATION_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(LOCATION, LOCATION);
108 private static final ReadOnlyTextPropertyDescriptor TYPE_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(TRACE_TYPE, TRACE_TYPE);
109 private static final ReadOnlyTextPropertyDescriptor IS_LINKED_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(IS_LINKED_PROPERTY, IS_LINKED_PROPERTY);
110 private static final ReadOnlyTextPropertyDescriptor SOURCE_LOCATION_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(SOURCE_LOCATION, SOURCE_LOCATION);
111 private static final ReadOnlyTextPropertyDescriptor TIME_OFFSET_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(TIME_OFFSET, TIME_OFFSET);
112 private static final ReadOnlyTextPropertyDescriptor LAST_MODIFIED_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(LAST_MODIFIED, LAST_MODIFIED);
113 private static final ReadOnlyTextPropertyDescriptor SIZE_DESCRIPTOR = new ReadOnlyTextPropertyDescriptor(SIZE, SIZE);
114
115 private static final IPropertyDescriptor[] sfDescriptors = { NAME_DESCRIPTOR, PATH_DESCRIPTOR, LOCATION_DESCRIPTOR,
116 TYPE_DESCRIPTOR, IS_LINKED_DESCRIPTOR, SOURCE_LOCATION_DESCRIPTOR,
117 TIME_OFFSET_DESCRIPTOR, LAST_MODIFIED_DESCRIPTOR, SIZE_DESCRIPTOR };
118
119 static {
120 NAME_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
121 PATH_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
122 LOCATION_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
123 TYPE_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
124 IS_LINKED_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
125 SOURCE_LOCATION_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
126 TIME_OFFSET_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
127 LAST_MODIFIED_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
128 SIZE_DESCRIPTOR.setCategory(RESOURCE_PROPERTIES_CATEGORY);
129 }
130
131 private static final TmfTimestampFormat OFFSET_FORMAT = new TmfTimestampFormat("T.SSS SSS SSS s"); //$NON-NLS-1$
132
133 private static final int FOLDER_MAX_COUNT = 1024;
134
135 // ------------------------------------------------------------------------
136 // Static initialization
137 // ------------------------------------------------------------------------
138
139 // The mapping of available trace type IDs to their corresponding
140 // configuration element
141 private static final Map<String, IConfigurationElement> TRACE_TYPE_ATTRIBUTES = new HashMap<>();
142 private static final Map<String, IConfigurationElement> TRACE_TYPE_UI_ATTRIBUTES = new HashMap<>();
143 private static final Map<String, IConfigurationElement> TRACE_CATEGORIES = new HashMap<>();
144
145 /**
146 * Initialize statically at startup by getting extensions from the platform
147 * extension registry.
148 */
149 public static void init() {
150 /* Read the tmf.core "tracetype" extension point */
151 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
152 for (IConfigurationElement ce : config) {
153 switch (ce.getName()) {
154 case TmfTraceType.TYPE_ELEM:
155 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
156 TRACE_TYPE_ATTRIBUTES.put(traceTypeId, ce);
157 break;
158 case TmfTraceType.CATEGORY_ELEM:
159 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
160 TRACE_CATEGORIES.put(categoryId, ce);
161 break;
162 default:
163 }
164 }
165
166 /*
167 * Read the corresponding tmf.ui "tracetypeui" extension point for this
168 * trace type, if it exists.
169 */
170 config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceTypeUIUtils.TMF_TRACE_TYPE_UI_ID);
171 for (IConfigurationElement ce : config) {
172 String elemName = ce.getName();
173 if (TmfTraceTypeUIUtils.TYPE_ELEM.equals(elemName)) {
174 String traceType = ce.getAttribute(TmfTraceTypeUIUtils.TRACETYPE_ATTR);
175 TRACE_TYPE_UI_ATTRIBUTES.put(traceType, ce);
176 }
177 }
178 }
179
180 // ------------------------------------------------------------------------
181 // Classes
182 // ------------------------------------------------------------------------
183
184 private class FileInfo {
185 long lastModified;
186 long size;
187 int count;
188 }
189
190 // ------------------------------------------------------------------------
191 // Attributes
192 // ------------------------------------------------------------------------
193
194 private FileInfo fFileInfo;
195
196 // ------------------------------------------------------------------------
197 // Constructors
198 // ------------------------------------------------------------------------
199 /**
200 * Constructor. Creates trace model element under the trace folder.
201 *
202 * @param name
203 * The name of trace
204 * @param trace
205 * The trace resource.
206 * @param parent
207 * The parent element (trace folder)
208 */
209 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
210 super(name, trace, parent);
211 }
212
213 /**
214 * Constructor. Creates trace model element under the experiment folder.
215 *
216 * @param name
217 * The name of trace
218 * @param trace
219 * The trace resource.
220 * @param parent
221 * The parent element (experiment folder)
222 */
223 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
224 super(name, trace, parent);
225 }
226
227 // ------------------------------------------------------------------------
228 // Operations
229 // ------------------------------------------------------------------------
230
231 /**
232 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
233 * the corresponding extension.
234 *
235 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
236 */
237 @Override
238 public ITmfTrace instantiateTrace() {
239 try {
240
241 // make sure that supplementary folder exists
242 refreshSupplementaryFolder();
243
244 String traceTypeId = getTraceType();
245 if (traceTypeId != null) {
246 if (CustomTxtTrace.isCustomTraceTypeId(traceTypeId)) {
247 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
248 String id = CustomTxtTrace.buildTraceTypeId(def.categoryName, def.definitionName);
249 if (traceTypeId.equals(id)) {
250 return new CustomTxtTrace(def);
251 }
252 }
253 }
254 if (CustomXmlTrace.isCustomTraceTypeId(traceTypeId)) {
255 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
256 String id = CustomXmlTrace.buildTraceTypeId(def.categoryName, def.definitionName);
257 if (traceTypeId.equals(id)) {
258 return new CustomXmlTrace(def);
259 }
260 }
261 }
262 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(traceTypeId);
263 if (ce == null) {
264 return null;
265 }
266 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
267 return trace;
268 }
269 } catch (CoreException e) {
270 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
271 }
272 return null;
273 }
274
275 /**
276 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
277 * the corresponding extension.
278 *
279 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
280 */
281 public ITmfEvent instantiateEvent() {
282 try {
283 String traceTypeId = getTraceType();
284 if (traceTypeId != null) {
285 if (CustomTxtTrace.isCustomTraceTypeId(traceTypeId)) {
286 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
287 String id = CustomTxtTrace.buildTraceTypeId(def.categoryName, def.definitionName);
288 if (traceTypeId.equals(id)) {
289 return new CustomTxtEvent(def);
290 }
291 }
292 }
293 if (CustomXmlTrace.isCustomTraceTypeId(traceTypeId)) {
294 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
295 String id = CustomXmlTrace.buildTraceTypeId(def.categoryName, def.definitionName);
296 if (traceTypeId.equals(id)) {
297 return new CustomXmlEvent(def);
298 }
299 }
300 }
301 IConfigurationElement ce = TRACE_TYPE_ATTRIBUTES.get(traceTypeId);
302 if (ce == null) {
303 return null;
304 }
305 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
306 return event;
307 }
308 } catch (CoreException e) {
309 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
310 }
311 return null;
312 }
313
314 @Override
315 public String getEditorId() {
316 String traceTypeId = getTraceType();
317 if (traceTypeId != null) {
318 if (CustomTxtTrace.isCustomTraceTypeId(traceTypeId) || CustomXmlTrace.isCustomTraceTypeId(traceTypeId)) {
319 return TmfEventsEditor.ID;
320 }
321
322 IConfigurationElement ce = TRACE_TYPE_UI_ATTRIBUTES.get(getTraceType());
323 if (ce == null) {
324 /* This trace type does not define UI attributes */
325 return null;
326 }
327 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceTypeUIUtils.DEFAULT_EDITOR_ELEM);
328 if (defaultEditorCE.length == 1) {
329 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
330 }
331 }
332 return null;
333 }
334
335 /**
336 * Returns the file resource used to store bookmarks after creating it if
337 * necessary. If the trace resource is a file, it is returned directly. If
338 * the trace resource is a folder, a linked file is returned. The file will
339 * be created if it does not exist.
340 *
341 * @return the bookmarks file
342 * @throws CoreException
343 * if the bookmarks file cannot be created
344 */
345 @Override
346 public IFile createBookmarksFile() throws CoreException {
347 IFile file = getBookmarksFile();
348 if (fResource instanceof IFolder) {
349 return createBookmarksFile(getProject().getTracesFolder().getResource(), ITmfEventsEditorConstants.TRACE_EDITOR_INPUT_TYPE);
350 }
351 return file;
352 }
353
354 /**
355 * Returns the file resource used to store bookmarks. The file may not
356 * exist.
357 *
358 * @return the bookmarks file
359 */
360 @Override
361 public IFile getBookmarksFile() {
362 IFile file = null;
363 if (fResource instanceof IFile) {
364 file = (IFile) fResource;
365 } else if (fResource instanceof IFolder) {
366 final IFolder folder = (IFolder) fResource;
367 file = folder.getFile(getName() + '_');
368 }
369 return file;
370 }
371
372 /**
373 * Returns the <code>TmfTraceElement</code> located under the
374 * <code>TmfTracesFolder</code>.
375 *
376 * @return <code>this</code> if this element is under the
377 * <code>TmfTracesFolder</code> else the corresponding
378 * <code>TmfTraceElement</code> if this element is under
379 * <code>TmfExperimentElement</code>.
380 */
381 public TmfTraceElement getElementUnderTraceFolder() {
382
383 // If trace is under an experiment, return original trace from the
384 // traces folder
385 if (getParent() instanceof TmfExperimentElement) {
386 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
387 if (aTrace.getElementPath().equals(getElementPath())) {
388 return aTrace;
389 }
390 }
391 }
392 return this;
393 }
394
395 @Override
396 public String getTypeName() {
397 return Messages.TmfTraceElement_TypeName;
398 }
399
400 // ------------------------------------------------------------------------
401 // IActionFilter
402 // ------------------------------------------------------------------------
403
404 @Override
405 public boolean testAttribute(Object target, String name, String value) {
406 if (name.equals(IS_LINKED)) {
407 boolean isLinked = getResource().isLinked();
408 return Boolean.toString(isLinked).equals(value);
409 }
410 return false;
411 }
412
413 // ------------------------------------------------------------------------
414 // IPropertySource2
415 // ------------------------------------------------------------------------
416
417 @Override
418 public Object getEditableValue() {
419 return null;
420 }
421
422 /**
423 * Get the trace properties of this traceElement if the corresponding trace
424 * is opened in an editor
425 *
426 * @return a map with the names and values of the trace properties
427 * respectively as keys and values
428 */
429 private Map<String, String> getTraceProperties() {
430 for (ITmfTrace openedTrace : TmfTraceManager.getInstance().getOpenedTraces()) {
431 for (ITmfTrace singleTrace : TmfTraceManager.getTraceSet(openedTrace)) {
432 if (getElementUnderTraceFolder().getResource().equals(singleTrace.getResource())) {
433 if (singleTrace instanceof ITmfPropertiesProvider) {
434 ITmfPropertiesProvider traceProperties = (ITmfPropertiesProvider) singleTrace;
435 return traceProperties.getProperties();
436 }
437 }
438 }
439 }
440 return new HashMap<>();
441 }
442
443 @Override
444 public IPropertyDescriptor[] getPropertyDescriptors() {
445 Map<String, String> traceProperties = getTraceProperties();
446 if (!traceProperties.isEmpty()) {
447 IPropertyDescriptor[] propertyDescriptorArray = new IPropertyDescriptor[traceProperties.size() + sfDescriptors.length];
448 int index = 0;
449 for (Map.Entry<String, String> varName : traceProperties.entrySet()) {
450 ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + "_" + varName.getKey(), varName.getKey()); //$NON-NLS-1$
451 descriptor.setCategory(TRACE_PROPERTIES_CATEGORY);
452 propertyDescriptorArray[index] = descriptor;
453 index++;
454 }
455 for (int i = 0; i < sfDescriptors.length; i++) {
456 propertyDescriptorArray[index] = sfDescriptors[i];
457 index++;
458 }
459 return propertyDescriptorArray;
460 }
461 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
462 }
463
464 @Override
465 public Object getPropertyValue(Object id) {
466
467 if (NAME.equals(id)) {
468 return getName();
469 }
470
471 if (PATH.equals(id)) {
472 return getPath().toString();
473 }
474
475 if (LOCATION.equals(id)) {
476 return URIUtil.toUnencodedString(getLocation());
477 }
478
479 if (IS_LINKED_PROPERTY.equals(id)) {
480 return Boolean.valueOf(getResource().isLinked()).toString();
481 }
482
483 if (SOURCE_LOCATION.equals(id)) {
484 try {
485 String sourceLocation = getElementUnderTraceFolder().getResource().getPersistentProperty(TmfCommonConstants.SOURCE_LOCATION);
486 if (sourceLocation != null) {
487 return sourceLocation;
488 }
489 } catch (CoreException e) {
490 }
491 return ""; //$NON-NLS-1$
492 }
493
494 if (LAST_MODIFIED.equals(id)) {
495 FileInfo fileInfo = getFileInfo();
496 if (fileInfo == null) {
497 return ""; //$NON-NLS-1$
498 }
499 long date = fileInfo.lastModified;
500 DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.MEDIUM);
501 return format.format(new Date(date));
502 }
503
504 if (SIZE.equals(id)) {
505 FileInfo fileInfo = getFileInfo();
506 if (fileInfo == null) {
507 return ""; //$NON-NLS-1$
508 }
509 if (getResource() instanceof IFolder) {
510 if (fileInfo.count <= FOLDER_MAX_COUNT) {
511 return NLS.bind(Messages.TmfTraceElement_FolderSizeString,
512 NumberFormat.getInstance().format(fileInfo.size), fileInfo.count);
513 }
514 return NLS.bind(Messages.TmfTraceElement_FolderSizeOverflowString,
515 NumberFormat.getInstance().format(fileInfo.size), FOLDER_MAX_COUNT);
516 }
517 return NLS.bind(Messages.TmfTraceElement_FileSizeString, NumberFormat.getInstance().format(fileInfo.size));
518 }
519
520 if (TRACE_TYPE.equals(id)) {
521 if (getTraceType() != null) {
522 TraceTypeHelper helper = TmfTraceType.getTraceType(getTraceType());
523 if (helper != null) {
524 return helper.getLabel();
525 }
526 }
527 return ""; //$NON-NLS-1$
528 }
529
530 if (TIME_OFFSET.equals(id)) {
531 long offset = TimestampTransformFactory.getTimestampTransform(getElementUnderTraceFolder().getResource()).transform(0);
532 if (offset != 0) {
533 return OFFSET_FORMAT.format(offset);
534 }
535 return ""; //$NON-NLS-1$
536 }
537
538 Map<String, String> traceProperties = getTraceProperties();
539 if (id != null && !traceProperties.isEmpty()) {
540 String key = (String) id;
541 key = key.substring(this.getName().length() + 1); // remove name_
542 String value = traceProperties.get(key);
543 return value;
544 }
545
546 return null;
547 }
548
549 private FileInfo getFileInfo() {
550 /* FileInfo is needed for both 'last modified' and 'size' properties.
551 * It is freshly computed for one, and reused for the other, then
552 * cleared so that the information can be refreshed the next time.
553 */
554 FileInfo fileInfo;
555 if (fFileInfo == null) {
556 try {
557 fileInfo = computeFileInfo(new FileInfo(), getResource());
558 } catch (CoreException e) {
559 return null;
560 }
561 fFileInfo = fileInfo;
562 } else {
563 fileInfo = fFileInfo;
564 fFileInfo = null;
565 }
566 return fileInfo;
567 }
568
569 private FileInfo computeFileInfo(FileInfo fileInfo, IResource resource) throws CoreException {
570 if (fileInfo == null || fileInfo.count > FOLDER_MAX_COUNT) {
571 return fileInfo;
572 }
573 if (resource instanceof IFolder) {
574 IFolder folder = (IFolder) resource;
575 for (IResource member : folder.members()) {
576 computeFileInfo(fileInfo, member);
577 }
578 return fileInfo;
579 }
580 IFileInfo info = EFS.getStore(resource.getLocationURI()).fetchInfo();
581 fileInfo.lastModified = Math.max(fileInfo.lastModified, info.getLastModified());
582 fileInfo.size += info.getLength();
583 fileInfo.count++;
584 return fileInfo;
585 }
586
587 @Override
588 public void resetPropertyValue(Object id) {
589 }
590
591 @Override
592 public void setPropertyValue(Object id, Object value) {
593 }
594
595 @Override
596 public boolean isPropertyResettable(Object id) {
597 return false;
598 }
599
600 @Override
601 public boolean isPropertySet(Object id) {
602 return false;
603 }
604
605 /**
606 * Copy this trace in the trace folder. No other parameters are mentioned so
607 * the trace is copied in this element's project trace folder
608 *
609 * @param newName
610 * The new trace name
611 * @return the new Resource object
612 */
613 public TmfTraceElement copy(String newName) {
614 TmfTraceFolder folder = (TmfTraceFolder) getParent();
615 IResource res = super.copy(newName, false);
616 for (TmfTraceElement trace : folder.getTraces()) {
617 if (trace.getResource().equals(res)) {
618 return trace;
619 }
620 }
621 return null;
622 }
623
624 /**
625 * Close opened editors associated with this trace.
626 */
627 @Override
628 public void closeEditors() {
629 super.closeEditors();
630
631 // Close experiments that contain the trace if open
632 if (getParent() instanceof TmfTraceFolder) {
633 TmfExperimentFolder experimentsFolder = getProject().getExperimentsFolder();
634 for (TmfExperimentElement experiment : experimentsFolder.getExperiments()) {
635 for (TmfTraceElement trace : experiment.getTraces()) {
636 if (trace.getElementPath().equals(getElementPath())) {
637 experiment.closeEditors();
638 break;
639 }
640 }
641 }
642 } else if (getParent() instanceof TmfExperimentElement) {
643 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
644 experiment.closeEditors();
645 }
646 }
647
648 /**
649 * Delete the trace resource, remove it from experiments and delete its
650 * supplementary files
651 *
652 * @param progressMonitor
653 * a progress monitor, or null if progress reporting is not
654 * desired
655 *
656 * @throws CoreException
657 * thrown when IResource.delete fails
658 */
659 public void delete(IProgressMonitor progressMonitor) throws CoreException {
660 // Close editors in UI Thread
661 Display.getDefault().syncExec(new Runnable() {
662 @Override
663 public void run() {
664 closeEditors();
665 }
666 });
667
668 IPath path = fResource.getLocation();
669 if (path != null) {
670 if (getParent() instanceof TmfTraceFolder) {
671 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
672
673 // Propagate the removal to traces
674 for (TmfExperimentElement experiment : experimentFolder.getExperiments()) {
675 List<TmfTraceElement> toRemove = new LinkedList<>();
676 for (TmfTraceElement trace : experiment.getTraces()) {
677 if (trace.getElementPath().equals(getElementPath())) {
678 toRemove.add(trace);
679 }
680 }
681 for (TmfTraceElement child : toRemove) {
682 experiment.removeTrace(child);
683 }
684 }
685
686 // Delete supplementary files
687 deleteSupplementaryFolder();
688
689 } else if (getParent() instanceof TmfExperimentElement) {
690 TmfExperimentElement experimentElement = (TmfExperimentElement) getParent();
691 experimentElement.removeTrace(this);
692 }
693 }
694
695 // Finally, delete the trace
696 fResource.delete(true, progressMonitor);
697 }
698
699 }
This page took 0.062469 seconds and 5 git commands to generate.