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
.TreeSelection
;
23 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
25 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
26 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalManager
;
27 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
30 import org
.eclipse
.swt
.widgets
.MessageBox
;
31 import org
.eclipse
.ui
.IEditorInput
;
32 import org
.eclipse
.ui
.IEditorPart
;
33 import org
.eclipse
.ui
.IReusableEditor
;
34 import org
.eclipse
.ui
.IWorkbench
;
35 import org
.eclipse
.ui
.IWorkbenchPage
;
36 import org
.eclipse
.ui
.IWorkbenchPart
;
37 import org
.eclipse
.ui
.IWorkbenchWindow
;
38 import org
.eclipse
.ui
.PartInitException
;
39 import org
.eclipse
.ui
.PlatformUI
;
42 * <b><u>OpenTraceHandler</u></b>
44 * TODO: Add support for multiple trace selection
46 public class OpenTraceHandler
extends AbstractHandler
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private TmfTraceElement fTrace
= null;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
59 public boolean isEnabled() {
61 // Check if we are closing down
62 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
67 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
68 IWorkbenchPart part
= page
.getActivePart();
69 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
71 // Make sure there is only one selection and that it is a trace
73 if (selection
instanceof TreeSelection
) {
74 TreeSelection sel
= (TreeSelection
) selection
;
75 // There should be only one item selected as per the plugin.xml
76 Object element
= sel
.getFirstElement();
77 if (element
instanceof TmfTraceElement
) {
78 fTrace
= (TmfTraceElement
) element
;
82 // We only enable opening from the Traces folder for now
83 return (fTrace
!= null);
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 @SuppressWarnings({ "rawtypes", "unchecked" })
92 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
94 // Check if we are closing down
95 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
99 // Check that the trace is valid
100 if (fTrace
== null) {
104 TmfEvent traceEvent
= fTrace
.instantiateEvent();
105 ITmfTrace trace
= fTrace
.instantiateTrace();
107 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
111 // Get the editor_id from the extension point
112 String editorId
= fTrace
.getEditorId();
113 boolean usesEditor
= editorId
!= null && editorId
.length() > 0;
116 trace
.initTrace(fTrace
.getLocation().getPath(), traceEvent
.getClass(), usesEditor
);
117 } catch (FileNotFoundException e
) {
118 displayErrorMsg(Messages
.OpenTraceHandler_NoTrace
);
124 IResource resource
= fTrace
.getResource();
125 IEditorInput editorInput
= new TmfEditorInput(resource
, trace
);
126 IWorkbench wb
= PlatformUI
.getWorkbench();
127 IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
129 IEditorPart editor
= activePage
.findEditor(editorInput
);
130 if (editor
!= null && editor
instanceof IReusableEditor
) {
131 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
132 activePage
.activate(editor
);
134 editor
= activePage
.openEditor(editorInput
, editorId
);
136 } catch (PartInitException e
) {
141 ITmfTrace
[] traces
= new ITmfTrace
[] { trace
};
142 TmfExperiment experiment
= new TmfExperiment(traceEvent
.getClass(), fTrace
.getName(), traces
,
143 trace
.getCacheSize());
144 TmfExperiment
.setCurrentExperiment(experiment
);
145 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
150 private void displayErrorMsg(String errorMsg
) {
151 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
152 mb
.setText(Messages
.OpenTraceHandler_Title
);
153 mb
.setMessage(errorMsg
);