1 |
|
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
* |
4 |
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
|
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
|
* 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 |
/** |
/** |
31 |
Copyright 2008 Stefan Alfons Krüger |
Copyright 2008 Stefan Alfons Krüger |
32 |
|
|
56 |
|
|
57 |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
import schmitzm.geotools.gui.FeatureCollectionTableModel; |
58 |
import schmitzm.swing.table.PipedTableModel; |
import schmitzm.swing.table.PipedTableModel; |
|
import schmitzm.swing.table.SelectionTableModel; |
|
|
import skrueger.geotools.selection.StyledFeatureLayerSelectionModel; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionEvent; |
|
|
import skrueger.geotools.selection.StyledLayerSelectionModel; |
|
59 |
|
|
60 |
/** |
/** |
61 |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
* This class keeps the selection of a (feature) {@link JTable} synchronized |
75 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
76 |
* (University of Bonn/Germany) |
* (University of Bonn/Germany) |
77 |
*/ |
*/ |
78 |
public class TableSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> implements ListSelectionListener { |
public class TableSelectionSynchronizer extends |
79 |
|
StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
80 |
|
implements ListSelectionListener { |
81 |
/** |
/** |
82 |
* Holds the table to keep synchronized with the layer selection model. |
* Holds the table to keep synchronized with the layer selection model. |
83 |
*/ |
*/ |
94 |
* @param table |
* @param table |
95 |
* table to keep synchronized with the layer selection model |
* table to keep synchronized with the layer selection model |
96 |
*/ |
*/ |
97 |
public TableSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel, JTable table) { |
public TableSelectionSynchronizer( |
98 |
|
StyledFeatureLayerSelectionModel layerSelModel, JTable table) { |
99 |
super(layerSelModel); |
super(layerSelModel); |
100 |
|
|
101 |
TableModel model = table.getModel(); |
TableModel model = table.getModel(); |
130 |
return; |
return; |
131 |
// Apply new selection on table (except this event is one of |
// Apply new selection on table (except this event is one of |
132 |
// some more events) |
// some more events) |
133 |
Vector<Feature> newSelection = layerSelModel.getSelection(); |
Vector<String> newSelection = layerSelModel.getSelection(); |
134 |
if (newSelection == null) |
if (newSelection == null) |
135 |
return; |
return; |
136 |
|
|
137 |
|
// LOGGER.debug("Table pop changed "+selEvt); |
138 |
|
// LOGGER.debug("Table pop changed to "+newSelection); |
139 |
|
|
140 |
// Avoid event circles in valueChanged(..) |
// Avoid event circles in valueChanged(..) |
141 |
selectionChangeCausedByMe = true; |
selectionChangeCausedByMe = true; |
142 |
|
|
143 |
ListSelectionModel tableSelModel = featureTable.getSelectionModel(); |
ListSelectionModel tableSelModel = featureTable.getSelectionModel(); |
144 |
tableSelModel.setValueIsAdjusting(true); |
tableSelModel.setValueIsAdjusting(true); |
145 |
tableSelModel.clearSelection(); |
tableSelModel.clearSelection(); |
146 |
for (Feature f : newSelection) { |
for (String fid : newSelection) { |
147 |
int modelIdx = featureTableModel.findFeature(f); |
int modelIdx = featureTableModel.findFeature(fid); |
148 |
if (modelIdx >= 0) { |
if (modelIdx >= 0) { |
149 |
int tableIdx = featureTable.convertRowIndexToView(modelIdx); |
int tableIdx = featureTable.convertRowIndexToView(modelIdx); |
150 |
tableSelModel.addSelectionInterval(tableIdx, tableIdx); |
tableSelModel.addSelectionInterval(tableIdx, tableIdx); |
151 |
|
} else { |
152 |
|
LOGGER |
153 |
|
.warn("Something that is not visible in the Table should be selected"); |
154 |
} |
} |
155 |
} |
} |
156 |
tableSelModel.setValueIsAdjusting(false); // event is fired |
tableSelModel.setValueIsAdjusting(false); // event is fired |
181 |
|
|
182 |
// reset the selection of the DpLayerSelectionModel |
// reset the selection of the DpLayerSelectionModel |
183 |
layerSelModel.setValueIsAdjusting(true); |
layerSelModel.setValueIsAdjusting(true); |
184 |
for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
LOGGER.debug("valueCahnged in TableSyncronizer vom Index " |
185 |
Feature changedFeature = featureTableModel.getFeature(featureTable |
+ evt.getFirstIndex() + " to " + evt.getLastIndex()); |
186 |
.convertRowIndexToModel(i)); |
|
187 |
if (featureTable.isRowSelected(i)) |
if (evt == null || (evt != null && evt.getFirstIndex() == -1)){ |
188 |
layerSelModel.addSelection(changedFeature); |
// If the value is changing because the filter has been changed (e.g. |
189 |
else |
// table structure change) the evt.getFirstIndex() is -1, but evt.getLastIndex is > -1 |
190 |
layerSelModel.removeSelection(changedFeature); |
layerSelModel.clearSelection(); |
191 |
|
} else{ |
192 |
|
|
193 |
|
for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
194 |
|
int featureIndex = featureTable.convertRowIndexToModel(i); |
195 |
|
Feature changedFeature = featureTableModel.getFeature(featureIndex); |
196 |
|
if (featureTable.isRowSelected(i)) |
197 |
|
layerSelModel.addSelection(changedFeature.getID()); |
198 |
|
else |
199 |
|
layerSelModel.removeSelection(changedFeature.getID()); |
200 |
|
} |
201 |
} |
} |
202 |
layerSelModel.setValueIsAdjusting(false); |
layerSelModel.setValueIsAdjusting(false); |
203 |
|
|