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