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 java
.util
.List
;
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
.IFile
;
21 import org
.eclipse
.core
.runtime
.CoreException
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfExperiment
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
31 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
32 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
33 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
34 import org
.eclipse
.swt
.widgets
.Display
;
35 import org
.eclipse
.swt
.widgets
.MessageBox
;
36 import org
.eclipse
.ui
.IEditorInput
;
37 import org
.eclipse
.ui
.IEditorPart
;
38 import org
.eclipse
.ui
.IReusableEditor
;
39 import org
.eclipse
.ui
.IWorkbench
;
40 import org
.eclipse
.ui
.IWorkbenchPage
;
41 import org
.eclipse
.ui
.IWorkbenchPart
;
42 import org
.eclipse
.ui
.IWorkbenchWindow
;
43 import org
.eclipse
.ui
.PlatformUI
;
44 import org
.eclipse
.ui
.ide
.IDE
;
45 import org
.eclipse
.ui
.part
.FileEditorInput
;
48 * <b><u>OpenExperimentHandler</u></b>
51 public class OpenExperimentHandler
extends AbstractHandler
{
53 private TmfExperimentElement fExperiment
= null;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 public boolean isEnabled() {
62 // Check if we are closing down
63 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
69 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
70 final IWorkbenchPart part
= page
.getActivePart();
74 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
75 if (selectionProvider
== null) {
78 final ISelection selection
= selectionProvider
.getSelection();
80 // Make sure there is only one selection and that it is an experiment
82 if (selection
instanceof TreeSelection
) {
83 final TreeSelection sel
= (TreeSelection
) selection
;
84 // There should be only one item selected as per the plugin.xml
85 final Object element
= sel
.getFirstElement();
86 if (element
instanceof TmfExperimentElement
) {
87 fExperiment
= (TmfExperimentElement
) element
;
91 // We only enable opening from the Traces folder for now
92 return (fExperiment
!= null);
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
102 // Check if we are closing down
103 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
104 if (window
== null) {
108 final TmfExperimentElement experimentElement
= fExperiment
;
110 Thread thread
= new Thread() {
116 file
= experimentElement
.createBookmarksFile();
117 } catch (final CoreException e
) {
118 Activator
.getDefault().logError("Error opening experiment " + experimentElement
.getName(), e
); //$NON-NLS-1$
119 displayErrorMsg(Messages
.OpenExperimentHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
123 /* Unlike traces, there is no instanceExperiment, so we call this function
124 * here alone. Maybe it would be better to do this on experiment's element
127 experimentElement
.refreshSupplementaryFolder();
129 // Instantiate the experiment's traces
130 final List
<TmfTraceElement
> traceEntries
= experimentElement
.getTraces();
131 final int nbTraces
= traceEntries
.size();
132 int cacheSize
= Integer
.MAX_VALUE
;
133 String commonEditorId
= null;
134 final ITmfTrace
[] traces
= new ITmfTrace
[nbTraces
];
135 for (int i
= 0; i
< nbTraces
; i
++) {
136 TmfTraceElement element
= traceEntries
.get(i
);
138 // Since trace is under an experiment, use the original trace from the traces folder
139 element
= element
.getElementUnderTraceFolder();
141 final ITmfTrace trace
= element
.instantiateTrace();
142 final ITmfEvent traceEvent
= element
.instantiateEvent();
143 if ((trace
== null) || (traceEvent
== null)) {
144 displayErrorMsg(Messages
.OpenExperimentHandler_NoTraceType
);
145 for (int j
= 0; j
< i
; j
++) {
154 trace
.initTrace(element
.getResource(), element
.getLocation().getPath(), traceEvent
.getClass());
155 } catch (final TmfTraceException e
) {
156 displayErrorMsg(Messages
.OpenTraceHandler_InitError
+ "\n\n" + e
); //$NON-NLS-1$
157 for (int j
= 0; j
< i
; j
++) {
163 cacheSize
= Math
.min(cacheSize
, trace
.getCacheSize());
165 // If all traces use the same editorId, use it, otherwise use the default
166 final String editorId
= element
.getEditorId();
167 if (commonEditorId
== null) {
168 commonEditorId
= (editorId
!= null) ? editorId
: TmfEventsEditor
.ID
;
169 } else if (!commonEditorId
.equals(editorId
)) {
170 commonEditorId
= TmfEventsEditor
.ID
;
175 // Create the experiment
176 final TmfExperiment experiment
= new TmfExperiment(ITmfEvent
.class, experimentElement
.getName(), traces
, cacheSize
, experimentElement
.getResource());
177 experiment
.setBookmarksFile(file
);
179 final String editorId
= commonEditorId
;
180 Display
.getDefault().asyncExec(new Runnable() {
184 final IEditorInput editorInput
= new TmfEditorInput(file
, experiment
);
185 final IWorkbench wb
= PlatformUI
.getWorkbench();
186 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
188 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
189 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
190 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
191 activePage
.activate(editor
);
193 activePage
.openEditor(editorInput
, editorId
);
195 IDE
.setDefaultEditor(file
, editorId
);
196 // editor should dispose the experiment on close
197 } catch (final CoreException e
) {
198 Activator
.getDefault().logError("Error opening experiment " + experimentElement
.getName(), e
); //$NON-NLS-1$
199 displayErrorMsg(Messages
.OpenExperimentHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
200 experiment
.dispose();
213 private static void displayErrorMsg(final String errorMsg
) {
214 Display
.getDefault().asyncExec(new Runnable() {
217 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
218 mb
.setText(Messages
.OpenExperimentHandler_Title
);
219 mb
.setMessage(errorMsg
);