/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
ViewVC logotype

Annotation of /branches/1.0-gt2-2.6/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 127 - (hide annotations)
Sat May 30 14:29:58 2009 UTC (15 years, 9 months ago) by mojays
Original Path: trunk/src/skrueger/geotools/selection/FeatureMapLayerSelectionSynchronizer.java
File MIME type: text/plain
File size: 11509 byte(s)
Many many many new JFreeChart stuff (selection panel and renderer, etc.)
- schmitzm.jfree.chart.SelectableChartPanel
- schmitzm.jfree.chart.ChartMouseSelectionTracker
- schmitzm.jfree.chart.selection.*
- schmitzm.jfree.chart.renderer.*
Plus: Imports organized!
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 alfonx 123 import java.util.Arrays;
19 alfonx 97 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 alfonx 123 import org.geotools.styling.FeatureTypeStyle;
29 alfonx 97 import org.geotools.styling.Style;
30 alfonx 101 import org.opengis.filter.Filter;
31     import org.opengis.filter.FilterVisitor;
32 alfonx 97
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 alfonx 104 import schmitzm.lang.LangUtil;
39 alfonx 111 import skrueger.geotools.MapPaneToolBar;
40 alfonx 97 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 alfonx 106 StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
62     implements JMapPaneListener {
63 alfonx 111 public static final String SELECTION_STYLING = "SELECTION";
64 alfonx 97 /**
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 alfonx 111 private final MapPaneToolBar toolBar;
72 alfonx 97
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 alfonx 111 JMapPane mapPane, MapPaneToolBar toolBar) {
87 alfonx 97
88     super(layerSelModel);
89     this.styledMapLayer = styledMapLayer;
90    
91     this.mapLayer = mapLayer;
92     this.mapPane = mapPane;
93 alfonx 111 this.toolBar = toolBar;
94 alfonx 97 }
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     if (!(evt instanceof StyledLayerSelectionEvent))
109     return;
110     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
111     // Only react on own layer and ignore events invoked by
112     // this component itself
113     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
114     return;
115     // Apply new selection on table (except this event is one of
116     // some more events)
117 alfonx 111 Vector<String> newSelection = layerSelModel.getSelection();
118 alfonx 97 if (newSelection == null)
119     return;
120    
121     // Avoid event circles in valueChanged(..)
122     selectionChangeCausedByMe = true;
123    
124     changeLayerStyle(newSelection);
125    
126     // Danger of event circles in valueChanged(..) banned
127     selectionChangeCausedByMe = false;
128     }
129    
130 alfonx 111 private void changeLayerStyle(final Vector<String> newSelection) {
131 alfonx 97 Style selectionMapStyle = null;
132     try {
133     if (newSelection.isEmpty()) {
134    
135     selectionMapStyle = styledMapLayer.getStyle();
136 alfonx 111 LOGGER.debug("NO SELECTION .. set to original style directly");
137 alfonx 106
138 alfonx 97 } else {
139 alfonx 100 LOGGER.debug("SELECTION .. change style");
140 alfonx 106
141 alfonx 123 // We take Style from the MapLayer that is displayed at the
142     // moment. We do not use the styledMapLayer.getStyle, because in
143     // the atlas, this always return the default style, but
144     // additionaly styles might be selected.
145     // Taking the style from the mapLayer indicated, that we have to
146 alfonx 125 // remove any selection rules first.
147 alfonx 123 Style originalStyle = mapLayer.getStyle();
148 alfonx 101
149 alfonx 123 // TODO The default style is not good here. We need
150     // .createSelectionStyle(normalStyle, geoObj);
151 alfonx 107 selectionMapStyle = StylingUtil
152 alfonx 106 .createDefaultStyle(styledMapLayer.getGeoObject());
153    
154 alfonx 123 selectionMapStyle.getFeatureTypeStyles()[0]
155     .setName(SELECTION_STYLING);
156    
157 alfonx 111 // Symbolizer sss =
158     // selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];
159     // if (sss instanceof PolygonSymbolizer) {
160     // PolygonSymbolizer ps = (PolygonSymbolizer) sss;
161     // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));
162     // }
163    
164 alfonx 106 // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
165 alfonx 107 selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
166 alfonx 106 .setFilter(new Filter() {
167    
168     @Override
169     public Object accept(FilterVisitor visitor,
170     Object extraData) {
171     return null;
172 alfonx 105 }
173 alfonx 106
174     @Override
175     public boolean evaluate(Object obj) {
176     if (obj instanceof Feature) {
177     Feature f = (Feature) obj;
178 alfonx 125 // TODO BAD CODE! says Martin.. i am fine
179     // with it.. well.. not great...
180 alfonx 111 for (String ffID : newSelection) {
181     if (ffID.equals(f.getID()))
182 alfonx 106 return true;
183     }
184     return false;
185    
186     }
187     return false;
188     }
189    
190     });
191    
192 alfonx 125 FeatureTypeStyle[] originalFeatureTypeStyles = originalStyle
193     .getFeatureTypeStyles();
194     FeatureTypeStyle[] newFeatureTypes;
195     if (originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
196     .getName() != null
197     && originalFeatureTypeStyles[originalFeatureTypeStyles.length - 1]
198     .getName().equals(SELECTION_STYLING)) {
199     newFeatureTypes = Arrays.copyOf(originalFeatureTypeStyles,
200     originalFeatureTypeStyles.length - 1);
201 alfonx 123 } else {
202     newFeatureTypes = originalFeatureTypeStyles;
203     }
204 alfonx 125 //
205     // // REMOVE any Selection_Styling FTS (usually the last one)
206     //
207     // for (FeatureTypeStyle fts : originalFeatureTypeStyles){
208     // if (fts.getName().equals(SELECTION_STYLING)) continue;
209     // newFeatureTypes =
210     //
211     // LangUtil.extendArray(
212     // originalFeatureTypeStyles, selectionMapStyle
213     // .getFeatureTypeStyles());
214     // }
215    
216 alfonx 123 // The last FTS is the selection FTS
217 alfonx 125 newFeatureTypes = LangUtil.extendArray(newFeatureTypes,
218     selectionMapStyle.getFeatureTypeStyles());
219    
220 alfonx 123 selectionMapStyle.setFeatureTypeStyles(newFeatureTypes);
221 alfonx 106
222     // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)
223    
224 alfonx 101 // Flat copy the style
225 alfonx 106
226     // selectionMapStyle =
227     // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());
228    
229 alfonx 97 }
230    
231     mapLayer.setStyle(selectionMapStyle);
232     mapPane.refresh();
233     } catch (Exception e) {
234     LOGGER.error("Error while trying to create a selection style", e);
235     }
236     }
237    
238     @Override
239     public void performMapPaneEvent(JMapPaneEvent e) {
240 alfonx 106
241 alfonx 111 /**
242     * Only listen to FeatureSelectionEvents if the appropriate tool is
243     * selected.
244     */
245     final int selectedTool = toolBar.getSelectedTool();
246     if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
247     && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
248     && selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR
249     && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
250     return;
251     }
252    
253 alfonx 105 if (!(e instanceof FeatureSelectedEvent)) {
254 alfonx 111 // LOGGER.debug("Ignoring event " + e);
255 alfonx 97 return;
256 alfonx 105 }
257 mojays 109 // only listen to events directly coming from JMapPane
258     // selection (ignore selections from FilterDialog)
259 alfonx 111 if (e.getSourceObject() != this.mapPane)
260     return;
261    
262 alfonx 97 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
263    
264     // ignore event if it is caused by the DpLayerVectorSelectionModel
265     if (selectionChangeCausedByMe)
266     return;
267    
268 alfonx 111 LOGGER.debug("Do event " + fse);
269    
270 alfonx 97 // Avoid event circles in propertyChange(..)
271     selectionChangeCausedByMe = true;
272    
273 alfonx 111 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) {
274     layerSelModel.clearSelection();
275     } else {
276     Collection<Feature> selectionResult = (Collection<Feature>) fse
277     .getSelectionResult();
278 alfonx 97
279 alfonx 111 // reset the selection of the DpLayerSelectionModel
280     // layerSelModel.setValueIsAdjusting(true);
281 alfonx 97
282 alfonx 111 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
283     layerSelModel.setValueIsAdjusting(true);
284     for (Feature f : selectionResult) {
285     layerSelModel.addSelection(f.getID());
286     }
287     layerSelModel.setValueIsAdjusting(false);
288     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
289     layerSelModel.setValueIsAdjusting(true);
290     layerSelModel.clearSelection();
291 alfonx 97
292 alfonx 111 for (Feature f : selectionResult) {
293     layerSelModel.addSelection(f.getID());
294     }
295    
296     LOGGER.debug("Setting selection to " + selectionResult.size());
297     layerSelModel.setValueIsAdjusting(false);
298     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
299     layerSelModel.setValueIsAdjusting(true);
300     for (Feature f : selectionResult) {
301     layerSelModel.removeSelection(f.getID());
302     }
303     layerSelModel.setValueIsAdjusting(false);
304     }
305     } // els it's not a Clear Selection command
306    
307     // Show selected features in the map, which is not automatically done by
308     // the origin: FeatureSelectedEvent
309 alfonx 97 changeLayerStyle(layerSelModel.getSelection());
310 alfonx 106
311 alfonx 97 // Danger of event circles in propertyChange(..) banned
312     selectionChangeCausedByMe = false;
313     }
314     }

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