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

Annotation of /trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 96 - (hide annotations)
Fri May 8 11:23:16 2009 UTC (15 years, 9 months ago) by alfonx
File MIME type: text/plain
File size: 6890 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    
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     * @param tableModel
63     * feature table's data
64     */
65     public TableSelectionSynchronizer(
66     StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
67    
68     super(layerSelModel);
69    
70     if (!(table.getModel() instanceof FeatureCollectionTableModel)) {
71     throw new IllegalArgumentException(
72     "Table must have a model instance of FeatureCollectionTableModel");
73     }
74    
75     this.featureTable = table;
76     this.featureTableModel = (FeatureCollectionTableModel) table.getModel();
77     }
78    
79     /**
80     * Called by {@link StyledLayerSelectionModel} when a the selection on other
81     * selection components (map, chart, ...) has changed. When calling
82     * this method changes the table selection according to the
83     * {@link StyledLayerSelectionModel} selection.
84     * @param evt an event
85     */
86     @Override
87     public void propertyChange(PropertyChangeEvent evt) {
88     if ( !(evt instanceof StyledLayerSelectionEvent) )
89     return;
90     StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent)evt;
91     // Only react on own layer and ignore events invoked by
92     // this component itself
93     if ( selEvt.getEmitter() != layerSelModel ||
94     selectionChangeCausedByMe )
95     return;
96     // Apply new selection on table (except this event is one of
97     // some more events)
98     Vector<Feature> newSelection = layerSelModel.getSelection();
99     if ( newSelection == null )
100     return;
101    
102     // Avoid event circles in valueChanged(..)
103     selectionChangeCausedByMe = true;
104    
105     ListSelectionModel tableSelModel = featureTable.getSelectionModel();
106     tableSelModel.setValueIsAdjusting(true);
107     tableSelModel.clearSelection();
108     for (Feature f : newSelection) {
109     int modelIdx = featureTableModel.findFeature(f);
110     if ( modelIdx >= 0 ) {
111     int tableIdx = featureTable.convertRowIndexToView(modelIdx);
112     tableSelModel.addSelectionInterval(tableIdx, tableIdx);
113     }
114     }
115     tableSelModel.setValueIsAdjusting(false); // event is fired autmatically!
116    
117     // Danger of event circles in valueChanged(..) banned
118     selectionChangeCausedByMe = false;
119     }
120    
121     /**
122     * Called when the table selection is changed by the user.
123     * When calling this method changes the selection of the
124     * {@link StyledLayerSelectionModel}.
125     * @param evt an event
126     */
127     @Override
128     public void valueChanged(ListSelectionEvent evt) {
129     // ignore event if it is part of multiple events
130     if ( evt != null && evt.getValueIsAdjusting() )
131     return;
132     // ignore event if it is caused by the DpLayerVectorSelectionModel
133     if ( selectionChangeCausedByMe )
134     return;
135    
136     // Avoid event circles in propertyChange(..)
137     selectionChangeCausedByMe = true;
138    
139     // reset the selection of the DpLayerSelectionModel
140     layerSelModel.setValueIsAdjusting(true);
141     for (int i=evt.getFirstIndex(); i<=evt.getLastIndex(); i++) {
142     Feature changedFeature = featureTableModel.getFeature( featureTable.convertRowIndexToModel(i) );
143     if ( featureTable.isRowSelected(i) )
144     layerSelModel.addSelection(changedFeature);
145     else
146     layerSelModel.removeSelection(changedFeature);
147     }
148     layerSelModel.setValueIsAdjusting(false);
149    
150     // Danger of event circles in propertyChange(..) banned
151     selectionChangeCausedByMe = false;
152     }
153     }

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