/[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 103 - (hide annotations)
Fri May 8 13:48:00 2009 UTC (15 years, 9 months ago) by mojays
File MIME type: text/plain
File size: 8459 byte(s)


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

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