/[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

revision 130 by mojays, Sat May 30 16:34:00 2009 UTC revision 136 by mojays, Wed Jun 3 19:53:33 2009 UTC
# Line 13  package skrueger.geotools.selection; Line 13  package skrueger.geotools.selection;
13    
14  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
15  import java.beans.PropertyChangeListener;  import java.beans.PropertyChangeListener;
16    import java.util.Vector;
17    
 import javax.swing.JTable;  
 import javax.swing.ListSelectionModel;  
 import javax.swing.event.ListSelectionListener;  
   
 import org.jfree.chart.JFreeChart;  
18  import org.jfree.data.general.Dataset;  import org.jfree.data.general.Dataset;
19    
20  import schmitzm.jfree.chart.renderer.SelectionRenderer;  import schmitzm.jfree.chart.renderer.SelectionRenderer;
21    import schmitzm.jfree.chart.selection.DatasetSelectionChangeEvent;
22  import schmitzm.jfree.chart.selection.DatasetSelectionListener;  import schmitzm.jfree.chart.selection.DatasetSelectionListener;
23  import schmitzm.jfree.chart.selection.DatasetSelectionModel;  import schmitzm.jfree.chart.selection.DatasetSelectionModel;
24  import schmitzm.jfree.chart.selection.SelectionChangeEvent;  import schmitzm.jfree.chart.selection.DatasetSelectionModelProvider;
25    import schmitzm.jfree.feature.FeatureDatasetSelectionModel;
26    
27  /**  /**
28   * This class keeps the selection of a {@link Dataset} (based on feature   * This class keeps the selection of a {@link Dataset} (based on feature
# Line 39  import schmitzm.jfree.chart.selection.Se Line 37  import schmitzm.jfree.chart.selection.Se
37   * </ul>   * </ul>
38   * After creating, the instance of this synchronizer must be added as listener   * After creating, the instance of this synchronizer must be added as listener
39   * to both, the {@link StyledLayerSelectionModel} and the chart's   * to both, the {@link StyledLayerSelectionModel} and the chart's
40   * {@link SelectionRenderer}.   * {@link DatasetSelectionModel} (e.g. the renderer).
41   *   * @see DatasetSelectionModelProvider
42   * @author <a href="mailto:[email protected]">Martin Schmitz</a>   * @author <a href="mailto:[email protected]">Martin Schmitz</a>
43   */   */
44  public class ChartSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>  public class ChartSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
45                                          implements DatasetSelectionListener                                          implements DatasetSelectionListener {
46                                          {  
47      /** Holds the chart datset to keep synchronized with the layer selection model. */
48      protected FeatureDatasetSelectionModel<?> datasetSelModel = null;
49    
50    /**    /**
51     * Creates a new synchronizer.     * Creates a new synchronizer.
52     * @param layerSelModel     * @param layerSelModel
53       *             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     */     */
59    public ChartSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel) {    public ChartSelectionSynchronizer(StyledFeatureLayerSelectionModel layerSelModel, FeatureDatasetSelectionModel<?> datasetSelModel) {
60      super(layerSelModel);      super(layerSelModel);
61        this.datasetSelModel = datasetSelModel;
62    }    }
63    
64      /**
65       * Called by {@link StyledLayerSelectionModel} when the selection on other
66       * 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    @Override    @Override
72    public void propertyChange(PropertyChangeEvent evt) {    public void propertyChange(PropertyChangeEvent evt) {
73      // TODO apply selection changes to the ChartSelectionModel      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        for (String fID : newSelection)
92          datasetSelModel.setItemSelected(fID, true);
93        datasetSelModel.setValueIsAdjusting(false); // event is fired autmatically!
94    
95        // Danger of event circles in valueChanged(..) banned
96        selectionChangeCausedByMe = false;
97    }    }
98    
99      /**
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    @Override    @Override
105    public void selectionChanged(SelectionChangeEvent e) {    public void selectionChanged(DatasetSelectionChangeEvent evt) {
106      // TODO Auto-generated method stub      // 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    }    }
126        
127    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26