/[schmitzm]/trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 106 - (hide annotations)
Fri May 8 15:13:49 2009 UTC (15 years, 9 months ago) by alfonx
File MIME type: text/plain
File size: 7079 byte(s)
* Improved styling for selected features.
1 alfonx 96 /**
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.Vector;
19    
20     import javax.swing.JTable;
21     import javax.swing.ListSelectionModel;
22     import javax.swing.event.ListSelectionEvent;
23     import javax.swing.event.ListSelectionListener;
24 alfonx 100 import javax.swing.table.TableModel;
25 alfonx 96
26     import org.geotools.feature.Feature;
27    
28     import schmitzm.geotools.gui.FeatureCollectionTableModel;
29 alfonx 100 import schmitzm.swing.table.PipedTableModel;
30     import schmitzm.swing.table.SelectionTableModel;
31 alfonx 96 import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;
32     import skrueger.geotools.selection.StyledLayerSelectionEvent;
33     import skrueger.geotools.selection.StyledLayerSelectionModel;
34    
35     /**
36     * This class keeps the selection of a (feature) {@link JTable} synchronized
37 alfonx 100 * with the {@link StyledLayerSelectionModel} of a layer. This is done by
38     * implementing:
39 alfonx 96 * <ul>
40 alfonx 100 * <li>a {@link PropertyChangeListener} which listens to the
41     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
42     * selection</li>
43     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
44     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
45 alfonx 96 * </ul>
46 alfonx 100 * After creating, the instance of this synchronizer must be added as listener
47     * to both, the {@link StyledLayerSelectionModel} and the table's
48     * {@link ListSelectionModel}.
49     *
50     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
51     * (University of Bonn/Germany)
52 alfonx 96 */
53 mojays 103 public class TableSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> implements ListSelectionListener {
54 alfonx 100 /**
55     * Holds the table to keep synchronized with the layer selection model.
56     */
57     protected JTable featureTable = null;
58     /** Holds the table's data. */
59     protected FeatureCollectionTableModel featureTableModel = null;
60    
61     /**
62 alfonx 96 * Creates a new synchronizer
63     *
64     * @param layerSelModel
65     * layer selection model to keep synchronized with the feature
66     * table
67     * @param table
68     * table to keep synchronized with the layer selection model
69     */
70 mojays 103 public TableSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
71 alfonx 96 super(layerSelModel);
72    
73 alfonx 100 TableModel model = table.getModel();
74     if (model instanceof PipedTableModel)
75     model = ((PipedTableModel) table.getModel()).getPipedModel();
76    
77     if (!(model instanceof FeatureCollectionTableModel))
78 alfonx 96 throw new IllegalArgumentException(
79     "Table must have a model instance of FeatureCollectionTableModel");
80    
81 alfonx 100 this.featureTableModel = (FeatureCollectionTableModel) model;
82 alfonx 96 this.featureTable = table;
83     }
84    
85 alfonx 100 /**
86     * Called by {@link StyledLayerSelectionModel} when a the selection on other
87     * selection components (map, chart, ...) has changed. When calling this
88     * method changes the table selection according to the
89     * {@link StyledLayerSelectionModel} selection.
90     *
91     * @param evt
92     * an event
93     */
94     @Override
95     public void propertyChange(PropertyChangeEvent evt) {
96     if (!(evt instanceof StyledLayerSelectionEvent))
97     return;
98     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
99     // Only react on own layer and ignore events invoked by
100     // this component itself
101     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
102     return;
103     // Apply new selection on table (except this event is one of
104     // some more events)
105     Vector<Feature> newSelection = layerSelModel.getSelection();
106     if (newSelection == null)
107     return;
108 alfonx 105
109 alfonx 106 // LOGGER.debug("Table pop changed "+selEvt);
110     // LOGGER.debug("Table pop changed to "+newSelection);
111 alfonx 96
112 alfonx 100 // Avoid event circles in valueChanged(..)
113     selectionChangeCausedByMe = true;
114 alfonx 96
115 alfonx 100 ListSelectionModel tableSelModel = featureTable.getSelectionModel();
116     tableSelModel.setValueIsAdjusting(true);
117     tableSelModel.clearSelection();
118     for (Feature f : newSelection) {
119     int modelIdx = featureTableModel.findFeature(f);
120     if (modelIdx >= 0) {
121     int tableIdx = featureTable.convertRowIndexToView(modelIdx);
122     tableSelModel.addSelectionInterval(tableIdx, tableIdx);
123     }
124     }
125     tableSelModel.setValueIsAdjusting(false); // event is fired
126     // autmatically!
127 alfonx 96
128 alfonx 100 // Danger of event circles in valueChanged(..) banned
129     selectionChangeCausedByMe = false;
130     }
131 alfonx 96
132 alfonx 100 /**
133     * Called when the table selection is changed by the user. When calling this
134     * method changes the selection of the {@link StyledLayerSelectionModel}.
135     *
136     * @param evt
137     * an event
138     */
139     @Override
140     public void valueChanged(ListSelectionEvent evt) {
141     // ignore event if it is part of multiple events
142     if (evt != null && evt.getValueIsAdjusting())
143     return;
144     // ignore event if it is caused by the DpLayerVectorSelectionModel
145     if (selectionChangeCausedByMe)
146     return;
147    
148     // Avoid event circles in propertyChange(..)
149     selectionChangeCausedByMe = true;
150    
151     // reset the selection of the DpLayerSelectionModel
152     layerSelModel.setValueIsAdjusting(true);
153     for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
154     Feature changedFeature = featureTableModel.getFeature(featureTable
155     .convertRowIndexToModel(i));
156     if (featureTable.isRowSelected(i))
157     layerSelModel.addSelection(changedFeature);
158     else
159     layerSelModel.removeSelection(changedFeature);
160     }
161     layerSelModel.setValueIsAdjusting(false);
162    
163     // Danger of event circles in propertyChange(..) banned
164     selectionChangeCausedByMe = false;
165     }
166 alfonx 96 }

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