/[schmitzm]/branches/2.1/src/skrueger/geotools/selection/ChartSelectionSynchronizer.java
ViewVC logotype

Diff of /branches/2.1/src/skrueger/geotools/selection/ChartSelectionSynchronizer.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.136  
changed lines
  Added in v.604

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26