1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
15 package org.simantics.utils.ui.jface;
17 import org.eclipse.core.runtime.ListenerList;
18 import org.eclipse.core.runtime.SafeRunner;
19 import org.eclipse.jface.util.SafeRunnable;
20 import org.eclipse.jface.viewers.IPostSelectionProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
30 * 2) call {@link #setAndFireSelection(ISelection)} to send a selection event
33 * Contains an empty StructuredSelection by default.
36 * @author Toni Kalajainen
37 * @author Tuukka Lehtonen
39 public class BasePostSelectionProvider extends BaseSelectionProvider implements IPostSelectionProvider {
41 protected ListenerList<ISelectionChangedListener> postSelectionListeners = new ListenerList<>();
43 public void clearListeners() {
44 super.clearListeners();
45 clearPostSelectionChangedListeners();
48 public void clearPostSelectionChangedListeners() {
49 postSelectionListeners.clear();
53 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
54 postSelectionListeners.add(listener);
58 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
59 postSelectionListeners.remove(listener);
62 protected Object[] getPostListeners() {
63 return postSelectionListeners.getListeners();
67 * Notify other UIs that selection has changed
68 * @param selection new selection
70 public void firePostSelection(ISelection selection) {
71 if (selection == null)
73 final SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
74 for (Object o : postSelectionListeners.getListeners()) {
75 final ISelectionChangedListener l = (ISelectionChangedListener) o;
76 SafeRunner.run(new SafeRunnable() {
78 public void run() throws Exception {
79 l.selectionChanged(e);