001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.model;
034:
035: import java.util.ArrayList;
036: import java.util.Arrays;
037: import java.util.Collection;
038: import java.util.Iterator;
039:
040: import com.vividsolutions.jts.geom.Envelope;
041: import com.vividsolutions.jump.feature.Feature;
042: import com.vividsolutions.jump.feature.FeatureCollection;
043: import com.vividsolutions.jump.feature.FeatureCollectionWrapper;
044:
045: /**
046: * Notifies listeners when features are added to or removed from a
047: * FeatureCollection.
048: * <p>
049: * Prefer #addAll and #removeAll to #add and #remove, so that fewer events
050: * will be fired.</p>
051: */
052: public class ObservableFeatureCollection extends
053: FeatureCollectionWrapper {
054: private ArrayList listeners = new ArrayList();
055:
056: public ObservableFeatureCollection(FeatureCollection fc) {
057: super (fc);
058: }
059:
060: public void add(Listener listener) {
061: listeners.add(listener);
062: }
063:
064: public void add(Feature feature) {
065: super .add(feature);
066: fireFeaturesAdded(Arrays.asList(new Feature[] { feature }));
067: }
068:
069: public void remove(Feature feature) {
070: super .remove(feature);
071: fireFeaturesRemoved(Arrays.asList(new Feature[] { feature }));
072: }
073:
074: private void fireFeaturesAdded(Collection features) {
075: for (Iterator i = listeners.iterator(); i.hasNext();) {
076: Listener listener = (Listener) i.next();
077: listener.featuresAdded(features);
078: }
079: }
080:
081: private void fireFeaturesRemoved(Collection features) {
082: for (Iterator i = listeners.iterator(); i.hasNext();) {
083: Listener listener = (Listener) i.next();
084: listener.featuresRemoved(features);
085: }
086: }
087:
088: public void addAll(Collection features) {
089: super .addAll(features);
090: fireFeaturesAdded(features);
091: }
092:
093: public void removeAll(Collection features) {
094: super .removeAll(features);
095: fireFeaturesRemoved(features);
096: }
097:
098: public Collection remove(Envelope env) {
099: Collection features = super .remove(env);
100: fireFeaturesRemoved(features);
101:
102: return features;
103: }
104:
105: /**
106: * Listens for features being added to or removed from a
107: * FeatureCollection.
108: */
109: public static interface Listener {
110: public void featuresAdded(Collection features);
111:
112: public void featuresRemoved(Collection features);
113: }
114:
115: }
|