| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2014 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 | * Patrick Tasse - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.internal.tmf.ui.project.handlers; |
| 14 | |
| 15 | import org.eclipse.core.commands.ExecutionException; |
| 16 | import org.eclipse.core.commands.NotEnabledException; |
| 17 | import org.eclipse.core.commands.NotHandledException; |
| 18 | import org.eclipse.core.commands.common.NotDefinedException; |
| 19 | import org.eclipse.jface.action.Action; |
| 20 | import org.eclipse.jface.viewers.ISelection; |
| 21 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 22 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 23 | import org.eclipse.tracecompass.internal.tmf.ui.Activator; |
| 24 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfAnalysisOutputElement; |
| 25 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfExperimentElement; |
| 26 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfOnDemandAnalysisElement; |
| 27 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfProjectModelElement; |
| 28 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfReportElement; |
| 29 | import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceElement; |
| 30 | import org.eclipse.ui.IWorkbenchPage; |
| 31 | import org.eclipse.ui.handlers.IHandlerService; |
| 32 | |
| 33 | /** |
| 34 | * <b><u>OpenAction</u></b> |
| 35 | */ |
| 36 | public class OpenAction extends Action { |
| 37 | |
| 38 | private static final String OPEN_COMMAND_ID = "org.eclipse.ui.navigate.openResource"; //$NON-NLS-1$ |
| 39 | |
| 40 | private final IWorkbenchPage page; |
| 41 | private final ISelectionProvider selectionProvider; |
| 42 | private TmfProjectModelElement element; |
| 43 | |
| 44 | /** |
| 45 | * Default constructor |
| 46 | * @param page the workbench page |
| 47 | * @param selectionProvider the selection provider |
| 48 | */ |
| 49 | public OpenAction(IWorkbenchPage page, ISelectionProvider selectionProvider) { |
| 50 | this.page = page; |
| 51 | this.selectionProvider = selectionProvider; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean isEnabled() { |
| 56 | ISelection selection = selectionProvider.getSelection(); |
| 57 | if (!selection.isEmpty()) { |
| 58 | IStructuredSelection sSelection = (IStructuredSelection) selection; |
| 59 | Object firstElement = sSelection.getFirstElement(); |
| 60 | if ((sSelection.size() == 1) && (firstElement instanceof TmfTraceElement || |
| 61 | firstElement instanceof TmfExperimentElement || |
| 62 | firstElement instanceof TmfOnDemandAnalysisElement || |
| 63 | firstElement instanceof TmfAnalysisOutputElement || |
| 64 | firstElement instanceof TmfReportElement)) { |
| 65 | element = (TmfProjectModelElement) firstElement; |
| 66 | return true; |
| 67 | } |
| 68 | } |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void run() { |
| 74 | try { |
| 75 | Object service = page.getActivePart().getSite().getService(IHandlerService.class); |
| 76 | IHandlerService handlerService = (IHandlerService) service; |
| 77 | boolean executeCommand = (element instanceof TmfTraceElement || |
| 78 | element instanceof TmfOnDemandAnalysisElement || |
| 79 | element instanceof TmfAnalysisOutputElement || |
| 80 | element instanceof TmfReportElement); |
| 81 | |
| 82 | if (!executeCommand && element instanceof TmfExperimentElement) { |
| 83 | TmfExperimentElement experiment = (TmfExperimentElement) element; |
| 84 | executeCommand = (experiment.getTraces().size() > 0); |
| 85 | } |
| 86 | |
| 87 | if (executeCommand) { |
| 88 | handlerService.executeCommand(OPEN_COMMAND_ID, null); |
| 89 | } |
| 90 | } catch (ExecutionException e) { |
| 91 | Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$ |
| 92 | } catch (NotDefinedException e) { |
| 93 | Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$ |
| 94 | } catch (NotEnabledException e) { |
| 95 | Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$ |
| 96 | } catch (NotHandledException e) { |
| 97 | Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$ |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |