Rationalize the TMF commands
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / OpenAction.java
1 /*******************************************************************************
2 * Copyright (c) 2012 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.linuxtools.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.linuxtools.internal.tmf.ui.Activator;
24 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
25 import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectModelElement;
26 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.handlers.IHandlerService;
29
30 /**
31 * <b><u>OpenAction</u></b>
32 * <p>
33 * Implement me. Please.
34 * <p>
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 /*
55 * (non-Javadoc)
56 * @see org.eclipse.jface.action.Action#isEnabled()
57 */
58 @Override
59 public boolean isEnabled() {
60 ISelection selection = selectionProvider.getSelection();
61 if (!selection.isEmpty()) {
62 IStructuredSelection sSelection = (IStructuredSelection) selection;
63 if (sSelection.size() == 1) {
64 if (sSelection.getFirstElement() instanceof TmfTraceElement ||
65 sSelection.getFirstElement() instanceof TmfExperimentElement) {
66 element = (TmfProjectModelElement) sSelection.getFirstElement();
67 return true;
68 }
69 }
70 }
71 return false;
72 }
73
74 /*
75 * (non-Javadoc)
76 * @see org.eclipse.jface.action.Action#run()
77 */
78 @Override
79 public void run() {
80 try {
81 IHandlerService handlerService = (IHandlerService) page.getActivePart().getSite().getService(IHandlerService.class);
82 if (element instanceof TmfTraceElement || element instanceof TmfExperimentElement) {
83 handlerService.executeCommand(OPEN_COMMAND_ID, null);
84 }
85 } catch (ExecutionException e) {
86 Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$
87 } catch (NotDefinedException e) {
88 Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$
89 } catch (NotEnabledException e) {
90 Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$
91 } catch (NotHandledException e) {
92 Activator.getDefault().logError("Error opening resource " + element.getName(), e); //$NON-NLS-1$
93 }
94 }
95
96 }
This page took 0.032122 seconds and 5 git commands to generate.