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
.tmf
.ui
.project
.handlers
;
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
;
23 * Utilities for UI handlers
25 * @author Philippe Proulx
29 public final class HandlerUtils
{
31 private HandlerUtils() {
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.
39 * @return The element consisting of the selection
41 public static @Nullable Object
getSelectedModelElement() {
42 // Check if we are closing down
43 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
49 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
50 final IWorkbenchPart part
= page
.getActivePart();
54 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
55 if (selectionProvider
== null) {
58 final ISelection selection
= selectionProvider
.getSelection();
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();