Close editors to release supplementary file resources
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 Ericsson, École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Added supplementary files handling
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
15 *******************************************************************************/
16
17 package org.eclipse.linuxtools.tmf.ui.project.model;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.util.Arrays;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IFolder;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IConfigurationElement;
30 import org.eclipse.core.runtime.Platform;
31 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
32 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtEvent;
33 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTrace;
34 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomTxtTraceDefinition;
35 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlEvent;
36 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTrace;
37 import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefinition;
38 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
39 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
40 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
41 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
42 import org.eclipse.linuxtools.tmf.core.util.ReadOnlyTextPropertyDescriptor;
43 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
44 import org.eclipse.ui.IActionFilter;
45 import org.eclipse.ui.IEditorReference;
46 import org.eclipse.ui.IWorkbench;
47 import org.eclipse.ui.IWorkbenchPage;
48 import org.eclipse.ui.IWorkbenchWindow;
49 import org.eclipse.ui.PartInitException;
50 import org.eclipse.ui.PlatformUI;
51 import org.eclipse.ui.part.FileEditorInput;
52 import org.eclipse.ui.views.properties.IPropertyDescriptor;
53 import org.eclipse.ui.views.properties.IPropertySource2;
54
55 /**
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.
59 *
60 * @version 1.0
61 * @author Francois Chouinard
62 */
63 public class TmfTraceElement extends TmfWithFolderElement implements IActionFilter, IPropertySource2 {
64
65 // ------------------------------------------------------------------------
66 // Constants
67 // ------------------------------------------------------------------------
68
69 // Other attributes
70 /**
71 * Bundle attribute name
72 */
73 public static final String BUNDLE = "bundle"; //$NON-NLS-1$
74 /**
75 * IsLinked attribute name.
76 */
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
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);
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 }
103
104 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
105
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
117 // The mapping of available trace type IDs to their corresponding
118 // configuration element
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
122 /**
123 * Initialize statically at startup by getting extensions from the platform
124 * extension registry.
125 */
126 public static void init() {
127 IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
128 for (IConfigurationElement ce : config) {
129 String elementName = ce.getName();
130 if (elementName.equals(TmfTraceType.TYPE_ELEM)) {
131 String traceTypeId = ce.getAttribute(TmfTraceType.ID_ATTR);
132 sfTraceTypeAttributes.put(traceTypeId, ce);
133 } else if (elementName.equals(TmfTraceType.CATEGORY_ELEM)) {
134 String categoryId = ce.getAttribute(TmfTraceType.ID_ATTR);
135 sfTraceCategories.put(categoryId, ce);
136 }
137 }
138 }
139
140 // ------------------------------------------------------------------------
141 // Constructors
142 // ------------------------------------------------------------------------
143 /**
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)
152 */
153 public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
154 this(name, trace, (TmfProjectModelElement) parent);
155 }
156
157 /**
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)
166 */
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 // ------------------------------------------------------------------------
180 /**
181 * Returns the trace type ID.
182 *
183 * @return trace type ID.
184 */
185 public String getTraceType() {
186 return fTraceTypeId;
187 }
188
189 /**
190 * Refreshes the trace type filed by reading the trace type persistent
191 * property of the resource referenece.
192 */
193 public void refreshTraceType() {
194 try {
195 fTraceTypeId = getResource().getPersistentProperty(TmfCommonConstants.TRACETYPE);
196 } catch (CoreException e) {
197 Activator.getDefault().logError("Error refreshing trace type pesistent property for trace " + getName(), e); //$NON-NLS-1$
198 }
199 }
200
201 /**
202 * Instantiate a <code>ITmfTrace</code> object based on the trace type and
203 * the corresponding extension.
204 *
205 * @return the <code>ITmfTrace</code> or <code>null</code> for an error
206 */
207 public ITmfTrace instantiateTrace() {
208 try {
209
210 // make sure that supplementary folder exists
211 refreshSupplementaryFolder();
212
213 if (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 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 }
228 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
229 if (ce == null) {
230 return null;
231 }
232 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
233 return trace;
234 }
235 } catch (CoreException e) {
236 Activator.getDefault().logError("Error instantiating ITmfTrace object for trace " + getName(), e); //$NON-NLS-1$
237 }
238 return null;
239 }
240
241 /**
242 * Instantiate a <code>ITmfEvent</code> object based on the trace type and
243 * the corresponding extension.
244 *
245 * @return the <code>ITmfEvent</code> or <code>null</code> for an error
246 */
247 public ITmfEvent instantiateEvent() {
248 try {
249 if (fTraceTypeId != null) {
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 }
264 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
265 if (ce == null) {
266 return null;
267 }
268 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
269 return event;
270 }
271 } catch (CoreException e) {
272 Activator.getDefault().logError("Error instantiating ITmfEvent object for trace " + getName(), e); //$NON-NLS-1$
273 }
274 return null;
275 }
276
277 /**
278 * Returns the optional editor ID from the trace type extension.
279 *
280 * @return the editor ID or <code>null</code> if not defined.
281 */
282 public String getEditorId() {
283 if (fTraceTypeId != null) {
284 if (fTraceTypeId.startsWith(CustomTxtTrace.class.getCanonicalName())) {
285 return TmfEventsEditor.ID;
286 }
287 if (fTraceTypeId.startsWith(CustomXmlTrace.class.getCanonicalName())) {
288 return TmfEventsEditor.ID;
289 }
290 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
291 IConfigurationElement[] defaultEditorCE = ce.getChildren(TmfTraceType.DEFAULT_EDITOR_ELEM);
292 if (defaultEditorCE.length == 1) {
293 return defaultEditorCE[0].getAttribute(TmfTraceType.ID_ATTR);
294 }
295 }
296 return null;
297 }
298
299 /**
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 *
305 * @return the bookmarks file
306 * @throws CoreException
307 * if the bookmarks file cannot be created
308 * @since 2.0
309 */
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 /**
329 * Returns the file resource used to store bookmarks. The file may not
330 * exist.
331 *
332 * @return the bookmarks file
333 * @since 2.0
334 */
335 public IFile getBookmarksFile() {
336 IFile file = null;
337 if (fResource instanceof IFile) {
338 file = (IFile) fResource;
339 } else if (fResource instanceof IFolder) {
340 final IFolder folder = (IFolder) fResource;
341 file = folder.getFile(getName() + '_');
342 }
343 return file;
344 }
345
346 /**
347 * Returns the <code>TmfTraceElement</code> located under the
348 * <code>TmfTracesFolder</code>.
349 *
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
353 * <code>TmfExperimentElement</code>.
354 */
355 public TmfTraceElement getElementUnderTraceFolder() {
356
357 // If trace is under an experiment, return original trace from the
358 // traces folder
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 }
368
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 // ------------------------------------------------------------------------
385
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() {
413 return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
414 }
415
416 @Override
417 public Object getPropertyValue(Object id) {
418
419 if (sfName.equals(id)) {
420 return getName();
421 }
422
423 if (sfPath.equals(id)) {
424 return getPath().toString();
425 }
426
427 if (sfLocation.equals(id)) {
428 return getLocation().toString();
429 }
430
431 if (sfIsLinked.equals(id)) {
432 return Boolean.valueOf(getResource().isLinked()).toString();
433 }
434
435 if (sfEventType.equals(id)) {
436 if (fTraceTypeId != null) {
437 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
438 return (ce != null) ? (getCategory(ce) + " : " + ce.getAttribute(TmfTraceType.NAME_ATTR)) : ""; //$NON-NLS-1$ //$NON-NLS-2$
439 }
440 }
441
442 return null;
443 }
444
445 private static String getCategory(IConfigurationElement ce) {
446 String categoryId = ce.getAttribute(TmfTraceType.CATEGORY_ATTR);
447 if (categoryId != null) {
448 IConfigurationElement category = sfTraceCategories.get(categoryId);
449 if (category != null) {
450 return category.getAttribute(TmfTraceType.NAME_ATTR);
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
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
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 }
528 }
This page took 0.04701 seconds and 6 git commands to generate.