Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5132a841f4cc596ccdc6a7c375f018898c9bb3de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*****************************************************************************
 * Copyright (c) 2014 CEA LIST and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.ui.util;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * @author VL222926
 *
 */
public class SelectionHelper {

	/**
	 * Constructor.
	 *
	 */
	private SelectionHelper() {
		// to avoid instanciation
	}

	/**
	 * 
	 * @return
	 *         the selection service or <code>null</code> if not found
	 * 
	 */
	public static final ISelectionService getSelectionService() {
		IWorkbench wb = PlatformUI.getWorkbench();
		if (wb != null) {
			// don't work
			// ISelectionService s1 = (ISelectionService) wb.getService(ISelectionService.class);
			IWorkbenchWindow ww = wb.getActiveWorkbenchWindow();
			if (ww != null) {
				return (ISelectionService) ww.getService(ISelectionService.class);
			}
		}
		return null;
	}

	/**
	 * 
	 * @return
	 *         the current selection or an empty selection. can't be <code>null</code>
	 */
	public static final ISelection getCurrentSelection() {
		ISelectionService selectionService = getSelectionService();
		if (selectionService != null) {
			ISelection currentSelection = selectionService.getSelection();
			if (currentSelection != null) {
				return currentSelection;
			}
		}
		return StructuredSelection.EMPTY;
	}

	/**
	 * 
	 * @param viewId
	 *            the id of the view for which we want the selection
	 * @return
	 *         the current selection for the view, the returned value can't be <code>null</code>
	 */
	public static final ISelection getCurrentSelection(String viewId) {
		ISelectionService selectionService = getSelectionService();
		if (selectionService != null) {
			ISelection currentSelection = selectionService.getSelection(viewId);
			if (currentSelection != null) {
				return currentSelection;
			}
		}
		return StructuredSelection.EMPTY;
	}

	/**
	 * 
	 * @return
	 *         a structured selection.
	 *         the returned value can't be <code>null</code>
	 */
	public static final IStructuredSelection getCurrentStructuredSelection() {
		ISelection selection = getCurrentSelection();
		if (selection instanceof IStructuredSelection) {
			return (IStructuredSelection) selection;
		}
		return StructuredSelection.EMPTY;
	}

	/**
	 * 
	 * @param viewId
	 *            the id of the view for which we want the selection
	 * @return
	 *         the current selection for the view, the returned value can't be <code>null</code>
	 */
	public static final IStructuredSelection getCurrentStructuredSelection(String viewId) {
		ISelection selection = getCurrentSelection(viewId);
		if (selection instanceof IStructuredSelection) {
			return (IStructuredSelection) selection;
		}
		return StructuredSelection.EMPTY;
	}
}

Back to the top