1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
alfonx |
256 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
alfonx |
244 |
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
|
* Stefan A. Krüger - additional utility classes |
29 |
|
|
******************************************************************************/ |
30 |
mojays |
102 |
/** |
31 |
|
|
Copyright 2008 Stefan Alfons Krüger |
32 |
|
|
|
33 |
alfonx |
96 |
atlas-framework - This file is part of the Atlas Framework |
34 |
|
|
|
35 |
|
|
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. |
36 |
|
|
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
37 |
|
|
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
38 |
|
|
|
39 |
|
|
Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version. |
40 |
|
|
Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License. |
41 |
|
|
Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA. |
42 |
|
|
**/ |
43 |
|
|
|
44 |
|
|
package skrueger.geotools.selection; |
45 |
|
|
|
46 |
|
|
import java.beans.PropertyChangeEvent; |
47 |
mojays |
102 |
import java.beans.PropertyChangeListener; |
48 |
alfonx |
96 |
import java.util.Collection; |
49 |
|
|
import java.util.HashSet; |
50 |
|
|
import java.util.Vector; |
51 |
|
|
|
52 |
mojays |
102 |
import javax.swing.event.EventListenerList; |
53 |
|
|
|
54 |
alfonx |
96 |
import org.apache.log4j.Logger; |
55 |
alfonx |
318 |
import org.geotools.feature.SimpleFeature; |
56 |
alfonx |
96 |
|
57 |
|
|
import schmitzm.swing.event.PropertyChangeEmitter; |
58 |
alfonx |
224 |
import skrueger.geotools.StyledLayerInterface; |
59 |
alfonx |
96 |
|
60 |
|
|
/** |
61 |
alfonx |
224 |
* This manager holds a set of objects which are <i>selected</i> for a {@link StyledLayerInterface}. |
62 |
mojays |
102 |
* Several components can connect to this model to keep their selection synchronized. |
63 |
|
|
* @see #addSelectionListener(java.beans.PropertyChangeListener) |
64 |
|
|
* @see StyledLayerSelectionModelSynchronizer |
65 |
alfonx |
96 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
66 |
|
|
*/ |
67 |
mojays |
102 |
public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter<StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>>> { |
68 |
alfonx |
96 |
/** A static logger for this class */ |
69 |
alfonx |
97 |
protected final Logger LOGGER = Logger.getLogger(this.getClass()); |
70 |
mojays |
102 |
|
71 |
alfonx |
96 |
/** Indicates that the selection has changed in {@link PropertyChangeEvent}. */ |
72 |
|
|
public final static String SELECTION = "SELECTION"; |
73 |
mojays |
102 |
|
74 |
|
|
|
75 |
|
|
/** Holds the connected listeners. */ |
76 |
|
|
protected EventListenerList listenerList = new EventListenerList(); |
77 |
|
|
|
78 |
alfonx |
318 |
/** Holds the current selection (e.g. {@link SimpleFeature Features}). */ |
79 |
alfonx |
105 |
protected final HashSet<E> selectionObjects; |
80 |
mojays |
102 |
|
81 |
|
|
/** Holds the styled map whose selection is managed by this class. */ |
82 |
alfonx |
224 |
protected final StyledLayerInterface<?> styledLayer; |
83 |
mojays |
102 |
|
84 |
|
|
|
85 |
alfonx |
96 |
/** Indicates whether a selection change is a part of multiple |
86 |
mojays |
102 |
* changes. If {@code true} {@link #refreshSelection()} has NO effect |
87 |
|
|
* until {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
88 |
alfonx |
96 |
* is called. */ |
89 |
|
|
private boolean valueIsAdjusting = false; |
90 |
|
|
|
91 |
|
|
/** |
92 |
mojays |
102 |
* Creates a new selection model. |
93 |
alfonx |
224 |
* @param styledLayer styled layer the selection is controlled of |
94 |
alfonx |
96 |
*/ |
95 |
alfonx |
224 |
public StyledLayerSelectionModel(StyledLayerInterface<?> styledLayer) { |
96 |
mojays |
102 |
this.selectionObjects = new HashSet<E>(); |
97 |
alfonx |
224 |
this.styledLayer = styledLayer; |
98 |
mojays |
102 |
} |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Fires an event to every connected listener to update their selection |
102 |
|
|
* according to the selection of this model.<br> |
103 |
|
|
* <b>Does nothing if adjusting is <code>true</code></b>. |
104 |
|
|
*/ |
105 |
alfonx |
96 |
public void refreshSelection() { |
106 |
|
|
if ( getValueIsAdjusting() ) |
107 |
|
|
return; |
108 |
|
|
firePropertyChangeEvent( createSelectionChangeEvent(null) ); |
109 |
|
|
} |
110 |
mojays |
102 |
|
111 |
alfonx |
96 |
/** |
112 |
|
|
* Clears the selection and fires an event to every connected listener |
113 |
|
|
* to update their selection. |
114 |
|
|
*/ |
115 |
|
|
public void clearSelection() { |
116 |
mojays |
102 |
selectionObjects.clear(); |
117 |
alfonx |
96 |
refreshSelection(); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Creates a simple {@link PropertyChangeEvent} for property {@link #SELECTION} |
122 |
|
|
* with no new and old value, just to inform the listeners THAT |
123 |
|
|
* some selection has changed. |
124 |
|
|
* Subclasses may overwrite this method to create more sophisticated |
125 |
|
|
* events. |
126 |
|
|
* @param respObject object which is responsible for the selection change; this |
127 |
|
|
* object should ignore this event to avoid event circles (can be {@code null} |
128 |
|
|
* in case of general refresh events; in this case all connected |
129 |
|
|
* components will refresh their selection) |
130 |
mojays |
102 |
* |
131 |
alfonx |
96 |
*/ |
132 |
|
|
public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) { |
133 |
|
|
if ( respObject == null ) |
134 |
|
|
respObject = this; |
135 |
|
|
return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null); |
136 |
|
|
} |
137 |
mojays |
102 |
|
138 |
|
|
|
139 |
|
|
/** |
140 |
mojays |
131 |
* Returns whether the current selection change is a part of multiple |
141 |
mojays |
102 |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
142 |
|
|
* and no {@link StyledLayerSelectionEvent} is fired until |
143 |
alfonx |
96 |
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
144 |
|
|
* is called. |
145 |
|
|
*/ |
146 |
|
|
public boolean getValueIsAdjusting() { |
147 |
|
|
return this.valueIsAdjusting; |
148 |
|
|
} |
149 |
mojays |
102 |
|
150 |
|
|
/** |
151 |
mojays |
131 |
* Sets whether the following selection changes are part of multiple |
152 |
mojays |
102 |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
153 |
|
|
* and no {@link StyledLayerSelectionEvent} event is fired. |
154 |
alfonx |
96 |
* If the value is set from {@code true} to {@code false} an automatic |
155 |
|
|
* refresh is initiated. |
156 |
|
|
* @see #refreshSelection() |
157 |
|
|
*/ |
158 |
|
|
public void setValueIsAdjusting(boolean valueIsAdjusting) { |
159 |
|
|
if ( valueIsAdjusting != this.valueIsAdjusting ) { |
160 |
|
|
this.valueIsAdjusting = valueIsAdjusting; |
161 |
|
|
if ( !this.valueIsAdjusting ) |
162 |
|
|
refreshSelection(); |
163 |
|
|
} |
164 |
|
|
} |
165 |
|
|
|
166 |
mojays |
102 |
|
167 |
alfonx |
96 |
/** |
168 |
mojays |
102 |
* Extends the current selection by an individual object. |
169 |
|
|
* @param selectedObject defines the (new) selected object |
170 |
alfonx |
96 |
* @return {@code true} if selection has changed by calling this method |
171 |
|
|
*/ |
172 |
mojays |
102 |
public boolean addSelection(E selectedObject) { |
173 |
alfonx |
111 |
|
174 |
mojays |
102 |
if ( selectionObjects.add(selectedObject) ) { |
175 |
alfonx |
96 |
refreshSelection(); |
176 |
|
|
return true; |
177 |
|
|
} |
178 |
|
|
return false; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
/** |
182 |
mojays |
102 |
* Removes an individual object from the current selection. |
183 |
|
|
* @param unselectedObject defines an object which will be unselected |
184 |
alfonx |
96 |
* @return {@code true} if selection has changed by calling this method |
185 |
|
|
*/ |
186 |
mojays |
102 |
public boolean removeSelection(E unselectedObject) { |
187 |
|
|
if ( selectionObjects.remove(unselectedObject) ) { |
188 |
alfonx |
96 |
refreshSelection(); |
189 |
|
|
return true; |
190 |
|
|
} |
191 |
|
|
return false; |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
/** |
195 |
mojays |
102 |
* Extends the current selection by individual objects. |
196 |
|
|
* @param selectedObjects defines some objects which will be selected |
197 |
alfonx |
96 |
* @return {@code true} if selection has changed by calling this method |
198 |
|
|
*/ |
199 |
mojays |
102 |
public boolean addSelection(Collection<E> selectedObjects) { |
200 |
|
|
if ( selectionObjects.addAll(selectedObjects) ) { |
201 |
alfonx |
96 |
refreshSelection(); |
202 |
|
|
return true; |
203 |
|
|
} |
204 |
|
|
return false; |
205 |
|
|
} |
206 |
mojays |
102 |
|
207 |
alfonx |
96 |
/** |
208 |
mojays |
102 |
* Removes individual objects from the current selection. |
209 |
|
|
* @param unselectedObjects defines some objects which will be unselected |
210 |
alfonx |
96 |
* @return {@code true} if selection has changed by calling this method |
211 |
|
|
*/ |
212 |
mojays |
102 |
public boolean removeSelection(Collection<E> unselectedObjects) { |
213 |
|
|
if ( selectionObjects.removeAll(unselectedObjects) ) { |
214 |
alfonx |
96 |
refreshSelection(); |
215 |
|
|
return true; |
216 |
|
|
} |
217 |
|
|
return false; |
218 |
|
|
} |
219 |
mojays |
102 |
|
220 |
alfonx |
96 |
/** |
221 |
mojays |
102 |
* Returns all selected objects as list. |
222 |
alfonx |
96 |
* @return a copy of the original list, so changes have no effect! |
223 |
|
|
*/ |
224 |
|
|
public Vector<E> getSelection() { |
225 |
mojays |
102 |
return new Vector<E>(selectionObjects); |
226 |
alfonx |
96 |
} |
227 |
|
|
|
228 |
mojays |
102 |
/** |
229 |
|
|
* Adds a listener to the listener list. Only alias for {@link #addListener(PropertyChangeListener)}. |
230 |
|
|
* @param listener a listener |
231 |
|
|
*/ |
232 |
|
|
public void addSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) { |
233 |
|
|
this.addListener(listener); |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
/** |
237 |
|
|
* Removes a listener from the listener list. Only alias for {@link #removeListener(PropertyChangeListener)}. |
238 |
|
|
* @param listener a listener |
239 |
|
|
*/ |
240 |
|
|
public void removeSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) { |
241 |
|
|
this.removeListener(listener); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
alfonx |
96 |
} |