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