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