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
.tmf
.ui
.project
.handlers
;
15 import java
.io
.FileNotFoundException
;
17 import org
.eclipse
.core
.commands
.AbstractHandler
;
18 import org
.eclipse
.core
.commands
.ExecutionEvent
;
19 import org
.eclipse
.core
.commands
.ExecutionException
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.jface
.viewers
.ISelection
;
22 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
23 import org
.eclipse
.jface
.viewers
.TreeSelection
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperiment
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
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
;
43 * <b><u>OpenTraceHandler</u></b>
45 * TODO: Add support for multiple trace selection
47 public class OpenTraceHandler
extends AbstractHandler
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 private TmfTraceElement fTrace
= null;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 public boolean isEnabled() {
62 // Check if we are closing down
63 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
68 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
69 IWorkbenchPart part
= page
.getActivePart();
70 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
71 if (selectionProvider
== null)
73 ISelection selection
= selectionProvider
.getSelection();
75 // Make sure there is only one selection and that it is a trace
77 if (selection
instanceof TreeSelection
) {
78 TreeSelection sel
= (TreeSelection
) selection
;
79 // There should be only one item selected as per the plugin.xml
80 Object element
= sel
.getFirstElement();
81 if (element
instanceof TmfTraceElement
) {
82 fTrace
= (TmfTraceElement
) element
;
86 // We only enable opening from the Traces folder for now
87 return (fTrace
!= null);
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 @SuppressWarnings({ "rawtypes", "unchecked" })
96 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
98 // Check if we are closing down
99 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
103 // Check that the trace is valid
104 if (fTrace
== null) {
108 TmfEvent traceEvent
= fTrace
.instantiateEvent();
109 ITmfTrace trace
= fTrace
.instantiateTrace();
111 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
115 // Get the editor_id from the extension point
116 String editorId
= fTrace
.getEditorId();
117 boolean usesEditor
= editorId
!= null && editorId
.length() > 0;
120 trace
.initTrace(fTrace
.getLocation().getPath(), traceEvent
.getClass(), usesEditor
);
121 } catch (FileNotFoundException e
) {
122 displayErrorMsg(Messages
.OpenTraceHandler_NoTrace
);
128 IResource resource
= fTrace
.getResource();
129 IEditorInput editorInput
= new TmfEditorInput(resource
, trace
);
130 IWorkbench wb
= PlatformUI
.getWorkbench();
131 IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
133 IEditorPart editor
= activePage
.findEditor(editorInput
);
134 if (editor
!= null && editor
instanceof IReusableEditor
) {
135 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
136 activePage
.activate(editor
);
138 editor
= activePage
.openEditor(editorInput
, editorId
);
140 } catch (PartInitException e
) {
145 // Close the current experiment, if any
146 TmfExperiment
<?
> currentExperiment
= TmfExperiment
.getCurrentExperiment();
147 if (currentExperiment
!= null) {
148 currentExperiment
.dispose();
151 // Open the new experiment and broadcast
152 ITmfTrace
[] traces
= new ITmfTrace
[] { trace
};
153 TmfExperiment experiment
= new TmfExperiment(traceEvent
.getClass(), fTrace
.getName(), traces
, trace
.getCacheSize());
154 TmfExperiment
.setCurrentExperiment(experiment
);
155 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
160 private void displayErrorMsg(String errorMsg
) {
161 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
162 mb
.setText(Messages
.OpenTraceHandler_Title
);
163 mb
.setMessage(errorMsg
);