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 |
67 |
* @param table |
* @param table |
68 |
* table to keep synchronized with the layer selection model |
* table to keep synchronized with the layer selection model |
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; |
// Avoid event circles in valueChanged(..) |
110 |
|
selectionChangeCausedByMe = true; |
111 |
ListSelectionModel tableSelModel = featureTable.getSelectionModel(); |
|
112 |
tableSelModel.setValueIsAdjusting(true); |
ListSelectionModel tableSelModel = featureTable.getSelectionModel(); |
113 |
tableSelModel.clearSelection(); |
tableSelModel.setValueIsAdjusting(true); |
114 |
for (Feature f : newSelection) { |
tableSelModel.clearSelection(); |
115 |
int modelIdx = featureTableModel.findFeature(f); |
for (Feature f : newSelection) { |
116 |
if ( modelIdx >= 0 ) { |
int modelIdx = featureTableModel.findFeature(f); |
117 |
int tableIdx = featureTable.convertRowIndexToView(modelIdx); |
if (modelIdx >= 0) { |
118 |
tableSelModel.addSelectionInterval(tableIdx, tableIdx); |
int tableIdx = featureTable.convertRowIndexToView(modelIdx); |
119 |
} |
tableSelModel.addSelectionInterval(tableIdx, tableIdx); |
120 |
} |
} |
121 |
tableSelModel.setValueIsAdjusting(false); // event is fired autmatically! |
} |
122 |
|
tableSelModel.setValueIsAdjusting(false); // event is fired |
123 |
// Danger of event circles in valueChanged(..) banned |
// autmatically! |
124 |
selectionChangeCausedByMe = false; |
|
125 |
} |
// Danger of event circles in valueChanged(..) banned |
126 |
|
selectionChangeCausedByMe = false; |
127 |
/** |
} |
128 |
* Called when the table selection is changed by the user. |
|
129 |
* When calling this method changes the selection of the |
/** |
130 |
* {@link StyledLayerSelectionModel}. |
* Called when the table selection is changed by the user. When calling this |
131 |
* @param evt an event |
* method changes the selection of the {@link StyledLayerSelectionModel}. |
132 |
*/ |
* |
133 |
@Override |
* @param evt |
134 |
public void valueChanged(ListSelectionEvent evt) { |
* an event |
135 |
// ignore event if it is part of multiple events |
*/ |
136 |
if ( evt != null && evt.getValueIsAdjusting() ) |
@Override |
137 |
return; |
public void valueChanged(ListSelectionEvent evt) { |
138 |
// ignore event if it is caused by the DpLayerVectorSelectionModel |
// ignore event if it is part of multiple events |
139 |
if ( selectionChangeCausedByMe ) |
if (evt != null && evt.getValueIsAdjusting()) |
140 |
return; |
return; |
141 |
|
// ignore event if it is caused by the DpLayerVectorSelectionModel |
142 |
// Avoid event circles in propertyChange(..) |
if (selectionChangeCausedByMe) |
143 |
selectionChangeCausedByMe = true; |
return; |
144 |
|
|
145 |
// reset the selection of the DpLayerSelectionModel |
// Avoid event circles in propertyChange(..) |
146 |
layerSelModel.setValueIsAdjusting(true); |
selectionChangeCausedByMe = true; |
147 |
for (int i=evt.getFirstIndex(); i<=evt.getLastIndex(); i++) { |
|
148 |
Feature changedFeature = featureTableModel.getFeature( featureTable.convertRowIndexToModel(i) ); |
// reset the selection of the DpLayerSelectionModel |
149 |
if ( featureTable.isRowSelected(i) ) |
layerSelModel.setValueIsAdjusting(true); |
150 |
layerSelModel.addSelection(changedFeature); |
for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) { |
151 |
else |
Feature changedFeature = featureTableModel.getFeature(featureTable |
152 |
layerSelModel.removeSelection(changedFeature); |
.convertRowIndexToModel(i)); |
153 |
} |
if (featureTable.isRowSelected(i)) |
154 |
layerSelModel.setValueIsAdjusting(false); |
layerSelModel.addSelection(changedFeature); |
155 |
|
else |
156 |
// Danger of event circles in propertyChange(..) banned |
layerSelModel.removeSelection(changedFeature); |
157 |
selectionChangeCausedByMe = false; |
} |
158 |
} |
layerSelModel.setValueIsAdjusting(false); |
159 |
|
|
160 |
|
// Danger of event circles in propertyChange(..) banned |
161 |
|
selectionChangeCausedByMe = false; |
162 |
|
} |
163 |
} |
} |