/[schmitzm]/branches/2.3.x/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (hide annotations)
Fri May 8 13:48:00 2009 UTC (15 years, 9 months ago) by mojays
Original Path: trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
File MIME type: text/plain
File size: 6973 byte(s)


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 96
109 alfonx 100 // Avoid event circles in valueChanged(..)
110     selectionChangeCausedByMe = true;
111 alfonx 96
112 alfonx 100 ListSelectionModel tableSelModel = featureTable.getSelectionModel();
113     tableSelModel.setValueIsAdjusting(true);
114     tableSelModel.clearSelection();
115     for (Feature f : newSelection) {
116     int modelIdx = featureTableModel.findFeature(f);
117     if (modelIdx >= 0) {
118     int tableIdx = featureTable.convertRowIndexToView(modelIdx);
119     tableSelModel.addSelectionInterval(tableIdx, tableIdx);
120     }
121     }
122     tableSelModel.setValueIsAdjusting(false); // event is fired
123     // autmatically!
124 alfonx 96
125 alfonx 100 // Danger of event circles in valueChanged(..) banned
126     selectionChangeCausedByMe = false;
127     }
128 alfonx 96
129 alfonx 100 /**
130     * Called when the table selection is changed by the user. When calling this
131     * method changes the selection of the {@link StyledLayerSelectionModel}.
132     *
133     * @param evt
134     * an event
135     */
136     @Override
137     public void valueChanged(ListSelectionEvent evt) {
138     // ignore event if it is part of multiple events
139     if (evt != null && evt.getValueIsAdjusting())
140     return;
141     // ignore event if it is caused by the DpLayerVectorSelectionModel
142     if (selectionChangeCausedByMe)
143     return;
144    
145     // Avoid event circles in propertyChange(..)
146     selectionChangeCausedByMe = true;
147    
148     // reset the selection of the DpLayerSelectionModel
149     layerSelModel.setValueIsAdjusting(true);
150     for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
151     Feature changedFeature = featureTableModel.getFeature(featureTable
152     .convertRowIndexToModel(i));
153     if (featureTable.isRowSelected(i))
154     layerSelModel.addSelection(changedFeature);
155     else
156     layerSelModel.removeSelection(changedFeature);
157     }
158     layerSelModel.setValueIsAdjusting(false);
159    
160     // Danger of event circles in propertyChange(..) banned
161     selectionChangeCausedByMe = false;
162     }
163 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