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