tmf: Move HandlerUtils to TMF
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / handlers / HandlerUtils.java
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.tmf.ui.project.handlers;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionProvider;
16 import org.eclipse.jface.viewers.TreeSelection;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchPart;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21
22 /**
23 * Utilities for UI handlers
24 *
25 * @author Philippe Proulx
26 * @since 2.3
27 */
28 @NonNullByDefault
29 public final class HandlerUtils {
30
31 private HandlerUtils() {
32 }
33
34 /**
35 * Get the current selected UI element. Can be used instead of
36 * {@link org.eclipse.ui.handlers.HandlerUtil#getCurrentSelection} when an
37 * ExecutionEvent is not available.
38 *
39 * @return The element consisting of the selection
40 */
41 public static @Nullable Object getSelectedModelElement() {
42 // Check if we are closing down
43 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
44 if (window == null) {
45 return null;
46 }
47
48 // Get the selection
49 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
50 final IWorkbenchPart part = page.getActivePart();
51 if (part == null) {
52 return null;
53 }
54 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
55 if (selectionProvider == null) {
56 return null;
57 }
58 final ISelection selection = selectionProvider.getSelection();
59
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 return sel.getFirstElement();
64 }
65
66 return null;
67 }
68
69 }
This page took 0.031923 seconds and 5 git commands to generate.