17 |
import java.beans.PropertyChangeListener; |
import java.beans.PropertyChangeListener; |
18 |
import java.util.Arrays; |
import java.util.Arrays; |
19 |
import java.util.Collection; |
import java.util.Collection; |
20 |
|
import java.util.HashSet; |
21 |
|
import java.util.Set; |
22 |
import java.util.Vector; |
import java.util.Vector; |
23 |
|
|
24 |
import javax.swing.JTable; |
import javax.swing.JTable; |
29 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
30 |
import org.geotools.styling.FeatureTypeStyle; |
import org.geotools.styling.FeatureTypeStyle; |
31 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
32 |
import org.opengis.filter.Filter; |
import org.opengis.filter.identity.FeatureId; |
|
import org.opengis.filter.FilterVisitor; |
|
33 |
|
|
34 |
|
import schmitzm.geotools.FilterUtil; |
35 |
import schmitzm.geotools.gui.JMapPane; |
import schmitzm.geotools.gui.JMapPane; |
36 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
37 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
107 |
*/ |
*/ |
108 |
@Override |
@Override |
109 |
public void propertyChange(PropertyChangeEvent evt) { |
public void propertyChange(PropertyChangeEvent evt) { |
110 |
|
|
111 |
|
if (!isEnabled()) |
112 |
|
return; |
113 |
|
|
114 |
if (!(evt instanceof StyledLayerSelectionEvent)) |
if (!(evt instanceof StyledLayerSelectionEvent)) |
115 |
return; |
return; |
116 |
StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt; |
StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt; |
133 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
134 |
} |
} |
135 |
|
|
136 |
|
/** |
137 |
|
* Changes the Style of the {@link MapLayer} to reflect changes of the |
138 |
|
* selection. |
139 |
|
* |
140 |
|
* @param newSelection |
141 |
|
* A {@link Vector} of Feature-IDs that are selected. |
142 |
|
*/ |
143 |
private void changeLayerStyle(final Vector<String> newSelection) { |
private void changeLayerStyle(final Vector<String> newSelection) { |
144 |
Style selectionMapStyle = null; |
Style selectionMapStyle = null; |
145 |
try { |
try { |
146 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
147 |
|
|
148 |
selectionMapStyle = styledMapLayer.getStyle(); |
selectionMapStyle = styledMapLayer.getStyle(); |
149 |
LOGGER.debug("NO SELECTION .. set to original style directly"); |
// LOGGER.debug("NO SELECTION .. set to original style directly"); |
150 |
|
|
151 |
} else { |
} else { |
152 |
LOGGER.debug("SELECTION .. change style"); |
LOGGER.debug("SELECTION .. change style"); |
163 |
// .createSelectionStyle(normalStyle, geoObj); |
// .createSelectionStyle(normalStyle, geoObj); |
164 |
selectionMapStyle = StylingUtil |
selectionMapStyle = StylingUtil |
165 |
.createSelectionStyle(styledMapLayer.getGeoObject()); |
.createSelectionStyle(styledMapLayer.getGeoObject()); |
166 |
// selectionMapStyle = StylingUtil |
// selectionMapStyle = StylingUtil |
167 |
// .createDefaultStyle(styledMapLayer.getGeoObject()); |
// .createDefaultStyle(styledMapLayer.getGeoObject()); |
168 |
|
|
169 |
selectionMapStyle.getFeatureTypeStyles()[0] |
selectionMapStyle.getFeatureTypeStyles()[0] |
170 |
.setName(SELECTION_STYLING); |
.setName(SELECTION_STYLING); |
171 |
|
|
172 |
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
/** |
173 |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
* |
174 |
.setFilter(new Filter() { |
* Add a Filter to the selectionMapStyle, so that it is only |
175 |
|
* used on objects that are selected. <br/> |
176 |
@Override |
* |
177 |
public Object accept(FilterVisitor visitor, |
* Note 1:<br/> |
178 |
Object extraData) { |
* It is NEVER allowed to GeoTools extend Filter () { .. } (and write |
179 |
return null; |
* tests into the evaluate block). Especially for the |
180 |
} |
* ShapeFileRenderer, we may only use a geotools Filter.<br/> |
181 |
|
* |
182 |
@Override |
* Note 2:<br/> |
183 |
public boolean evaluate(Object obj) { |
* The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects! |
184 |
if (obj instanceof Feature) { |
*/ |
185 |
Feature f = (Feature) obj; |
|
186 |
// TODO BAD CODE! says Martin.. i am fine |
Set<FeatureId> fids = new HashSet<FeatureId>(); |
187 |
// with it.. well.. not great... |
for (String fid : newSelection) { |
188 |
for (String ffID : newSelection) { |
fids.add(FilterUtil.FILTER_FAC2.featureId(fid)); |
189 |
if (ffID.equals(f.getID())) |
} |
|
return true; |
|
|
} |
|
|
return false; |
|
|
|
|
|
} |
|
|
return false; |
|
|
} |
|
190 |
|
|
191 |
}); |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
192 |
|
.setFilter(FilterUtil.FILTER_FAC2.id(fids)); |
193 |
|
|
194 |
FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle |
FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle |
195 |
.getFeatureTypeStyles(); |
.getFeatureTypeStyles(); |
203 |
} else { |
} else { |
204 |
newFeatureTypes = originalFeatureTypeStyles; |
newFeatureTypes = originalFeatureTypeStyles; |
205 |
} |
} |
206 |
|
|
207 |
// The last FTS is the selection FTS |
// The last FTS is the selection FTS |
208 |
newFeatureTypes = LangUtil.extendArray(newFeatureTypes, |
newFeatureTypes = LangUtil.extendArray(newFeatureTypes, |
209 |
selectionMapStyle.getFeatureTypeStyles()); |
selectionMapStyle.getFeatureTypeStyles()); |
219 |
} |
} |
220 |
} |
} |
221 |
|
|
222 |
|
/** |
223 |
|
* Used to synchronize {@link FeatureSelectedEvent}s with the |
224 |
|
* {@link StyledFeatureLayerSelectionModel} |
225 |
|
*/ |
226 |
@Override |
@Override |
227 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
228 |
|
|
229 |
|
// Ignore event if it is caused by us or the synchronizer is disabled. |
230 |
|
if (!isEnabled() || selectionChangeCausedByMe) |
231 |
|
return; |
232 |
|
|
233 |
|
if (!(e instanceof FeatureSelectedEvent)) { |
234 |
|
// LOGGER.debug("Ignoring event " + e); |
235 |
|
return; |
236 |
|
} |
237 |
|
|
238 |
/** |
/** |
239 |
* Only listen to FeatureSelectionEvents if the appropriate tool is |
* Only listen to FeatureSelectedEvents if an appropriate tool is |
240 |
* selected. |
* selected. |
241 |
*/ |
*/ |
242 |
final int selectedTool = toolBar.getSelectedTool(); |
final int selectedTool = toolBar.getSelectedTool(); |
243 |
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
244 |
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR |
|
245 |
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
|
if (!(e instanceof FeatureSelectedEvent)) { |
|
|
// LOGGER.debug("Ignoring event " + e); |
|
|
return; |
|
|
} |
|
249 |
// only listen to events directly coming from JMapPane |
// only listen to events directly coming from JMapPane |
250 |
// selection (ignore selections from FilterDialog) |
// selection (ignore selections from FilterDialog) |
251 |
if (e.getSourceObject() != this.mapPane) |
if (e.getSourceObject() != this.mapPane) |
253 |
|
|
254 |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
255 |
|
|
256 |
// ignore event if it is caused by the DpLayerVectorSelectionModel |
/** |
257 |
if (selectionChangeCausedByMe) |
* Checking, that the FeatureSelectedEvent actually contains features |
258 |
|
* from this layer |
259 |
|
*/ |
260 |
|
final String sourceID = fse.getSourceLayer().getTitle(); |
261 |
|
final String syncForID = mapLayer.getTitle(); |
262 |
|
if (sourceID != null && syncForID != null |
263 |
|
&& !sourceID.equals(syncForID)) { |
264 |
|
LOGGER.debug("Ignoring a FeatureSelectedEvent from " + sourceID); |
265 |
return; |
return; |
266 |
|
} |
267 |
|
|
268 |
LOGGER.debug("Do event " + fse); |
LOGGER.debug("do event " + fse); |
269 |
|
|
270 |
// Avoid event circles in propertyChange(..) |
// Avoid event circles in propertyChange(..) |
271 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
272 |
|
|
273 |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) { |
Collection<Feature> selectionResult = (Collection<Feature>) fse |
274 |
|
.getSelectionResult(); |
275 |
|
|
276 |
|
// reset the selection of the DpLayerSelectionModel |
277 |
|
// layerSelModel.setValueIsAdjusting(true); |
278 |
|
|
279 |
|
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
280 |
|
layerSelModel.setValueIsAdjusting(true); |
281 |
|
for (Feature f : selectionResult) { |
282 |
|
layerSelModel.addSelection(f.getID()); |
283 |
|
} |
284 |
|
layerSelModel.setValueIsAdjusting(false); |
285 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) { |
286 |
|
layerSelModel.setValueIsAdjusting(true); |
287 |
layerSelModel.clearSelection(); |
layerSelModel.clearSelection(); |
|
} else { |
|
|
Collection<Feature> selectionResult = (Collection<Feature>) fse |
|
|
.getSelectionResult(); |
|
|
|
|
|
// reset the selection of the DpLayerSelectionModel |
|
|
// layerSelModel.setValueIsAdjusting(true); |
|
|
|
|
|
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
|
|
layerSelModel.setValueIsAdjusting(true); |
|
|
for (Feature f : selectionResult) { |
|
|
layerSelModel.addSelection(f.getID()); |
|
|
} |
|
|
layerSelModel.setValueIsAdjusting(false); |
|
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) { |
|
|
layerSelModel.setValueIsAdjusting(true); |
|
|
layerSelModel.clearSelection(); |
|
288 |
|
|
289 |
for (Feature f : selectionResult) { |
for (Feature f : selectionResult) { |
290 |
layerSelModel.addSelection(f.getID()); |
layerSelModel.addSelection(f.getID()); |
291 |
} |
} |
292 |
|
|
293 |
LOGGER.debug("Setting selection to " + selectionResult.size()); |
LOGGER.debug("Setting selection to " + selectionResult.size()); |
294 |
layerSelModel.setValueIsAdjusting(false); |
layerSelModel.setValueIsAdjusting(false); |
295 |
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
296 |
layerSelModel.setValueIsAdjusting(true); |
layerSelModel.setValueIsAdjusting(true); |
297 |
for (Feature f : selectionResult) { |
for (Feature f : selectionResult) { |
298 |
layerSelModel.removeSelection(f.getID()); |
layerSelModel.removeSelection(f.getID()); |
|
} |
|
|
layerSelModel.setValueIsAdjusting(false); |
|
299 |
} |
} |
300 |
} // els it's not a Clear Selection command |
layerSelModel.setValueIsAdjusting(false); |
301 |
|
} |
302 |
|
|
303 |
// Show selected features in the map, which is not automatically done by |
// Show selected features in the map, which is not automatically done by |
304 |
// the origin: FeatureSelectedEvent |
// the origin: FeatureSelectedEvent |
307 |
// Danger of event circles in propertyChange(..) banned |
// Danger of event circles in propertyChange(..) banned |
308 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
309 |
} |
} |
310 |
|
|
311 |
} |
} |