/[schmitzm]/branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Contents of /branches/2.1/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (show annotations)
Fri Jun 19 16:22:29 2009 UTC (15 years, 8 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
File MIME type: text/plain
File size: 11256 byte(s)
* Selection stuff technically ready to use! :-) :-) Next translating tooltips etc... 
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.Arrays;
19 import java.util.Collection;
20 import java.util.Vector;
21
22 import javax.swing.JTable;
23 import javax.swing.ListSelectionModel;
24 import javax.swing.event.ListSelectionListener;
25
26 import org.geotools.feature.Feature;
27 import org.geotools.map.MapLayer;
28 import org.geotools.styling.FeatureTypeStyle;
29 import org.geotools.styling.Style;
30 import org.opengis.filter.Filter;
31 import org.opengis.filter.FilterVisitor;
32
33 import schmitzm.geotools.gui.JMapPane;
34 import schmitzm.geotools.map.event.FeatureSelectedEvent;
35 import schmitzm.geotools.map.event.JMapPaneEvent;
36 import schmitzm.geotools.map.event.JMapPaneListener;
37 import schmitzm.geotools.styling.StylingUtil;
38 import schmitzm.lang.LangUtil;
39 import skrueger.geotools.MapPaneToolBar;
40 import skrueger.geotools.StyledMapInterface;
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<StyledFeatureLayerSelectionModel>
62 implements JMapPaneListener {
63 public static final String SELECTION_STYLING = "SELECTION";
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 private final MapPaneToolBar toolBar;
72
73 /**
74 * Creates a new synchronizer
75 *
76 * @param layerSelModel
77 * layer selection model to keep synchronized with the
78 * {@link MapLayer}
79 *
80 * @param mapLayer
81 * {@link MapLayer} to keep synchronized with.
82 */
83 public FeatureMapLayerSelectionSynchronizer(
84 StyledFeatureLayerSelectionModel layerSelModel,
85 StyledMapInterface<?> styledMapLayer, MapLayer mapLayer,
86 JMapPane mapPane, MapPaneToolBar toolBar) {
87
88 super(layerSelModel);
89 this.styledMapLayer = styledMapLayer;
90
91 this.mapLayer = mapLayer;
92 this.mapPane = mapPane;
93 this.toolBar = toolBar;
94 }
95
96 /**
97 * Called by {@link StyledLayerSelectionModel} when a the selection on other
98 * selection components (map, chart, ...) has changed. When calling this
99 *
100 * method changes the {@link MapLayer} selection according to the
101 * {@link StyledLayerSelectionModel} selection.
102 *
103 * @param evt
104 * an event
105 */
106 @Override
107 public void propertyChange(PropertyChangeEvent evt) {
108
109 if (!isEnabled())
110 return;
111
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<String> 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 /**
135 * Changes the Style of the {@link MapLayer} to reflect changes of the
136 * selection.
137 *
138 * @param newSelection
139 * A {@link Vector} of Feature-IDs that are selected.
140 */
141 private void changeLayerStyle(final Vector<String> newSelection) {
142 Style selectionMapStyle = null;
143 try {
144 if (newSelection.isEmpty()) {
145
146 selectionMapStyle = styledMapLayer.getStyle();
147 // LOGGER.debug("NO SELECTION .. set to original style directly");
148
149 } else {
150 LOGGER.debug("SELECTION .. change style");
151
152 // We take Style from the MapLayer that is displayed at the
153 // moment. We do not use the styledMapLayer.getStyle, because in
154 // the atlas, this always return the default style, but
155 // additionaly styles might be selected.
156 // Taking the style from the mapLayer indicated, that we have to
157 // remove any selection rules first.
158 Style originalStyle = mapLayer.getStyle();
159
160 // TODO The default style is not good here. We need
161 // .createSelectionStyle(normalStyle, geoObj);
162 selectionMapStyle = StylingUtil
163 .createSelectionStyle(styledMapLayer.getGeoObject());
164 // selectionMapStyle = StylingUtil
165 // .createDefaultStyle(styledMapLayer.getGeoObject());
166
167 selectionMapStyle.getFeatureTypeStyles()[0]
168 .setName(SELECTION_STYLING);
169
170 // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
171 selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
172 .setFilter(new Filter() {
173
174 @Override
175 public Object accept(FilterVisitor visitor,
176 Object extraData) {
177 return null;
178 }
179
180 @Override
181 public boolean evaluate(Object obj) {
182 if (obj instanceof Feature) {
183 Feature f = (Feature) obj;
184 // TODO BAD CODE! says Martin.. i am fine
185 // with it.. well.. not great...
186 for (String ffID : newSelection) {
187 if (ffID.equals(f.getID()))
188 return true;
189 }
190 return false;
191
192 }
193 return false;
194 }
195
196 });
197
198 FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
199 .getFeatureTypeStyles();
200 FeatureTypeStyle[] newFeatureTypes;
201 if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
202 .getName() != null
203 && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
204 .getName().equals(SELECTION_STYLING)) {
205 newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,
206 originalFeatureTypeStyles.length - 1);
207 } else {
208 newFeatureTypes = originalFeatureTypeStyles;
209 }
210
211 // The last FTS is the selection FTS
212 newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
213 selectionMapStyle.getFeatureTypeStyles());
214
215 selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
216
217 }
218
219 mapLayer.setStyle(selectionMapStyle);
220 mapPane.refresh();
221 } catch (Exception e) {
222 LOGGER.error("Error while trying to create a selection style", e);
223 }
224 }
225
226 /**
227 * Used to synchronize {@link FeatureSelectedEvent}s with the
228 * {@link StyledFeatureLayerSelectionModel}
229 */
230 @Override
231 public void performMapPaneEvent(JMapPaneEvent e) {
232
233 // Ignore event if it is caused by us or the synchronizer is disabled.
234 if (!isEnabled() || selectionChangeCausedByMe)
235 return;
236
237 if (!(e instanceof FeatureSelectedEvent)) {
238 // LOGGER.debug("Ignoring event " + e);
239 return;
240 }
241
242 /**
243 * Only listen to FeatureSelectedEvents if an appropriate tool is
244 * selected.
245 */
246 final int selectedTool = toolBar.getSelectedTool();
247 if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
248 && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
249 && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250 return;
251 }
252
253 // only listen to events directly coming from JMapPane
254 // selection (ignore selections from FilterDialog)
255 if (e.getSourceObject() != this.mapPane)
256 return;
257
258 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
259
260 /**
261 * Checking, that the FeatureSelectedEvent actually contains features from this layer
262 */
263 final String sourceID = fse.getSourceLayer().getTitle();
264 final String syncForID = mapLayer.getTitle();
265 if (sourceID != null && syncForID != null
266 && !sourceID.equals(syncForID)){
267 LOGGER.debug("Ignoring a FeatureSelectedEvent from "+sourceID);
268 return;
269 }
270
271
272 LOGGER.debug("do event " + fse);
273
274 // Avoid event circles in propertyChange(..)
275 selectionChangeCausedByMe = true;
276
277 Collection<Feature> selectionResult = (Collection<Feature>) fse
278 .getSelectionResult();
279
280 // reset the selection of the DpLayerSelectionModel
281 // layerSelModel.setValueIsAdjusting(true);
282
283 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
284 layerSelModel.setValueIsAdjusting(true);
285 for (Feature f : selectionResult) {
286 layerSelModel.addSelection(f.getID());
287 }
288 layerSelModel.setValueIsAdjusting(false);
289 } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
290 layerSelModel.setValueIsAdjusting(true);
291 layerSelModel.clearSelection();
292
293 for (Feature f : selectionResult) {
294 layerSelModel.addSelection(f.getID());
295 }
296
297 LOGGER.debug("Setting selection to " + selectionResult.size());
298 layerSelModel.setValueIsAdjusting(false);
299 } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
300 layerSelModel.setValueIsAdjusting(true);
301 for (Feature f : selectionResult) {
302 layerSelModel.removeSelection(f.getID());
303 }
304 layerSelModel.setValueIsAdjusting(false);
305 }
306
307 // Show selected features in the map, which is not automatically done by
308 // the origin: FeatureSelectedEvent
309 changeLayerStyle(layerSelModel.getSelection());
310
311 // Danger of event circles in propertyChange(..) banned
312 selectionChangeCausedByMe = false;
313 }
314
315 }

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