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
;
16 import java
.util
.List
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
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
.project
.model
.TmfExperimentElement
;
30 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
31 import org
.eclipse
.swt
.widgets
.MessageBox
;
32 import org
.eclipse
.ui
.IWorkbenchPage
;
33 import org
.eclipse
.ui
.IWorkbenchPart
;
34 import org
.eclipse
.ui
.IWorkbenchWindow
;
35 import org
.eclipse
.ui
.PlatformUI
;
38 * <b><u>OpenExperimentHandler</u></b>
41 public class OpenExperimentHandler
extends AbstractHandler
{
43 private TmfExperimentElement fExperiment
= null;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 public boolean isEnabled() {
52 // Check if we are closing down
53 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
58 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
59 IWorkbenchPart part
= page
.getActivePart();
60 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
61 if (selectionProvider
== null)
63 ISelection selection
= selectionProvider
.getSelection();
65 // Make sure there is only one selection and that it is an experiment
67 if (selection
instanceof TreeSelection
) {
68 TreeSelection sel
= (TreeSelection
) selection
;
69 // There should be only one item selected as per the plugin.xml
70 Object element
= sel
.getFirstElement();
71 if (element
instanceof TmfExperimentElement
) {
72 fExperiment
= (TmfExperimentElement
) element
;
76 // We only enable opening from the Traces folder for now
77 return (fExperiment
!= null);
80 // ------------------------------------------------------------------------
82 // ------------------------------------------------------------------------
84 @SuppressWarnings({ "rawtypes", "unchecked" })
86 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
88 // Check if we are closing down
89 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
93 // Close the current experiment, if any
94 TmfExperiment
<?
> currentExperiment
= TmfExperiment
.getCurrentExperiment();
95 if (currentExperiment
!= null) {
96 currentExperiment
.dispose();
99 // Instantiate the experiment's traces
100 List
<TmfTraceElement
> traceEntries
= fExperiment
.getTraces();
101 int nbTraces
= traceEntries
.size();
102 ITmfTrace
<?
>[] traces
= new ITmfTrace
[nbTraces
];
103 for (int i
= 0; i
< nbTraces
; i
++) {
104 TmfTraceElement element
= traceEntries
.get(i
);
105 ITmfTrace trace
= element
.instantiateTrace();
106 TmfEvent traceEvent
= element
.instantiateEvent();
108 trace
.initTrace(element
.getLocation().getPath(), traceEvent
.getClass());
109 } catch (FileNotFoundException e
) {
110 displayErrorMsg(""); //$NON-NLS-1$
115 // Create the experiment and signal
116 TmfExperiment experiment
= new TmfExperiment(traces
[0].getClass(), fExperiment
.getName(), traces
, traces
[0].getCacheSize());
117 TmfExperiment
.setCurrentExperiment(experiment
);
118 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
123 private void displayErrorMsg(String errorMsg
) {
124 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
125 mb
.setText(Messages
.OpenTraceHandler_Title
);
126 mb
.setMessage(errorMsg
);