1 |
/** |
/** |
2 |
Copyright 2008 Stefan Alfons Krüger |
Copyright 2008 Stefan Alfons Krüger |
3 |
|
|
4 |
atlas-framework - This file is part of the Atlas Framework |
atlas-framework - This file is part of the Atlas Framework |
5 |
|
|
6 |
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. |
15 |
package skrueger.geotools.selection; |
package skrueger.geotools.selection; |
16 |
|
|
17 |
import java.beans.PropertyChangeEvent; |
import java.beans.PropertyChangeEvent; |
18 |
|
import java.beans.PropertyChangeListener; |
19 |
import java.util.Collection; |
import java.util.Collection; |
20 |
import java.util.HashSet; |
import java.util.HashSet; |
|
import java.util.Set; |
|
21 |
import java.util.Vector; |
import java.util.Vector; |
22 |
|
|
23 |
|
import javax.swing.event.EventListenerList; |
24 |
|
|
25 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
26 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
27 |
|
|
28 |
import schmitzm.swing.event.PropertyChangeEmitter; |
import schmitzm.swing.event.PropertyChangeEmitter; |
29 |
|
import skrueger.geotools.StyledLayerInterface; |
30 |
|
|
31 |
/** |
/** |
32 |
* 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}. |
33 |
* in the several atlas components and informs the appropriate components |
* Several components can connect to this model to keep their selection synchronized. |
34 |
* to keep the selection synchronized. |
* @see #addSelectionListener(java.beans.PropertyChangeListener) |
35 |
|
* @see StyledLayerSelectionModelSynchronizer |
36 |
* @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) |
37 |
*/ |
*/ |
38 |
public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter { |
public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter<StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>>> { |
39 |
/** A static logger for this class */ |
/** A static logger for this class */ |
40 |
protected final Logger LOGGER = Logger.getLogger(this.getClass()); |
protected final Logger LOGGER = Logger.getLogger(this.getClass()); |
41 |
|
|
|
/** Holds the current selection as {@link Feature}-list. This |
|
|
* is the basis to create the other selection objects. */ |
|
|
protected final Set<E> selectionFeatures; |
|
|
|
|
42 |
/** Indicates that the selection has changed in {@link PropertyChangeEvent}. */ |
/** Indicates that the selection has changed in {@link PropertyChangeEvent}. */ |
43 |
public final static String SELECTION = "SELECTION"; |
public final static String SELECTION = "SELECTION"; |
44 |
|
|
45 |
|
|
46 |
|
/** Holds the connected listeners. */ |
47 |
|
protected EventListenerList listenerList = new EventListenerList(); |
48 |
|
|
49 |
|
/** Holds the current selection (e.g. {@link Feature Features}). */ |
50 |
|
protected final HashSet<E> selectionObjects; |
51 |
|
|
52 |
|
/** Holds the styled map whose selection is managed by this class. */ |
53 |
|
protected final StyledLayerInterface<?> styledLayer; |
54 |
|
|
55 |
|
|
56 |
/** Indicates whether a selection change is a part of multiple |
/** Indicates whether a selection change is a part of multiple |
57 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has NO effect |
58 |
* called and no {@code DpLayerSelectionEvent} event is fired until |
* until {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
|
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
|
59 |
* is called. */ |
* is called. */ |
60 |
private boolean valueIsAdjusting = false; |
private boolean valueIsAdjusting = false; |
61 |
|
|
|
public StyledLayerSelectionModel() { |
|
|
this.selectionFeatures = new HashSet<E>(); |
|
|
} |
|
62 |
/** |
/** |
63 |
* Reset all selection objects and fires an event to every connected |
* Creates a new selection model. |
64 |
* listener to update their selection according to the selection |
* @param styledLayer styled layer the selection is controlled of |
65 |
* of this manager. <b>Does nothing if adjusting is <code>true</code></b>. |
*/ |
66 |
|
public StyledLayerSelectionModel(StyledLayerInterface<?> styledLayer) { |
67 |
|
this.selectionObjects = new HashSet<E>(); |
68 |
|
this.styledLayer = styledLayer; |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
|
* Fires an event to every connected listener to update their selection |
73 |
|
* according to the selection of this model.<br> |
74 |
|
* <b>Does nothing if adjusting is <code>true</code></b>. |
75 |
*/ |
*/ |
76 |
public void refreshSelection() { |
public void refreshSelection() { |
77 |
if ( getValueIsAdjusting() ) |
if ( getValueIsAdjusting() ) |
78 |
return; |
return; |
79 |
firePropertyChangeEvent( createSelectionChangeEvent(null) ); |
firePropertyChangeEvent( createSelectionChangeEvent(null) ); |
80 |
} |
} |
81 |
|
|
82 |
/** |
/** |
83 |
* Clears the selection and fires an event to every connected listener |
* Clears the selection and fires an event to every connected listener |
84 |
* to update their selection. |
* to update their selection. |
85 |
*/ |
*/ |
86 |
public void clearSelection() { |
public void clearSelection() { |
87 |
clearSelectionObjects(); |
selectionObjects.clear(); |
88 |
refreshSelection(); |
refreshSelection(); |
89 |
} |
} |
90 |
|
|
98 |
* 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} |
99 |
* in case of general refresh events; in this case all connected |
* in case of general refresh events; in this case all connected |
100 |
* components will refresh their selection) |
* components will refresh their selection) |
101 |
* |
* |
102 |
*/ |
*/ |
103 |
public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) { |
public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) { |
104 |
if ( respObject == null ) |
if ( respObject == null ) |
105 |
respObject = this; |
respObject = this; |
106 |
return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null); |
return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null); |
107 |
} |
} |
108 |
|
|
109 |
|
|
110 |
/** |
/** |
111 |
* Returns whether the current selection changes are a part of multiple |
* Returns whether the current selection change is a part of multiple |
112 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
113 |
* called and no {@code DpLayerSelectionEvent} event is fired until |
* and no {@link StyledLayerSelectionEvent} is fired until |
114 |
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
* {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)} |
115 |
* is called. |
* is called. |
116 |
*/ |
*/ |
117 |
public boolean getValueIsAdjusting() { |
public boolean getValueIsAdjusting() { |
118 |
return this.valueIsAdjusting; |
return this.valueIsAdjusting; |
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* Sets whether the following selection changes are a part of multiple |
* Sets whether the following selection changes are part of multiple |
123 |
* changes. If {@code true} {@link #resetSelectionObjects()} is not |
* changes. If {@code true} {@link #refreshSelection()} has no effect |
124 |
* called and no {@code DpLayerSelectionEvent} event is fired. |
* and no {@link StyledLayerSelectionEvent} event is fired. |
125 |
* 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 |
126 |
* refresh is initiated. |
* refresh is initiated. |
127 |
* @see #refreshSelection() |
* @see #refreshSelection() |
133 |
refreshSelection(); |
refreshSelection(); |
134 |
} |
} |
135 |
} |
} |
136 |
|
|
|
/** |
|
|
* Clears all selection objects. |
|
|
*/ |
|
|
protected void clearSelectionObjects() { |
|
|
selectionFeatures.clear(); |
|
|
refreshSelection(); |
|
|
} |
|
137 |
|
|
138 |
/** |
/** |
139 |
* Extends the current selection by an individual feature. |
* Extends the current selection by an individual object. |
140 |
* @param feature defines a feature of the layer |
* @param selectedObject defines the (new) selected object |
141 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
142 |
*/ |
*/ |
143 |
public boolean addSelection(E feature) { |
public boolean addSelection(E selectedObject) { |
144 |
if ( selectionFeatures.add(feature) ) { |
|
145 |
|
if ( selectionObjects.add(selectedObject) ) { |
146 |
refreshSelection(); |
refreshSelection(); |
147 |
return true; |
return true; |
148 |
} |
} |
150 |
} |
} |
151 |
|
|
152 |
/** |
/** |
153 |
* Removes an individual feature from the current selection. |
* Removes an individual object from the current selection. |
154 |
* @param feature defines a feature of the layer |
* @param unselectedObject defines an object which will be unselected |
155 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
156 |
*/ |
*/ |
157 |
public boolean removeSelection(E feature) { |
public boolean removeSelection(E unselectedObject) { |
158 |
if ( selectionFeatures.remove(feature) ) { |
if ( selectionObjects.remove(unselectedObject) ) { |
159 |
refreshSelection(); |
refreshSelection(); |
160 |
return true; |
return true; |
161 |
} |
} |
163 |
} |
} |
164 |
|
|
165 |
/** |
/** |
166 |
* Extends the current selection by individual features. |
* Extends the current selection by individual objects. |
167 |
* @param features defines some features of the layer |
* @param selectedObjects defines some objects which will be selected |
168 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
169 |
*/ |
*/ |
170 |
public boolean addSelection(Collection<E> features) { |
public boolean addSelection(Collection<E> selectedObjects) { |
171 |
if ( selectionFeatures.addAll(features) ) { |
if ( selectionObjects.addAll(selectedObjects) ) { |
172 |
refreshSelection(); |
refreshSelection(); |
173 |
return true; |
return true; |
174 |
} |
} |
175 |
return false; |
return false; |
176 |
} |
} |
177 |
|
|
178 |
/** |
/** |
179 |
* Removes individual features from the current selection. |
* Removes individual objects from the current selection. |
180 |
* @param features defines some features of the layer |
* @param unselectedObjects defines some objects which will be unselected |
181 |
* @return {@code true} if selection has changed by calling this method |
* @return {@code true} if selection has changed by calling this method |
182 |
*/ |
*/ |
183 |
public boolean removeSelection(Collection<E> features) { |
public boolean removeSelection(Collection<E> unselectedObjects) { |
184 |
if ( selectionFeatures.removeAll(features) ) { |
if ( selectionObjects.removeAll(unselectedObjects) ) { |
185 |
refreshSelection(); |
refreshSelection(); |
186 |
return true; |
return true; |
187 |
} |
} |
188 |
return false; |
return false; |
189 |
} |
} |
190 |
|
|
191 |
/** |
/** |
192 |
* Returns all selected {@link Feature Features} as list. |
* Returns all selected objects as list. |
193 |
* @return a copy of the original list, so changes have no effect! |
* @return a copy of the original list, so changes have no effect! |
194 |
*/ |
*/ |
195 |
public Vector<E> getSelection() { |
public Vector<E> getSelection() { |
196 |
return new Vector<E>(selectionFeatures); |
return new Vector<E>(selectionObjects); |
197 |
} |
} |
198 |
|
|
199 |
|
/** |
200 |
|
* Adds a listener to the listener list. Only alias for {@link #addListener(PropertyChangeListener)}. |
201 |
|
* @param listener a listener |
202 |
|
*/ |
203 |
|
public void addSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) { |
204 |
|
this.addListener(listener); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
208 |
|
* Removes a listener from the listener list. Only alias for {@link #removeListener(PropertyChangeListener)}. |
209 |
|
* @param listener a listener |
210 |
|
*/ |
211 |
|
public void removeSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) { |
212 |
|
this.removeListener(listener); |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
} |
} |