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