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

Diff of /branches/1.0-gt2-2.6/src/skrueger/geotools/selection/TableSelectionSynchronizer.java

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

revision 97 by alfonx, Fri May 8 12:10:42 2009 UTC revision 244 by alfonx, Wed Jul 29 09:33:33 2009 UTC
# Line 1  Line 1 
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, focussing (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  /**  /**
31   Copyright 2008 Stefan Alfons Krüger   Copyright 2008 Stefan Alfons Krüger
32    
# Line 21  import javax.swing.JTable; Line 50  import javax.swing.JTable;
50  import javax.swing.ListSelectionModel;  import javax.swing.ListSelectionModel;
51  import javax.swing.event.ListSelectionEvent;  import javax.swing.event.ListSelectionEvent;
52  import javax.swing.event.ListSelectionListener;  import javax.swing.event.ListSelectionListener;
53    import javax.swing.table.TableModel;
54    
55  import org.geotools.feature.Feature;  import org.geotools.feature.Feature;
56    
57  import schmitzm.geotools.gui.FeatureCollectionTableModel;  import schmitzm.geotools.gui.FeatureCollectionTableModel;
58  import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;  import schmitzm.swing.table.PipedTableModel;
 import skrueger.geotools.selection.StyledLayerSelectionEvent;  
 import skrueger.geotools.selection.StyledLayerSelectionModel;  
59    
60  /**  /**
61   * This class keeps the selection of a (feature) {@link JTable} synchronized   * This class keeps the selection of a (feature) {@link JTable} synchronized
62   * with the {@link StyledLayerSelectionModel} of a layer.   * with the {@link StyledLayerSelectionModel} of a layer. This is done by
63   * This is done by implementing:   * implementing:
64   * <ul>   * <ul>
65   *   <li>a {@link PropertyChangeListener} which listens to the   * <li>a {@link PropertyChangeListener} which listens to the
66   *       {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}   * {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
67   *       selection</li>   * selection</li>
68   *   <li>a {@link ListSelectionListener} which listens to the {@link JTable}   * <li>a {@link ListSelectionListener} which listens to the {@link JTable} and
69   *       and accordingly changes the {@link StyledLayerSelectionModel} selection</li>   * accordingly changes the {@link StyledLayerSelectionModel} selection</li>
70   * </ul>   * </ul>
71   * 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
72   * both, the {@link StyledLayerSelectionModel} and the table's {@link ListSelectionModel}.   * to both, the {@link StyledLayerSelectionModel} and the table's
73   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   * {@link ListSelectionModel}.
74     *
75     * @author <a href="mailto:[email protected]">Martin Schmitz</a>
76     *         (University of Bonn/Germany)
77   */   */
78  public class TableSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<Feature> implements ListSelectionListener {  public class TableSelectionSynchronizer extends
79    /** Holds the table to keep synchronized with the layer selection                  StyledLayerSelectionModelSynchronizer<StyledFeatureLayerSelectionModel>
80     *  model. */                  implements ListSelectionListener {
81    protected JTable featureTable = null;          /**
82    /** Holds the table's data. */           * Holds the table to keep synchronized with the layer selection model.
83    protected FeatureCollectionTableModel featureTableModel = null;           */
84              protected JTable featureTable = null;
85    /**          /** Holds the table's data. */
86            protected FeatureCollectionTableModel featureTableModel = null;
87    
88            /**
89           * Creates a new synchronizer           * Creates a new synchronizer
90           *           *
91           * @param layerSelModel           * @param layerSelModel
# Line 62  public class TableSelectionSynchronizer Line 96  public class TableSelectionSynchronizer
96           */           */
97          public TableSelectionSynchronizer(          public TableSelectionSynchronizer(
98                          StyledFeatureLayerSelectionModel layerSelModel, JTable table) {                          StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
                   
99                  super(layerSelModel);                  super(layerSelModel);
100    
101                  if (!(table.getModel() instanceof FeatureCollectionTableModel)) {                  TableModel model = table.getModel();
102                    if (model instanceof PipedTableModel)
103                            model = ((PipedTableModel) table.getModel()).getPipedModel();
104    
105                    if (!(model instanceof FeatureCollectionTableModel))
106                          throw new IllegalArgumentException(                          throw new IllegalArgumentException(
107                                          "Table must have a model instance of FeatureCollectionTableModel");                                          "Table must have a model instance of FeatureCollectionTableModel");
                 }  
108    
109                    this.featureTableModel = (FeatureCollectionTableModel) model;
110                  this.featureTable = table;                  this.featureTable = table;
                 this.featureTableModel = (FeatureCollectionTableModel) table.getModel();  
111          }          }
112      
113    /**          /**
114     * Called by {@link StyledLayerSelectionModel} when a the selection on other           * Called by {@link StyledLayerSelectionModel} when a the selection on other
115     * selection components (map, chart, ...) has changed. When calling           * selection components (map, chart, ...) has changed. When calling this
116     * this method changes the table selection according to the           * method changes the table selection according to the
117     * {@link StyledLayerSelectionModel} selection.           * {@link StyledLayerSelectionModel} selection.
118     * @param evt an event           *
119     */           * @param evt
120    @Override           *            an event
121    public void propertyChange(PropertyChangeEvent evt) {           */
122      if ( !(evt instanceof StyledLayerSelectionEvent) )          @Override
123        return;          public void propertyChange(PropertyChangeEvent evt) {
124      StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent)evt;                  if (!(evt instanceof StyledLayerSelectionEvent))
125      // Only react on own layer and ignore events invoked by                          return;
126      // this component itself                  StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent) evt;
127      if ( selEvt.getEmitter() != layerSelModel ||                  // Only react on own layer and ignore events invoked by
128           selectionChangeCausedByMe )                  // this component itself
129        return;                  if (selEvt.getEmitter() != layerSelModel || selectionChangeCausedByMe)
130      // Apply new selection on table (except this event is one of                          return;
131      // some more events)                  // Apply new selection on table (except this event is one of
132      Vector<Feature> newSelection = layerSelModel.getSelection();                  // some more events)
133      if ( newSelection == null )                  Vector<String> newSelection = layerSelModel.getSelection();
134        return;                  if (newSelection == null)
135                            return;
136      // Avoid event circles in valueChanged(..)  
137      selectionChangeCausedByMe = true;                  // LOGGER.debug("Table pop changed "+selEvt);
138                    // LOGGER.debug("Table pop changed to "+newSelection);
139      ListSelectionModel tableSelModel = featureTable.getSelectionModel();  
140      tableSelModel.setValueIsAdjusting(true);                  // Avoid event circles in valueChanged(..)
141      tableSelModel.clearSelection();                  selectionChangeCausedByMe = true;
142      for (Feature f : newSelection) {  
143        int modelIdx = featureTableModel.findFeature(f);                  ListSelectionModel tableSelModel = featureTable.getSelectionModel();
144        if ( modelIdx >= 0 ) {                  tableSelModel.setValueIsAdjusting(true);
145          int tableIdx = featureTable.convertRowIndexToView(modelIdx);                  tableSelModel.clearSelection();
146          tableSelModel.addSelectionInterval(tableIdx, tableIdx);                  for (String fid : newSelection) {
147        }                          int modelIdx = featureTableModel.findFeature(fid);
148      }                          if (modelIdx >= 0) {
149      tableSelModel.setValueIsAdjusting(false); // event is fired autmatically!                                  int tableIdx = featureTable.convertRowIndexToView(modelIdx);
150                                    tableSelModel.addSelectionInterval(tableIdx, tableIdx);
151      // Danger of event circles in valueChanged(..) banned                          } else {
152      selectionChangeCausedByMe = false;                                  LOGGER
153    }                                                  .warn("Something that is not visible in the Table should be selected");
154                              }
155    /**                  }
156     * Called when the table selection is changed by the user.                  tableSelModel.setValueIsAdjusting(false); // event is fired
157     * When calling this method changes the selection of the                  // autmatically!
158     * {@link StyledLayerSelectionModel}.  
159     * @param evt an event                  // Danger of event circles in valueChanged(..) banned
160     */                  selectionChangeCausedByMe = false;
161    @Override          }
162    public void valueChanged(ListSelectionEvent evt) {  
163      // ignore event if it is part of multiple events          /**
164      if ( evt != null && evt.getValueIsAdjusting() )           * Called when the table selection is changed by the user. When calling this
165        return;           * method changes the selection of the {@link StyledLayerSelectionModel}.
166      // ignore event if it is caused by the DpLayerVectorSelectionModel           *
167      if ( selectionChangeCausedByMe )           * @param evt
168        return;           *            an event
169             */
170      // Avoid event circles in propertyChange(..)          @Override
171      selectionChangeCausedByMe = true;          public void valueChanged(ListSelectionEvent evt) {
172                        // ignore event if it is part of multiple events
173      // reset the selection of the DpLayerSelectionModel                  if (evt != null && evt.getValueIsAdjusting())
174      layerSelModel.setValueIsAdjusting(true);                          return;
175      for (int i=evt.getFirstIndex(); i<=evt.getLastIndex(); i++) {                  // ignore event if it is caused by the DpLayerVectorSelectionModel
176        Feature changedFeature = featureTableModel.getFeature( featureTable.convertRowIndexToModel(i) );                  if (selectionChangeCausedByMe)
177        if ( featureTable.isRowSelected(i) )                          return;
178          layerSelModel.addSelection(changedFeature);  
179        else                  // Avoid event circles in propertyChange(..)
180          layerSelModel.removeSelection(changedFeature);                  selectionChangeCausedByMe = true;
181      }  
182      layerSelModel.setValueIsAdjusting(false);                  // reset the selection of the DpLayerSelectionModel
183                    layerSelModel.setValueIsAdjusting(true);
184      // Danger of event circles in propertyChange(..) banned                  LOGGER.debug("valueCahnged in TableSyncronizer vom Index "
185      selectionChangeCausedByMe = false;                                  + evt.getFirstIndex() + " to " + evt.getLastIndex());
186    }  
187                    if (evt == null || (evt != null && evt.getFirstIndex() == -1)){
188                            // If the value is changing because the filter has been changed (e.g.
189                            // table structure change) the evt.getFirstIndex() is -1, but evt.getLastIndex is > -1
190                            layerSelModel.clearSelection();
191                    } else{
192                            
193                            for (int i = evt.getFirstIndex(); i <= evt.getLastIndex(); i++) {
194                                    int featureIndex = featureTable.convertRowIndexToModel(i);
195                                    Feature changedFeature = featureTableModel.getFeature(featureIndex);
196                                    if (featureTable.isRowSelected(i))
197                                            layerSelModel.addSelection(changedFeature.getID());
198                                    else
199                                            layerSelModel.removeSelection(changedFeature.getID());
200                            }
201                    }
202                    layerSelModel.setValueIsAdjusting(false);
203    
204                    // Danger of event circles in propertyChange(..) banned
205                    selectionChangeCausedByMe = false;
206            }
207  }  }

Legend:
Removed from v.97  
changed lines
  Added in v.244

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26