]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/BaseSelectionProvider.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / BaseSelectionProvider.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  * 23.8.2006\r
14  */\r
15 package org.simantics.utils.ui.jface;\r
16 \r
17 import org.eclipse.core.runtime.ListenerList;\r
18 import org.eclipse.jface.viewers.ISelection;\r
19 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
20 import org.eclipse.jface.viewers.ISelectionProvider;\r
21 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
22 import org.eclipse.jface.viewers.StructuredSelection;\r
23 \r
24 /**\r
25  * BaseSelectionProvider is a base implementation ISelectionProvider -interface.\r
26  * <p>\r
27  * Usage: 1) instantiate 2) call {@link #setAndFireSelection(ISelection)} to\r
28  * send a selection event\r
29  * \r
30  * <p>\r
31  * Contains an empty StructuredSelection by default.\r
32  * {@link #setSelection(ISelection)} by default will not notify\r
33  * {@link ISelectionChangedListener}s of changes in the selection.\r
34  * </p>\r
35  * \r
36  * @author Toni Kalajainen\r
37  * \r
38  * @see ActiveSelectionProvider\r
39  */\r
40 public class BaseSelectionProvider implements ISelectionProvider {\r
41 \r
42     protected ListenerList selectionListeners = new ListenerList();\r
43 \r
44     protected ISelection selection = StructuredSelection.EMPTY;\r
45 \r
46     /**\r
47      * Create new BaseSelectionProvider. Selection change notifications will run\r
48      * synchronously in the thread that invoked\r
49      * {@link ISelectionProvider#setSelection(ISelection)}.\r
50      */\r
51     public BaseSelectionProvider() {\r
52     }\r
53 \r
54     public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
55         selectionListeners.add(listener);\r
56     }\r
57 \r
58     public ISelection getSelection() {\r
59         return selection;\r
60     }\r
61 \r
62     public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
63         selectionListeners.remove(listener);\r
64     }\r
65 \r
66     public void setSelection(ISelection selection) {\r
67         setSelectionWithoutFiring(selection);\r
68     }\r
69 \r
70     protected Object[] getListeners() {\r
71         return selectionListeners.getListeners();\r
72     }\r
73 \r
74     /**\r
75      * Notify other UIs that selection has changed\r
76      * @param selection new selection\r
77      */\r
78     public void fireSelection(ISelection selection) {\r
79         if (selection == null)\r
80             return;\r
81         SelectionChangedEvent e = new SelectionChangedEvent(BaseSelectionProvider.this, selection);\r
82         for (Object l : getListeners())\r
83             ((ISelectionChangedListener) l).selectionChanged(e);\r
84     }\r
85 \r
86     public void setSelectionWithoutFiring(ISelection selection) {\r
87         this.selection = selection;\r
88     }\r
89 \r
90 \r
91     /**\r
92      * Sets a new selection and always fires a SelectionChangedEvent about it.\r
93      * \r
94      * @param selection the new selection\r
95      */\r
96     public void setAndFireSelection(ISelection selection) {\r
97         setSelection(selection);\r
98         fireSelection(selection);\r
99     }\r
100 \r
101     /**\r
102      * Sets the new selection for this provider and fires all selection change\r
103      * listeners if the specified selection differs from the current selection.\r
104      * If the selection is either the same object or considered equal to the\r
105      * current selection, the listeners are not fired.\r
106      * \r
107      * @param selection the new selection\r
108      */\r
109     public void setAndFireNonEqualSelection(ISelection selection) {\r
110         ISelection old = getSelection();\r
111         if (selection == old)\r
112             return;\r
113 \r
114         this.selection = selection;\r
115         if (selection != null && !selection.equals(old))\r
116             fireSelection(selection);\r
117     }\r
118 \r
119     public boolean selectionEquals(ISelection s) {\r
120         if (s == selection)\r
121             return true;\r
122         if (s == null)\r
123             // Old selection had to be non-null\r
124             return true;\r
125         return s.equals(selection);\r
126     }\r
127 \r
128 }\r