/[schmitzm]/branches/2.0-RC2/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
ViewVC logotype

Diff of /branches/2.0-RC2/src/skrueger/geotools/selection/StyledLayerSelectionModel.java

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

revision 101 by alfonx, Fri May 8 12:10:42 2009 UTC revision 102 by mojays, Fri May 8 13:47:33 2009 UTC
# Line 1  Line 1 
1  /**  /**
2   Copyright 2008 Stefan Alfons Krüger   Copyright 2008 Stefan Alfons Krüger
3    
4   atlas-framework - This file is part of the Atlas Framework   atlas-framework - This file is part of the Atlas Framework
5    
6   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 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.
# Line 15  Line 15 
15  package skrueger.geotools.selection;  package skrueger.geotools.selection;
16    
17  import java.beans.PropertyChangeEvent;  import java.beans.PropertyChangeEvent;
18    import java.beans.PropertyChangeListener;
19  import java.util.Collection;  import java.util.Collection;
20  import java.util.HashSet;  import java.util.HashSet;
21  import java.util.Set;  import java.util.Set;
22  import java.util.Vector;  import java.util.Vector;
23    
24    import javax.swing.event.EventListenerList;
25    
26  import org.apache.log4j.Logger;  import org.apache.log4j.Logger;
27  import org.geotools.feature.Feature;  import org.geotools.feature.Feature;
28    
29  import schmitzm.swing.event.PropertyChangeEmitter;  import schmitzm.swing.event.PropertyChangeEmitter;
30    import skrueger.geotools.StyledMapInterface;
31    
32  /**  /**
33   * This manager takes care of every object selection for a {@link DpLayer}   * This manager holds a set of objects which are <i>selected</i> for a {@link StyledMapInterface}.
34   * in the several atlas components and informs the appropriate components     * Several components can connect to this model to keep their selection synchronized.
35   * to keep the selection synchronized.   * @see #addSelectionListener(java.beans.PropertyChangeListener)
36     * @see StyledLayerSelectionModelSynchronizer
37   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)
38   */   */
39  public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter {  public abstract class StyledLayerSelectionModel<E> extends PropertyChangeEmitter<StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>>> {
40    /** A static logger for this class  */    /** A static logger for this class  */
41    protected final Logger LOGGER = Logger.getLogger(this.getClass());    protected final Logger LOGGER = Logger.getLogger(this.getClass());
42            
   /** Holds the current selection as {@link Feature}-list. This  
    *  is the basis to create the other selection objects. */  
   protected final Set<E> selectionFeatures;  
           
43    /** Indicates that the selection has changed in {@link PropertyChangeEvent}. */    /** Indicates that the selection has changed in {@link PropertyChangeEvent}. */
44    public final static String SELECTION = "SELECTION";    public final static String SELECTION = "SELECTION";
45      
46      
47      /** Holds the connected listeners. */
48      protected EventListenerList listenerList = new EventListenerList();
49    
50      /** Holds the current selection (e.g. {@link Feature Features}). */
51      protected final Set<E> selectionObjects;
52    
53      /** Holds the styled map whose selection is managed by this class. */
54      protected final StyledMapInterface<?> styledMap;
55    
56    
57    /** Indicates whether a selection change is a part of multiple    /** Indicates whether a selection change is a part of multiple
58     *  changes. If {@code true} {@link #resetSelectionObjects()} is not     *  changes. If {@code true} {@link #refreshSelection()} has NO effect
59     *  called and no {@code DpLayerSelectionEvent} event is fired until     *  until {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}
    *  {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}  
60     *  is called. */     *  is called. */
61    private boolean valueIsAdjusting = false;    private boolean valueIsAdjusting = false;
62    
   public StyledLayerSelectionModel() {  
           this.selectionFeatures = new HashSet<E>();  
 }  
63    /**    /**
64     * Reset all selection objects and fires an event to every connected     * Creates a new selection model.
65     * listener to update their selection according to the selection     * @param styledMap styled map the selection is controlled of
66     * of this manager. <b>Does nothing if adjusting is <code>true</code></b>.     */
67      public StyledLayerSelectionModel(StyledMapInterface<?> styledMap) {
68            this.selectionObjects = new HashSet<E>();
69            this.styledMap         = styledMap;
70      }
71    
72      /**
73       * Fires an event to every connected listener to update their selection
74       * according to the selection of this model.<br>
75       * <b>Does nothing if adjusting is <code>true</code></b>.
76     */     */
77    public void refreshSelection() {    public void refreshSelection() {
78      if ( getValueIsAdjusting() )      if ( getValueIsAdjusting() )
79        return;        return;
80      firePropertyChangeEvent( createSelectionChangeEvent(null) );      firePropertyChangeEvent( createSelectionChangeEvent(null) );
81    }    }
82      
83    /**    /**
84     * Clears the selection and fires an event to every connected listener     * Clears the selection and fires an event to every connected listener
85     * to update their selection.     * to update their selection.
86     */     */
87    public void clearSelection() {    public void clearSelection() {
88      clearSelectionObjects();      selectionObjects.clear();
89      refreshSelection();      refreshSelection();
90    }    }
91    
# Line 83  public abstract class StyledLayerSelecti Line 99  public abstract class StyledLayerSelecti
99     *                   object should ignore this event to avoid event circles (can be {@code null}     *                   object should ignore this event to avoid event circles (can be {@code null}
100     *                   in case of general refresh events; in this case all connected     *                   in case of general refresh events; in this case all connected
101     *                   components will refresh their selection)     *                   components will refresh their selection)
102     *                       *
103     */     */
104    public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) {    public StyledLayerSelectionEvent createSelectionChangeEvent(Object respObject) {
105      if ( respObject == null )      if ( respObject == null )
106        respObject = this;        respObject = this;
107      return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null);      return new StyledLayerSelectionEvent(this,respObject,SELECTION,null,null);
108    }    }
109      
110      
111    /**    /**
112     * Returns whether the current selection changes are a part of multiple     * Returns whether the current selection changes are a part of multiple
113     * changes. If {@code true} {@link #resetSelectionObjects()} is not     * changes. If {@code true} {@link #refreshSelection()} has no effect
114     * called and no {@code DpLayerSelectionEvent} event is fired until     * and no {@link StyledLayerSelectionEvent} is fired until
115     * {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}     * {@link #setValueIsAdjusting(boolean) setValueIsAdjusting(false)}
116     * is called.     * is called.
117     */     */
118    public boolean getValueIsAdjusting() {    public boolean getValueIsAdjusting() {
119      return this.valueIsAdjusting;      return this.valueIsAdjusting;
120    }    }
121      
122    /**    /**
123     * Sets whether the following selection changes are a part of multiple     * Sets whether the following selection changes are a part of multiple
124     * changes. If {@code true} {@link #resetSelectionObjects()} is not     * changes. If {@code true} {@link #refreshSelection()} has no effect
125     * called and no {@code DpLayerSelectionEvent} event is fired.     * and no {@link StyledLayerSelectionEvent} event is fired.
126     * If the value is set from {@code true} to {@code false} an automatic     * If the value is set from {@code true} to {@code false} an automatic
127     * refresh is initiated.     * refresh is initiated.
128     * @see #refreshSelection()     * @see #refreshSelection()
# Line 118  public abstract class StyledLayerSelecti Line 134  public abstract class StyledLayerSelecti
134          refreshSelection();          refreshSelection();
135      }      }
136    }    }
137      
   /**  
    * Clears all selection objects.  
    */  
   protected void clearSelectionObjects() {  
     selectionFeatures.clear();  
     refreshSelection();  
   }  
