1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import org
.eclipse
.core
.commands
.AbstractHandler
;
16 import org
.eclipse
.core
.commands
.ExecutionEvent
;
17 import org
.eclipse
.core
.commands
.ExecutionException
;
18 import org
.eclipse
.core
.resources
.IFile
;
19 import org
.eclipse
.core
.runtime
.CoreException
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
27 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.MessageBox
;
32 import org
.eclipse
.ui
.IEditorInput
;
33 import org
.eclipse
.ui
.IEditorPart
;
34 import org
.eclipse
.ui
.IReusableEditor
;
35 import org
.eclipse
.ui
.IWorkbench
;
36 import org
.eclipse
.ui
.IWorkbenchPage
;
37 import org
.eclipse
.ui
.IWorkbenchPart
;
38 import org
.eclipse
.ui
.IWorkbenchWindow
;
39 import org
.eclipse
.ui
.PartInitException
;
40 import org
.eclipse
.ui
.PlatformUI
;
41 import org
.eclipse
.ui
.ide
.IDE
;
42 import org
.eclipse
.ui
.part
.FileEditorInput
;
45 * <b><u>OpenTraceHandler</u></b>
47 * TODO: Add support for multiple trace selection
49 public class OpenTraceHandler
extends AbstractHandler
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private TmfTraceElement fTrace
= null;
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public boolean isEnabled() {
64 // Check if we are closing down
65 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
71 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
72 final IWorkbenchPart part
= page
.getActivePart();
76 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
77 if (selectionProvider
== null) {
80 final ISelection selection
= selectionProvider
.getSelection();
82 // Make sure there is only one selection and that it is a trace
84 if (selection
instanceof TreeSelection
) {
85 final TreeSelection sel
= (TreeSelection
) selection
;
86 // There should be only one item selected as per the plugin.xml
87 final Object element
= sel
.getFirstElement();
88 if (element
instanceof TmfTraceElement
) {
89 fTrace
= (TmfTraceElement
) element
;
93 // We only enable opening from the Traces folder for now
94 return (fTrace
!= null);
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
104 // Check if we are closing down
105 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
106 if (window
== null) {
110 // Check that the trace is valid
111 if (fTrace
== null) {
115 // If trace is under an experiment, use the original trace from the traces folder
116 final TmfTraceElement traceElement
= fTrace
.getElementUnderTraceFolder();
118 Thread thread
= new Thread() {
122 final ITmfTrace trace
= traceElement
.instantiateTrace();
123 final ITmfEvent traceEvent
= traceElement
.instantiateEvent();
124 if ((trace
== null) || (traceEvent
== null)) {
125 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
132 // Get the editor_id from the extension point
133 String traceEditorId
= traceElement
.getEditorId();
134 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
137 trace
.initTrace(traceElement
.getResource(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
138 } catch (final TmfTraceException e
) {
139 displayErrorMsg(Messages
.OpenTraceHandler_InitError
+ "\n\n" + e
); //$NON-NLS-1$
146 file
= traceElement
.createBookmarksFile();
147 } catch (final CoreException e
) {
148 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
149 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
154 Display
.getDefault().asyncExec(new Runnable() {
158 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
159 final IWorkbench wb
= PlatformUI
.getWorkbench();
160 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
162 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
163 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
164 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
165 activePage
.activate(editor
);
167 activePage
.openEditor(editorInput
, editorId
);
168 IDE
.setDefaultEditor(file
, editorId
);
169 // editor should dispose the trace on close
171 } catch (final PartInitException e
) {
172 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
173 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
186 private static void displayErrorMsg(final String errorMsg
) {
187 Display
.getDefault().asyncExec(new Runnable() {
190 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
191 mb
.setText(Messages
.OpenTraceHandler_Title
);
192 mb
.setMessage(errorMsg
);