| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010, 2011 Ericsson |
| 3 | * |
| 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 |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.project.handlers; |
| 14 | |
| 15 | import java.io.FileNotFoundException; |
| 16 | import java.util.List; |
| 17 | |
| 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; |
| 36 | |
| 37 | /** |
| 38 | * <b><u>OpenExperimentHandler</u></b> |
| 39 | * <p> |
| 40 | */ |
| 41 | public class OpenExperimentHandler extends AbstractHandler { |
| 42 | |
| 43 | private TmfExperimentElement fExperiment = null; |
| 44 | |
| 45 | // ------------------------------------------------------------------------ |
| 46 | // Validation |
| 47 | // ------------------------------------------------------------------------ |
| 48 | |
| 49 | @Override |
| 50 | public boolean isEnabled() { |
| 51 | |
| 52 | // Check if we are closing down |
| 53 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 54 | if (window == null) |
| 55 | return false; |
| 56 | |
| 57 | // Get the selection |
| 58 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 59 | IWorkbenchPart part = page.getActivePart(); |
| 60 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
| 61 | if (selectionProvider == null) |
| 62 | return false; |
| 63 | ISelection selection = selectionProvider.getSelection(); |
| 64 | |
| 65 | // Make sure there is only one selection and that it is an experiment |
| 66 | fExperiment = null; |
| 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; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // We only enable opening from the Traces folder for now |
| 77 | return (fExperiment != null); |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | // Execution |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 85 | @Override |
| 86 | public Object execute(ExecutionEvent event) throws ExecutionException { |
| 87 | |
| 88 | // Check if we are closing down |
| 89 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 90 | if (window == null) |
| 91 | return false; |
| 92 | |
| 93 | // Close the current experiment, if any |
| 94 | TmfExperiment<?> currentExperiment = TmfExperiment.getCurrentExperiment(); |
| 95 | if (currentExperiment != null) { |
| 96 | currentExperiment.dispose(); |
| 97 | } |
| 98 | |
| 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(); |
| 107 | try { |
| 108 | trace.initTrace(element.getLocation().getPath(), traceEvent.getClass()); |
| 109 | } catch (FileNotFoundException e) { |
| 110 | displayErrorMsg(""); //$NON-NLS-1$ |
| 111 | } |
| 112 | traces[i] = trace; |
| 113 | } |
| 114 | |
| 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)); |
| 119 | |
| 120 | return null; |
| 121 | } |
| 122 | |
| 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); |
| 127 | mb.open(); |
| 128 | } |
| 129 | |
| 130 | } |