/[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 604 - (hide annotations)
Wed Dec 9 14:15:53 2009 UTC (15 years, 2 months ago) by alfonx
Original Path: branches/2.0-RC1/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
File MIME type: text/plain
File size: 8837 byte(s)
2.0-RC1 branch ist für GP1.3 bugfixes...  1.0-gt2-2.6  ist der entwicklungs brnach 
1 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     * Martin O. J. Schmitz - initial API and implementation
28     * Stefan A. Krüger - additional utility classes
29     ******************************************************************************/
30 alfonx 96 /**
31     Copyright 2008 Stefan Alfons Krüger
32    
33     atlas-framework - This file is part of the Atlas Framework
34    
35     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.
36     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.
37     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
38    
39     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.
40     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.
41     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.
42     **/
43     package skrueger.geotools.selection;
44    
45     import java.beans.PropertyChangeEvent;
46     import java.beans.PropertyChangeListener;
47     import java.util.Vector;
48    
49     import javax.swing.JTable;
50     import javax.swing.ListSelectionModel;
51     import javax.swing.event.ListSelectionEvent;
52     import javax.swing.event.ListSelectionListener;
53 alfonx 100 import javax.swing.table.TableModel;
54 alfonx 96
55 alfonx 322 import org.opengis.feature.simple.SimpleFeature;
56 alfonx 96
57     import schmitzm.geotools.gui.FeatureCollectionTableModel;
58 alfonx 100 import schmitzm.swing.table.PipedTableModel;
59 alfonx 96
60     /**
61     * This class keeps the selection of a (feature) {@link JTable} synchronized
62 alfonx 100 * with the {@link StyledLayerSelectionModel} of a layer. This is done by
63     * implementing:
64 alfonx 96 * <ul>
65 alfonx 100 * <li>a {@link PropertyChangeListener} which listens to the
66     * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
67     * selection</li>
68     * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
69     * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
70 alfonx 96 * </ul>
71 alfonx 100 * After creating, the instance of this synchronizer must be added as listener
72     * to both, the {@link StyledLayerSelectionModel} and the table's
73     * {@link ListSelectionModel}.
74     *
75     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
76     * (University of Bonn/Germany)
77 alfonx 96 */
78 alfonx 111 public class TableSelectionSynchronizer extends
79     StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
80     implements ListSelectionListener {
81 alfonx 100 /**
82     * Holds the table to keep synchronized with the layer selection model.
83     */
84     protected JTable featureTable = null;
85     /** Holds the table's data. */
86     protected FeatureCollectionTableModel featureTableModel = null;
87    
88     /**
89 alfonx 96 * Creates a new synchronizer
90     *
91     * @param layerSelModel
92     * layer selection model to keep synchronized with the feature
93     * table
94     * @param table
95     * table to keep synchronized with the layer selection model
96     */
97 alfonx 111 public TableSelectionSynchronizer(
98     StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
99 alfonx 96 super(layerSelModel);
100    
101 alfonx 100 TableModel model = table.getModel();
102     if (model instanceof PipedTableModel)
103     model = ((PipedTableModel) table.getModel()).getPipedModel();
104    
105     if (!(model instanceof FeatureCollectionTableModel))
106 alfonx 96 throw new IllegalArgumentException(
107     "Table must have a model instance of FeatureCollectionTableModel");
108    
109 alfonx 100 this.featureTableModel = (FeatureCollectionTableModel) model;
110 alfonx 96 this.featureTable = table;
111     }
112    
113 alfonx 100 /**
114     * Called by {@link StyledLayerSelectionModel} when a the selection on other
115     * selection components (map, chart, ...) has changed. When calling this
116     * method changes the table selection according to the
117     * {@link StyledLayerSelectionModel} selection.
118     *
119     * @param evt
120     * an event
121     */
122     @Override
123     public void propertyChange(PropertyChangeEvent evt) {
124     if (!(evt instanceof StyledLayerSelectionEvent))
125     return;
126     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
127     // Only react on own layer and ignore events invoked by
128     // this component itself
129     if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
130     return;
131     // Apply new selection on table (except this event is one of
132     // some more events)
133 alfonx 111 Vector<String> newSelection = layerSelModel.getSelection();
134 alfonx 100 if (newSelection == null)
135     return;
136 alfonx 96
137 alfonx 111 // LOGGER.debug("Table pop changed "+selEvt);
138     // LOGGER.debug("Table pop changed to "+newSelection);
139    
140 alfonx 100 // Avoid event circles in valueChanged(..)
141     selectionChangeCausedByMe = true;
142 alfonx 96
143 alfonx 100 ListSelectionModel tableSelModel = featureTable.getSelectionModel();
144     tableSelModel.setValueIsAdjusting(true);
145     tableSelModel.clearSelection();
146 alfonx 111 for (String fid : newSelection) {
147     int modelIdx = featureTableModel.findFeature(fid);
148 alfonx 100 if (modelIdx >= 0) {
149     int tableIdx = featureTable.convertRowIndexToView(modelIdx);
150     tableSelModel.addSelectionInterval(tableIdx, tableIdx);
151 alfonx 111 } else {
152     LOGGER
153     .warn("Something that is not visible in the Table should be selected");
154 alfonx 100 }
155     }
156     tableSelModel.setValueIsAdjusting(false); // event is fired
157     // autmatically!
158 alfonx 96
159 alfonx 100 // Danger of event circles in valueChanged(..) banned
160     selectionChangeCausedByMe = false;
161     }
162 alfonx 96
163 alfonx 100 /**
164     * Called when the table selection is changed by the user. When calling this
165     * method changes the selection of the {@link StyledLayerSelectionModel}.
166     *
167     * @param evt
168     * an event
169     */
170     @Override
171     public void valueChanged(ListSelectionEvent evt) {
172     // ignore event if it is part of multiple events
173     if (evt != null && evt.getValueIsAdjusting())
174     return;
175     // ignore event if it is caused by the DpLayerVectorSelectionModel
176     if (selectionChangeCausedByMe)
177     return;
178    
179     // Avoid event circles in propertyChange(..)
180     selectionChangeCausedByMe = true;
181    
182     // reset the selection of the DpLayerSelectionModel
183     layerSelModel.setValueIsAdjusting(true);
184 alfonx 111 LOGGER.debug("valueCahnged in TableSyncronizer vom Index "
185     + evt.getFirstIndex() + " to " + evt.getLastIndex());
186    
187     if (evt == null || (evt != null && evt.getFirstIndex() == -1)){
188     // If the value is changing because the filter has been changed (e.g.
189     // table structure change) the evt.getFirstIndex() is -1, but evt.getLastIndex is > -1
190     layerSelModel.clearSelection();
191     } else{
192    
193     for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
194     int featureIndex = featureTable.convertRowIndexToModel(i);
195 alfonx 318 SimpleFeature changedFeature = featureTableModel.getFeature(featureIndex);
196 alfonx 111 if (featureTable.isRowSelected(i))
197     layerSelModel.addSelection(changedFeature.getID());
198     else
199     layerSelModel.removeSelection(changedFeature.getID());
200     }
201 alfonx 100 }
202     layerSelModel.setValueIsAdjusting(false);
203    
204     // Danger of event circles in propertyChange(..) banned
205     selectionChangeCausedByMe = false;
206     }
207 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