| 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.TmfOpenTraceHelper; |
| 22 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
| 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>OpenTraceHandler</u></b> |
| 30 | * <p> |
| 31 | * TODO: Add support for multiple trace selection |
| 32 | */ |
| 33 | public class OpenTraceHandler extends AbstractHandler { |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | // Attributes |
| 37 | // ------------------------------------------------------------------------ |
| 38 | |
| 39 | private TmfTraceElement fTrace = null; |
| 40 | |
| 41 | // ------------------------------------------------------------------------ |
| 42 | // Validation |
| 43 | // ------------------------------------------------------------------------ |
| 44 | |
| 45 | @Override |
| 46 | public boolean isEnabled() { |
| 47 | |
| 48 | // Check if we are closing down |
| 49 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 50 | if (window == null) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // Get the selection |
| 55 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 56 | final IWorkbenchPart part = page.getActivePart(); |
| 57 | if (part == null) { |
| 58 | return false; |
| 59 | } |
| 60 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
| 61 | if (selectionProvider == null) { |
| 62 | return false; |
| 63 | } |
| 64 | final ISelection selection = selectionProvider.getSelection(); |
| 65 | |
| 66 | // Make sure there is only one selection and that it is a trace |
| 67 | fTrace = null; |
| 68 | if (selection instanceof TreeSelection) { |
| 69 | final TreeSelection sel = (TreeSelection) selection; |
| 70 | // There should be only one item selected as per the plugin.xml |
| 71 | final Object element = sel.getFirstElement(); |
| 72 | if (element instanceof TmfTraceElement) { |
| 73 | fTrace = (TmfTraceElement) element; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // We only enable opening from the Traces folder for now |
| 78 | return (fTrace != null); |
| 79 | } |
| 80 | |
| 81 | // ------------------------------------------------------------------------ |
| 82 | // Execution |
| 83 | // ------------------------------------------------------------------------ |
| 84 | |
| 85 | @Override |
| 86 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
| 87 | |
| 88 | // Check if we are closing down |
| 89 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
| 90 | if (window == null) { |
| 91 | return null; |
| 92 | } |
| 93 | |
| 94 | // Check that the trace is valid |
| 95 | if (fTrace == null) { |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | // If trace is under an experiment, use the original trace from the traces folder |
| 100 | TmfOpenTraceHelper.openTraceFromElement(fTrace.getElementUnderTraceFolder()); |
| 101 | return null; |
| 102 | } |
| 103 | |
| 104 | } |