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