/[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 109 - (hide annotations)
Tue May 12 13:14:26 2009 UTC (15 years, 9 months ago) by mojays
File MIME type: text/plain
File size: 9710 byte(s)
SelectionSynchronizer must ignore SelectionEvents coming from FilterDialog
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     import java.util.Collection;
19     import java.util.Vector;
20    
21     import javax.swing.JTable;
22     import javax.swing.ListSelectionModel;
23     import javax.swing.event.ListSelectionEvent;
24     import javax.swing.event.ListSelectionListener;
25 alfonx 100 import javax.swing.text.Utilities;
26 alfonx 97
27     import org.geotools.feature.Feature;
28     import org.geotools.map.MapLayer;
29 alfonx 107 import org.geotools.styling.PolygonSymbolizer;
30 alfonx 101 import org.geotools.styling.Rule;
31     import org.geotools.styling.RuleImpl;
32 alfonx 97 import org.geotools.styling.Style;
33 alfonx 101 import org.geotools.styling.StyleImpl;
34 alfonx 104 import org.geotools.styling.Symbolizer;
35 alfonx 101 import org.opengis.filter.Filter;
36     import org.opengis.filter.FilterVisitor;
37     import org.opengis.layer.StyleURL;
38 alfonx 97
39 alfonx 107 import schmitzm.geotools.FilterUtil;
40 alfonx 97 import schmitzm.geotools.gui.FeatureCollectionTableModel;
41     import schmitzm.geotools.gui.JMapPane;
42     import schmitzm.geotools.map.event.FeatureSelectedEvent;
43     import schmitzm.geotools.map.event.JMapPaneEvent;
44     import schmitzm.geotools.map.event.JMapPaneListener;
45     import schmitzm.geotools.styling.StylingUtil;
46 alfonx 104 import schmitzm.lang.LangUtil;
47 alfonx 97 import skrueger.geotools.StyledFeatureCollectionInterface;
48     import skrueger.geotools.StyledMapInterface;
49     import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;
50     import skrueger.geotools.selection.StyledLayerSelectionEvent;
51     import skrueger.geotools.selection.StyledLayerSelectionModel;
52    
53     /**
54     * This class keeps the selection of a (feature) {@link JTable} synchronized
55     * with the {@link StyledLayerSelectionModel} of a layer. This is done by
56     * implementing:
57     * <ul>
58     * <li>a {@link PropertyChangeListener} which listens to the
59     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
60     * selection</li>
61     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
62     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
63     * </ul>
64     * After creating, the instance of this synchronizer must be added as listener
65     * to both, the {@link StyledLayerSelectionModel} and the table's
66     * {@link ListSelectionModel}.
67     *
68     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
69     * (University of Bonn/Germany)
70     */
71     public class FeatureMapLayerSelectionSynchronizer extends
72 alfonx 106 StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
73     implements JMapPaneListener {
74 alfonx 97 /**
75     * Holds the {@link MapLayer} to keep synchronized with the layer selection
76     * model.
77     */
78     protected final MapLayer mapLayer;
79     protected final StyledMapInterface<?> styledMapLayer;
80     protected final JMapPane mapPane;
81    
82     /**
83     * Creates a new synchronizer
84     *
85     * @param layerSelModel
86     * layer selection model to keep synchronized with the
87     * {@link MapLayer}
88     *
89     * @param mapLayer
90     * {@link MapLayer} to keep synchronized with.
91     */
92     public FeatureMapLayerSelectionSynchronizer(
93     StyledFeatureLayerSelectionModel layerSelModel,
94     StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
95     JMapPane mapPane) {
96    
97     super(layerSelModel);
98     this.styledMapLayer = styledMapLayer;
99    
100     this.mapLayer = mapLayer;
101     this.mapPane = mapPane;
102     }
103    
104     /**
105     * Called by {@link StyledLayerSelectionModel} when a the selection on other
106     * selection components (map, chart, ...) has changed. When calling this
107     *
108     * method changes the {@link MapLayer} selection according to the
109     * {@link StyledLayerSelectionModel} selection.
110     *
111     * @param evt
112     * an event
113     */
114     @Override
115     public void propertyChange(PropertyChangeEvent evt) {
116     if (!(evt instanceof StyledLayerSelectionEvent))
117     return;
118     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
119     // Only react on own layer and ignore events invoked by
120     // this component itself
121     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
122     return;
123     // Apply new selection on table (except this event is one of
124     // some more events)
125     Vector<Feature> newSelection = layerSelModel.getSelection();
126     if (newSelection == null)
127     return;
128    
129     // Avoid event circles in valueChanged(..)
130     selectionChangeCausedByMe = true;
131    
132     changeLayerStyle(newSelection);
133    
134     // Danger of event circles in valueChanged(..) banned
135     selectionChangeCausedByMe = false;
136     }
137    
138 alfonx 104 private void changeLayerStyle(final Vector<Feature> newSelection) {
139 alfonx 97 Style selectionMapStyle = null;
140     try {
141     if (newSelection.isEmpty()) {
142    
143     selectionMapStyle = styledMapLayer.getStyle();
144 alfonx 100 LOGGER.debug("NO SELECTION .. reset original style");
145 alfonx 106
146 alfonx 97 } else {
147 alfonx 100 LOGGER.debug("SELECTION .. change style");
148 alfonx 106
149 alfonx 101 Style originalStyle = styledMapLayer.getStyle();
150    
151 alfonx 106 // TODO This clone is a deep clone and it is slow..
152 alfonx 107 selectionMapStyle = StylingUtil
153 alfonx 106 .createDefaultStyle(styledMapLayer.getGeoObject());
154 alfonx 107
155     // Symbolizer sss = selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];
156     // if (sss instanceof PolygonSymbolizer) {
157     // PolygonSymbolizer ps = (PolygonSymbolizer) sss;
158     // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));
159     // }
160 alfonx 106
161     // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
162 alfonx 107 selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
163 alfonx 106 .setFilter(new Filter() {
164    
165     @Override
166     public Object accept(FilterVisitor visitor,
167     Object extraData) {
168     return null;
169 alfonx 105 }
170 alfonx 106
171     @Override
172     public boolean evaluate(Object obj) {
173     if (obj instanceof Feature) {
174     Feature f = (Feature) obj;
175     // TODO BAD CODE
176     for (Feature ff : newSelection) {
177     if (ff.getID().equals(f.getID()))
178     return true;
179     }
180     return false;
181    
182     }
183     return false;
184     }
185    
186     });
187    
188     selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray(
189 alfonx 107 originalStyle
190     .getFeatureTypeStyles(), selectionMapStyle.getFeatureTypeStyles()));
191 alfonx 106
192     // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)
193    
194 alfonx 101 // Flat copy the style
195 alfonx 106
196     // selectionMapStyle =
197     // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());
198    
199 alfonx 97 }
200    
201     mapLayer.setStyle(selectionMapStyle);
202     mapPane.refresh();
203     } catch (Exception e) {
204     LOGGER.error("Error while trying to create a selection style", e);
205     }
206     }
207    
208     //
209     // /**
210     // * Called when the table selection is changed by the user. When calling
211     // this
212     // * method changes the selection of the {@link StyledLayerSelectionModel}.
213     // *
214     // * @param evt
215     // * an event
216     // */
217     // @Override
218     // public void valueChanged(ListSelectionEvent evt) {
219     //
220     // }
221    
222     @Override
223     public void performMapPaneEvent(JMapPaneEvent e) {
224 alfonx 106
225 alfonx 105 if (!(e instanceof FeatureSelectedEvent)) {
226 alfonx 106 // LOGGER.debug("Ignoring event " + e);
227 alfonx 97 return;
228 alfonx 105 }
229 mojays 109 // only listen to events directly coming from JMapPane
230     // selection (ignore selections from FilterDialog)
231     if ( e.getSourceObject() != this.mapPane )
232     return;
233    
234 alfonx 97 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
235 alfonx 106 // LOGGER.debug("Do event " + fse);
236     // LOGGER.debug("Do event " + fse.getSelectionResult());
237 alfonx 97
238     // ignore event if it is caused by the DpLayerVectorSelectionModel
239     if (selectionChangeCausedByMe)
240     return;
241    
242     // Avoid event circles in propertyChange(..)
243     selectionChangeCausedByMe = true;
244    
245     // reset the selection of the DpLayerSelectionModel
246     // layerSelModel.setValueIsAdjusting(true);
247    
248     layerSelModel.addSelection((Collection<Feature>) fse
249     .getSelectionResult());
250    
251     // for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
252     // Feature changedFeature = featureTableModel.getFeature(featureTable
253     // .convertRowIndexToModel(i));
254     // if (featureTable.isRowSelected(i))
255     // layerSelModel.addSelection(changedFeature);
256     // else
257     // layerSelModel.removeSelection(changedFeature);
258     // }
259     // layerSelModel.setValueIsAdjusting(false);
260    
261     // Show selected feature in the map, which is not automatically done by
262     // the FeatureSelectedEvent
263     changeLayerStyle(layerSelModel.getSelection());
264 alfonx 106
265 alfonx 97 // Danger of event circles in propertyChange(..) banned
266     selectionChangeCausedByMe = false;
267     }
268     }

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