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; |
25 |
|
|
26 |
import org.geotools.feature.Feature; |
import org.geotools.feature.Feature; |
27 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
28 |
|
import org.geotools.styling.FeatureTypeStyle; |
29 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
30 |
|
import org.opengis.filter.Filter; |
31 |
|
import org.opengis.filter.FilterVisitor; |
32 |
|
|
|
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
|
33 |
import schmitzm.geotools.gui.JMapPane; |
import schmitzm.geotools.gui.JMapPane; |
34 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
35 |
import schmitzm.geotools.map.event.JMapPaneEvent; |
import schmitzm.geotools.map.event.JMapPaneEvent; |
36 |
import schmitzm.geotools.map.event.JMapPaneListener; |
import schmitzm.geotools.map.event.JMapPaneListener; |
37 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
38 |
import skrueger.geotools.StyledFeatureCollectionInterface; |
import schmitzm.lang.LangUtil; |
39 |
|
import skrueger.geotools.MapPaneToolBar; |
40 |
import skrueger.geotools.StyledMapInterface; |
import skrueger.geotools.StyledMapInterface; |
|
import skrueger.geotools.selection.StyledFeatureLayerSelectionModel; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionEvent; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionModel; |
|
41 |
|
|
42 |
/** |
/** |
43 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
58 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
59 |
*/ |
*/ |
60 |
public class FeatureMapLayerSelectionSynchronizer extends |
public class FeatureMapLayerSelectionSynchronizer extends |
61 |
StyledLayerSelectionModelSynchronizer<Feature> implements |
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
62 |
JMapPaneListener { |
implements JMapPaneListener { |
63 |
|
public static final String SELECTION_STYLING = "SELECTION"; |
64 |
/** |
/** |
65 |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
* Holds the {@link MapLayer} to keep synchronized with the layer selection |
66 |
* model. |
* model. |
68 |
protected final MapLayer mapLayer; |
protected final MapLayer mapLayer; |
69 |
protected final StyledMapInterface<?> styledMapLayer; |
protected final StyledMapInterface<?> styledMapLayer; |
70 |
protected final JMapPane mapPane; |
protected final JMapPane mapPane; |
71 |
|
private final MapPaneToolBar toolBar; |
72 |
|
|
73 |
/** |
/** |
74 |
* Creates a new synchronizer |
* Creates a new synchronizer |
83 |
public FeatureMapLayerSelectionSynchronizer( |
public FeatureMapLayerSelectionSynchronizer( |
84 |
StyledFeatureLayerSelectionModel layerSelModel, |
StyledFeatureLayerSelectionModel layerSelModel, |
85 |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
StyledMapInterface<?> styledMapLayer, MapLayer mapLayer, |
86 |
JMapPane mapPane) { |
JMapPane mapPane, MapPaneToolBar toolBar) { |
87 |
|
|
88 |
super(layerSelModel); |
super(layerSelModel); |
89 |
this.styledMapLayer = styledMapLayer; |
this.styledMapLayer = styledMapLayer; |
90 |
|
|
91 |
this.mapLayer = mapLayer; |
this.mapLayer = mapLayer; |
92 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
93 |
|
this.toolBar = toolBar; |
94 |
} |
} |
95 |
|
|
96 |
/** |
/** |
114 |
return; |
return; |
115 |
// Apply new selection on table (except this event is one of |
// Apply new selection on table (except this event is one of |
116 |
// some more events) |
// some more events) |
117 |
Vector<Feature> newSelection = layerSelModel.getSelection(); |
Vector<String> newSelection = layerSelModel.getSelection(); |
118 |
if (newSelection == null) |
if (newSelection == null) |
119 |
return; |
return; |
120 |
|
|
127 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
128 |
} |
} |
129 |
|
|
130 |
private void changeLayerStyle(Vector<Feature> newSelection) { |
private void changeLayerStyle(final Vector<String> newSelection) { |
131 |
Style selectionMapStyle = null; |
Style selectionMapStyle = null; |
132 |
try { |
try { |
133 |
if (newSelection.isEmpty()) { |
if (newSelection.isEmpty()) { |
134 |
|
|
|
LOGGER.debug("NO SELECTION"); |
|
135 |
selectionMapStyle = styledMapLayer.getStyle(); |
selectionMapStyle = styledMapLayer.getStyle(); |
136 |
|
LOGGER.debug("NO SELECTION .. set to original style directly"); |
137 |
|
|
138 |
} else { |
} else { |
139 |
LOGGER.debug("SELECTION EXISTEnT"); |
LOGGER.debug("SELECTION .. change style"); |
140 |
|
|
141 |
|
// We take Style from the MapLayer that is displayed at the |
142 |
|
// moment. We do not use the styledMapLayer.getStyle, because in |
143 |
|
// the atlas, this always return the default style, but |
144 |
|
// additionaly styles might be selected. |
145 |
|
// Taking the style from the mapLayer indicated, that we have to |
146 |
|
// remove any selection rules first. |
147 |
|
Style originalStyle = mapLayer.getStyle(); |
148 |
|
|
149 |
|
// TODO The default style is not good here. We need |
150 |
|
// .createSelectionStyle(normalStyle, geoObj); |
151 |
selectionMapStyle = StylingUtil |
selectionMapStyle = StylingUtil |
152 |
.createDefaultStyle(styledMapLayer.getGeoObject()); |
.createSelectionStyle(styledMapLayer.getGeoObject()); |
153 |
|
// selectionMapStyle = StylingUtil |
154 |
|
// .createDefaultStyle(styledMapLayer.getGeoObject()); |
155 |
|
|
156 |
|
selectionMapStyle.getFeatureTypeStyles()[0] |
157 |
|
.setName(SELECTION_STYLING); |
158 |
|
|
159 |
|
// Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule(); |
160 |
|
selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0] |
161 |
|
.setFilter(new Filter() { |
162 |
|
|
163 |
|
@Override |
164 |
|
public Object accept(FilterVisitor visitor, |
165 |
|
Object extraData) { |
166 |
|
return null; |
167 |
|
} |
168 |
|
|
169 |
|
@Override |
170 |
|
public boolean evaluate(Object obj) { |
171 |
|
if (obj instanceof Feature) { |
172 |
|
Feature f = (Feature) obj; |
173 |
|
// TODO BAD CODE! says Martin.. i am fine |
174 |
|
// with it.. well.. not great... |
175 |
|
for (String ffID : newSelection) { |
176 |
|
if (ffID.equals(f.getID())) |
177 |
|
return true; |
178 |
|
} |
179 |
|
return false; |
180 |
|
|
181 |
|
} |
182 |
|
return false; |
183 |
|
} |
184 |
|
|
185 |
|
}); |
186 |
|
|
187 |
|
FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle |
188 |
|
.getFeatureTypeStyles(); |
189 |
|
FeatureTypeStyle[] newFeatureTypes; |
190 |
|
if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1] |
191 |
|
.getName() != null |
192 |
|
&& originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1] |
193 |
|
.getName().equals(SELECTION_STYLING)) { |
194 |
|
newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles, |
195 |
|
originalFeatureTypeStyles.length - 1); |
196 |
|
} else { |
197 |
|
newFeatureTypes = originalFeatureTypeStyles; |
198 |
|
} |
199 |
|
|
200 |
|
// The last FTS is the selection FTS |
201 |
|
newFeatureTypes = LangUtil.extendArray(newFeatureTypes, |
202 |
|
selectionMapStyle.getFeatureTypeStyles()); |
203 |
|
|
204 |
|
selectionMapStyle.setFeatureTypeStyles(newFeatureTypes); |
205 |
|
|
206 |
} |
} |
207 |
|
|
208 |
mapLayer.setStyle(selectionMapStyle); |
mapLayer.setStyle(selectionMapStyle); |
212 |
} |
} |
213 |
} |
} |
214 |
|
|
|
// |
|
|
// /** |
|
|
// * 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) { |
|
|
// |
|
|
// } |
|
|
|
|
215 |
@Override |
@Override |
216 |
public void performMapPaneEvent(JMapPaneEvent e) { |
public void performMapPaneEvent(JMapPaneEvent e) { |
217 |
if (!(e instanceof FeatureSelectedEvent)) |
|
218 |
|
/** |
219 |
|
* Only listen to FeatureSelectionEvents if the appropriate tool is |
220 |
|
* selected. |
221 |
|
*/ |
222 |
|
final int selectedTool = toolBar.getSelectedTool(); |
223 |
|
if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD |
224 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE |
225 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR |
226 |
|
&& selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) { |
227 |
|
return; |
228 |
|
} |
229 |
|
|
230 |
|
if (!(e instanceof FeatureSelectedEvent)) { |
231 |
|
// LOGGER.debug("Ignoring event " + e); |
232 |
|
return; |
233 |
|
} |
234 |
|
// only listen to events directly coming from JMapPane |
235 |
|
// selection (ignore selections from FilterDialog) |
236 |
|
if (e.getSourceObject() != this.mapPane) |
237 |
return; |
return; |
238 |
|
|
239 |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
FeatureSelectedEvent fse = (FeatureSelectedEvent) e; |
242 |
if (selectionChangeCausedByMe) |
if (selectionChangeCausedByMe) |
243 |
return; |
return; |
244 |
|
|
245 |
|
LOGGER.debug("Do event " + fse); |
246 |
|
|
247 |
// Avoid event circles in propertyChange(..) |
// Avoid event circles in propertyChange(..) |
248 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
249 |
|
|
250 |
// reset the selection of the DpLayerSelectionModel |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) { |
251 |
// layerSelModel.setValueIsAdjusting(true); |
layerSelModel.clearSelection(); |
252 |
|
} else { |
253 |
layerSelModel.addSelection((Collection<Feature>) fse |
Collection<Feature> selectionResult = (Collection<Feature>) fse |
254 |
.getSelectionResult()); |
.getSelectionResult(); |
255 |
|
|
256 |
// for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
// reset the selection of the DpLayerSelectionModel |
257 |
// Feature changedFeature = featureTableModel.getFeature(featureTable |
// layerSelModel.setValueIsAdjusting(true); |
258 |
// .convertRowIndexToModel(i)); |
|
259 |
// if (featureTable.isRowSelected(i)) |
if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) { |
260 |
// layerSelModel.addSelection(changedFeature); |
layerSelModel.setValueIsAdjusting(true); |
261 |
// else |
for (Feature f : selectionResult) { |
262 |
// layerSelModel.removeSelection(changedFeature); |
layerSelModel.addSelection(f.getID()); |
263 |
// } |
} |
264 |
// layerSelModel.setValueIsAdjusting(false); |
layerSelModel.setValueIsAdjusting(false); |
265 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) { |
266 |
|
layerSelModel.setValueIsAdjusting(true); |
267 |
|
layerSelModel.clearSelection(); |
268 |
|
|
269 |
|
for (Feature f : selectionResult) { |
270 |
|
layerSelModel.addSelection(f.getID()); |
271 |
|
} |
272 |
|
|
273 |
|
LOGGER.debug("Setting selection to " + selectionResult.size()); |
274 |
|
layerSelModel.setValueIsAdjusting(false); |
275 |
|
} else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) { |
276 |
|
layerSelModel.setValueIsAdjusting(true); |
277 |
|
for (Feature f : selectionResult) { |
278 |
|
layerSelModel.removeSelection(f.getID()); |
279 |
|
} |
280 |
|
layerSelModel.setValueIsAdjusting(false); |
281 |
|
} |
282 |
|
} // els it's not a Clear Selection command |
283 |
|
|
284 |
// Show selected feature in the map, which is not automatically done by |
// Show selected features in the map, which is not automatically done by |
285 |
// the FeatureSelectedEvent |
// the origin: FeatureSelectedEvent |
286 |
changeLayerStyle(layerSelModel.getSelection()); |
changeLayerStyle(layerSelModel.getSelection()); |
287 |
|
|
288 |
// Danger of event circles in propertyChange(..) banned |
// Danger of event circles in propertyChange(..) banned |
289 |
selectionChangeCausedByMe = false; |
selectionChangeCausedByMe = false; |
290 |
} |
} |