/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/selection/ChartSelectionSynchronizer.java
ViewVC logotype

Contents of /branches/1.0-gt2-2.6/src/skrueger/geotools/selection/ChartSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 487 - (show annotations)
Tue Oct 20 18:33:53 2009 UTC (15 years, 4 months ago) by alfonx
File size: 6105 byte(s)
* Improved listener & synchronizer dependencies in the Chart/Selection module.
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * and the Geotools library.
7 *
8 * The SCHMITZM project is hosted at:
9 * http://wald.intevation.org/projects/schmitzm/
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License (license.txt)
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * or try this link: http://www.gnu.org/licenses/lgpl.html
25 *
26 * Contributors:
27 * Martin O. J. Schmitz - initial API and implementation
28 * Stefan A. Krüger - additional utility classes
29 ******************************************************************************/
30 package skrueger.geotools.selection;
31
32 import java.beans.PropertyChangeEvent;
33 import java.beans.PropertyChangeListener;
34 import java.util.Vector;
35
36 import org.jfree.data.general.Dataset;
37
38 import schmitzm.jfree.chart.renderer.SelectionRenderer;
39 import schmitzm.jfree.chart.selection.DatasetSelectionChangeEvent;
40 import schmitzm.jfree.chart.selection.DatasetSelectionListener;
41 import schmitzm.jfree.chart.selection.DatasetSelectionModel;
42 import schmitzm.jfree.chart.selection.DatasetSelectionModelProvider;
43 import schmitzm.jfree.feature.FeatureDatasetSelectionModel;
44
45 /**
46 * This class keeps the selection of a {@link Dataset} (based on feature
47 * attributes) synchronized with the {@link StyledLayerSelectionModel} of a layer.
48 * This is done by implementing:
49 * <ul>
50 * <li>a {@link PropertyChangeListener} which listens to the
51 * {@link StyledLayerSelectionModel} and accordingly changes the {@link SelectionRenderer}
52 * selection</li>
53 * <li>a {@link DatasetSelectionModel} which listens to the {@link SelectionRenderer} and
54 * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
55 * </ul>
56 * After creating, the instance of this synchronizer must be added as listener
57 * to both, the {@link StyledLayerSelectionModel} and the chart's
58 * {@link DatasetSelectionModel} (e.g. the renderer).
59 * @see DatasetSelectionModelProvider
60 * @author <a href="mailto:[email protected]">Martin Schmitz</a>
61 */
62 public class ChartSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
63 implements DatasetSelectionListener {
64
65 /** Holds the chart datset to keep synchronized with the layer selection model. */
66 protected FeatureDatasetSelectionModel<?,?,?> datasetSelModel = null;
67
68 /**
69 * Creates a new synchronizer.
70 * @param layerSelModel
71 * layer selection model to keep synchronized with the dataset
72 * selection model
73 * @param datasetSelModel
74 * dataset selection model to keep synchronized with the layer
75 * selection model
76 */
77 public ChartSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel, FeatureDatasetSelectionModel<?,?,?> datasetSelModel) {
78 super(layerSelModel);
79 this.datasetSelModel = datasetSelModel;
80 }
81
82 /**
83 * Called by {@link StyledLayerSelectionModel} when the selection on other
84 * selection components (map, table, ...) has changed. When calling this
85 * method changes the dataset selection according to the
86 * {@link StyledLayerSelectionModel} selection.
87 * @param evt an event
88 */
89 @Override
90 public void propertyChange(PropertyChangeEvent evt) {
91
92 if (!isEnabled()) {
93 return;
94 }
95
96 if (!(evt instanceof StyledLayerSelectionEvent))
97 return;
98 StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
99 // Only react on own layer and ignore events invoked by
100 // this component itself
101 if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
102 return;
103 // Apply new selection on chart (except this event is one of
104 // some more events)
105 Vector<String> newSelection = layerSelModel.getSelection();
106 if (newSelection == null)
107 return;
108
109 // Avoid event circles in valueChanged(..)
110 selectionChangeCausedByMe = true;
111
112 datasetSelModel.setValueIsAdjusting(true);
113 datasetSelModel.clearSelection();
114 for (String fID : newSelection)
115 datasetSelModel.setItemSelected(fID, true);
116 datasetSelModel.setValueIsAdjusting(false); // event is fired autmatically!
117
118 // Danger of event circles in valueChanged(..) banned
119 selectionChangeCausedByMe = false;
120 }
121
122 /**
123 * Called when the chart selection is changed by the user. When calling this
124 * method changes the selection of the {@link StyledLayerSelectionModel}.
125 * @param evt an event
126 */
127 @Override
128 public void selectionChanged(DatasetSelectionChangeEvent evt) {
129 // ignore event if it is part of multiple events
130 if (evt != null && evt.getSource().getValueIsAdjusting())
131 return;
132 // ignore event if it is caused by the ChartSelectionSynchronizer
133 if (selectionChangeCausedByMe)
134 return;
135
136 // Avoid event circles in propertyChange(..)
137 selectionChangeCausedByMe = true;
138
139 // reset the selection of the DpLayerSelectionModel
140 layerSelModel.setValueIsAdjusting(true);
141 layerSelModel.clearSelection();
142 for (String featureID : datasetSelModel.getSelectedFeatures())
143 layerSelModel.addSelection(featureID);
144 layerSelModel.setValueIsAdjusting(false);
145
146 // Danger of event circles in propertyChange(..) banned
147 selectionChangeCausedByMe = false;
148 }
149
150
151 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26