/[schmitzm]/branches/2.3.x/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
ViewVC logotype

Annotation of /branches/2.3.x/src/skrueger/geotools/selection/StyledLayerSelectionModel.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 131 - (hide annotations)
Sat May 30 19:58:13 2009 UTC (15 years, 9 months ago) by mojays
Original Path: trunk/src/skrueger/geotools/selection/StyledLayerSelectionModel.java
File MIME type: text/plain
File size: 8545 byte(s)


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

Properties

Name Value
svn:eol-style native
svn:keywords Id
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26