[Schmitzm-commits] r96 - in trunk/src/skrueger/geotools: . selection

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 8 13:23:19 CEST 2009


Author: alfonx
Date: 2009-05-08 13:23:16 +0200 (Fri, 08 May 2009)
New Revision: 96

Added:
   trunk/src/skrueger/geotools/selection/
   trunk/src/skrueger/geotools/selection/StyledFeatureLayerSelectionModel.java
   trunk/src/skrueger/geotools/selection/StyledLayerSelectionEvent.java
   trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
   trunk/src/skrueger/geotools/selection/StyledLayerSelectionModelSynchronizer.java
   trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
Log:
(Hacking Session in action - atomic commits can't be ensured)
- Remodeling the SelectionModels

Added: trunk/src/skrueger/geotools/selection/StyledFeatureLayerSelectionModel.java
===================================================================
--- trunk/src/skrueger/geotools/selection/StyledFeatureLayerSelectionModel.java	2009-05-04 13:27:44 UTC (rev 95)
+++ trunk/src/skrueger/geotools/selection/StyledFeatureLayerSelectionModel.java	2009-05-08 11:23:16 UTC (rev 96)
@@ -0,0 +1,177 @@
+/** 
+ Copyright 2008 Stefan Alfons Krüger 
+ 
+ atlas-framework - This file is part of the Atlas Framework
+
+ 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.
+ 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.
+ 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
+
+ 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.
+ 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.
+ 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.
+ **/
+
+package skrueger.geotools.selection;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Vector;
+
+import org.apache.log4j.Logger;
+import org.geotools.feature.Feature;
+import org.geotools.filter.AbstractFilterImpl;
+import org.geotools.styling.Style;
+import org.opengis.filter.Filter;
+
+import schmitzm.geotools.feature.FeatureUtil;
+
+import com.vividsolutions.jts.geom.Envelope;
+
+/**
+ * This manager takes care of every object selection for a {@link DpLayerVector}
+ * in the several atlas components and informs the appropriate components  
+ * to keep the selection synchronized.
+ * <ul>
+ *   <li>Geographical map of the layer</li>
+ *   <li>Feature attribute table of the layer</li>
+ *   <li>Chart visualizations of the layer</li>
+ * </ul>
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ */
+public class StyledFeatureLayerSelectionModel extends StyledLayerSelectionModel<Feature> {
+	/** A static logger for this class  */
+  private static final Logger LOGGER = Logger.getLogger(StyledFeatureLayerSelectionModel.class);
+  /** Holds the current selection as {@link Style}. */
+  protected Style selectionMapStyle = null;
+  /** Holds the current selection as {@link Filter}. */
+  protected Filter selectionFilter = null;
+  /** Holds the current selection as {@code ChartStyle}.
+   *  TODO: determine an appropriate type for the chart styling. */
+  protected Object selectionChartStyle = null;
+//    
+//  /**
+//   * Creates a new selection model.
+//   * @param layer layer to control the selection of
+//   */
+//  public StyledFeatureLayerSelectionModel() {
+//  }
+//  
+//  /**
+//   * Not yet implemented!
+//   * Extends the current selection by all features contained by
+//   * the specified {@link Envelope}.
+//   * @param env defines a bounding box
+//   */
+//  public void addSelection(Envelope env) {
+//    // TODO: implement me!
+//    refreshSelection();
+//  }
+//  
+//  /**
+//   * Not yet implemented!
+//   * Reduces the current selection by all features contained by
+//   * the specified {@link Envelope}.
+//   * @param env defines a bounding box
+//   */
+//  public void removeSelection(Envelope env) {
+//    // TODO: implement me!
+//    refreshSelection(); 
+//  }
+//
+//  /**
+//   * Not yet implemented!
+//   * Extends the current selection by all features accepted by
+//   * the specified {@link Filter}.
+//   * @param filter defines some features of the layer
+//   * @return {@code true} if selection has changed by calling this method
+//   */
+//  public boolean addSelection(Filter filter) {
+//    // TODO: implement me!
+//    refreshSelection();
+//    return false;
+//  }
+//  
+//  /**
+//   * Not yet implemented!
+//   * Removes all features accepted by the specified {@link Filter} from the
+//   * selection.
+//   * @param filter defines some features of the layer
+//   * @return {@code true} if selection has changed by calling this method
+//   */
+//  public boolean removeSelection(Filter filter) {
+//    // TODO: implement me!
+//    refreshSelection();
+//    return false;
+//  }
+//
+//  /**
+//   * Returns a {@link Filter}, which defines the current selection.
+//   */
+//  public Filter getSelectionAsFilter() {
+//    return selectionFilter;
+//  }
+//
+//  /**
+//   * Returns a {@link Style}, which highlights the current selection.
+//   */
+//  public Style getSelectionAsStyle() {
+//    return selectionMapStyle;
+//  }
+//
+//  /**
+//   * Resets all selection objects with the new selection.
+//   */
+//  protected void resetSelectionObjects() {
+////    resetMapStyle(); // TODO go to mapLayerSync
+//    resetFilter();
+//    resetChartStyle();
+//  }
+//  
+//
+////  
+////  /**
+////   * Creates a {@link Style} to highlight the current selection.
+////   */
+////  protected void resetMapStyle() {
+////    selectionMapStyle = null;
+////    try {
+////      if ( selectionFeatures.isEmpty() )
+////        selectionMapStyle = getLayer().getStyle();
+////      else
+////        selectionMapStyle = StylingUtil.createDefaultStyle(getLayer().getGeoObject());
+////      
+//////      getLayer().setStyle(selectionMapStyle);
+////    } catch (Exception e) {
+////      LOGGER.error("Error while trying to create a selection style",e);
+////    }
+////  }
+//
+//  /**
+//   * Creates a {@link Filter} for the current selection.
+//   */
+//  protected void resetFilter() {
+//    // create a filter which accepts all Features contained
+//    // in the selection list
+//    selectionFilter = new AbstractFilterImpl( FeatureUtil.FILTER_FACTORY ) {
+//      public boolean evaluate(Object feature) {
+//        if ( feature instanceof Feature )
+//          return evaluate((Filter)feature);
+//        return false;
+//      }
+//      public boolean evaluate(Feature feature) {
+//        return selectionFeatures.contains(feature);
+//      }
+//    };
+//  }
+//
+//  /**
+//   * Not yet implemented!
+//   * Creates a {@code ChartStyle} to highlight the current selection.
+//   */
+//  protected void resetChartStyle() {
+//    selectionChartStyle = null;
+//    // TODO: define the selectionChartStyle
+//  }
+}


Property changes on: trunk/src/skrueger/geotools/selection/StyledFeatureLayerSelectionModel.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/selection/StyledLayerSelectionEvent.java
===================================================================
--- trunk/src/skrueger/geotools/selection/StyledLayerSelectionEvent.java	2009-05-04 13:27:44 UTC (rev 95)
+++ trunk/src/skrueger/geotools/selection/StyledLayerSelectionEvent.java	2009-05-08 11:23:16 UTC (rev 96)
@@ -0,0 +1,50 @@
+/** 
+ Copyright 2008 Stefan Alfons Krüger 
+ 
+ atlas-framework - This file is part of the Atlas Framework
+
+ 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.
+ 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.
+ 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
+
+ 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.
+ 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.
+ 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.
+ **/
+package skrueger.geotools.selection;
+
+import java.beans.PropertyChangeEvent;
+
+/**
+ * Extends the {@link PropertyChangeEvent} by maintaining the event emitter.
+ * The emitter is the {@link StyledLayerSelectionModel} while the event {@link #getSource() source}
+ * holds the component which is component which causes the {@link StyledLayerSelectionModel}
+ * to invoke the event (e.g. a chart, a map or a feature table). 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ */
+public class StyledLayerSelectionEvent extends PropertyChangeEvent {
+  /** Holds the {@link StyledLayerSelectionModel} which invokes the event. */
+  protected  StyledLayerSelectionModel emitter = null;
+  
+  /**
+   * Creates a new event.
+   * @param emitter      the {@link StyledLayerSelectionModel} which invokes the event
+   * @param source       the object which is responsable for the change (e.g. map or table);
+   *                     can be {@code null} if its a refresh event with no responsible
+   * @param propertyName the name of the changed property
+   * @param oldValue     the old property value (can be <code>null</code>)
+   * @param newValue     the new property value (can be <code>null</code>)
+   */
+  public StyledLayerSelectionEvent(StyledLayerSelectionModel emitter, Object source, String propertyName, Object oldValue, Object newValue) {
+    super(source, propertyName, oldValue, newValue);
+    this.emitter = emitter;
+  }
+  
+  /** 
+   * Returns the {@link StyledLayerSelectionModel} which invokes the event.
+   */
+  public StyledLayerSelectionModel getEmitter() {
+    return emitter;
+  }
+
+}


Property changes on: trunk/src/skrueger/geotools/selection/StyledLayerSelectionEvent.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
===================================================================
--- trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java	2009-05-04 13:27:44 UTC (rev 95)
+++ trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java	2009-05-08 11:23:16 UTC (rev 96)
@@ -0,0 +1,191 @@
+/** 
+ Copyright 2008 Stefan Alfons Krüger 
+ 
+ atlas-framework - This file is part of the Atlas Framework
+
+ 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.
+ 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.
+ 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
+
+ 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.
+ 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.
+ 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.
+ **/
+
+package skrueger.geotools.selection;
+
+import java.beans.PropertyChangeEvent;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.Vector;
+
+import org.apache.log4j.Logger;
+import org.geotools.feature.Feature;
+
+import schmitzm.swing.event.PropertyChangeEmitter;
+
+/**
+ * This manager takes care of every object selection for a {@link DpLayer}
+ * in the several atlas components and informs the appropriate components  
+ * to keep the selection synchronized.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ */
+public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter {
+  /** A static logger for this class  */
+  private static final Logger LOGGER = Logger.getLogger(StyledLayerSelectionModel.class);
+	
+  /** Holds the current selection as {@link Feature}-list. This
+   *  is the basis to create the other selection objects. */
+  protected final Set<E> selectionFeatures;
+	
+  /** Indicates that the selection has changed in {@link PropertyChangeEvent}. */
+  public final static String SELECTION = "SELECTION";
+  
+  
+  /** Indicates whether a selection change is a part of multiple
+   *  changes. If {@code true} {@link #resetSelectionObjects()} is not
+   *  called and no {@code DpLayerSelectionEvent} event is fired until
+   *  {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}
+   *  is called. */
+  private boolean valueIsAdjusting = false;
+
+  public StyledLayerSelectionModel() {
+	  this.selectionFeatures = new HashSet<E>();
+}
+  /**
+   * Reset all selection objects and fires an event to every connected
+   * listener to update their selection according to the selection
+   * of this manager. <b>Does nothing if adjusting is <code>true</code></b>.
+   */
+  public void refreshSelection() {
+    if ( getValueIsAdjusting() )
+      return;
+    firePropertyChangeEvent( createSelectionChangeEvent(null) );
+  }
+  
+  /**
+   * Clears the selection and fires an event to every connected listener
+   * to update their selection.
+   */
+  public void clearSelection() {
+    clearSelectionObjects();
+    refreshSelection();
+  }
+
+  /**
+   * Creates a simple {@link PropertyChangeEvent} for property {@link #SELECTION}
+   * with no new and old value, just to inform the listeners THAT
+   * some selection has changed.
+   * Subclasses may overwrite this method to create more sophisticated
+   * events.
+   * @param respObject object which is responsible for the selection change; this
+   *                   object should ignore this event to avoid event circles (can be {@code null}
+   *                   in case of general refresh events; in this case all connected
+   *                   components will refresh their selection)
+   *                   
+   */
+  public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) {
+    if ( respObject == null )
+      respObject = this;
+    return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null);
+  }
+  
+  
+  /** 
+   * Returns whether the current selection changes are a part of multiple
+   * changes. If {@code true} {@link #resetSelectionObjects()} is not
+   * called and no {@code DpLayerSelectionEvent} event is fired until
+   * {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}
+   * is called.
+   */
+  public boolean getValueIsAdjusting() {
+    return this.valueIsAdjusting;
+  }
+  
+  /** 
+   * Sets whether the following selection changes are a part of multiple
+   * changes. If {@code true} {@link #resetSelectionObjects()} is not
+   * called and no {@code DpLayerSelectionEvent} event is fired.
+   * If the value is set from {@code true} to {@code false} an automatic
+   * refresh is initiated.
+   * @see #refreshSelection()
+   */
+  public void setValueIsAdjusting(boolean valueIsAdjusting) {
+    if ( valueIsAdjusting != this.valueIsAdjusting ) {
+      this.valueIsAdjusting = valueIsAdjusting;
+      if ( !this.valueIsAdjusting )
+        refreshSelection();
+    }
+  }
+  
+  /**
+   * Clears all selection objects.
+   */
+  protected void clearSelectionObjects() {
+    selectionFeatures.clear();
+    refreshSelection();
+  }
+
+  /**
+   * Extends the current selection by an individual feature.
+   * @param feature defines a feature of the layer
+   * @return {@code true} if selection has changed by calling this method
+   */
+  public boolean addSelection(E feature) {
+    if ( selectionFeatures.add(feature) ) {
+      refreshSelection();
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * Removes an individual feature from the current selection.
+   * @param feature defines a feature of the layer
+   * @return {@code true} if selection has changed by calling this method
+   */
+  public boolean removeSelection(E feature) {
+    if ( selectionFeatures.remove(feature) ) {
+      refreshSelection();
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * Extends the current selection by individual features.
+   * @param features defines some features of the layer
+   * @return {@code true} if selection has changed by calling this method
+   */
+  public boolean addSelection(Collection<E> features) {
+    if ( selectionFeatures.addAll(features) ) {
+      refreshSelection();
+      return true;
+    }
+    return false;
+  }
+  
+  /**
+   * Removes individual features from the current selection.
+   * @param features defines some features of the layer
+   * @return {@code true} if selection has changed by calling this method
+   */
+  public boolean removeSelection(Collection<E> features) {
+    if ( selectionFeatures.removeAll(features) ) {
+      refreshSelection();
+      return true;
+    }
+    return false;
+  }
+  
+  /**
+   * Returns all selected {@link Feature Features} as list.
+   * @return a copy of the original list, so changes have no effect!
+   */
+  public Vector<E> getSelection() {
+    return new Vector<E>(selectionFeatures);
+  }
+
+  
+}


Property changes on: trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/selection/StyledLayerSelectionModelSynchronizer.java
===================================================================
--- trunk/src/skrueger/geotools/selection/StyledLayerSelectionModelSynchronizer.java	2009-05-04 13:27:44 UTC (rev 95)
+++ trunk/src/skrueger/geotools/selection/StyledLayerSelectionModelSynchronizer.java	2009-05-08 11:23:16 UTC (rev 96)
@@ -0,0 +1,30 @@
+package skrueger.geotools.selection;
+
+import java.beans.PropertyChangeListener;
+
+import javax.swing.ListSelectionModel;
+
+import skrueger.geotools.selection.StyledLayerSelectionModel;
+
+public abstract class StyledLayerSelectionModelSynchronizer<E> implements PropertyChangeListener{
+	
+	  /** Flag to avoid event circles between {@link DpLayerVectorSelectionModel}
+	   *  and {@link ListSelectionModel} of the feature table. */
+	  protected boolean selectionChangeCausedByMe = false;
+	  /** Holds the selection model of the layer to keep synchronized
+	   *  with the feature table. */
+	  protected final StyledLayerSelectionModel<E> layerSelModel;
+
+	  /**
+		 * Creates a new synchronizer
+		 * 
+		 * @param layerSelModel
+		 *            layer selection model to keep synchronized with 
+		 */
+		protected StyledLayerSelectionModelSynchronizer(
+				StyledLayerSelectionModel<E> layerSelModel) {
+
+			this.layerSelModel = layerSelModel;
+		}
+	  
+}


Property changes on: trunk/src/skrueger/geotools/selection/StyledLayerSelectionModelSynchronizer.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
===================================================================
--- trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java	2009-05-04 13:27:44 UTC (rev 95)
+++ trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java	2009-05-08 11:23:16 UTC (rev 96)
@@ -0,0 +1,153 @@
+/** 
+ Copyright 2008 Stefan Alfons Krüger 
+ 
+ atlas-framework - This file is part of the Atlas Framework
+
+ 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.
+ 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.
+ 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
+
+ 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.
+ 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.
+ 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.
+ **/
+package skrueger.geotools.selection;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.Vector;
+
+import javax.swing.JTable;
+import javax.swing.ListSelectionModel;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+
+import org.geotools.feature.Feature;
+
+import schmitzm.geotools.gui.FeatureCollectionTableModel;
+import skrueger.geotools.selection.StyledFeatureLayerSelectionModel;
+import skrueger.geotools.selection.StyledLayerSelectionEvent;
+import skrueger.geotools.selection.StyledLayerSelectionModel;
+
+/**
+ * This class keeps the selection of a (feature) {@link JTable} synchronized
+ * with the {@link StyledLayerSelectionModel} of a layer.
+ * This is done by implementing:
+ * <ul>
+ *   <li>a {@link PropertyChangeListener} which listens to the
+ *       {@link StyledLayerSelectionModel} and accordingly changes the {@link JTable}
+ *       selection</li>
+ *   <li>a {@link ListSelectionListener} which listens to the {@link JTable}
+ *       and accordingly changes the {@link StyledLayerSelectionModel} selection</li>
+ * </ul>
+ * After creating, the instance of this synchronizer must be added as listener to
+ * both, the {@link StyledLayerSelectionModel} and the table's {@link ListSelectionModel}.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ */
+public class TableSelectionSynchronizer extends StyledLayerSelectionModelSynchronizer<Feature> implements ListSelectionListener {
+  /** Holds the table to keep synchronized with the layer selection
+   *  model. */
+  protected JTable featureTable = null;
+  /** Holds the table's data. */
+  protected FeatureCollectionTableModel featureTableModel = null;
+  
+  /**
+	 * Creates a new synchronizer
+	 * 
+	 * @param layerSelModel
+	 *            layer selection model to keep synchronized with the feature
+	 *            table
+	 * @param table
+	 *            table to keep synchronized with the layer selection model
+	 * @param tableModel
+	 *            feature table's data
+	 */
+	public TableSelectionSynchronizer(
+			StyledFeatureLayerSelectionModel layerSelModel, JTable table) {
+		
+		super(layerSelModel);
+
+		if (!(table.getModel() instanceof FeatureCollectionTableModel)) {
+			throw new IllegalArgumentException(
+					"Table must have a model instance of FeatureCollectionTableModel");
+		}
+
+		this.featureTable = table;
+		this.featureTableModel = (FeatureCollectionTableModel) table.getModel();
+	}
+  
+  /**
+   * Called by {@link StyledLayerSelectionModel} when a the selection on other
+   * selection components (map, chart, ...) has changed. When calling
+   * this method changes the table selection according to the
+   * {@link StyledLayerSelectionModel} selection.
+   * @param evt an event
+   */
+  @Override
+  public void propertyChange(PropertyChangeEvent evt) {
+    if ( !(evt instanceof StyledLayerSelectionEvent) )
+      return;
+    StyledLayerSelectionEvent selEvt = (StyledLayerSelectionEvent)evt;
+    // Only react on own layer and ignore events invoked by
+    // this component itself
+    if ( selEvt.getEmitter() != layerSelModel ||
+         selectionChangeCausedByMe )
+      return;
+    // Apply new selection on table (except this event is one of
+    // some more events)
+    Vector<Feature> newSelection = layerSelModel.getSelection();
+    if ( newSelection == null )
+      return;
+
+    // Avoid event circles in valueChanged(..)
+    selectionChangeCausedByMe = true;
+
+    ListSelectionModel tableSelModel = featureTable.getSelectionModel();
+    tableSelModel.setValueIsAdjusting(true);
+    tableSelModel.clearSelection();
+    for (Feature f : newSelection) {
+      int modelIdx = featureTableModel.findFeature(f);
+      if ( modelIdx >= 0 ) {
+        int tableIdx = featureTable.convertRowIndexToView(modelIdx);
+        tableSelModel.addSelectionInterval(tableIdx, tableIdx);
+      }
+    }
+    tableSelModel.setValueIsAdjusting(false); // event is fired autmatically!
+
+    // Danger of event circles in valueChanged(..) banned
+    selectionChangeCausedByMe = false;
+  }
+  
+  /**
+   * Called when the table selection is changed by the user.
+   * When calling this method changes the selection of the
+   * {@link StyledLayerSelectionModel}.
+   * @param evt an event
+   */
+  @Override
+  public void valueChanged(ListSelectionEvent evt) {
+    // ignore event if it is part of multiple events
+    if ( evt != null && evt.getValueIsAdjusting() )
+      return;
+    // ignore event if it is caused by the DpLayerVectorSelectionModel
+    if ( selectionChangeCausedByMe )
+      return;
+
+    // Avoid event circles in propertyChange(..)
+    selectionChangeCausedByMe = true;
+    
+    // reset the selection of the DpLayerSelectionModel
+    layerSelModel.setValueIsAdjusting(true);
+    for (int i=evt.getFirstIndex(); i<=evt.getLastIndex(); i++) {
+      Feature changedFeature = featureTableModel.getFeature( featureTable.convertRowIndexToModel(i) ); 
+      if ( featureTable.isRowSelected(i) )
+        layerSelModel.addSelection(changedFeature);
+      else
+        layerSelModel.removeSelection(changedFeature);
+    }
+    layerSelModel.setValueIsAdjusting(false);
+
+    // Danger of event circles in propertyChange(..) banned
+    selectionChangeCausedByMe = false;
+  }
+}


Property changes on: trunk/src/skrueger/geotools/selection/TableSelectionSynchronizer.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list