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

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