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