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

Contents of /branches/2.2.x/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


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

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