Commit | Line | Data |
---|---|---|
c068a752 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 École Polytechnique de Montréal | |
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 | * Geneviève Bastien - 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.TmfAnalysisElement; | |
22 | import org.eclipse.swt.widgets.Display; | |
23 | import org.eclipse.swt.widgets.MessageBox; | |
24 | import org.eclipse.ui.IWorkbenchPage; | |
25 | import org.eclipse.ui.IWorkbenchPart; | |
26 | import org.eclipse.ui.IWorkbenchWindow; | |
27 | import org.eclipse.ui.PlatformUI; | |
28 | ||
29 | /** | |
30 | * Handler for when user wants to open the analysis help text | |
31 | * | |
32 | * @author Geneviève Bastien | |
33 | */ | |
34 | public class OpenAnalysisHelpHandler extends AbstractHandler { | |
35 | ||
36 | private TmfAnalysisElement fAnalysis; | |
37 | ||
38 | @Override | |
39 | public boolean isEnabled() { | |
40 | // Check if we are closing down | |
41 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
42 | if (window == null) { | |
43 | return false; | |
44 | } | |
45 | ||
46 | // Get the selection | |
47 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
48 | final IWorkbenchPart part = page.getActivePart(); | |
49 | if (part == null) { | |
50 | return false; | |
51 | } | |
52 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
53 | if (selectionProvider == null) { | |
54 | return false; | |
55 | } | |
56 | final ISelection selection = selectionProvider.getSelection(); | |
57 | ||
58 | // Make sure there is only one selection and that it is a trace | |
59 | fAnalysis = null; | |
60 | if (selection instanceof TreeSelection) { | |
61 | final TreeSelection sel = (TreeSelection) selection; | |
62 | // There should be only one item selected as per the plugin.xml | |
63 | final Object element = sel.getFirstElement(); | |
64 | if (element instanceof TmfAnalysisElement) { | |
65 | fAnalysis = (TmfAnalysisElement) element; | |
66 | } | |
67 | } | |
68 | ||
69 | return (fAnalysis != null); | |
70 | } | |
71 | ||
72 | @Override | |
73 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
74 | ||
75 | // Check if we are closing down | |
76 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
77 | if (window == null) { | |
78 | return null; | |
79 | } | |
80 | ||
81 | // Check that the trace is valid | |
82 | if (fAnalysis == null) { | |
83 | return null; | |
84 | } | |
85 | ||
86 | Thread thread = new Thread() { | |
87 | @Override | |
88 | public void run() { | |
89 | displayHelpMsg(fAnalysis.getHelpMessage()); | |
90 | } | |
91 | }; | |
92 | ||
93 | thread.start(); | |
94 | ||
95 | return null; | |
96 | } | |
97 | ||
98 | private static void displayHelpMsg(final String errorMsg) { | |
99 | Display.getDefault().asyncExec(new Runnable() { | |
100 | @Override | |
101 | public void run() { | |
102 | /* | |
103 | * TODO: A message box is not the best place to show help. | |
104 | * Something should be done with the Eclipse help | |
105 | */ | |
106 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
107 | mb.setText(Messages.AnalysisModule_Help); | |
108 | mb.setMessage(errorMsg); | |
109 | mb.open(); | |
110 | } | |
111 | }); | |
112 | } | |
113 | ||
114 | } |