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