138    
139    /**    /**
140     * Extends the current selection by an individual feature.     * Extends the current selection by an individual object.
141     * @param feature defines a feature of the layer     * @param selectedObject defines the (new) selected object
142     * @return {@code true} if selection has changed by calling this method     * @return {@code true} if selection has changed by calling this method
143     */     */
144    public boolean addSelection(E feature) {    public boolean addSelection(E selectedObject) {
145      if ( selectionFeatures.add(feature) ) {      if ( selectionObjects.add(selectedObject) ) {
146        refreshSelection();        refreshSelection();
147        return true;        return true;
148      }      }
# Line 141  public abstract class StyledLayerSelecti Line 150  public abstract class StyledLayerSelecti
150    }    }
151    
152    /**    /**
153     * Removes an individual feature from the current selection.     * Removes an individual object from the current selection.
154     * @param feature defines a feature of the layer     * @param unselectedObject defines an object which will be unselected
155     * @return {@code true} if selection has changed by calling this method     * @return {@code true} if selection has changed by calling this method
156     */     */
157    public boolean removeSelection(E feature) {    public boolean removeSelection(E unselectedObject) {
158      if ( selectionFeatures.remove(feature) ) {      if ( selectionObjects.remove(unselectedObject) ) {
159        refreshSelection();        refreshSelection();
160        return true;        return true;
161      }      }
# Line 154  public abstract class StyledLayerSelecti Line 163  public abstract class StyledLayerSelecti
163    }    }
164    
165    /**    /**
166     * Extends the current selection by individual features.     * Extends the current selection by individual objects.
167     * @param features defines some features of the layer     * @param selectedObjects defines some objects which will be selected
168     * @return {@code true} if selection has changed by calling this method     * @return {@code true} if selection has changed by calling this method
169     */     */
170    public boolean addSelection(Collection<E> features) {    public boolean addSelection(Collection<E> selectedObjects) {
171      if ( selectionFeatures.addAll(features) ) {      if ( selectionObjects.addAll(selectedObjects) ) {
172        refreshSelection();        refreshSelection();
173        return true;        return true;
174      }      }
175      return false;      return false;
176    }    }
177      
178    /**    /**
179     * Removes individual features from the current selection.     * Removes individual objects from the current selection.
180     * @param features defines some features of the layer     * @param unselectedObjects defines some objects which will be unselected
181     * @return {@code true} if selection has changed by calling this method     * @return {@code true} if selection has changed by calling this method
182     */     */
183    public boolean removeSelection(Collection<E> features) {    public boolean removeSelection(Collection<E> unselectedObjects) {
184      if ( selectionFeatures.removeAll(features) ) {      if ( selectionObjects.removeAll(unselectedObjects) ) {
185        refreshSelection();        refreshSelection();
186        return true;        return true;
187      }      }
188      return false;      return false;
189    }    }
190      
191    /**    /**
192     * Returns all selected {@link Feature Features} as list.     * Returns all selected objects as list.
193     * @return a copy of the original list, so changes have no effect!     * @return a copy of the original list, so changes have no effect!
194     */     */
195    public Vector<E> getSelection() {    public Vector<E> getSelection() {
196      return new Vector<E>(selectionFeatures);      return new Vector<E>(selectionObjects);
197    }    }
198    
199        /**
200       * Adds a listener to the listener list. Only alias for {@link #addListener(PropertyChangeListener)}.
201       * @param listener a listener
202       */
203      public void addSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) {
204        this.addListener(listener);
205      }
206    
207      /**
208       * Removes a listener from the listener list. Only alias for {@link #removeListener(PropertyChangeListener)}.
209       * @param listener a listener
210       */
211      public void removeSelectionListener(StyledLayerSelectionModelSynchronizer<StyledLayerSelectionModel<E>> listener) {
212        this.removeListener(listener);
213      }
214    
215    
216  }  }

Legend:
Removed from v.101  
changed lines
  Added in v.102

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26