]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BasePostSelectionProvider.java
Usability fixes for GraphExplorerImpl -related WB selection propagation
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / BasePostSelectionProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * 23.8.2006
14  */
15 package org.simantics.utils.ui.jface;
16
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;
24
25 /**
26  * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
27  * <p>
28  * Usage:
29  *  1) instantiate
30  *  2) call {@link #setAndFireSelection(ISelection)} to send a selection event
31  * 
32  * <p>
33  * Contains an empty StructuredSelection by default.
34  * </p>
35  * 
36  * @author Toni Kalajainen
37  * @author Tuukka Lehtonen
38  */
39 public class BasePostSelectionProvider extends BaseSelectionProvider implements IPostSelectionProvider {
40
41     protected ListenerList<ISelectionChangedListener> postSelectionListeners = new ListenerList<>();
42
43     public void clearListeners() {
44         super.clearListeners();
45         clearPostSelectionChangedListeners();
46     }
47
48     public void clearPostSelectionChangedListeners() {
49         postSelectionListeners.clear();
50     }
51
52     @Override
53     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
54         postSelectionListeners.add(listener);
55     }
56
57     @Override
58     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
59         postSelectionListeners.remove(listener);
60     }
61
62     protected Object[] getPostListeners() {
63         return postSelectionListeners.getListeners();
64     }
65
66     /**
67      * Notify other UIs that selection has changed
68      * @param selection new selection
69      */
70     public void firePostSelection(ISelection selection) {
71         if (selection == null)
72             return;
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() {
77                 @Override
78                 public void run() throws Exception {
79                     l.selectionChanged(e);
80                 }
81             });
82         }
83     }
84
85 }