/[schmitzm]/trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 158 - (hide annotations)
Mon Jun 22 13:45:32 2009 UTC (15 years, 8 months ago) by alfonx
File MIME type: text/plain
File size: 11492 byte(s)
* Changed the Selection-Stlying-Filter: It's now based on GeoTools Filters! The new Filter() { .. } approach is not recommended in GeoTools and will probably produce other problems also. Because the ShapeFileRenderer is optimizing the filters (thats why it's 10 times faster), its doing some stuff that only works on GeoTools Filters:

Which leads to a problem: Any "Martin-Filter" from Schmitzm does not work when using the ShapefileRenderer
1 alfonx 97 /**
2     Copyright 2008 Stefan Alfons Krüger
3    
4     atlas-framework - This file is part of the Atlas Framework
5    
6     This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
7     This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
8     You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
9    
10     Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
11     Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
12     Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
13     **/
14     package skrueger.geotools.selection;
15    
16     import java.beans.PropertyChangeEvent;
17     import java.beans.PropertyChangeListener;
18 alfonx 123 import java.util.Arrays;
19 alfonx 97 import java.util.Collection;
20 alfonx 158 import java.util.HashSet;
21     import java.util.Set;
22 alfonx 97 import java.util.Vector;
23    
24     import javax.swing.JTable;
25     import javax.swing.ListSelectionModel;
26     import javax.swing.event.ListSelectionListener;
27    
28     import org.geotools.feature.Feature;
29 alfonx 158 import org.geotools.filter.FidFilter;
30     import org.geotools.filter.FidFilterImpl;
31 alfonx 97 import org.geotools.map.MapLayer;
32 alfonx 123 import org.geotools.styling.FeatureTypeStyle;
33 alfonx 97 import org.geotools.styling.Style;
34 alfonx 101 import org.opengis.filter.Filter;
35     import org.opengis.filter.FilterVisitor;
36 alfonx 158 import org.opengis.filter.identity.FeatureId;
37 alfonx 97
38 alfonx 158 import schmitzm.geotools.FilterUtil;
39 alfonx 97 import schmitzm.geotools.gui.JMapPane;
40     import schmitzm.geotools.map.event.FeatureSelectedEvent;
41     import schmitzm.geotools.map.event.JMapPaneEvent;
42     import schmitzm.geotools.map.event.JMapPaneListener;
43     import schmitzm.geotools.styling.StylingUtil;
44 alfonx 104 import schmitzm.lang.LangUtil;
45 alfonx 111 import skrueger.geotools.MapPaneToolBar;
46 alfonx 97 import skrueger.geotools.StyledMapInterface;
47    
48     /**
49     * This class keeps the selection of a (feature) {@link JTable} synchronized
50     * with the {@link StyledLayerSelectionModel} of a layer. This is done by
51     * implementing:
52     * <ul>
53     * <li>a {@link PropertyChangeListener} which listens to the
54     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
55     * selection</li>
56     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
57     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
58     * </ul>
59     * After creating, the instance of this synchronizer must be added as listener
60     * to both, the {@link StyledLayerSelectionModel} and the table's
61     * {@link ListSelectionModel}.
62     *
63     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
64     * (University of Bonn/Germany)
65     */
66     public class FeatureMapLayerSelectionSynchronizer extends
67 alfonx 106 StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
68     implements JMapPaneListener {
69 alfonx 111 public static final String SELECTION_STYLING = "SELECTION";
70 alfonx 97 /**
71     * Holds the {@link MapLayer} to keep synchronized with the layer selection
72     * model.
73     */
74     protected final MapLayer mapLayer;
75     protected final StyledMapInterface<?> styledMapLayer;
76     protected final JMapPane mapPane;
77 alfonx 111 private final MapPaneToolBar toolBar;
78 alfonx 97
79     /**
80     * Creates a new synchronizer
81     *
82     * @param layerSelModel
83     * layer selection model to keep synchronized with the
84     * {@link MapLayer}
85     *
86     * @param mapLayer
87     * {@link MapLayer} to keep synchronized with.
88     */
89     public FeatureMapLayerSelectionSynchronizer(
90     StyledFeatureLayerSelectionModel layerSelModel,
91     StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
92 alfonx 111 JMapPane mapPane, MapPaneToolBar toolBar) {
93 alfonx 97
94     super(layerSelModel);
95     this.styledMapLayer = styledMapLayer;
96    
97     this.mapLayer = mapLayer;
98     this.mapPane = mapPane;
99 alfonx 111 this.toolBar = toolBar;
100 alfonx 97 }
101    
102     /**
103     * Called by {@link StyledLayerSelectionModel} when a the selection on other
104     * selection components (map, chart, ...) has changed. When calling this
105     *
106     * method changes the {@link MapLayer} selection according to the
107     * {@link StyledLayerSelectionModel} selection.
108     *
109     * @param evt
110     * an event
111     */
112     @Override
113     public void propertyChange(PropertyChangeEvent evt) {
114 alfonx 151
115     if (!isEnabled())
116     return;
117    
118 alfonx 97 if (!(evt instanceof StyledLayerSelectionEvent))
119     return;
120     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
121     // Only react on own layer and ignore events invoked by
122     // this component itself
123     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
124     return;
125     // Apply new selection on table (except this event is one of
126     // some more events)
127 alfonx 111 Vector<String> newSelection = layerSelModel.getSelection();
128 alfonx 97 if (newSelection == null)
129     return;
130    
131     // Avoid event circles in valueChanged(..)
132     selectionChangeCausedByMe = true;
133    
134     changeLayerStyle(newSelection);
135    
136     // Danger of event circles in valueChanged(..) banned
137     selectionChangeCausedByMe = false;
138     }
139    
140 alfonx 151 /**
141     * Changes the Style of the {@link MapLayer} to reflect changes of the
142     * selection.
143     *
144     * @param newSelection
145     * A {@link Vector} of Feature-IDs that are selected.
146     */
147 alfonx 111 private void changeLayerStyle(final Vector<String> newSelection) {
148 alfonx 97 Style selectionMapStyle = null;
149     try {
150     if (newSelection.isEmpty()) {
151    
152     selectionMapStyle = styledMapLayer.getStyle();
153 alfonx 151 // LOGGER.debug("NO SELECTION .. set to original style directly");
154 alfonx 106
155 alfonx 97 } else {
156 alfonx 100 LOGGER.debug("SELECTION .. change style");
157 alfonx 106
158 alfonx 123 // We take Style from the MapLayer that is displayed at the
159     // moment. We do not use the styledMapLayer.getStyle, because in
160     // the atlas, this always return the default style, but
161     // additionaly styles might be selected.
162     // Taking the style from the mapLayer indicated, that we have to
163 alfonx 125 // remove any selection rules first.
164 alfonx 123 Style originalStyle = mapLayer.getStyle();
165 alfonx 101
166 alfonx 123 // TODO The default style is not good here. We need
167     // .createSelectionStyle(normalStyle, geoObj);
168 alfonx 107 selectionMapStyle = StylingUtil
169 alfonx 128 .createSelectionStyle(styledMapLayer.getGeoObject());
170 alfonx 151 // selectionMapStyle = StylingUtil
171     // .createDefaultStyle(styledMapLayer.getGeoObject());
172 alfonx 106
173 alfonx 123 selectionMapStyle.getFeatureTypeStyles()[0]
174     .setName(SELECTION_STYLING);
175    
176 alfonx 158 /**
177     *
178     * Add a Filter to the selectionMapStyle, so that it is only
179     * used on objects that are selected. <br/>
180     *
181     * Note 1:<br/>
182     * It is NEVER allowed to GeoTools extend Filter () { .. } (and write
183     * tests into the evaluate block). Especially for the
184     * ShapeFileRenderer, we may only use a geotools Filter.<br/>
185     *
186     * Note 2:<br/>
187     * The FilterUtil.FILTER_FAC2.id(fids) wants a set of FeatureId-Objects!
188     */
189 alfonx 106
190 alfonx 158 Set<FeatureId> fids = new HashSet<FeatureId>();
191     for (String fid : newSelection) {
192     fids.add(FilterUtil.FILTER_FAC2.featureId(fid));
193     }
194 alfonx 106
195 alfonx 158 selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
196     .setFilter(FilterUtil.FILTER_FAC2.id(fids));
197 alfonx 106
198 alfonx 125 FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
199     .getFeatureTypeStyles();
200     FeatureTypeStyle[] newFeatureTypes;
201     if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
202     .getName() != null
203     && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
204     .getName().equals(SELECTION_STYLING)) {
205     newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,
206     originalFeatureTypeStyles.length - 1);
207 alfonx 123 } else {
208     newFeatureTypes = originalFeatureTypeStyles;
209     }
210 alfonx 151
211 alfonx 123 // The last FTS is the selection FTS
212 alfonx 125 newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
213     selectionMapStyle.getFeatureTypeStyles());
214    
215 alfonx 123 selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
216 alfonx 106
217 alfonx 97 }
218    
219     mapLayer.setStyle(selectionMapStyle);
220     mapPane.refresh();
221     } catch (Exception e) {
222     LOGGER.error("Error while trying to create a selection style", e);
223     }
224     }
225    
226 alfonx 151 /**
227     * Used to synchronize {@link FeatureSelectedEvent}s with the
228     * {@link StyledFeatureLayerSelectionModel}
229     */
230 alfonx 97 @Override
231     public void performMapPaneEvent(JMapPaneEvent e) {
232 alfonx 106
233 alfonx 151 // Ignore event if it is caused by us or the synchronizer is disabled.
234     if (!isEnabled() || selectionChangeCausedByMe)
235     return;
236    
237     if (!(e instanceof FeatureSelectedEvent)) {
238     // LOGGER.debug("Ignoring event " + e);
239     return;
240     }
241    
242 alfonx 111 /**
243 alfonx 151 * Only listen to FeatureSelectedEvents if an appropriate tool is
244 alfonx 111 * selected.
245     */
246     final int selectedTool = toolBar.getSelectedTool();
247     if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
248     && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
249     && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250     return;
251     }
252    
253 mojays 109 // only listen to events directly coming from JMapPane
254     // selection (ignore selections from FilterDialog)
255 alfonx 111 if (e.getSourceObject() != this.mapPane)
256     return;
257    
258 alfonx 97 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
259    
260 alfonx 151 /**
261 alfonx 158 * Checking, that the FeatureSelectedEvent actually contains features
262     * from this layer
263 alfonx 151 */
264     final String sourceID = fse.getSourceLayer().getTitle();
265     final String syncForID = mapLayer.getTitle();
266     if (sourceID != null && syncForID != null
267 alfonx 158 && !sourceID.equals(syncForID)) {
268     LOGGER.debug("Ignoring a FeatureSelectedEvent from " + sourceID);
269 alfonx 97 return;
270 alfonx 151 }
271 alfonx 97
272 alfonx 151 LOGGER.debug("do event " + fse);
273 alfonx 111
274 alfonx 97 // Avoid event circles in propertyChange(..)
275     selectionChangeCausedByMe = true;
276    
277 alfonx 151 Collection<Feature> selectionResult = (Collection<Feature>) fse
278     .getSelectionResult();
279 alfonx 97
280 alfonx 151 // reset the selection of the DpLayerSelectionModel
281     // layerSelModel.setValueIsAdjusting(true);
282 alfonx 97
283 alfonx 151 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
284     layerSelModel.setValueIsAdjusting(true);
285     for (Feature f : selectionResult) {
286     layerSelModel.addSelection(f.getID());
287     }
288     layerSelModel.setValueIsAdjusting(false);
289     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
290     layerSelModel.setValueIsAdjusting(true);
291     layerSelModel.clearSelection();
292 alfonx 97
293 alfonx 151 for (Feature f : selectionResult) {
294     layerSelModel.addSelection(f.getID());
295     }
296 alfonx 111
297 alfonx 151 LOGGER.debug("Setting selection to " + selectionResult.size());
298     layerSelModel.setValueIsAdjusting(false);
299     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
300     layerSelModel.setValueIsAdjusting(true);
301     for (Feature f : selectionResult) {
302     layerSelModel.removeSelection(f.getID());
303 alfonx 111 }
304 alfonx 151 layerSelModel.setValueIsAdjusting(false);
305     }
306 alfonx 111
307     // Show selected features in the map, which is not automatically done by
308     // the origin: FeatureSelectedEvent
309 alfonx 97 changeLayerStyle(layerSelModel.getSelection());
310 alfonx 106
311 alfonx 97 // Danger of event circles in propertyChange(..) banned
312     selectionChangeCausedByMe = false;
313     }
314 alfonx 151
315 alfonx 97 }

Properties

Name Value
svn:eol-style native
svn:keywords Id
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26