]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java
Usability fixes for GraphExplorerImpl -related WB selection propagation
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / BaseSelectionProvider.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.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.simantics.utils.ObjectUtils;
24
25 /**
26  * BaseSelectionProvider is a base implementation ISelectionProvider -interface.
27  * <p>
28  * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to
29  * send a selection event
30  * 
31  * <p>
32  * Contains an empty StructuredSelection by default.
33  * {@link #setSelection(ISelection)} by default will not notify
34  * {@link ISelectionChangedListener}s of changes in the selection.
35  * </p>
36  * 
37  * @author Toni Kalajainen
38  * 
39  * @see ActiveSelectionProvider
40  */
41 public class BaseSelectionProvider implements ISelectionProvider {
42
43     protected ListenerList<ISelectionChangedListener> selectionListeners = new ListenerList<>();
44
45     protected ISelection selection = StructuredSelection.EMPTY;
46
47     /**
48      * Create new BaseSelectionProvider. Selection change notifications will run
49      * synchronously in the thread that invoked
50      * {@link ISelectionProvider#setSelection(ISelection)}.
51      */
52     public BaseSelectionProvider() {
53     }
54
55     public void addSelectionChangedListener(ISelectionChangedListener listener) {
56         selectionListeners.add(listener);
57     }
58
59     public ISelection getSelection() {
60         return selection;
61     }
62
63     public void clearListeners() {
64         clearSelectionChangedListeners();
65     }
66
67     public void clearSelectionChangedListeners() {
68         selectionListeners.clear();
69     }
70
71     public void removeSelectionChangedListener(ISelectionChangedListener listener) {
72         selectionListeners.remove(listener);
73     }
74
75     public void setSelection(ISelection selection) {
76         setSelectionWithoutFiring(selection);
77     }
78
79     protected Object[] getListeners() {
80         return selectionListeners.getListeners();
81     }
82
83     /**
84      * Notify other UIs that selection has changed
85      * @param selection new selection
86      */
87     public void fireSelection(ISelection selection) {
88         if (selection == null)
89             return;
90         SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);
91         for (Object l : getListeners())
92             ((ISelectionChangedListener) l).selectionChanged(e);
93     }
94
95     public void setSelectionWithoutFiring(ISelection selection) {
96         this.selection = selection;
97     }
98
99
100     /**
101      * Sets a new selection and always fires a SelectionChangedEvent about it.
102      * 
103      * @param selection the new selection
104      */
105     public void setAndFireSelection(ISelection selection) {
106         setSelection(selection);
107         fireSelection(selection);
108     }
109
110     /**
111      * Sets the new selection for this provider and fires all selection change
112      * listeners if the specified selection differs from the current selection.
113      * If the selection is either the same object or considered equal to the
114      * current selection, the listeners are not fired.
115      * 
116      * @param selection the new selection
117      */
118     public boolean setAndFireNonEqualSelection(ISelection selection) {
119         ISelection old = getSelection();
120         if (ObjectUtils.objectEquals(old, selection))
121             return false;
122
123         this.selection = selection;
124         if (selection != null)
125             fireSelection(selection);
126         return true;
127     }
128
129     public boolean selectionEquals(ISelection s) {
130         if (s == selection)
131             return true;
132         if (s == null)
133             // Old selection had to be non-null
134             return true;
135         return s.equals(selection);
136     }
137
138 }