20 |
|
|
21 |
import javax.swing.JTable; |
import javax.swing.JTable; |
22 |
import javax.swing.ListSelectionModel; |
import javax.swing.ListSelectionModel; |
|
import javax.swing.event.ListSelectionEvent; |
|
23 |
import javax.swing.event.ListSelectionListener; |
import javax.swing.event.ListSelectionListener; |
24 |
|
|
25 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
26 |
|
import org.geotools.feature.FeatureCollection; |
27 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
28 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
29 |
|
import org.opengis.filter.Filter; |
30 |
|
import org.opengis.filter.FilterVisitor; |
31 |
|
|
|
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
|
32 |
import schmitzm.geotools.gui.JMapPane; |
import schmitzm.geotools.gui.JMapPane; |
33 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
34 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
35 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
36 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
37 |
import skrueger.geotools.StyledFeatureCollectionInterface; |
import schmitzm.lang.LangUtil; |
38 |
|
import skrueger.geotools.MapPaneToolBar; |
39 |
import skrueger.geotools.StyledMapInterface; |
import skrueger.geotools.StyledMapInterface; |
|
import skrueger.geotools.selection.StyledFeatureLayerSelectionModel; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionEvent; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionModel; |
|
40 |
|
|
41 |
/** |
/** |
42 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
57 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
58 |
*/ |
*/ |
59 |
public class FeatureMapLayerSelectionSynchronizer extends |
public class FeatureMapLayerSelectionSynchronizer extends |
60 |
StyledLayerSelectionModelSynchronizer<Feature> implements |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
61 |
JMapPaneListener { |
implements JMapPaneListener { |
62 |
|
public static final String SELECTION_STYLING = "SELECTION"; |
63 |
/** |
/** |
64 |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
65 |
* model. |
* model. |
67 |
protected final MapLayer mapLayer; |
protected final MapLayer mapLayer; |
68 |
protected final StyledMapInterface<?> styledMapLayer; |
protected final StyledMapInterface<?> styledMapLayer; |
69 |
protected final JMapPane mapPane; |
protected final JMapPane mapPane; |
70 |
|
private final MapPaneToolBar toolBar; |
71 |
|
|
72 |
/** |
/** |
73 |
* Creates a new synchronizer |
* Creates a new synchronizer |
82 |
public FeatureMapLayerSelectionSynchronizer( |
public FeatureMapLayerSelectionSynchronizer( |
83 |
StyledFeatureLayerSelectionModel layerSelModel, |
StyledFeatureLayerSelectionModel layerSelModel, |
84 |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
85 |
JMapPane mapPane) { |
JMapPane mapPane, MapPaneToolBar toolBar) { |
86 |
|
|
87 |
super(layerSelModel); |
super(layerSelModel); |
88 |
this.styledMapLayer = styledMapLayer; |
this.styledMapLayer = styledMapLayer; |
89 |
|
|
90 |
this.mapLayer = mapLayer; |
this.mapLayer = mapLayer; |
91 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
92 |
|
this.toolBar = toolBar; |
93 |
} |
} |
94 |
|
|
95 |
/** |
/** |
113 |
return; |
return; |
114 |
// Apply new selection on table (except this event is one of |
// Apply new selection on table (except this event is one of |
115 |
// some more events) |
// some more events) |
116 |
Vector<Feature> newSelection = layerSelModel.getSelection(); |
Vector<String> newSelection = layerSelModel.getSelection(); |
117 |
if (newSelection == null) |
if (newSelection == null) |
118 |
return; |
return; |
119 |
|
|
126 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
127 |
} |
} |
128 |
|
|
129 |
private void changeLayerStyle(Vector<Feature> newSelection) { |
private void changeLayerStyle(final Vector<String> newSelection) { |
130 |
Style selectionMapStyle = null; |
Style selectionMapStyle = null; |
131 |
try { |
try { |
132 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
133 |
|
|
|
LOGGER.debug("NO SELECTION"); |
|
134 |
selectionMapStyle = styledMapLayer.getStyle(); |
selectionMapStyle = styledMapLayer.getStyle(); |
135 |
|
LOGGER.debug("NO SELECTION .. set to original style directly"); |
136 |
|
|
137 |
} else { |
} else { |
138 |
LOGGER.debug("SELECTION EXISTEnT"); |
LOGGER.debug("SELECTION .. change style"); |
139 |
|
|
140 |
|
Style originalStyle = styledMapLayer.getStyle(); |
141 |
|
|
142 |
|
// TODO This clone is a deep clone and it is slow.. |
143 |
selectionMapStyle = StylingUtil |
selectionMapStyle = StylingUtil |
144 |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
145 |
|
selectionMapStyle.getFeatureTypeStyles()[0].setName(SELECTION_STYLING); |
146 |
|
|
147 |
|
// Symbolizer sss = |
148 |
|
// selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0]; |
149 |
|
// if (sss instanceof PolygonSymbolizer) { |
150 |
|
// PolygonSymbolizer ps = (PolygonSymbolizer) sss; |
151 |
|
// ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5)); |
152 |
|
// } |
153 |
|
|
154 |
|
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
155 |
|
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
156 |
|
.setFilter(new Filter() { |
157 |
|
|
158 |
|
@Override |
159 |
|
public Object accept(FilterVisitor visitor, |
160 |
|
Object extraData) { |
161 |
|
return null; |
162 |
|
} |
163 |
|
|
164 |
|
@Override |
165 |
|
public boolean evaluate(Object obj) { |
166 |
|
if (obj instanceof Feature) { |
167 |
|
Feature f = (Feature) obj; |
168 |
|
// TODO BAD CODE? |
169 |
|
for (String ffID : newSelection) { |
170 |
|
if (ffID.equals(f.getID())) |
171 |
|
return true; |
172 |
|
} |
173 |
|
return false; |
174 |
|
|
175 |
|
} |
176 |
|
return false; |
177 |
|
} |
178 |
|
|
179 |
|
}); |
180 |
|
|
181 |
|
selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray( |
182 |
|
originalStyle.getFeatureTypeStyles(), selectionMapStyle |
183 |
|
.getFeatureTypeStyles())); |
184 |
|
|
185 |
|
// selectionMapStyle.setFeatureTypeStyles(originalStyle.getF) |
186 |
|
|
187 |
|
// Flat copy the style |
188 |
|
|
189 |
|
// selectionMapStyle = |
190 |
|
// StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject()); |
191 |
|
|
192 |
} |
} |
193 |
|
|
194 |
mapLayer.setStyle(selectionMapStyle); |
mapLayer.setStyle(selectionMapStyle); |
198 |
} |
} |
199 |
} |
} |
200 |
|
|
|
// |
|
|
// /** |
|
|
// * 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) { |
|
|
// |
|
|
// } |
|
|
|
|
201 |
@Override |
@Override |
202 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
203 |
if (!(e instanceof FeatureSelectedEvent)) |
|
204 |
|
/** |
205 |
|
* Only listen to FeatureSelectionEvents if the appropriate tool is |
206 |
|
* selected. |
207 |
|
*/ |
208 |
|
final int selectedTool = toolBar.getSelectedTool(); |
209 |
|
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
210 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
211 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR |
212 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
213 |
|
return; |
214 |
|
} |
215 |
|
|
216 |
|
if (!(e instanceof FeatureSelectedEvent)) { |
217 |
|
// LOGGER.debug("Ignoring event " + e); |
218 |
|
return; |
219 |
|
} |
220 |
|
// only listen to events directly coming from JMapPane |
221 |
|
// selection (ignore selections from FilterDialog) |
222 |
|
if (e.getSourceObject() != this.mapPane) |
223 |
return; |
return; |
224 |
|
|
225 |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
228 |
if (selectionChangeCausedByMe) |
if (selectionChangeCausedByMe) |
229 |
return; |
return; |
230 |
|
|
231 |
|
LOGGER.debug("Do event " + fse); |
232 |
|
|
233 |
// Avoid event circles in propertyChange(..) |
// Avoid event circles in propertyChange(..) |
234 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
235 |
|
|
236 |
// reset the selection of the DpLayerSelectionModel |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) { |
237 |
// layerSelModel.setValueIsAdjusting(true); |
layerSelModel.clearSelection(); |
238 |
|
} else { |
239 |
layerSelModel.addSelection((Collection<Feature>) fse |
Collection<Feature> selectionResult = (Collection<Feature>) fse |
240 |
.getSelectionResult()); |
.getSelectionResult(); |
241 |
|
|
242 |
// for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
// reset the selection of the DpLayerSelectionModel |
243 |
// Feature changedFeature = featureTableModel.getFeature(featureTable |
// layerSelModel.setValueIsAdjusting(true); |
244 |
// .convertRowIndexToModel(i)); |
|
245 |
// if (featureTable.isRowSelected(i)) |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
246 |
// layerSelModel.addSelection(changedFeature); |
layerSelModel.setValueIsAdjusting(true); |
247 |
// else |
for (Feature f : selectionResult) { |
248 |
// layerSelModel.removeSelection(changedFeature); |
layerSelModel.addSelection(f.getID()); |
249 |
// } |
} |
250 |
// layerSelModel.setValueIsAdjusting(false); |
layerSelModel.setValueIsAdjusting(false); |
251 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) { |
252 |
|
layerSelModel.setValueIsAdjusting(true); |
253 |
|
layerSelModel.clearSelection(); |
254 |
|
|
255 |
|
for (Feature f : selectionResult) { |
256 |
|
layerSelModel.addSelection(f.getID()); |
257 |
|
} |
258 |
|
|
259 |
|
LOGGER.debug("Setting selection to " + selectionResult.size()); |
260 |
|
layerSelModel.setValueIsAdjusting(false); |
261 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
262 |
|
layerSelModel.setValueIsAdjusting(true); |
263 |
|
for (Feature f : selectionResult) { |
264 |
|
layerSelModel.removeSelection(f.getID()); |
265 |
|
} |
266 |
|
layerSelModel.setValueIsAdjusting(false); |
267 |
|
} |
268 |
|
} // els it's not a Clear Selection command |
269 |
|
|
270 |
// Show selected feature in the map, which is not automatically done by |
// Show selected features in the map, which is not automatically done by |
271 |
// the FeatureSelectedEvent |
// the origin: FeatureSelectedEvent |
272 |
changeLayerStyle(layerSelModel.getSelection()); |
changeLayerStyle(layerSelModel.getSelection()); |
273 |
|
|
274 |
// Danger of event circles in propertyChange(..) banned |
// Danger of event circles in propertyChange(..) banned |
275 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
276 |
} |
} |