58 |
|
|
59 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
60 |
import org.geotools.feature.FeatureIterator; |
import org.geotools.feature.FeatureIterator; |
61 |
|
import org.geotools.filter.FidFilterImpl; |
62 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
63 |
import org.geotools.renderer.GTRenderer; |
import org.geotools.renderer.GTRenderer; |
64 |
import org.geotools.renderer.label.LabelCacheImpl; |
import org.geotools.renderer.label.LabelCacheImpl; |
68 |
import org.geotools.styling.visitor.DuplicatingStyleVisitor; |
import org.geotools.styling.visitor.DuplicatingStyleVisitor; |
69 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
70 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
71 |
|
import org.opengis.filter.Filter; |
72 |
|
import org.opengis.filter.Not; |
73 |
import org.opengis.filter.identity.FeatureId; |
import org.opengis.filter.identity.FeatureId; |
74 |
|
|
75 |
import schmitzm.geotools.FilterUtil; |
import schmitzm.geotools.FilterUtil; |
76 |
import schmitzm.geotools.GTUtil; |
import schmitzm.geotools.GTUtil; |
77 |
import schmitzm.geotools.gui.JMapPane; |
import schmitzm.geotools.gui.SelectableXMapPane; |
78 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
|
import schmitzm.geotools.map.event.JMapPaneEvent; |
|
79 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
80 |
|
import schmitzm.geotools.map.event.MapPaneEvent; |
81 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
82 |
import skrueger.geotools.MapPaneToolBar; |
import skrueger.geotools.MapPaneToolBar; |
83 |
import skrueger.geotools.StyledFeaturesInterface; |
import skrueger.geotools.StyledFeaturesInterface; |
|
import skrueger.geotools.StyledLayerInterface; |
|
84 |
|
|
85 |
/** |
/** |
86 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
103 |
public class FeatureMapLayerSelectionSynchronizer extends |
public class FeatureMapLayerSelectionSynchronizer extends |
104 |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
105 |
implements JMapPaneListener { |
implements JMapPaneListener { |
106 |
public static final String SELECTION_STYLING = "SELECTION"; |
|
107 |
|
/** |
108 |
|
* This constant is set as the {@link FeatureTypeStyle#getName()} attribute |
109 |
|
* in the {@link FeatureTypeStyle}s that only exist to present the selected |
110 |
|
* features |
111 |
|
**/ |
112 |
|
public static final String SELECTION_STYLING_FTS_NAME = "SELECTION"; |
113 |
|
|
114 |
/** |
/** |
115 |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
116 |
* model. |
* model. |
117 |
*/ |
*/ |
118 |
protected final MapLayer mapLayer; |
protected final MapLayer mapLayer; |
119 |
protected final StyledFeaturesInterface<?> styledLayer; |
protected final StyledFeaturesInterface<?> styledLayer; |
120 |
protected final JMapPane mapPane; |
protected final SelectableXMapPane mapPane; |
121 |
private final MapPaneToolBar toolBar; |
private final MapPaneToolBar toolBar; |
|
private final HashMap<Object, Object> defaultGTRenderingHints; |
|
122 |
|
|
123 |
/** |
/** |
124 |
* Creates a new synchronizer |
* Creates a new synchronizer |
133 |
public FeatureMapLayerSelectionSynchronizer( |
public FeatureMapLayerSelectionSynchronizer( |
134 |
StyledFeatureLayerSelectionModel layerSelModel, |
StyledFeatureLayerSelectionModel layerSelModel, |
135 |
StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer, |
StyledFeaturesInterface<?> styledLayer, MapLayer mapLayer, |
136 |
JMapPane mapPane, MapPaneToolBar toolBar, |
SelectableXMapPane mapPane, MapPaneToolBar toolBar) { |
|
HashMap<Object, Object> defaultGTRenderingHints) { |
|
137 |
|
|
138 |
super(layerSelModel); |
super(layerSelModel); |
139 |
this.styledLayer = styledLayer; |
this.styledLayer = styledLayer; |
141 |
this.mapLayer = mapLayer; |
this.mapLayer = mapLayer; |
142 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
143 |
this.toolBar = toolBar; |
this.toolBar = toolBar; |
|
if (defaultGTRenderingHints != null) |
|
|
this.defaultGTRenderingHints = defaultGTRenderingHints; |
|
|
else |
|
|
this.defaultGTRenderingHints = new HashMap<Object, Object>(); |
|
144 |
} |
} |
145 |
|
|
146 |
/** |
/** |
192 |
try { |
try { |
193 |
|
|
194 |
Style originalStyle = mapLayer.getStyle(); |
Style originalStyle = mapLayer.getStyle(); |
195 |
|
|
196 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
197 |
|
|
198 |
// Check if the Style contains a SELECTION FTS |
// Check if the Style contains a SELECTION FTS |
202 |
|
|
203 |
for (FeatureTypeStyle fts : clone) { |
for (FeatureTypeStyle fts : clone) { |
204 |
if (fts.getName() != null |
if (fts.getName() != null |
205 |
&& fts.getName().equals(SELECTION_STYLING)) { |
&& fts.getName().equals(SELECTION_STYLING_FTS_NAME)) { |
206 |
originalStyle.featureTypeStyles().remove(fts); |
originalStyle.featureTypeStyles().remove(fts); |
207 |
|
|
208 |
replaceRenderer(); |
mapLayer.setStyle(originalStyle); |
209 |
// mapPane.refresh(); |
// replaceRenderer(); |
210 |
|
// // mapPane.refresh(); |
211 |
|
|
212 |
return; |
return; |
213 |
} |
} |
214 |
} |
} |
215 |
|
|
216 |
} else { |
} else { |
217 |
|
|
218 |
LOGGER.debug("SELECTION .. change style"); |
LOGGER.debug("SELECTION .. change style"); |
219 |
|
|
220 |
|
// Saving a repaint if the selection didn't change |
221 |
|
if (!selectionChanged(newSelection, originalStyle)) { |
222 |
|
return; |
223 |
|
} |
224 |
|
|
225 |
// We take Style from the MapLayer that is displayed at the |
// We take Style from the MapLayer that is displayed at the |
226 |
// moment. We do not use the styledLayer.getStyle, because in |
// moment. We do not use the styledLayer.getStyle, because in |
227 |
// the atlas, this always return the default style, but |
// the atlas, this always return the default style, but |
232 |
FeatureTypeStyle selectionFTStyle = StylingUtil |
FeatureTypeStyle selectionFTStyle = StylingUtil |
233 |
.createSelectionStyle(styledLayer.getGeoObject()); |
.createSelectionStyle(styledLayer.getGeoObject()); |
234 |
|
|
235 |
selectionFTStyle.setName(SELECTION_STYLING); |
selectionFTStyle.setName(SELECTION_STYLING_FTS_NAME); |
236 |
|
|
237 |
/** |
/** |
238 |
* |
* |
243 |
* inverted to be shorter. |
* inverted to be shorter. |
244 |
* |
* |
245 |
*/ |
*/ |
|
|
|
246 |
final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer |
final FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollectionFiltered = styledLayer |
247 |
.getFeatureCollectionFiltered(); |
.getFeatureCollectionFiltered(); |
248 |
if (newSelection.size() > featureCollectionFiltered.size() / 2) { |
if (newSelection.size() > featureCollectionFiltered.size() / 2) { |
280 |
boolean foundAndReplaced = false; |
boolean foundAndReplaced = false; |
281 |
for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) { |
for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) { |
282 |
if (fts.getName() != null |
if (fts.getName() != null |
283 |
&& fts.getName().equals(SELECTION_STYLING)) { |
&& fts.getName().equals(SELECTION_STYLING_FTS_NAME)) { |
284 |
foundAndReplaced = true; |
foundAndReplaced = true; |
285 |
fts.rules().clear(); |
fts.rules().clear(); |
286 |
fts.rules().addAll(selectionFTStyle.rules()); |
fts.rules().addAll(selectionFTStyle.rules()); |
297 |
DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor(); |
DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor(); |
298 |
dsv.visit(originalStyle); |
dsv.visit(originalStyle); |
299 |
Style newStyle = (Style) dsv.getCopy(); |
Style newStyle = (Style) dsv.getCopy(); |
300 |
// |
|
301 |
// try { |
// SK-Debug |
302 |
//// |
try { |
303 |
//// StylingUtil.saveStyleToSLD(newStyle, new File( |
// |
304 |
//// "/home/stefan/Desktop/selection.sld")); |
StylingUtil.saveStyleToSLD(newStyle, new File( |
305 |
// } catch (Exception e) { |
"/home/stefan/Desktop/selection.sld")); |
306 |
// } |
} catch (Exception e) { |
307 |
// |
} |
308 |
// // DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor(); |
|
309 |
|
// DuplicatingStyleVisitor dsv = new DuplicatingStyleVisitor(); |
310 |
// dsv.visit(originalStyle); |
// dsv.visit(originalStyle); |
311 |
// Style newStyle = originalStyle; |
// Style newStyle = originalStyle; |
312 |
|
|
322 |
} |
} |
323 |
} |
} |
324 |
|
|
325 |
private void replaceRenderer() { |
/** |
326 |
// |
* Analyzes whether the selection has changed in comparison to the selection |
327 |
// // Has to be done before we apply the new Renderer |
* stored in the mapLayer.Style |
328 |
// mapLayer.setStyle(style); |
* |
329 |
|
* @param newSelection a List<String> of all newly selected FIDs |
330 |
|
* @param originalStyle the original {@link Style} that has an earlier selection coded into the {@link Style} |
331 |
|
* @return <code>true</code> if changed. |
332 |
|
*/ |
333 |
|
private boolean selectionChanged(Vector<String> newSelection, |
334 |
|
Style originalStyle) { |
335 |
|
|
336 |
GTRenderer oldRenderer = mapPane.getRenderer(); |
boolean SELECTION_STYLING_foundInMapStyle = false; |
337 |
|
|
338 |
/** |
/** |
339 |
* Explicitly putting a new instance of LabelCacheDefault into the |
* testing, whether the selection really changed. If not, we can save |
340 |
* renderer instance, so JMapPane doesn't reuse the old one. This is |
* one rendering! |
|
* very useful when changing the TextSymbolizer with AtlasStyler<br/> |
|
|
* SK 9.7.09: It's not enought to user LabelCache.clear(). We can not |
|
|
* reuse the old Renderer - better to create a new one! |
|
341 |
*/ |
*/ |
342 |
final GTRenderer newRenderer = GTUtil.createGTRenderer(); |
for (FeatureTypeStyle fts : originalStyle.featureTypeStyles()) { |
343 |
|
|
344 |
final HashMap<Object, Object> rendererHints = defaultGTRenderingHints; |
if (fts.getName() != null |
345 |
rendererHints.put(StreamingRenderer.LABEL_CACHE_KEY, |
&& fts.getName().equals(SELECTION_STYLING_FTS_NAME)) { |
|
new LabelCacheImpl()); |
|
346 |
|
|
347 |
newRenderer.setRendererHints(rendererHints); |
SELECTION_STYLING_foundInMapStyle = true; |
|
mapPane.setRenderer(newRenderer); |
|
348 |
|
|
349 |
if (oldRenderer != null) { |
Filter filter = fts.rules().get(0).getFilter(); |
350 |
|
if (filter instanceof Not) { |
351 |
|
FidFilterImpl antiOrigFidsFilter = (FidFilterImpl) ((Not) filter) |
352 |
|
.getFilter(); |
353 |
|
|
354 |
RenderingHints java2DHints = oldRenderer.getJava2DHints(); |
// Check one way |
355 |
if (java2DHints != null) { |
final Set<String> antiFids = antiOrigFidsFilter |
356 |
newRenderer.setJava2DHints(java2DHints); |
.getFidsSet(); |
357 |
} |
|
358 |
|
if (antiFids.isEmpty() && !newSelection.isEmpty()) |
359 |
|
return true; |
360 |
|
|
361 |
|
for (String fid : antiFids) { |
362 |
|
if (newSelection.contains(fid)) { |
363 |
|
return true; |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
|
// Check the other way |
368 |
|
for (String fid : newSelection) { |
369 |
|
if (antiFids.contains(fid)) { |
370 |
|
return true; |
371 |
|
} |
372 |
|
} |
373 |
|
|
374 |
|
if (antiFids.size() + newSelection.size() != styledLayer |
375 |
|
.getFeatureCollectionFiltered().size()) |
376 |
|
return true; |
377 |
|
|
378 |
|
} else { |
379 |
|
FidFilterImpl origFidsFilter = (FidFilterImpl) filter; |
380 |
|
|
381 |
|
// Check one way |
382 |
|
final Set<String> fids = origFidsFilter.getFidsSet(); |
383 |
|
|
384 |
|
if (fids.isEmpty() && newSelection.isEmpty()) |
385 |
|
return false; |
386 |
|
if (fids.size() != newSelection.size()) |
387 |
|
return true; |
388 |
|
|
389 |
|
for (String fid : fids) { |
390 |
|
if (!newSelection.contains(fid)) { |
391 |
|
return true; |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
// Check the other way |
396 |
|
for (String fid : newSelection) { |
397 |
|
if (!fids.contains(fid)) { |
398 |
|
return true; |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
|
} |
403 |
|
|
404 |
oldRenderer.setContext(null); |
break; |
405 |
oldRenderer = null; |
} |
406 |
} |
} |
407 |
|
|
408 |
mapPane.refresh(); |
if (!SELECTION_STYLING_foundInMapStyle && !newSelection.isEmpty()) |
409 |
|
return true; |
410 |
|
|
411 |
|
return false; |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
|
* Replaces the local renderer |
416 |
|
*/ |
417 |
|
private void replaceRenderer() { |
418 |
} |
} |
419 |
|
|
420 |
/** |
/** |
422 |
* {@link StyledFeatureLayerSelectionModel} |
* {@link StyledFeatureLayerSelectionModel} |
423 |
*/ |
*/ |
424 |
@Override |
@Override |
425 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(MapPaneEvent e) { |
426 |
|
|
427 |
// Ignore event if it is caused by us or the synchronizer is disabled. |
// Ignore event if it is caused by us or the synchronizer is disabled. |
428 |
if (!isEnabled() || selectionChangeCausedByMe) |
if (!isEnabled() || selectionChangeCausedByMe) |
449 |
if (e.getSourceObject() != this.mapPane) |
if (e.getSourceObject() != this.mapPane) |
450 |
return; |
return; |
451 |
|
|
|
|
|
452 |
/** |
/** |
453 |
* Checking, that the FeatureSelectedEvent actually contains features |
* Checking, that the FeatureSelectedEvent actually contains features |
454 |
* from this layer |
* from this layer |