/[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 111 - (hide annotations)
Tue May 12 23:33:14 2009 UTC (15 years, 9 months ago) by alfonx
File MIME type: text/plain
File size: 10061 byte(s)
* Fixed some bugs with the SelectionListeners and the JMapPane. To make this work,  StyledFeatureLayerSelectionModel now extends StyledLayerSelectionModel<String>. So the selection is remembered as a Set of Feature-IDs. This change was needed, because Feature.java doesn't overwrite the equals method, and therefore the HashSet didn't function as expected.
* Added new Tools and BUttons to MapPaneToolBar.java to select features
* Changed a lot in MapPaneToolBar.java. It now allows to position Spaces, Actions and Tools via the ID field. (the new feature is to mix them)
* Fixed a bug in AV's ClickInfoPanel that would suddenly pop up an AtlasViewer if started from Geopublisher under special circumstances.
* Moving layers in the legend is using MapContext's move method instead of remove and insert.
* LayerPanel's remember* Maps now all have the MapLayer's ID as a key. 

This commit includes latest schmitzm.jar and av.jar. The av.jar is also commited to the ISDSS, but the ISDSS will still have the old schmitzm.jar. Latest schmitzm.jar in ISDSS should be updated ASAP. I just don't know where to put it.
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.ListSelectionListener;
24    
25     import org.geotools.feature.Feature;
26 alfonx 111 import org.geotools.feature.FeatureCollection;
27 alfonx 97 import org.geotools.map.MapLayer;
28     import org.geotools.styling.Style;
29 alfonx 101 import org.opengis.filter.Filter;
30     import org.opengis.filter.FilterVisitor;
31 alfonx 97
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 alfonx 104 import schmitzm.lang.LangUtil;
38 alfonx 111 import skrueger.geotools.MapPaneToolBar;
39 alfonx 97 import skrueger.geotools.StyledMapInterface;
40    
41     /**
42     * This class keeps the selection of a (feature) {@link JTable} synchronized
43     * with the {@link StyledLayerSelectionModel} of a layer. This is done by
44     * implementing:
45     * <ul>
46     * <li>a {@link PropertyChangeListener} which listens to the
47     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
48     * selection</li>
49     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
50     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
51     * </ul>
52     * After creating, the instance of this synchronizer must be added as listener
53     * to both, the {@link StyledLayerSelectionModel} and the table's
54     * {@link ListSelectionModel}.
55     *
56     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
57     * (University of Bonn/Germany)
58     */
59     public class FeatureMapLayerSelectionSynchronizer extends
60 alfonx 106 StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
61     implements JMapPaneListener {
62 alfonx 111 public static final String SELECTION_STYLING = "SELECTION";
63 alfonx 97 /**
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 alfonx 111 private final MapPaneToolBar toolBar;
71 alfonx 97
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 alfonx 111 JMapPane mapPane, MapPaneToolBar toolBar) {
86 alfonx 97
87     super(layerSelModel);
88     this.styledMapLayer = styledMapLayer;
89    
90     this.mapLayer = mapLayer;
91     this.mapPane = mapPane;
92 alfonx 111 this.toolBar = toolBar;
93 alfonx 97 }
94    
95     /**
96     * Called by {@link StyledLayerSelectionModel} when a the selection on other
97     * selection components (map, chart, ...) has changed. When calling this
98     *
99     * method changes the {@link MapLayer} selection according to the
100     * {@link StyledLayerSelectionModel} selection.
101     *
102     * @param evt
103     * an event
104     */
105     @Override
106     public void propertyChange(PropertyChangeEvent evt) {
107     if (!(evt instanceof StyledLayerSelectionEvent))
108     return;
109     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
110     // Only react on own layer and ignore events invoked by
111     // this component itself
112     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
113     return;
114     // Apply new selection on table (except this event is one of
115     // some more events)
116 alfonx 111 Vector<String> newSelection = layerSelModel.getSelection();
117 alfonx 97 if (newSelection == null)
118     return;
119    
120     // Avoid event circles in valueChanged(..)
121     selectionChangeCausedByMe = true;
122    
123     changeLayerStyle(newSelection);
124    
125     // Danger of event circles in valueChanged(..) banned
126     selectionChangeCausedByMe = false;
127     }
128    
129 alfonx 111 private void changeLayerStyle(final Vector<String> newSelection) {
130 alfonx 97 Style selectionMapStyle = null;
131     try {
132     if (newSelection.isEmpty()) {
133    
134     selectionMapStyle = styledMapLayer.getStyle();
135 alfonx 111 LOGGER.debug("NO SELECTION .. set to original style directly");
136 alfonx 106
137 alfonx 97 } else {
138 alfonx 100 LOGGER.debug("SELECTION .. change style");
139 alfonx 106
140 alfonx 101 Style originalStyle = styledMapLayer.getStyle();
141    
142 alfonx 106 // TODO This clone is a deep clone and it is slow..
143 alfonx 107 selectionMapStyle = StylingUtil
144 alfonx 106 .createDefaultStyle(styledMapLayer.getGeoObject());
145 alfonx 111 selectionMapStyle.getFeatureTypeStyles()[0].setName(SELECTION_STYLING);
146 alfonx 106
147 alfonx 111 // Symbolizer sss =
148     // selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0].getSymbolizers()[0];
149     // if (sss instanceof PolygonSymbolizer) {
150     // PolygonSymbolizer ps = (PolygonSymbolizer) sss;
151     // ps.getFill().setOpacity( FilterUtil.FILTER_FAC.literal(0.5));
152     // }
153    
154 alfonx 106 // Rule selectedRule = StylingUtil.STYLE_FACTORY.createRule();
155 alfonx 107 selectionMapStyle.getFeatureTypeStyles()[0].getRules()[0]
156 alfonx 106 .setFilter(new Filter() {
157    
158     @Override
159     public Object accept(FilterVisitor visitor,
160     Object extraData) {
161     return null;
162 alfonx 105 }
163 alfonx 106
164     @Override
165     public boolean evaluate(Object obj) {
166     if (obj instanceof Feature) {
167     Feature f = (Feature) obj;
168 alfonx 111 // TODO BAD CODE?
169     for (String ffID : newSelection) {
170     if (ffID.equals(f.getID()))
171 alfonx 106 return true;
172     }
173     return false;
174    
175     }
176     return false;
177     }
178    
179     });
180    
181     selectionMapStyle.setFeatureTypeStyles(LangUtil.extendArray(
182 alfonx 111 originalStyle.getFeatureTypeStyles(), selectionMapStyle
183     .getFeatureTypeStyles()));
184 alfonx 106
185     // selectionMapStyle.setFeatureTypeStyles(originalStyle.getF)
186    
187 alfonx 101 // Flat copy the style
188 alfonx 106
189     // selectionMapStyle =
190     // StylingUtil.createDefaultStyle(styledMapLayer.getGeoObject());
191    
192 alfonx 97 }
193    
194     mapLayer.setStyle(selectionMapStyle);
195     mapPane.refresh();
196     } catch (Exception e) {
197     LOGGER.error("Error while trying to create a selection style", e);
198     }
199     }
200    
201     @Override
202     public void performMapPaneEvent(JMapPaneEvent e) {
203 alfonx 106
204 alfonx 111 /**
205     * Only listen to FeatureSelectionEvents if the appropriate tool is
206     * selected.
207     */
208     final int selectedTool = toolBar.getSelectedTool();
209     if (selectedTool != MapPaneToolBar.TOOL_SELECTION_ADD
210     && selectedTool != MapPaneToolBar.TOOL_SELECTION_REMOVE
211     && selectedTool != MapPaneToolBar.TOOL_SELECTION_CLEAR
212     && selectedTool != MapPaneToolBar.TOOL_SELECTION_SET) {
213     return;
214     }
215    
216 alfonx 105 if (!(e instanceof FeatureSelectedEvent)) {
217 alfonx 111 // LOGGER.debug("Ignoring event " + e);
218 alfonx 97 return;
219 alfonx 105 }
220 mojays 109 // only listen to events directly coming from JMapPane
221     // selection (ignore selections from FilterDialog)
222 alfonx 111 if (e.getSourceObject() != this.mapPane)
223     return;
224    
225 alfonx 97 FeatureSelectedEvent fse = (FeatureSelectedEvent) e;
226    
227     // ignore event if it is caused by the DpLayerVectorSelectionModel
228     if (selectionChangeCausedByMe)
229     return;
230    
231 alfonx 111 LOGGER.debug("Do event " + fse);
232    
233 alfonx 97 // Avoid event circles in propertyChange(..)
234     selectionChangeCausedByMe = true;
235    
236 alfonx 111 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_CLEAR) {
237     layerSelModel.clearSelection();
238     } else {
239     Collection<Feature> selectionResult = (Collection<Feature>) fse
240     .getSelectionResult();
241 alfonx 97
242 alfonx 111 // reset the selection of the DpLayerSelectionModel
243     // layerSelModel.setValueIsAdjusting(true);
244 alfonx 97
245 alfonx 111 if (selectedTool == MapPaneToolBar.TOOL_SELECTION_ADD) {
246     layerSelModel.setValueIsAdjusting(true);
247     for (Feature f : selectionResult) {
248     layerSelModel.addSelection(f.getID());
249     }
250     layerSelModel.setValueIsAdjusting(false);
251     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_SET) {
252     layerSelModel.setValueIsAdjusting(true);
253     layerSelModel.clearSelection();
254 alfonx 97
255 alfonx 111 for (Feature f : selectionResult) {
256     layerSelModel.addSelection(f.getID());
257     }
258    
259     LOGGER.debug("Setting selection to " + selectionResult.size());
260     layerSelModel.setValueIsAdjusting(false);
261     } else if (selectedTool == MapPaneToolBar.TOOL_SELECTION_REMOVE) {
262     layerSelModel.setValueIsAdjusting(true);
263     for (Feature f : selectionResult) {
264     layerSelModel.removeSelection(f.getID());
265     }
266     layerSelModel.setValueIsAdjusting(false);
267     }
268     } // els it's not a Clear Selection command
269    
270     // Show selected features in the map, which is not automatically done by
271     // the origin: FeatureSelectedEvent
272 alfonx 97 changeLayerStyle(layerSelModel.getSelection());
273 alfonx 106
274 alfonx 97 // Danger of event circles in propertyChange(..) banned
275     selectionChangeCausedByMe = false;
276     }
277     }

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