Remove UI dependencies in tmf.core.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
CommitLineData
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
12c155f5
FC
15 *******************************************************************************/
16
17package org.eclipse.linuxtools.tmf.ui.project.model;
18
c4c81d91
PT
19import java.io.ByteArrayInputStream;
20import java.io.InputStream;
5a5c2fc7 21import java.util.Arrays;
12c155f5
FC
22import java.util.HashMap;
23import java.util.Map;
24
c4c81d91 25import org.eclipse.core.resources.IFile;
5e4bf87d 26import org.eclipse.core.resources.IFolder;
12c155f5
FC
27import org.eclipse.core.resources.IResource;
28import org.eclipse.core.runtime.CoreException;
29import org.eclipse.core.runtime.IConfigurationElement;
30import org.eclipse.core.runtime.Platform;
8fd82db5 31import org.eclipse.linuxtools.internal.tmf.ui.Activator;
d34665f9
FC
32import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
33import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
34import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
35import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
36import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
37import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
e12ecd30 38import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
ce2388e0 39import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
6c13869b 40import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
c4c81d91 41import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
4bf17f4a 42import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
080600d9 43import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
12c155f5 44import org.eclipse.ui.IActionFilter;
a72a6830
PT
45import org.eclipse.ui.IEditorReference;
46import org.eclipse.ui.IWorkbench;
47import org.eclipse.ui.IWorkbenchPage;
48import org.eclipse.ui.IWorkbenchWindow;
49import org.eclipse.ui.PartInitException;
50import org.eclipse.ui.PlatformUI;
51import org.eclipse.ui.part.FileEditorInput;
12c155f5
FC
52import org.eclipse.ui.views.properties.IPropertyDescriptor;
53import org.eclipse.ui.views.properties.IPropertySource2;
12c155f5
FC
54
55/**
d04ec5a7
MK
56 * Implementation of trace model element representing a trace. It provides
57 * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
58 * well as editor ID from the trace type extension definition.
abbdd66a 59 *
b544077e
BH
60 * @version 1.0
61 * @author Francois Chouinard
12c155f5 62 */
99504bb8 63public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
12c155f5
FC
64
65 // ------------------------------------------------------------------------
66 // Constants
67 // ------------------------------------------------------------------------
68
12c155f5 69 // Other attributes
b544077e
BH
70 /**
71 * Bundle attribute name
72 */
12c155f5 73 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
b544077e
BH
74 /**
75 * IsLinked attribute name.
76 */
12c155f5
FC
77 public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
78
79 // Property View stuff
80 private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
81 private static final String sfName = "name"; //$NON-NLS-1$
82 private static final String sfPath = "path"; //$NON-NLS-1$
83 private static final String sfLocation = "location"; //$NON-NLS-1$
84 private static final String sfEventType = "type"; //$NON-NLS-1$
85 private static final String sfIsLinked = "linked"; //$NON-NLS-1$
86
253d5be1
BH
87 private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
88 private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
89 private static final ReadOnlyTextPropertyDescriptor sfLocationDescriptor = new ReadOnlyTextPropertyDescriptor(sfLocation, sfLocation);
90 private static final ReadOnlyTextPropertyDescriptor sfTypeDescriptor = new ReadOnlyTextPropertyDescriptor(sfEventType, sfEventType);
91 private static final ReadOnlyTextPropertyDescriptor sfIsLinkedDescriptor = new ReadOnlyTextPropertyDescriptor(sfIsLinked, sfIsLinked);
12c155f5
FC
92
93 private static final IPropertyDescriptor[] sfDescriptors = { sfNameDescriptor, sfPathDescriptor, sfLocationDescriptor,
94 sfTypeDescriptor, sfIsLinkedDescriptor };
95
96 static {
97 sfNameDescriptor.setCategory(sfInfoCategory);
98 sfPathDescriptor.setCategory(sfInfoCategory);
99 sfLocationDescriptor.setCategory(sfInfoCategory);
100 sfTypeDescriptor.setCategory(sfInfoCategory);
101 sfIsLinkedDescriptor.setCategory(sfInfoCategory);
102 }
6256d8ad 103
c4c81d91
PT
104 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
105
12c155f5
FC
106 // ------------------------------------------------------------------------
107 // Attributes
108 // ------------------------------------------------------------------------
109
110 // This trace type ID as defined in plugin.xml
111 private String fTraceTypeId = null;
112
113 // ------------------------------------------------------------------------
114 // Static initialization
115 // ------------------------------------------------------------------------
116
d04ec5a7
MK
117 // The mapping of available trace type IDs to their corresponding
118 // configuration element
12c155f5
FC
119 private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
120 private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
121
b544077e 122 /**
d04ec5a7
MK
123 * Initialize statically at startup by getting extensions from the platform
124 * extension registry.
b544077e 125 */
12c155f5 126 public static void init() {
4bf17f4a 127 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
12c155f5 128 for (IConfigurationElement ce : config) {
4bf17f4a 129 String elementName = ce.getName();
130 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
131 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5 132 sfTraceTypeAttributes.put(traceTypeId, ce);
4bf17f4a 133 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
134 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
135 sfTraceCategories.put(categoryId, ce);
136 }
137 }
138 }
139
140 // ------------------------------------------------------------------------
141 // Constructors
142 // ------------------------------------------------------------------------
b544077e 143 /**
d04ec5a7
MK
144 * Constructor. Creates trace model element under the trace folder.
145 *
146 * @param name
147 * The name of trace
148 * @param trace
149 * The trace resource.
150 * @param parent
151 * The parent element (trace folder)
b544077e 152 */
12c155f5
FC
153 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
154 this(name, trace, (TmfProjectModelElement) parent);
155 }
d04ec5a7 156
b544077e 157 /**
d04ec5a7
MK
158 * Constructor. Creates trace model element under the experiment folder.
159 *
160 * @param name
161 * The name of trace
162 * @param trace
163 * The trace resource.
164 * @param parent
165 * The parent element (experiment folder)
b544077e 166 */
12c155f5
FC
167 public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
168 this(name, trace, (TmfProjectModelElement) parent);
169 }
170
171 private TmfTraceElement(String name, IResource trace, TmfProjectModelElement parent) {
172 super(name, trace, parent);
173 parent.addChild(this);
174 refreshTraceType();
175 }
176
177 // ------------------------------------------------------------------------
178 // Operations
179 // ------------------------------------------------------------------------
b544077e
BH
180 /**
181 * Returns the trace type ID.
d04ec5a7 182 *
b544077e
BH
183 * @return trace type ID.
184 */
12c155f5
FC
185 public String getTraceType() {
186 return fTraceTypeId;
187 }
188
b544077e 189 /**
d04ec5a7
MK
190 * Refreshes the trace type filed by reading the trace type persistent
191 * property of the resource referenece.
b544077e 192 */
12c155f5
FC
193 public void refreshTraceType() {
194 try {
e12ecd30 195 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
12c155f5 196 } catch (CoreException e) {
8fd82db5 197 Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
198 }
199 }
200
b544077e 201 /**
d04ec5a7
MK
202 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
203 * the corresponding extension.
abbdd66a 204 *
b544077e
BH
205 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
206 */
6256d8ad 207 public ITmfTrace instantiateTrace() {
12c155f5 208 try {
e12ecd30
BH
209
210 // make sure that supplementary folder exists
211 refreshSupplementaryFolder();
212
12c155f5 213 if (fTraceTypeId != null) {
4bf17f4a 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 return new CustomTxtTrace(def);
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 return new CustomXmlTrace(def);
225 }
226 }
227 }
12c155f5 228 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
d04ec5a7
MK
229 if (ce == null) {
230 return null;
231 }
6256d8ad 232 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
12c155f5
FC
233 return trace;
234 }
235 } catch (CoreException e) {
8fd82db5 236 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
237 }
238 return null;
239 }
240
b544077e 241 /**
d04ec5a7
MK
242 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
243 * the corresponding extension.
abbdd66a 244 *
b544077e
BH
245 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
246 */
ce2388e0 247 public ITmfEvent instantiateEvent() {
12c155f5
FC
248 try {
249 if (fTraceTypeId != null) {
4bf17f4a 250 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
251 for (CustomTxtTraceDefinition def : CustomTxtTraceDefinition.loadAll()) {
252 if (fTraceTypeId.equals(CustomTxtTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
253 return new CustomTxtEvent(def);
254 }
255 }
256 }
257 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
258 for (CustomXmlTraceDefinition def : CustomXmlTraceDefinition.loadAll()) {
259 if (fTraceTypeId.equals(CustomXmlTrace.class.getCanonicalName() + ":" + def.definitionName)) { //$NON-NLS-1$
260 return new CustomXmlEvent(def);
261 }
262 }
263 }
12c155f5 264 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
d04ec5a7
MK
265 if (ce == null) {
266 return null;
267 }
ce2388e0 268 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
12c155f5
FC
269 return event;
270 }
271 } catch (CoreException e) {
8fd82db5 272 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
12c155f5
FC
273 }
274 return null;
275 }
276
b544077e
BH
277 /**
278 * Returns the optional editor ID from the trace type extension.
d04ec5a7 279 *
b544077e
BH
280 * @return the editor ID or <code>null</code> if not defined.
281 */
12c155f5
FC
282 public String getEditorId() {
283 if (fTraceTypeId != null) {
4bf17f4a 284 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
285 return TmfEventsEditor.ID;
286 }
287 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
288 return TmfEventsEditor.ID;
289 }
12c155f5 290 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 291 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
12c155f5 292 if (defaultEditorCE.length == 1) {
4bf17f4a 293 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
12c155f5
FC
294 }
295 }
296 return null;
297 }
e12ecd30 298
c4c81d91 299 /**
d04ec5a7
MK
300 * Returns the file resource used to store bookmarks after creating it if
301 * necessary. If the trace resource is a file, it is returned directly. If
302 * the trace resource is a folder, a linked file is returned. The file will
303 * be created if it does not exist.
304 *
c4c81d91 305 * @return the bookmarks file
d04ec5a7
MK
306 * @throws CoreException
307 * if the bookmarks file cannot be created
c4c81d91
PT
308 * @since 2.0
309 */
81fe3479
PT
310 public IFile createBookmarksFile() throws CoreException {
311 IFile file = getBookmarksFile();
312 if (fResource instanceof IFolder) {
313 if (!file.exists()) {
314 final IFile bookmarksFile = getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
315 if (!bookmarksFile.exists()) {
316 final InputStream source = new ByteArrayInputStream(new byte[0]);
317 bookmarksFile.create(source, true, null);
318 }
319 bookmarksFile.setHidden(true);
320 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
321 file.setHidden(true);
322 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName());
323 }
324 }
325 return file;
326 }
327
328 /**
d04ec5a7
MK
329 * Returns the file resource used to store bookmarks. The file may not
330 * exist.
331 *
81fe3479
PT
332 * @return the bookmarks file
333 * @since 2.0
334 */
335 public IFile getBookmarksFile() {
c4c81d91
PT
336 IFile file = null;
337 if (fResource instanceof IFile) {
338 file = (IFile) fResource;
339 } else if (fResource instanceof IFolder) {
c4c81d91
PT
340 final IFolder folder = (IFolder) fResource;
341 file = folder.getFile(getName() + '_');
c4c81d91
PT
342 }
343 return file;
344 }
345
e12ecd30 346 /**
d04ec5a7
MK
347 * Returns the <code>TmfTraceElement</code> located under the
348 * <code>TmfTracesFolder</code>.
6256d8ad 349 *
d04ec5a7
MK
350 * @return <code>this</code> if this element is under the
351 * <code>TmfTracesFolder</code> else the corresponding
352 * <code>TmfTraceElement</code> if this element is under
e12ecd30
BH
353 * <code>TmfExperimentElement</code>.
354 */
5e4bf87d 355 public TmfTraceElement getElementUnderTraceFolder() {
e12ecd30 356
d04ec5a7
MK
357 // If trace is under an experiment, return original trace from the
358 // traces folder
5e4bf87d
BH
359 if (getParent() instanceof TmfExperimentElement) {
360 for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
361 if (aTrace.getName().equals(getName())) {
362 return aTrace;
363 }
364 }
365 }
366 return this;
367 }
6256d8ad 368
12c155f5
FC
369 // ------------------------------------------------------------------------
370 // IActionFilter
371 // ------------------------------------------------------------------------
372
373 @Override
374 public boolean testAttribute(Object target, String name, String value) {
375 if (name.equals(IS_LINKED)) {
376 boolean isLinked = getResource().isLinked();
377 return Boolean.toString(isLinked).equals(value);
378 }
379 return false;
380 }
381
382 // ------------------------------------------------------------------------
383 // TmfTraceElement
384 // ------------------------------------------------------------------------
11252342 385
12c155f5
FC
386 @Override
387 public TmfProjectElement getProject() {
388 if (getParent() instanceof TmfTraceFolder) {
389 TmfTraceFolder folder = (TmfTraceFolder) getParent();
390 TmfProjectElement project = (TmfProjectElement) folder.getParent();
391 return project;
392 }
393 if (getParent() instanceof TmfExperimentElement) {
394 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
395 TmfExperimentFolder folder = (TmfExperimentFolder) experiment.getParent();
396 TmfProjectElement project = (TmfProjectElement) folder.getParent();
397 return project;
398 }
399 return null;
400 }
401
402 // ------------------------------------------------------------------------
403 // IPropertySource2
404 // ------------------------------------------------------------------------
405
406 @Override
407 public Object getEditableValue() {
408 return null;
409 }
410
411 @Override
412 public IPropertyDescriptor[] getPropertyDescriptors() {
77fdc5df 413 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
12c155f5
FC
414 }
415
416 @Override
417 public Object getPropertyValue(Object id) {
418
ce2388e0 419 if (sfName.equals(id)) {
12c155f5 420 return getName();
ce2388e0 421 }
12c155f5 422
ce2388e0 423 if (sfPath.equals(id)) {
12c155f5 424 return getPath().toString();
ce2388e0 425 }
12c155f5 426
ce2388e0 427 if (sfLocation.equals(id)) {
12c155f5 428 return getLocation().toString();
ce2388e0 429 }
12c155f5 430
ce2388e0 431 if (sfIsLinked.equals(id)) {
12c155f5 432 return Boolean.valueOf(getResource().isLinked()).toString();
ce2388e0 433 }
12c155f5
FC
434
435 if (sfEventType.equals(id)) {
436 if (fTraceTypeId != null) {
437 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
4bf17f4a 438 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
12c155f5
FC
439 }
440 }
441
442 return null;
443 }
444
abbdd66a 445 private static String getCategory(IConfigurationElement ce) {
4bf17f4a 446 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
12c155f5
FC
447 if (categoryId != null) {
448 IConfigurationElement category = sfTraceCategories.get(categoryId);
4bf17f4a 449 if (category != null) {
450 return category.getAttribute(TmfTraceType.NAME_ATTR);
12c155f5
FC
451 }
452 }
453 return "[no category]"; //$NON-NLS-1$
454 }
455
456 @Override
457 public void resetPropertyValue(Object id) {
458 }
459
460 @Override
461 public void setPropertyValue(Object id, Object value) {
462 }
463
464 @Override
465 public boolean isPropertyResettable(Object id) {
466 return false;
467 }
468
469 @Override
470 public boolean isPropertySet(Object id) {
471 return false;
472 }
473
beb19106
GB
474 /**
475 * Copy this trace in the trace folder. No other parameters are mentioned so
476 * the trace is copied in this element's project trace folder
477 *
478 * @param string
479 * The new trace name
480 * @return the new Resource object
481 * @since 2.0
482 */
483 public TmfTraceElement copy(String string) {
484 TmfTraceFolder folder = this.getProject().getTracesFolder();
485 IResource res = super.copy(string, false);
486 return new TmfTraceElement(string, res, folder);
487 }
488
a72a6830
PT
489 /**
490 * Close opened editors associated with this trace.
491 * @since 2.0
492 */
493 public void closeEditors() {
494 // Close the trace if open
495 IFile file = getBookmarksFile();
496 FileEditorInput input = new FileEditorInput(file);
497 IWorkbench wb = PlatformUI.getWorkbench();
498 for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
499 for (IWorkbenchPage wbPage : wbWindow.getPages()) {
500 for (IEditorReference editorReference : wbPage.getEditorReferences()) {
501 try {
502 if (editorReference.getEditorInput().equals(input)) {
503 wbPage.closeEditor(editorReference.getEditor(false), false);
504 }
505 } catch (PartInitException e) {
506 Activator.getDefault().logError("Error closing editor for trace " + getName(), e); //$NON-NLS-1$
507 }
508 }
509 }
510 }
511
512 // Close experiments that contain the trace if open
513 if (getParent() instanceof TmfTraceFolder) {
514 TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
515 for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
516 for (ITmfProjectModelElement child : experiment.getChildren()) {
517 if (child.getName().equals(getName())) {
518 ((TmfExperimentElement) experiment).closeEditors();
519 break;
520 }
521 }
522 }
523 } else if (getParent() instanceof TmfExperimentElement) {
524 TmfExperimentElement experiment = (TmfExperimentElement) getParent();
525 experiment.closeEditors();
526 }
527 }
12c155f5 528}
This page took 0.07554 seconds and 5 git commands to generate.