Commit | Line | Data |
---|---|---|
664dac59 PP |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir, Philippe Proulx | |
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 | ||
10 | package org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.handler; | |
11 | ||
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | import org.eclipse.jface.viewers.ISelection; | |
14 | import org.eclipse.jface.viewers.ISelectionProvider; | |
15 | import org.eclipse.jface.viewers.TreeSelection; | |
16 | import org.eclipse.ui.IWorkbenchPage; | |
17 | import org.eclipse.ui.IWorkbenchPart; | |
18 | import org.eclipse.ui.IWorkbenchWindow; | |
19 | import org.eclipse.ui.PlatformUI; | |
20 | ||
21 | /** | |
22 | * Utilities for UI handlers | |
23 | * | |
24 | * @author Philippe Proulx | |
25 | */ | |
26 | public final class HandlerUtils { | |
27 | ||
28 | private HandlerUtils() { | |
29 | } | |
30 | ||
31 | /** | |
32 | * Get the current selected UI element. Can be used instead of | |
33 | * {@link org.eclipse.ui.handlers.HandlerUtil#getCurrentSelection} when an | |
34 | * ExecutionEvent is not available. | |
35 | * | |
36 | * @return The element consisting of the selection | |
37 | */ | |
38 | public static @Nullable Object getSelectedModelElement() { | |
39 | // Check if we are closing down | |
40 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
41 | if (window == null) { | |
42 | return null; | |
43 | } | |
44 | ||
45 | // Get the selection | |
46 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
47 | final IWorkbenchPart part = page.getActivePart(); | |
48 | if (part == null) { | |
49 | return null; | |
50 | } | |
51 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
52 | if (selectionProvider == null) { | |
53 | return null; | |
54 | } | |
55 | final ISelection selection = selectionProvider.getSelection(); | |
56 | ||
57 | if (selection instanceof TreeSelection) { | |
58 | final TreeSelection sel = (TreeSelection) selection; | |
59 | // There should be only one item selected as per the plugin.xml | |
60 | return sel.getFirstElement(); | |
61 | } | |
62 | ||
63 | return null; | |
64 | } | |
65 | ||
66 | } |