1 |
/** |
/******************************************************************************* |
2 |
Copyright 2008 Stefan Alfons Krüger |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
* |
4 |
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
|
* classes based on Java 1.6, focussing (not only) on Java Swing |
6 |
|
* 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 |
|
/** |
31 |
|
Copyright 2008 Stefan Alfons Krüger |
32 |
|
|
33 |
atlas-framework - This file is part of the Atlas Framework |
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. |
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. |
44 |
package skrueger.geotools.selection; |
package skrueger.geotools.selection; |
45 |
|
|
46 |
import java.beans.PropertyChangeEvent; |
import java.beans.PropertyChangeEvent; |
47 |
|
import java.beans.PropertyChangeListener; |
48 |
import java.util.Collection; |
import java.util.Collection; |
49 |
import java.util.HashSet; |
import java.util.HashSet; |
|
import java.util.Set; |
|
50 |
import java.util.Vector; |
import java.util.Vector; |
51 |
|
|
52 |
|
import javax.swing.event.EventListenerList; |
53 |
|
|
54 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
55 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
56 |
|
|
57 |
import schmitzm.swing.event.PropertyChangeEmitter; |
import schmitzm.swing.event.PropertyChangeEmitter; |
58 |
|
import skrueger.geotools.StyledLayerInterface; |
59 |
|
|
60 |
/** |
/** |
61 |
* This manager takes care of every object selection for a {@link DpLayer} |
* This manager holds a set of objects which are <i>selected</i> for a {@link StyledLayerInterface}. |
62 |
* in the several atlas components and informs the appropriate components |
* Several components can connect to this model to keep their selection synchronized. |
63 |
* to keep the selection synchronized. |
* @see #addSelectionListener(java.beans.PropertyChangeListener) |
64 |
|
* @see StyledLayerSelectionModelSynchronizer |
65 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany) |
66 |
*/ |
*/ |
67 |
public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter { |
public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter<StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>>> { |
68 |
/** A static logger for this class */ |
/** A static logger for this class */ |
69 |
protected final Logger LOGGER = Logger.getLogger(this.getClass()); |
protected final Logger LOGGER = Logger.getLogger(this.getClass()); |
70 |
|
|
|
/** Holds the current selection as {@link Feature}-list. This |
|
|
* is the basis to create the other selection objects. */ |
|
|
protected final Set<E> selectionFeatures; |
|
|
|
|
71 |
/** Indicates that the selection has changed in {@link PropertyChangeEvent}. */ |
/** Indicates that the selection has changed in {@link PropertyChangeEvent}. */ |
72 |
public final static String SELECTION = "SELECTION"; |
public final static String SELECTION = "SELECTION"; |
73 |
|
|
74 |
|
|
75 |
|
/** Holds the connected listeners. */ |
76 |
|
protected EventListenerList listenerList = new EventListenerList(); |
77 |
|
|
78 |
|
/** Holds the current selection (e.g. {@link Feature Features}). */ |
79 |
|
protected final HashSet<E> selectionObjects; |
80 |
|
|
81 |
|
/** Holds the styled map whose selection is managed by this class. */ |
82 |
|
protected final StyledLayerInterface<?> styledLayer; |
83 |
|
|
84 |
|
|
85 |
/** Indicates whether a selection change is a part of multiple |
/** Indicates whether a selection change is a part of multiple |
86 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has NO effect |
87 |
* called and no {@code DpLayerSelectionEvent} event is fired until |
* until {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
|
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
|
88 |
* is called. */ |
* is called. */ |
89 |
private boolean valueIsAdjusting = false; |
private boolean valueIsAdjusting = false; |
90 |
|
|
|
public StyledLayerSelectionModel() { |
|
|
this.selectionFeatures = new HashSet<E>(); |
|
|
} |
|
91 |
/** |
/** |
92 |
* Reset all selection objects and fires an event to every connected |
* Creates a new selection model. |
93 |
* listener to update their selection according to the selection |
* @param styledLayer styled layer the selection is controlled of |
94 |
* of this manager. <b>Does nothing if adjusting is <code>true</code></b>. |
*/ |
95 |
|
public StyledLayerSelectionModel(StyledLayerInterface<?> styledLayer) { |
96 |
|
this.selectionObjects = new HashSet<E>(); |
97 |
|
this.styledLayer = styledLayer; |
98 |
|
} |
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 |
public void refreshSelection() { |
public void refreshSelection() { |
106 |
if ( getValueIsAdjusting() ) |
if ( getValueIsAdjusting() ) |
107 |
return; |
return; |
108 |
firePropertyChangeEvent( createSelectionChangeEvent(null) ); |
firePropertyChangeEvent( createSelectionChangeEvent(null) ); |
109 |
} |
} |
110 |
|
|
111 |
/** |
/** |
112 |
* Clears the selection and fires an event to every connected listener |
* Clears the selection and fires an event to every connected listener |
113 |
* to update their selection. |
* to update their selection. |
114 |
*/ |
*/ |
115 |
public void clearSelection() { |
public void clearSelection() { |
116 |
clearSelectionObjects(); |
selectionObjects.clear(); |
117 |
refreshSelection(); |
refreshSelection(); |
118 |
} |
} |
119 |
|
|
127 |
* object should ignore this event to avoid event circles (can be {@code null} |
* 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 |
* in case of general refresh events; in this case all connected |
129 |
* components will refresh their selection) |
* components will refresh their selection) |
130 |
* |
* |
131 |
*/ |
*/ |
132 |
public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) { |
public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) { |
133 |
if ( respObject == null ) |
if ( respObject == null ) |
134 |
respObject = this; |
respObject = this; |
135 |
return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null); |
return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null); |
136 |
} |
} |
137 |
|
|
138 |
|
|
139 |
/** |
/** |
140 |
* Returns whether the current selection changes are a part of multiple |
* Returns whether the current selection change is a part of multiple |
141 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
142 |
* called and no {@code DpLayerSelectionEvent} event is fired until |
* and no {@link StyledLayerSelectionEvent} is fired until |
143 |
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
144 |
* is called. |
* is called. |
145 |
*/ |
*/ |
146 |
public boolean getValueIsAdjusting() { |
public boolean getValueIsAdjusting() { |
147 |
return this.valueIsAdjusting; |
return this.valueIsAdjusting; |
148 |
} |
} |
149 |
|
|
150 |
/** |
/** |
151 |
* Sets whether the following selection changes are a part of multiple |
* Sets whether the following selection changes are part of multiple |
152 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
153 |
* called and no {@code DpLayerSelectionEvent} event is fired. |
* and no {@link StyledLayerSelectionEvent} event is fired. |
154 |
* If the value is set from {@code true} to {@code false} an automatic |
* If the value is set from {@code true} to {@code false} an automatic |
155 |
* refresh is initiated. |
* refresh is initiated. |
156 |
* @see #refreshSelection() |
* @see #refreshSelection() |
162 |
refreshSelection(); |
refreshSelection(); |
163 |
} |
} |
164 |
} |
} |
165 |
|
|
|
/** |
|
|
* Clears all selection objects. |
|
|
*/ |
|
|
protected void clearSelectionObjects() { |
|
|
selectionFeatures.clear(); |
|
|
refreshSelection(); |
|
|
} |
|
166 |
|
|
167 |
/** |
/** |
168 |
* Extends the current selection by an individual feature. |
* Extends the current selection by an individual object. |
169 |
* @param feature defines a feature of the layer |
* @param selectedObject defines the (new) selected object |
170 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
171 |
*/ |
*/ |
172 |
public boolean addSelection(E feature) { |
public boolean addSelection(E selectedObject) { |
173 |
if ( selectionFeatures.add(feature) ) { |
|
174 |
|
if ( selectionObjects.add(selectedObject) ) { |
175 |
refreshSelection(); |
refreshSelection(); |
176 |
return true; |
return true; |
177 |
} |
} |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
182 |
* Removes an individual feature from the current selection. |
* Removes an individual object from the current selection. |
183 |
* @param feature defines a feature of the layer |
* @param unselectedObject defines an object which will be unselected |
184 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
185 |
*/ |
*/ |
186 |
public boolean removeSelection(E feature) { |
public boolean removeSelection(E unselectedObject) { |
187 |
if ( selectionFeatures.remove(feature) ) { |
if ( selectionObjects.remove(unselectedObject) ) { |
188 |
refreshSelection(); |
refreshSelection(); |
189 |
return true; |
return true; |
190 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
/** |
/** |
195 |
* Extends the current selection by individual features. |
* Extends the current selection by individual objects. |
196 |
* @param features defines some features of the layer |
* @param selectedObjects defines some objects which will be selected |
197 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
198 |
*/ |
*/ |
199 |
public boolean addSelection(Collection<E> features) { |
public boolean addSelection(Collection<E> selectedObjects) { |
200 |
if ( selectionFeatures.addAll(features) ) { |
if ( selectionObjects.addAll(selectedObjects) ) { |
201 |
refreshSelection(); |
refreshSelection(); |
202 |
return true; |
return true; |
203 |
} |
} |
204 |
return false; |
return false; |
205 |
} |
} |
206 |
|
|
207 |
/** |
/** |
208 |
* Removes individual features from the current selection. |
* Removes individual objects from the current selection. |
209 |
* @param features defines some features of the layer |
* @param unselectedObjects defines some objects which will be unselected |
210 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
211 |
*/ |
*/ |
212 |
public boolean removeSelection(Collection<E> features) { |
public boolean removeSelection(Collection<E> unselectedObjects) { |
213 |
if ( selectionFeatures.removeAll(features) ) { |
if ( selectionObjects.removeAll(unselectedObjects) ) { |
214 |
refreshSelection(); |
refreshSelection(); |
215 |
return true; |
return true; |
216 |
} |
} |
217 |
return false; |
return false; |
218 |
} |
} |
219 |
|
|
220 |
/** |
/** |
221 |
* Returns all selected {@link Feature Features} as list. |
* Returns all selected objects as list. |
222 |
* @return a copy of the original list, so changes have no effect! |
* @return a copy of the original list, so changes have no effect! |
223 |
*/ |
*/ |
224 |
public Vector<E> getSelection() { |
public Vector<E> getSelection() { |
225 |
return new Vector<E>(selectionFeatures); |
return new Vector<E>(selectionObjects); |
226 |
} |
} |
227 |
|
|
228 |
|
/** |
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 |
} |
} |