1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 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();
73 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
74 if (selectionProvider
== null) {
77 final ISelection selection
= selectionProvider
.getSelection();
79 // Make sure there is only one selection and that it is a trace
81 if (selection
instanceof TreeSelection
) {
82 final TreeSelection sel
= (TreeSelection
) selection
;
83 // There should be only one item selected as per the plugin.xml
84 final Object element
= sel
.getFirstElement();
85 if (element
instanceof TmfTraceElement
) {
86 fTrace
= (TmfTraceElement
) element
;
90 // We only enable opening from the Traces folder for now
91 return (fTrace
!= null);
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
101 // Check if we are closing down
102 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
103 if (window
== null) {
107 // Check that the trace is valid
108 if (fTrace
== null) {
112 // If trace is under an experiment, use the original trace from the traces folder
113 final TmfTraceElement traceElement
= fTrace
.getElementUnderTraceFolder();
115 Thread thread
= new Thread() {
119 final ITmfTrace trace
= traceElement
.instantiateTrace();
120 final ITmfEvent traceEvent
= traceElement
.instantiateEvent();
121 if ((trace
== null) || (traceEvent
== null)) {
122 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
129 // Get the editor_id from the extension point
130 String traceEditorId
= traceElement
.getEditorId();
131 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
134 trace
.initTrace(traceElement
.getResource(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
135 } catch (final TmfTraceException e
) {
136 displayErrorMsg(Messages
.OpenTraceHandler_InitError
+ "\n\n" + e
); //$NON-NLS-1$
143 file
= traceElement
.getBookmarksFile();
144 } catch (final CoreException e
) {
145 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
146 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
151 Display
.getDefault().asyncExec(new Runnable() {
155 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
156 final IWorkbench wb
= PlatformUI
.getWorkbench();
157 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
159 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
160 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
161 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
162 activePage
.activate(editor
);
164 activePage
.openEditor(editorInput
, editorId
);
165 IDE
.setDefaultEditor(file
, editorId
);
166 // editor should dispose the trace on close
168 } catch (final PartInitException e
) {
169 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
170 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
183 private static void displayErrorMsg(final String errorMsg
) {
184 Display
.getDefault().asyncExec(new Runnable() {
187 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
188 mb
.setText(Messages
.OpenTraceHandler_Title
);
189 mb
.setMessage(errorMsg
);