1 |
mojays |
130 |
/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM) |
2 |
|
|
|
3 |
|
|
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. |
4 |
|
|
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
5 |
|
|
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA |
6 |
|
|
|
7 |
|
|
Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version. |
8 |
|
|
Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License. |
9 |
|
|
Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA. |
10 |
|
|
**/ |
11 |
|
|
|
12 |
|
|
package skrueger.geotools.selection; |
13 |
|
|
|
14 |
|
|
import java.beans.PropertyChangeEvent; |
15 |
|
|
import java.beans.PropertyChangeListener; |
16 |
mojays |
131 |
import java.util.Vector; |
17 |
mojays |
130 |
|
18 |
|
|
import org.jfree.data.general.Dataset; |
19 |
|
|
|
20 |
|
|
import schmitzm.jfree.chart.renderer.SelectionRenderer; |
21 |
mojays |
136 |
import schmitzm.jfree.chart.selection.DatasetSelectionChangeEvent; |
22 |
mojays |
130 |
import schmitzm.jfree.chart.selection.DatasetSelectionListener; |
23 |
|
|
import schmitzm.jfree.chart.selection.DatasetSelectionModel; |
24 |
mojays |
131 |
import schmitzm.jfree.chart.selection.DatasetSelectionModelProvider; |
25 |
mojays |
136 |
import schmitzm.jfree.feature.FeatureDatasetSelectionModel; |
26 |
mojays |
130 |
|
27 |
|
|
/** |
28 |
|
|
* This class keeps the selection of a {@link Dataset} (based on feature |
29 |
|
|
* attributes) synchronized with the {@link StyledLayerSelectionModel} of a layer. |
30 |
|
|
* This is done by implementing: |
31 |
|
|
* <ul> |
32 |
|
|
* <li>a {@link PropertyChangeListener} which listens to the |
33 |
|
|
* {@link StyledLayerSelectionModel} and accordingly changes the {@link SelectionRenderer} |
34 |
|
|
* selection</li> |
35 |
|
|
* <li>a {@link DatasetSelectionModel} which listens to the {@link SelectionRenderer} and |
36 |
|
|
* accordingly changes the {@link StyledLayerSelectionModel} selection</li> |
37 |
|
|
* </ul> |
38 |
|
|
* After creating, the instance of this synchronizer must be added as listener |
39 |
|
|
* to both, the {@link StyledLayerSelectionModel} and the chart's |
40 |
mojays |
131 |
* {@link DatasetSelectionModel} (e.g. the renderer). |
41 |
|
|
* @see DatasetSelectionModelProvider |
42 |
mojays |
130 |
* @author <a href="mailto:[email protected]">Martin Schmitz</a> |
43 |
|
|
*/ |
44 |
|
|
public class ChartSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel> |
45 |
mojays |
131 |
implements DatasetSelectionListener { |
46 |
mojays |
130 |
|
47 |
mojays |
134 |
/** Holds the chart datset to keep synchronized with the layer selection model. */ |
48 |
mojays |
136 |
protected FeatureDatasetSelectionModel<?> datasetSelModel = null; |
49 |
|
|
|
50 |
mojays |
131 |
/** |
51 |
mojays |
130 |
* Creates a new synchronizer. |
52 |
|
|
* @param layerSelModel |
53 |
mojays |
131 |
* layer selection model to keep synchronized with the dataset |
54 |
|
|
* selection model |
55 |
|
|
* @param datasetSelModel |
56 |
|
|
* dataset selection model to keep synchronized with the layer |
57 |
|
|
* selection model |
58 |
mojays |
130 |
*/ |
59 |
mojays |
136 |
public ChartSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel, FeatureDatasetSelectionModel<?> datasetSelModel) { |
60 |
mojays |
130 |
super(layerSelModel); |
61 |
mojays |
131 |
this.datasetSelModel = datasetSelModel; |
62 |
mojays |
130 |
} |
63 |
|
|
|
64 |
mojays |
131 |
/** |
65 |
mojays |
136 |
* Called by {@link StyledLayerSelectionModel} when the selection on other |
66 |
mojays |
131 |
* selection components (map, table, ...) has changed. When calling this |
67 |
|
|
* method changes the dataset selection according to the |
68 |
|
|
* {@link StyledLayerSelectionModel} selection. |
69 |
|
|
* @param evt an event |
70 |
|
|
*/ |
71 |
mojays |
130 |
@Override |
72 |
|
|
public void propertyChange(PropertyChangeEvent evt) { |
73 |
mojays |
131 |
if (!(evt instanceof StyledLayerSelectionEvent)) |
74 |
|
|
return; |
75 |
|
|
StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt; |
76 |
|
|
// Only react on own layer and ignore events invoked by |
77 |
|
|
// this component itself |
78 |
|
|
if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe) |
79 |
|
|
return; |
80 |
|
|
// Apply new selection on chart (except this event is one of |
81 |
|
|
// some more events) |
82 |
|
|
Vector<String> newSelection = layerSelModel.getSelection(); |
83 |
|
|
if (newSelection == null) |
84 |
|
|
return; |
85 |
|
|
|
86 |
|
|
// Avoid event circles in valueChanged(..) |
87 |
|
|
selectionChangeCausedByMe = true; |
88 |
|
|
|
89 |
|
|
datasetSelModel.setValueIsAdjusting(true); |
90 |
|
|
datasetSelModel.clearSelection(); |
91 |
mojays |
136 |
for (String fID : newSelection) |
92 |
|
|
datasetSelModel.setItemSelected(fID, true); |
93 |
mojays |
131 |
datasetSelModel.setValueIsAdjusting(false); // event is fired autmatically! |
94 |
|
|
|
95 |
|
|
// Danger of event circles in valueChanged(..) banned |
96 |
|
|
selectionChangeCausedByMe = false; |
97 |
mojays |
130 |
} |
98 |
|
|
|
99 |
mojays |
136 |
/** |
100 |
|
|
* Called when the chart selection is changed by the user. When calling this |
101 |
|
|
* method changes the selection of the {@link StyledLayerSelectionModel}. |
102 |
|
|
* @param evt an event |
103 |
|
|
*/ |
104 |
mojays |
130 |
@Override |
105 |
mojays |
136 |
public void selectionChanged(DatasetSelectionChangeEvent evt) { |
106 |
|
|
// ignore event if it is part of multiple events |
107 |
|
|
if (evt != null && evt.getSource().getValueIsAdjusting()) |
108 |
|
|
return; |
109 |
|
|
// ignore event if it is caused by the ChartSelectionSynchronizer |
110 |
|
|
if (selectionChangeCausedByMe) |
111 |
|
|
return; |
112 |
|
|
|
113 |
|
|
// Avoid event circles in propertyChange(..) |
114 |
|
|
selectionChangeCausedByMe = true; |
115 |
|
|
|
116 |
|
|
// reset the selection of the DpLayerSelectionModel |
117 |
|
|
layerSelModel.setValueIsAdjusting(true); |
118 |
|
|
layerSelModel.clearSelection(); |
119 |
|
|
for (String featureID : datasetSelModel.getSelectedFeatures()) |
120 |
|
|
layerSelModel.addSelection(featureID); |
121 |
|
|
layerSelModel.setValueIsAdjusting(false); |
122 |
|
|
|
123 |
|
|
// Danger of event circles in propertyChange(..) banned |
124 |
|
|
selectionChangeCausedByMe = false; |
125 |
mojays |
130 |
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
} |