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
.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
.project
.model
.TmfExperimentElement
;
29 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
30 import org
.eclipse
.swt
.widgets
.MessageBox
;
31 import org
.eclipse
.ui
.IWorkbenchPage
;
32 import org
.eclipse
.ui
.IWorkbenchPart
;
33 import org
.eclipse
.ui
.IWorkbenchWindow
;
34 import org
.eclipse
.ui
.PlatformUI
;
37 * <b><u>OpenExperimentHandler</u></b>
39 * TODO: Implement me. Please.
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 ISelection selection
= part
.getSite().getSelectionProvider().getSelection();
62 // Make sure there is only one selection and that it is an experiment
64 if (selection
instanceof TreeSelection
) {
65 TreeSelection sel
= (TreeSelection
) selection
;
66 // There should be only one item selected as per the plugin.xml
67 Object element
= sel
.getFirstElement();
68 if (element
instanceof TmfExperimentElement
) {
69 fExperiment
= (TmfExperimentElement
) element
;
73 // We only enable opening from the Traces folder for now
74 return (fExperiment
!= null);
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 @SuppressWarnings({ "rawtypes", "unchecked" })
83 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
85 // Check if we are closing down
86 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
90 // Close the current experiment, if any
91 TmfExperiment
<?
> currentExperiment
= TmfExperiment
.getCurrentExperiment();
92 if (currentExperiment
!= null) {
93 currentExperiment
.dispose();
96 // Instantiate the experiment's traces
97 List
<TmfTraceElement
> traceEntries
= fExperiment
.getTraces();
98 int nbTraces
= traceEntries
.size();
99 ITmfTrace
<?
>[] traces
= new ITmfTrace
[nbTraces
];
100 for (int i
= 0; i
< nbTraces
; i
++) {
101 TmfTraceElement element
= traceEntries
.get(i
);
102 ITmfTrace trace
= element
.instantiateTrace();
103 TmfEvent traceEvent
= element
.instantiateEvent();
105 trace
.initTrace(element
.getLocation().getPath(), traceEvent
.getClass());
106 } catch (FileNotFoundException e
) {
107 displayErrorMsg(""); //$NON-NLS-1$
112 // Create the experiment and signal
113 TmfExperiment experiment
= new TmfExperiment(traces
[0].getClass(), fExperiment
.getName(), traces
, traces
[0].getCacheSize());
114 TmfExperiment
.setCurrentExperiment(experiment
);
115 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
120 private void displayErrorMsg(String errorMsg
) {
121 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
122 mb
.setText(Messages
.OpenTraceHandler_Title
);
123 mb
.setMessage(errorMsg
);