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

Contents of /branches/2.1/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


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

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