]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BasePostSelectionProvider.java
c5ca0e754cada161035bbcee68289b4dc135062a
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / BasePostSelectionProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.simantics.utils.ObjectUtils;
26
27 /**
28  * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
29  * <p>
30  * Usage:
31  *  1) instantiate
32  *  2) call {@link #setAndFireSelection(ISelection)} to send a selection event
33  * 
34  * <p>
35  * Contains an empty StructuredSelection by default.
36  * </p>
37  * 
38  * @author Toni Kalajainen
39  */
40 public class BasePostSelectionProvider implements IPostSelectionProvider {
41
42     protected ListenerList selectionListeners = new ListenerList();
43
44     protected ListenerList postSelectionListeners = new ListenerList();
45
46     protected ISelection selection = StructuredSelection.EMPTY;
47
48     public void addSelectionChangedListener(ISelectionChangedListener listener) {
49         selectionListeners.add(listener);
50     }
51
52     public ISelection getSelection() {
53         return selection;
54     }
55
56     public void clearListeners() {
57         selectionListeners.clear();
58         postSelectionListeners.clear();
59     }
60
61     public void clearSelectionChangedListeners() {
62         postSelectionListeners.clear();
63     }
64
65     public void clearPostSelectionChangedListeners() {
66         postSelectionListeners.clear();
67     }
68
69     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
70         selectionListeners.remove(listener);
71     }
72
73     @Override
74     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
75         postSelectionListeners.add(listener);
76     }
77
78     @Override
79     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
80         postSelectionListeners.remove(listener);
81     }
82
83     public void setSelection(ISelection selection) {
84         setSelectionWithoutFiring(selection);
85     }
86
87     protected Object[] getListeners() {
88         return selectionListeners.getListeners();
89     }
90
91     protected Object[] getPostListeners() {
92         return postSelectionListeners.getListeners();
93     }
94
95     /**
96      * Notify other UIs that selection has changed
97      * @param selection new selection
98      */
99     public void fireSelection(ISelection selection) {
100         if (selection == null)
101             return;
102         SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
103         for (Object l : getListeners())
104             ((ISelectionChangedListener) l).selectionChanged(e);
105     }
106
107     /**
108      * Notify other UIs that selection has changed
109      * @param selection new selection
110      */
111     public void firePostSelection(ISelection selection) {
112         if (selection == null)
113             return;
114         final SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
115         for (Object o : postSelectionListeners.getListeners()) {
116             final ISelectionChangedListener l = (ISelectionChangedListener) o;
117             SafeRunner.run(new SafeRunnable() {
118                 @Override
119                 public void run() throws Exception {
120                     l.selectionChanged(e);
121                 }
122             });
123         }
124     }
125
126     public void setSelectionWithoutFiring(ISelection selection) {
127         this.selection = selection;
128     }
129
130
131     /**
132      * Sets a new selection and always fires a SelectionChangedEvent about it.
133      * 
134      * @param selection the new selection
135      */
136     public void setAndFireSelection(ISelection selection) {
137         setSelection(selection);
138         fireSelection(selection);
139     }
140
141     /**
142      * Sets the new selection for this provider and fires all selection change
143      * listeners if the specified selection differs from the current selection.
144      * If the selection is either the same object or considered equal to the
145      * current selection, the listeners are not fired.
146      * 
147      * @param selection the new selection
148      */
149     public void setAndFireNonEqualSelection(ISelection selection) {
150         ISelection old = getSelection();
151         if (ObjectUtils.objectEquals(old, selection))
152             return;
153
154         this.selection = selection;
155         if (selection != null && !selection.equals(old))
156             fireSelection(selection);
157     }
158
159     public boolean selectionEquals(ISelection s) {
160         if (s == selection)
161             return true;
162         if (s == null)
163             // Old selection had to be non-null
164             return true;
165         return s.equals(selection);
166     }
167
168 }