1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Alexandre Montplaisir, Philippe Proulx
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.ui
.handler
;
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
;
22 * Utilities for UI handlers
24 * @author Philippe Proulx
26 public final class HandlerUtils
{
28 private HandlerUtils() {
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.
36 * @return The element consisting of the selection
38 public static @Nullable Object
getSelectedModelElement() {
39 // Check if we are closing down
40 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
46 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
47 final IWorkbenchPart part
= page
.getActivePart();
51 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
52 if (selectionProvider
== null) {
55 final ISelection selection
= selectionProvider
.getSelection();
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();