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.Vector; |
import java.util.Vector; |
21 |
|
|
22 |
import javax.swing.JTable; |
import javax.swing.JTable; |
23 |
import javax.swing.ListSelectionModel; |
import javax.swing.ListSelectionModel; |
|
import javax.swing.event.ListSelectionEvent; |
|
24 |
import javax.swing.event.ListSelectionListener; |
import javax.swing.event.ListSelectionListener; |
|
import javax.swing.text.Utilities; |
|
25 |
|
|
26 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
27 |
|
import org.geotools.feature.FeatureCollection; |
28 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
29 |
import org.geotools.styling.PolygonSymbolizer; |
import org.geotools.styling.FeatureTypeStyle; |
|
import org.geotools.styling.Rule; |
|
|
import org.geotools.styling.RuleImpl; |
|
30 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
|
import org.geotools.styling.StyleImpl; |
|
|
import org.geotools.styling.Symbolizer; |
|
31 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
32 |
import org.opengis.filter.FilterVisitor; |
import org.opengis.filter.FilterVisitor; |
|
import org.opengis.layer.StyleURL; |
|
33 |
|
|
|
import schmitzm.geotools.FilterUtil; |
|
|
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
|
34 |
import schmitzm.geotools.gui.JMapPane; |
import schmitzm.geotools.gui.JMapPane; |
35 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
36 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
37 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
38 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
39 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
40 |
import skrueger.geotools.StyledFeatureCollectionInterface; |
import skrueger.geotools.MapPaneToolBar; |
41 |
import skrueger.geotools.StyledMapInterface; |
import skrueger.geotools.StyledMapInterface; |
|
import skrueger.geotools.selection.StyledFeatureLayerSelectionModel; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionEvent; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionModel; |
|
42 |
|
|
43 |
/** |
/** |
44 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
61 |
public class FeatureMapLayerSelectionSynchronizer extends |
public class FeatureMapLayerSelectionSynchronizer extends |
62 |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
63 |
implements JMapPaneListener { |
implements JMapPaneListener { |
64 |
|
public static final String SELECTION_STYLING = "SELECTION"; |
65 |
/** |
/** |
66 |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
67 |
* model. |
* model. |
69 |
protected final MapLayer mapLayer; |
protected final MapLayer mapLayer; |
70 |
protected final StyledMapInterface<?> styledMapLayer; |
protected final StyledMapInterface<?> styledMapLayer; |
71 |
protected final JMapPane mapPane; |
protected final JMapPane mapPane; |
72 |
|
private final MapPaneToolBar toolBar; |
73 |
|
|
74 |
/** |
/** |
75 |
* Creates a new synchronizer |
* Creates a new synchronizer |
84 |
public FeatureMapLayerSelectionSynchronizer( |
public FeatureMapLayerSelectionSynchronizer( |
85 |
StyledFeatureLayerSelectionModel layerSelModel, |
StyledFeatureLayerSelectionModel layerSelModel, |
86 |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
87 |
JMapPane mapPane) { |
JMapPane mapPane, MapPaneToolBar toolBar) { |
88 |
|
|
89 |
super(layerSelModel); |
super(layerSelModel); |
90 |
this.styledMapLayer = styledMapLayer; |
this.styledMapLayer = styledMapLayer; |
91 |
|
|
92 |
this.mapLayer = mapLayer; |
this.mapLayer = mapLayer; |
93 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
94 |
|
this.toolBar = toolBar; |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
115 |
return; |
return; |
116 |
// Apply new selection on table (except this event is one of |
// Apply new selection on table (except this event is one of |
117 |
// some more events) |
// some more events) |
118 |
Vector<Feature> newSelection = layerSelModel.getSelection(); |
Vector<String> newSelection = layerSelModel.getSelection(); |
119 |
if (newSelection == null) |
if (newSelection == null) |
120 |
return; |
return; |
121 |
|
|
128 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
129 |
} |
} |
130 |
|
|
131 |
private void changeLayerStyle(final Vector<Feature> newSelection) { |
private void changeLayerStyle(final Vector<String> newSelection) { |
132 |
Style selectionMapStyle = null; |
Style selectionMapStyle = null; |
133 |
try { |
try { |
134 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
135 |
|
|
136 |
selectionMapStyle = styledMapLayer.getStyle(); |
selectionMapStyle = styledMapLayer.getStyle(); |
137 |
LOGGER.debug("NO SELECTION .. reset original style"); |
LOGGER.debug("NO SELECTION .. set to original style directly"); |
138 |
|
|
139 |
} else { |
} else { |
140 |
LOGGER.debug("SELECTION .. change style"); |
LOGGER.debug("SELECTION .. change style"); |
141 |
|
|
142 |
Style originalStyle = styledMapLayer.getStyle(); |
// We take Style from the MapLayer that is displayed at the |
143 |
|
// moment. We do not use the styledMapLayer.getStyle, because in |
144 |
|
// the atlas, this always return the default style, but |
145 |
|
// additionaly styles might be selected. |
146 |
|
// Taking the style from the mapLayer indicated, that we have to |
147 |
|
// remove any selection rules first. |
148 |
|
Style originalStyle = mapLayer.getStyle(); |
149 |
|
|
150 |
// TODO This clone is a deep clone and it is slow.. |
// TODO The default style is not good here. We need |
151 |
|
// .createSelectionStyle(normalStyle, geoObj); |
152 |
selectionMapStyle = StylingUtil |
selectionMapStyle = StylingUtil |
153 |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
154 |
|
|
155 |
// Symbolizer sss = selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0]; |
selectionMapStyle.getFeatureTypeStyles()[0] |
156 |
// if (sss instanceof PolygonSymbolizer) { |
.setName(SELECTION_STYLING); |
157 |
// PolygonSymbolizer ps = (PolygonSymbolizer) sss; |
|
158 |
// ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5)); |
// Symbolizer sss = |
159 |
// } |
// selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0]; |
160 |
|
// if (sss instanceof PolygonSymbolizer) { |
161 |
|
// PolygonSymbolizer ps = (PolygonSymbolizer) sss; |
162 |
|
// ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5)); |
163 |
|
// } |
164 |
|
|
165 |
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
166 |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
176 |
public boolean evaluate(Object obj) { |
public boolean evaluate(Object obj) { |
177 |
if (obj instanceof Feature) { |
if (obj instanceof Feature) { |
178 |
Feature f = (Feature) obj; |
Feature f = (Feature) obj; |
179 |
// TODO BAD CODE |
// TODO BAD CODE! says Martin.. i am fine with it.. well.. not great... |
180 |
for (Feature ff : newSelection) { |
for (String ffID : newSelection) { |
181 |
if (ff.getID().equals(f.getID())) |
if (ffID.equals(f.getID())) |
182 |
return true; |
return true; |
183 |
} |
} |
184 |
return false; |
return false; |
189 |
|
|
190 |
}); |
}); |
191 |
|
|
192 |
selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray( |
FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle.getFeatureTypeStyles(); |
193 |
originalStyle |
FeatureTypeStyle[] newFeatureTypes ; |
194 |
.getFeatureTypeStyles(), selectionMapStyle.getFeatureTypeStyles())); |
if (originalFeatureTypeStyles[originalFeatureTypeStyles.length-1].getName() != null && originalFeatureTypeStyles[originalFeatureTypeStyles.length-1].getName().equals(SELECTION_STYLING)){ |
195 |
|
newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles, originalFeatureTypeStyles.length-1); |
196 |
|
} else { |
197 |
|
newFeatureTypes = originalFeatureTypeStyles; |
198 |
|
} |
199 |
|
// |
200 |
|
// // REMOVE any Selection_Styling FTS (usually the last one) |
201 |
|
// |
202 |
|
// for (FeatureTypeStyle fts : originalFeatureTypeStyles){ |
203 |
|
// if (fts.getName().equals(SELECTION_STYLING)) continue; |
204 |
|
// newFeatureTypes = |
205 |
|
// |
206 |
|
// LangUtil.extendArray( |
207 |
|
// originalFeatureTypeStyles, selectionMapStyle |
208 |
|
// .getFeatureTypeStyles()); |
209 |
|
// } |
210 |
|
|
211 |
|
// The last FTS is the selection FTS |
212 |
|
newFeatureTypes = LangUtil.extendArray(newFeatureTypes, selectionMapStyle |
213 |
|
.getFeatureTypeStyles()); |
214 |
|
|
215 |
|
|
216 |
|
selectionMapStyle.setFeatureTypeStyles(newFeatureTypes); |
217 |
|
|
218 |
// selectionMapStyle.setFeatureTypeStyles(originalStyle.getF) |
// selectionMapStyle.setFeatureTypeStyles(originalStyle.getF) |
219 |
|
|
231 |
} |
} |
232 |
} |
} |
233 |
|
|
|
// |
|
|
// /** |
|
|
// * Called when the table selection is changed by the user. When calling |
|
|
// this |
|
|
// * method changes the selection of the {@link StyledLayerSelectionModel}. |
|
|
// * |
|
|
// * @param evt |
|
|
// * an event |
|
|
// */ |
|
|
// @Override |
|
|
// public void valueChanged(ListSelectionEvent evt) { |
|
|
// |
|
|
// } |
|
|
|
|
234 |
@Override |
@Override |
235 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
236 |
|
|
237 |
|
/** |
238 |
|
* Only listen to FeatureSelectionEvents if the appropriate tool is |
239 |
|
* selected. |
240 |
|
*/ |
241 |
|
final int selectedTool = toolBar.getSelectedTool(); |
242 |
|
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
243 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
244 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR |
245 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
246 |
|
return; |
247 |
|
} |
248 |
|
|
249 |
if (!(e instanceof FeatureSelectedEvent)) { |
if (!(e instanceof FeatureSelectedEvent)) { |
250 |
// LOGGER.debug("Ignoring event " + e); |
// LOGGER.debug("Ignoring event " + e); |
251 |
return; |
return; |
252 |
} |
} |
253 |
|
// only listen to events directly coming from JMapPane |
254 |
|
// selection (ignore selections from FilterDialog) |
255 |
|
if (e.getSourceObject() != this.mapPane) |
256 |
|
return; |
257 |
|
|
258 |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
|
// LOGGER.debug("Do event " + fse); |
|
|
// LOGGER.debug("Do event " + fse.getSelectionResult()); |
|
259 |
|
|
260 |
// ignore event if it is caused by the DpLayerVectorSelectionModel |
// ignore event if it is caused by the DpLayerVectorSelectionModel |
261 |
if (selectionChangeCausedByMe) |
if (selectionChangeCausedByMe) |
262 |
return; |
return; |
263 |
|
|
264 |
|
LOGGER.debug("Do event " + fse); |
265 |
|
|
266 |
// Avoid event circles in propertyChange(..) |
// Avoid event circles in propertyChange(..) |
267 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
268 |
|
|
269 |
// reset the selection of the DpLayerSelectionModel |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) { |
270 |
// layerSelModel.setValueIsAdjusting(true); |
layerSelModel.clearSelection(); |
271 |
|
} else { |
272 |
layerSelModel.addSelection((Collection<Feature>) fse |
Collection<Feature> selectionResult = (Collection<Feature>) fse |
273 |
.getSelectionResult()); |
.getSelectionResult(); |
274 |
|
|
275 |
// for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
// reset the selection of the DpLayerSelectionModel |
276 |
// Feature changedFeature = featureTableModel.getFeature(featureTable |
// layerSelModel.setValueIsAdjusting(true); |
277 |
// .convertRowIndexToModel(i)); |
|
278 |
// if (featureTable.isRowSelected(i)) |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
279 |
// layerSelModel.addSelection(changedFeature); |
layerSelModel.setValueIsAdjusting(true); |
280 |
// else |
for (Feature f : selectionResult) { |
281 |
// layerSelModel.removeSelection(changedFeature); |
layerSelModel.addSelection(f.getID()); |
282 |
// } |
} |
283 |
// layerSelModel.setValueIsAdjusting(false); |
layerSelModel.setValueIsAdjusting(false); |
284 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) { |
285 |
|
layerSelModel.setValueIsAdjusting(true); |
286 |
|
layerSelModel.clearSelection(); |
287 |
|
|
288 |
|
for (Feature f : selectionResult) { |
289 |
|
layerSelModel.addSelection(f.getID()); |
290 |
|
} |
291 |
|
|
292 |
|
LOGGER.debug("Setting selection to " + selectionResult.size()); |
293 |
|
layerSelModel.setValueIsAdjusting(false); |
294 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
295 |
|
layerSelModel.setValueIsAdjusting(true); |
296 |
|
for (Feature f : selectionResult) { |
297 |
|
layerSelModel.removeSelection(f.getID()); |
298 |
|
} |
299 |
|
layerSelModel.setValueIsAdjusting(false); |
300 |
|
} |
301 |
|
} // els it's not a Clear Selection command |
302 |
|
|
303 |
// Show selected feature in the map, which is not automatically done by |
// Show selected features in the map, which is not automatically done by |
304 |
// the FeatureSelectedEvent |
// 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 |