15 |
|
|
16 |
import java.beans.PropertyChangeEvent; |
import java.beans.PropertyChangeEvent; |
17 |
import java.beans.PropertyChangeListener; |
import java.beans.PropertyChangeListener; |
18 |
|
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; |
25 |
import javax.swing.ListSelectionModel; |
import javax.swing.ListSelectionModel; |
|
import javax.swing.event.ListSelectionEvent; |
|
26 |
import javax.swing.event.ListSelectionListener; |
import javax.swing.event.ListSelectionListener; |
|
import javax.swing.text.Utilities; |
|
27 |
|
|
28 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
29 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
30 |
import org.geotools.styling.PolygonSymbolizer; |
import org.geotools.styling.FeatureTypeStyle; |
|
import org.geotools.styling.Rule; |
|
|
import org.geotools.styling.RuleImpl; |
|
31 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
32 |
import org.geotools.styling.StyleImpl; |
import org.opengis.filter.identity.FeatureId; |
|
import org.geotools.styling.Symbolizer; |
|
|
import org.opengis.filter.Filter; |
|
|
import org.opengis.filter.FilterVisitor; |
|
|
import org.opengis.layer.StyleURL; |
|
33 |
|
|
34 |
import schmitzm.geotools.FilterUtil; |
import schmitzm.geotools.FilterUtil; |
|
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
|
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; |
38 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
39 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
40 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
41 |
import skrueger.geotools.StyledFeatureCollectionInterface; |
import skrueger.geotools.MapPaneToolBar; |
42 |
import skrueger.geotools.StyledMapInterface; |
import skrueger.geotools.StyledLayerInterface; |
|
import skrueger.geotools.selection.StyledFeatureLayerSelectionModel; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionEvent; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionModel; |
|
43 |
|
|
44 |
/** |
/** |
45 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
62 |
public class FeatureMapLayerSelectionSynchronizer extends |
public class FeatureMapLayerSelectionSynchronizer extends |
63 |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
64 |
implements JMapPaneListener { |
implements JMapPaneListener { |
65 |
|
public static final String SELECTION_STYLING = "SELECTION"; |
66 |
/** |
/** |
67 |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
68 |
* model. |
* model. |
69 |
*/ |
*/ |
70 |
protected final MapLayer mapLayer; |
protected final MapLayer mapLayer; |
71 |
protected final StyledMapInterface<?> styledMapLayer; |
protected final StyledLayerInterface<?> styledLayer; |
72 |
protected final JMapPane mapPane; |
protected final JMapPane mapPane; |
73 |
|
private final MapPaneToolBar toolBar; |
74 |
|
|
75 |
/** |
/** |
76 |
* Creates a new synchronizer |
* Creates a new synchronizer |
84 |
*/ |
*/ |
85 |
public FeatureMapLayerSelectionSynchronizer( |
public FeatureMapLayerSelectionSynchronizer( |
86 |
StyledFeatureLayerSelectionModel layerSelModel, |
StyledFeatureLayerSelectionModel layerSelModel, |
87 |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
StyledLayerInterface<?> styledLayer, MapLayer mapLayer, |
88 |
JMapPane mapPane) { |
JMapPane mapPane, MapPaneToolBar toolBar) { |
89 |
|
|
90 |
super(layerSelModel); |
super(layerSelModel); |
91 |
this.styledMapLayer = styledMapLayer; |
this.styledLayer = styledLayer; |
92 |
|
|
93 |
this.mapLayer = mapLayer; |
this.mapLayer = mapLayer; |
94 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
95 |
|
this.toolBar = toolBar; |
96 |
} |
} |
97 |
|
|
98 |
/** |
/** |
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; |
120 |
return; |
return; |
121 |
// Apply new selection on table (except this event is one of |
// Apply new selection on table (except this event is one of |
122 |
// some more events) |
// some more events) |
123 |
Vector<Feature> newSelection = layerSelModel.getSelection(); |
Vector<String> newSelection = layerSelModel.getSelection(); |
124 |
if (newSelection == null) |
if (newSelection == null) |
125 |
return; |
return; |
126 |
|
|
133 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
134 |
} |
} |
135 |
|
|
136 |
private void changeLayerStyle(final Vector<Feature> newSelection) { |
/** |
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) { |
144 |
Style selectionMapStyle = null; |
Style selectionMapStyle = null; |
145 |
try { |
try { |
146 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
147 |
|
|
148 |
selectionMapStyle = styledMapLayer.getStyle(); |
selectionMapStyle = styledLayer.getStyle(); |
149 |
LOGGER.debug("NO SELECTION .. reset original style"); |
// LOGGER.debug("NO SELECTION .. set to original style directly"); |
150 |
|
|
151 |
} else { |
} else { |
152 |
LOGGER.debug("SELECTION .. change style"); |
LOGGER.debug("SELECTION .. change style"); |
153 |
|
|
154 |
Style originalStyle = styledMapLayer.getStyle(); |
// We take Style from the MapLayer that is displayed at the |
155 |
|
// moment. We do not use the styledLayer.getStyle, because in |
156 |
|
// the atlas, this always return the default style, but |
157 |
|
// additional styles might be selected. |
158 |
|
// Taking the style from the mapLayer indicated, that we have to |
159 |
|
// remove any selection rules first. |
160 |
|
Style originalStyle = mapLayer.getStyle(); |
161 |
|
|
162 |
// TODO This clone is a deep clone and it is slow.. |
// TODO The default style is not good here. We need |
163 |
|
// .createSelectionStyle(normalStyle, geoObj); |
164 |
selectionMapStyle = StylingUtil |
selectionMapStyle = StylingUtil |
165 |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
.createSelectionStyle(styledLayer.getGeoObject()); |
166 |
|
// selectionMapStyle = StylingUtil |
167 |
// Symbolizer sss = selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0]; |
// .createDefaultStyle(styledLayer.getGeoObject()); |
168 |
// if (sss instanceof PolygonSymbolizer) { |
|
169 |
// PolygonSymbolizer ps = (PolygonSymbolizer) sss; |
selectionMapStyle.getFeatureTypeStyles()[0] |
170 |
// ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5)); |
.setName(SELECTION_STYLING); |
171 |
// } |
|
172 |
|
/** |
173 |
|
* |
174 |
|
* Add a Filter to the selectionMapStyle, so that it is only |
175 |
|
* used on objects that are selected. <br/> |
176 |
|
* |
177 |
|
* Note 1:<br/> |
178 |
|
* It is NEVER allowed to GeoTools extend Filter () { .. } (and write |
179 |
|
* tests into the evaluate block). Especially for the |
180 |
|
* ShapeFileRenderer, we may only use a geotools Filter.<br/> |
181 |
|
* |
182 |
|
* Note 2:<br/> |
183 |
|
* The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects! |
184 |
|
*/ |
185 |
|
|
186 |
|
Set<FeatureId> fids = new HashSet<FeatureId>(); |
187 |
|
for (String fid : newSelection) { |
188 |
|
fids.add(FilterUtil.FILTER_FAC2.featureId(fid)); |
189 |
|
} |
190 |
|
|
|
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
|
191 |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
192 |
.setFilter(new Filter() { |
.setFilter(FilterUtil.FILTER_FAC2.id(fids)); |
|
|
|
|
@Override |
|
|
public Object accept(FilterVisitor visitor, |
|
|
Object extraData) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
@Override |
|
|
public boolean evaluate(Object obj) { |
|
|
if (obj instanceof Feature) { |
|
|
Feature f = (Feature) obj; |
|
|
// TODO BAD CODE |
|
|
for (Feature ff : newSelection) { |
|
|
if (ff.getID().equals(f.getID())) |
|
|
return true; |
|
|
} |
|
|
return false; |
|
|
|
|
|
} |
|
|
return false; |
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray( |
|
|
originalStyle |
|
|
.getFeatureTypeStyles(), selectionMapStyle.getFeatureTypeStyles())); |
|
193 |
|
|
194 |
// selectionMapStyle.setFeatureTypeStyles(originalStyle.getF) |
FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle |
195 |
|
.getFeatureTypeStyles(); |
196 |
|
FeatureTypeStyle[] newFeatureTypes; |
197 |
|
if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1] |
198 |
|
.getName() != null |
199 |
|
&& originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1] |
200 |
|
.getName().equals(SELECTION_STYLING)) { |
201 |
|
newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles, |
202 |
|
originalFeatureTypeStyles.length - 1); |
203 |
|
} else { |
204 |
|
newFeatureTypes = originalFeatureTypeStyles; |
205 |
|
} |
206 |
|
|
207 |
|
// The last FTS is the selection FTS |
208 |
|
newFeatureTypes = LangUtil.extendArray(newFeatureTypes, |
209 |
|
selectionMapStyle.getFeatureTypeStyles()); |
210 |
|
|
211 |
// Flat copy the style |
selectionMapStyle.setFeatureTypeStyles(newFeatureTypes); |
|
|
|
|
// selectionMapStyle = |
|
|
// StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject()); |
|
212 |
|
|
213 |
} |
} |
214 |
|
|
219 |
} |
} |
220 |
} |
} |
221 |
|
|
222 |
// |
/** |
223 |
// /** |
* Used to synchronize {@link FeatureSelectedEvent}s with the |
224 |
// * Called when the table selection is changed by the user. When calling |
* {@link StyledFeatureLayerSelectionModel} |
225 |
// this |
*/ |
|
// * method changes the selection of the {@link StyledLayerSelectionModel}. |
|
|
// * |
|
|
// * @param evt |
|
|
// * an event |
|
|
// */ |
|
|
// @Override |
|
|
// public void valueChanged(ListSelectionEvent evt) { |
|
|
// |
|
|
// } |
|
|
|
|
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)) { |
if (!(e instanceof FeatureSelectedEvent)) { |
234 |
// LOGGER.debug("Ignoring event " + e); |
// LOGGER.debug("Ignoring event " + e); |
235 |
|
return; |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* Only listen to FeatureSelectedEvents if an appropriate tool is |
240 |
|
* selected. |
241 |
|
*/ |
242 |
|
final int selectedTool = toolBar.getSelectedTool(); |
243 |
|
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
244 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
245 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
246 |
return; |
return; |
247 |
} |
} |
248 |
|
|
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) |
252 |
return; |
return; |
253 |
|
|
254 |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
|
// LOGGER.debug("Do event " + fse); |
|
|
// LOGGER.debug("Do event " + fse.getSelectionResult()); |
|
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); |
269 |
|
|
270 |
// Avoid event circles in propertyChange(..) |
// Avoid event circles in propertyChange(..) |
271 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
272 |
|
|
273 |
|
Collection<Feature> selectionResult = (Collection<Feature>) fse |
274 |
|
.getSelectionResult(); |
275 |
|
|
276 |
// reset the selection of the DpLayerSelectionModel |
// reset the selection of the DpLayerSelectionModel |
277 |
// layerSelModel.setValueIsAdjusting(true); |
// layerSelModel.setValueIsAdjusting(true); |
278 |
|
|
279 |
layerSelModel.addSelection((Collection<Feature>) fse |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
280 |
.getSelectionResult()); |
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(); |
288 |
|
|
289 |
// for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
for (Feature f : selectionResult) { |
290 |
// Feature changedFeature = featureTableModel.getFeature(featureTable |
layerSelModel.addSelection(f.getID()); |
291 |
// .convertRowIndexToModel(i)); |
} |
|
// if (featureTable.isRowSelected(i)) |
|
|
// layerSelModel.addSelection(changedFeature); |
|
|
// else |
|
|
// layerSelModel.removeSelection(changedFeature); |
|
|
// } |
|
|
// layerSelModel.setValueIsAdjusting(false); |
|
292 |
|
|
293 |
// Show selected feature in the map, which is not automatically done by |
LOGGER.debug("Setting selection to " + selectionResult.size()); |
294 |
// the FeatureSelectedEvent |
layerSelModel.setValueIsAdjusting(false); |
295 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
296 |
|
layerSelModel.setValueIsAdjusting(true); |
297 |
|
for (Feature f : selectionResult) { |
298 |
|
layerSelModel.removeSelection(f.getID()); |
299 |
|
} |
300 |
|
layerSelModel.setValueIsAdjusting(false); |
301 |
|
} |
302 |
|
|
303 |
|
// Show selected features in the map, which is not automatically done by |
304 |
|
// the origin: FeatureSelectedEvent |
305 |
changeLayerStyle(layerSelModel.getSelection()); |
changeLayerStyle(layerSelModel.getSelection()); |
306 |
|
|
307 |
// Danger of event circles in propertyChange(..) banned |
// Danger of event circles in propertyChange(..) banned |
308 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
309 |
} |
} |
310 |
|
|
311 |
} |
} |