/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Contents of /branches/2.0-RC2/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


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

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