tmf: Bug 415707: Trace already opened should not be re-opened
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / OpenExperimentHandler.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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.internal.tmf.ui.project.handlers;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionProvider;
20 import org.eclipse.jface.viewers.TreeSelection;
21 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
22 import org.eclipse.linuxtools.tmf.ui.project.model.TmfOpenTraceHelper;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.PlatformUI;
27
28 /**
29 * <b><u>OpenExperimentHandler</u></b>
30 * <p>
31 */
32 public class OpenExperimentHandler extends AbstractHandler {
33
34 private TmfExperimentElement fExperiment = null;
35
36 // ------------------------------------------------------------------------
37 // Validation
38 // ------------------------------------------------------------------------
39
40 @Override
41 public boolean isEnabled() {
42
43 // Check if we are closing down
44 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
45 if (window == null) {
46 return false;
47 }
48
49 // Get the selection
50 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
51 final IWorkbenchPart part = page.getActivePart();
52 if (part == null) {
53 return false;
54 }
55 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
56 if (selectionProvider == null) {
57 return false;
58 }
59 final ISelection selection = selectionProvider.getSelection();
60
61 // Make sure there is only one selection and that it is an experiment
62 fExperiment = null;
63 if (selection instanceof TreeSelection) {
64 final TreeSelection sel = (TreeSelection) selection;
65 // There should be only one item selected as per the plugin.xml
66 final Object element = sel.getFirstElement();
67 if (element instanceof TmfExperimentElement) {
68 fExperiment = (TmfExperimentElement) element;
69 }
70 }
71
72 // We only enable opening from the Traces folder for now
73 return ((fExperiment != null) && (fExperiment.getTraces().size() > 0));
74 }
75
76 // ------------------------------------------------------------------------
77 // Execution
78 // ------------------------------------------------------------------------
79
80 @Override
81 public Object execute(final ExecutionEvent event) throws ExecutionException {
82
83 // Check if we are closing down
84 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
85 if (window == null) {
86 return null;
87 }
88
89 // Check that the experiment is valid
90 if (fExperiment == null) {
91 return null;
92 }
93
94 TmfOpenTraceHelper.openExperimentFromElement(fExperiment);
95 return null;
96 }
97
98 }
This page took 0.032409 seconds and 5 git commands to generate.