/[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 100 - (hide annotations)
Fri May 8 12:29:45 2009 UTC (15 years, 9 months ago) by alfonx
File MIME type: text/plain
File size: 7653 byte(s)
(Hacking Session in action - atomic commits can't be ensured)
- Remodeling the SelectionModels
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     import org.geotools.styling.Style;
30    
31     import schmitzm.geotools.gui.FeatureCollectionTableModel;
32     import schmitzm.geotools.gui.JMapPane;
33     import schmitzm.geotools.map.event.FeatureSelectedEvent;
34     import schmitzm.geotools.map.event.JMapPaneEvent;
35     import schmitzm.geotools.map.event.JMapPaneListener;
36     import schmitzm.geotools.styling.StylingUtil;
37     import skrueger.geotools.StyledFeatureCollectionInterface;
38     import skrueger.geotools.StyledMapInterface;
39     import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;
40     import skrueger.geotools.selection.StyledLayerSelectionEvent;
41     import skrueger.geotools.selection.StyledLayerSelectionModel;
42    
43     /**
44     * This class keeps the selection of a (feature) {@link JTable} synchronized
45     * with the {@link StyledLayerSelectionModel} of a layer. This is done by
46     * implementing:
47     * <ul>
48     * <li>a {@link PropertyChangeListener} which listens to the
49     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
50     * selection</li>
51     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
52     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
53     * </ul>
54     * After creating, the instance of this synchronizer must be added as listener
55     * to both, the {@link StyledLayerSelectionModel} and the table's
56     * {@link ListSelectionModel}.
57     *
58     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
59     * (University of Bonn/Germany)
60     */
61     public class FeatureMapLayerSelectionSynchronizer extends
62     StyledLayerSelectionModelSynchronizer<Feature> implements
63     JMapPaneListener {
64     /**
65     * Holds the {@link MapLayer} to keep synchronized with the layer selection
66     * model.
67     */
68     protected final MapLayer mapLayer;
69     protected final StyledMapInterface<?> styledMapLayer;
70     protected final JMapPane mapPane;
71    
72     /**
73     * Creates a new synchronizer
74     *
75     * @param layerSelModel
76     * layer selection model to keep synchronized with the
77     * {@link MapLayer}
78     *
79     * @param mapLayer
80     * {@link MapLayer} to keep synchronized with.
81     */
82     public FeatureMapLayerSelectionSynchronizer(
83     StyledFeatureLayerSelectionModel layerSelModel,
84     StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
85     JMapPane mapPane) {
86    
87     super(layerSelModel);
88     this.styledMapLayer = styledMapLayer;
89    
90     this.mapLayer = mapLayer;
91     this.mapPane = mapPane;
92     }
93    
94     /**
95     * Called by {@link StyledLayerSelectionModel} when a the selection on other
96     * selection components (map, chart, ...) has changed. When calling this
97     *
98     * method changes the {@link MapLayer} selection according to the
99     * {@link StyledLayerSelectionModel} selection.
100     *
101     * @param evt
102     * an event
103     */
104     @Override
105     public void propertyChange(PropertyChangeEvent evt) {
106     if (!(evt instanceof StyledLayerSelectionEvent))
107     return;
108     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
109     // Only react on own layer and ignore events invoked by
110     // this component itself
111     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
112     return;
113     // Apply new selection on table (except this event is one of
114     // some more events)
115     Vector<Feature> newSelection = layerSelModel.getSelection();
116     if (newSelection == null)
117     return;
118    
119     // Avoid event circles in valueChanged(..)
120     selectionChangeCausedByMe = true;
121    
122     changeLayerStyle(newSelection);
123    
124     // Danger of event circles in valueChanged(..) banned
125     selectionChangeCausedByMe = false;
126     }
127    
128     private void changeLayerStyle(Vector<Feature> newSelection) {
129     Style selectionMapStyle = null;
130     try {
131     if (newSelection.isEmpty()) {
132    
133     selectionMapStyle = styledMapLayer.getStyle();
134 alfonx 100 LOGGER.debug("NO SELECTION .. reset original style");
135    
136 alfonx 97 } else {
137 alfonx 100 LOGGER.debug("SELECTION .. change style");
138 alfonx 97 selectionMapStyle = StylingUtil
139     .createDefaultStyle(styledMapLayer.getGeoObject());
140 alfonx 100
141     selectionMapStyle = styledMapLayer.getStyle();
142    
143 alfonx 97 }
144    
145     mapLayer.setStyle(selectionMapStyle);
146     mapPane.refresh();
147     } catch (Exception e) {
148     LOGGER.error("Error while trying to create a selection style", e);
149     }
150     }
151    
152     //
153     // /**
154     // * Called when the table selection is changed by the user. When calling
155     // this
156     // * method changes the selection of the {@link StyledLayerSelectionModel}.
157     // *
158     // * @param evt
159     // * an event
160     // */
161     // @Override
162     // public void valueChanged(ListSelectionEvent evt) {
163     //
164     // }
165    
166     @Override
167     public void performMapPaneEvent(JMapPaneEvent e) {
168     if (!(e instanceof FeatureSelectedEvent))
169     return;
170    
171     FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
172    
173     // ignore event if it is caused by the DpLayerVectorSelectionModel
174     if (selectionChangeCausedByMe)
175     return;
176    
177     // Avoid event circles in propertyChange(..)
178     selectionChangeCausedByMe = true;
179    
180     // reset the selection of the DpLayerSelectionModel
181     // layerSelModel.setValueIsAdjusting(true);
182    
183     layerSelModel.addSelection((Collection<Feature>) fse
184     .getSelectionResult());
185    
186     // for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
187     // Feature changedFeature = featureTableModel.getFeature(featureTable
188     // .convertRowIndexToModel(i));
189     // if (featureTable.isRowSelected(i))
190     // layerSelModel.addSelection(changedFeature);
191     // else
192     // layerSelModel.removeSelection(changedFeature);
193     // }
194     // layerSelModel.setValueIsAdjusting(false);
195    
196     // Show selected feature in the map, which is not automatically done by
197     // the FeatureSelectedEvent
198     changeLayerStyle(layerSelModel.getSelection());
199    
200     // Danger of event circles in propertyChange(..) banned
201     selectionChangeCausedByMe = false;
202     }
203     }

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