/[schmitzm]/branches/2.0-GP14/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
ViewVC logotype

Annotation of /branches/2.0-GP14/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (hide annotations)
Fri May 8 12:29:45 2009 UTC (15 years, 9 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
File MIME type: text/plain
File size: 6957 byte(s)
(Hacking Session in action - atomic commits can't be ensured)
- Remodeling the SelectionModels
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 alfonx 100 public class TableSelectionSynchronizer extends
54     StyledLayerSelectionModelSynchronizer<Feature> implements
55     ListSelectionListener {
56     /**
57     * Holds the table to keep synchronized with the layer selection model.
58     */
59     protected JTable featureTable = null;
60     /** Holds the table's data. */
61     protected FeatureCollectionTableModel featureTableModel = null;
62    
63     /**
64 alfonx 96 * Creates a new synchronizer
65     *
66     * @param layerSelModel
67     * layer selection model to keep synchronized with the feature
68     * table
69     * @param table
70     * table to keep synchronized with the layer selection model
71     */
72     public TableSelectionSynchronizer(
73     StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
74 alfonx 100
75 alfonx 96 super(layerSelModel);
76    
77 alfonx 100 TableModel model = table.getModel();
78     if (model instanceof PipedTableModel)
79     model = ((PipedTableModel) table.getModel()).getPipedModel();
80    
81     if (!(model instanceof FeatureCollectionTableModel))
82 alfonx 96 throw new IllegalArgumentException(
83     "Table must have a model instance of FeatureCollectionTableModel");
84    
85 alfonx 100 this.featureTableModel = (FeatureCollectionTableModel) model;
86 alfonx 96 this.featureTable = table;
87     }
88    
89 alfonx 100 /**
90     * Called by {@link StyledLayerSelectionModel} when a the selection on other
91     * selection components (map, chart, ...) has changed. When calling this
92     * method changes the table selection according to the
93     * {@link StyledLayerSelectionModel} selection.
94     *
95     * @param evt
96     * an event
97     */
98     @Override
99     public void propertyChange(PropertyChangeEvent evt) {
100     if (!(evt instanceof StyledLayerSelectionEvent))
101     return;
102     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
103     // Only react on own layer and ignore events invoked by
104     // this component itself
105     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
106     return;
107     // Apply new selection on table (except this event is one of
108     // some more events)
109     Vector<Feature> newSelection = layerSelModel.getSelection();
110     if (newSelection == null)
111     return;
112 alfonx 96
113 alfonx 100 // Avoid event circles in valueChanged(..)
114     selectionChangeCausedByMe = true;
115 alfonx 96
116 alfonx 100 ListSelectionModel tableSelModel = featureTable.getSelectionModel();
117     tableSelModel.setValueIsAdjusting(true);
118     tableSelModel.clearSelection();
119     for (Feature f : newSelection) {
120     int modelIdx = featureTableModel.findFeature(f);
121     if (modelIdx >= 0) {
122     int tableIdx = featureTable.convertRowIndexToView(modelIdx);
123     tableSelModel.addSelectionInterval(tableIdx, tableIdx);
124     }
125     }
126     tableSelModel.setValueIsAdjusting(false); // event is fired
127     // autmatically!
128 alfonx 96
129 alfonx 100 // Danger of event circles in valueChanged(..) banned
130     selectionChangeCausedByMe = false;
131     }
132 alfonx 96
133 alfonx 100 /**
134     * Called when the table selection is changed by the user. When calling this
135     * method changes the selection of the {@link StyledLayerSelectionModel}.
136     *
137     * @param evt
138     * an event
139     */
140     @Override
141     public void valueChanged(ListSelectionEvent evt) {
142     // ignore event if it is part of multiple events
143     if (evt != null && evt.getValueIsAdjusting())
144     return;
145     // ignore event if it is caused by the DpLayerVectorSelectionModel
146     if (selectionChangeCausedByMe)
147     return;
148    
149     // Avoid event circles in propertyChange(..)
150     selectionChangeCausedByMe = true;
151    
152     // reset the selection of the DpLayerSelectionModel
153     layerSelModel.setValueIsAdjusting(true);
154     for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
155     Feature changedFeature = featureTableModel.getFeature(featureTable
156     .convertRowIndexToModel(i));
157     if (featureTable.isRowSelected(i))
158     layerSelModel.addSelection(changedFeature);
159     else
160     layerSelModel.removeSelection(changedFeature);
161     }
162     layerSelModel.setValueIsAdjusting(false);
163    
164     // Danger of event circles in propertyChange(..) banned
165     selectionChangeCausedByMe = false;
166     }
167 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