Commit | Line | Data |
---|---|---|
828e5592 PT |
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 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
828e5592 PT |
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.tmf.ui.project.model.TmfExperimentElement; | |
24 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfProjectModelElement; | |
25 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
26 | import org.eclipse.ui.IWorkbenchPage; | |
27 | import org.eclipse.ui.handlers.IHandlerService; | |
28 | ||
29 | public class OpenAction extends Action { | |
30 | ||
31 | private static final String OPEN_TRACE_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.trace.open"; //$NON-NLS-1$ | |
32 | private static final String OPEN_EXPERIMENT_COMMAND_ID = "org.eclipse.linuxtools.tmf.ui.command.project.experiment.open"; //$NON-NLS-1$ | |
33 | ||
34 | private IWorkbenchPage page; | |
35 | private ISelectionProvider selectionProvider; | |
36 | private TmfProjectModelElement element; | |
37 | ||
38 | /** | |
39 | * Default constructor | |
40 | * @param page the workbench page | |
41 | * @param selectionProvider the selection provider | |
42 | */ | |
43 | public OpenAction(IWorkbenchPage page, ISelectionProvider selectionProvider) { | |
44 | this.page = page; | |
45 | this.selectionProvider = selectionProvider; | |
46 | } | |
47 | ||
48 | /* | |
49 | * (non-Javadoc) | |
50 | * @see org.eclipse.jface.action.Action#isEnabled() | |
51 | */ | |
52 | @Override | |
53 | public boolean isEnabled() { | |
54 | ISelection selection = selectionProvider.getSelection(); | |
55 | if (!selection.isEmpty()) { | |
56 | IStructuredSelection sSelection = (IStructuredSelection) selection; | |
57 | if (sSelection.size() == 1) { | |
58 | if (sSelection.getFirstElement() instanceof TmfTraceElement || | |
59 | sSelection.getFirstElement() instanceof TmfExperimentElement) { | |
60 | element = (TmfProjectModelElement) sSelection.getFirstElement(); | |
61 | return true; | |
62 | } | |
63 | } | |
64 | } | |
65 | return false; | |
66 | } | |
67 | ||
68 | /* | |
69 | * (non-Javadoc) | |
70 | * @see org.eclipse.jface.action.Action#run() | |
71 | */ | |
72 | @Override | |
73 | public void run() { | |
74 | try { | |
75 | IHandlerService handlerService = (IHandlerService) page.getActivePart().getSite().getService(IHandlerService.class); | |
76 | if (element instanceof TmfTraceElement) { | |
77 | handlerService.executeCommand(OPEN_TRACE_COMMAND_ID, null); | |
78 | } else if (element instanceof TmfExperimentElement) { | |
79 | handlerService.executeCommand(OPEN_EXPERIMENT_COMMAND_ID, null); | |
80 | } | |
81 | } catch (ExecutionException e) { | |
82 | e.printStackTrace(); | |
83 | } catch (NotDefinedException e) { | |
84 | e.printStackTrace(); | |
85 | } catch (NotEnabledException e) { | |
86 | e.printStackTrace(); | |
87 | } catch (NotHandledException e) { | |
88 | e.printStackTrace(); | |
89 | } | |
90 | } | |
91 | ||
92 | } |