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

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