/* * GeoTools - The Open Source Java GIS Toolkit * http://geotools.org * * (C) 2003-2008, Open Source Geospatial Foundation (OSGeo) * * 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; * version 2.1 of the License. * * 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. */ package org.geotools.data; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Map; import java.util.WeakHashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.event.EventListenerList; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.geometry.jts.ReferencedEnvelope; import org.opengis.feature.Feature; import org.opengis.feature.type.FeatureType; /** * This class is used by DataStore implementations to provide FeatureListener * support for the FeatureSources they create. * *
* FeatureWriters created by the DataStore will need to make use of this class * to provide the required FeatureEvents. *
* This class has been updated to store listeners using weak references * in order to cut down on memory leaks. * * @author Jody Garnett, Refractions Research * * * @source $URL$ */ public class FeatureListenerManager { private static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geotools.data"); /** * Hold on to provided FeatureListener using a weak reference. ** If I was really smart I could do this with the DynamicProxy class; I am not * that smart... *
* @author Jody Garnett
*/
class WeakFeatureListener implements FeatureListener {
WeakReference
* A SimpleFeatureSource is considered a match when typeName and Transaction
* agree. Transaction.AUTO_COMMIT will match with any change.
*
* This method is called by:
*
* NOTE requiring to fire this event at FeatureWriter.next() is quite
* a gap inherited from an old API when {@link FeatureWriter#write()} didn't
* exist yet. It's a good idea though to fire the event at FeatureWriter.write()
* instead of FeatureWriter.next() so there are actually changes to notify for.
*
* This method is called by:
*
* NOTE requiring to fire this event at FeatureWriter.next() is quite
* a gap inherited from an old API when {@link FeatureWriter#write()} didn't
* exist yet. It's a good idea though to fire the event at FeatureWriter.write()
* instead of FeatureWriter.next() so there are actually changes to notify for.
*
* This method is called by:
*
* This method is called by:
*
*
*
* - when an existing Feature is removed with Tranasaction.AUTO_COMMIT all
* listeners registered with SimpleFeatureSource of typeName will be notified.
*
* - when an existing Feature is removed with a Transaction all listeners
* registered with SimpleFeatureSource of typeName and with the same Transaction
* will be notified.
* null
if unknown)
* @param commit true if
*/
public void fireFeaturesAdded(String typeName, Transaction transaction,
ReferencedEnvelope bounds, boolean commit) {
if (commit) {
fireCommit(typeName, transaction, FeatureEvent.FEATURES_ADDED, bounds );
} else {
fireEvent(typeName, transaction, FeatureEvent.FEATURES_ADDED, bounds );
}
}
/**
* Provided event will be used as a template for notifying all FeatureSources
* for the provided typeName.
*
* @param typeName
* @param transaction
* @param event
*/
public void fireEvent(String typeName, Transaction transaction, FeatureEvent event ){
if( event.getType() == FeatureEvent.Type.COMMIT ||
event.getType() == FeatureEvent.Type.ROLLBACK ){
// This is a commit event; it needs to go out to everyone
// Listeners on the Transaction need to be told about any feature ids that were changed
// Listeners on AUTO_COMMIT need to be told that something happened
Map
*
*
* - when an existing Feature is modified with Tranasaction.AUTO_COMMIT
* all listeners registered with SimpleFeatureSource of typeName will be
* notified.
*
* - when an existing Feature is modified, with a Transaction all
* listeners registered with SimpleFeatureSource of typeName and with the same
* Transaction will be notified.
* null
if
* unknown)
*/
public void fireFeaturesChanged(String typeName, Transaction transaction,
ReferencedEnvelope bounds, boolean commit) {
if (commit) {
fireCommit(typeName, transaction, FeatureEvent.FEATURES_CHANGED, bounds );
} else {
fireEvent(typeName, transaction, FeatureEvent.FEATURES_CHANGED, bounds );
}
}
/**
* Notify all listeners that have registered interest for notification on
* this event type.
*
*
*
*
*
* @param typeName typeName being modified
* @param transaction Transaction used for change
* @param commit
- when changes have occured on a Transaction
* all listeners registered with SimpleFeatureSource of typeName will be
* notified except those with the Same Transaction
*
- when changes have been reverted only those
* listeners registered with SimpleFeatureSource of typeName and with the same
* Transaction will be notified.
* true
for commit
,
* false
for rollback
*/
public void fireChanged(String typeName, Transaction transaction,
boolean commit) {
if (commit) {
fireCommit(typeName, transaction, FeatureEvent.FEATURES_CHANGED, null );
} else {
fireEvent(typeName, transaction, FeatureEvent.FEATURES_CHANGED, null );
}
}
/**
* Fire notifications out to everyone.
*
* @param typeName
* @param transaction
*/
private void fireCommit( String typeName, Transaction transaction, int type, ReferencedEnvelope bounds) {
FeatureSource extends FeatureType, ? extends Feature> featureSource;
FeatureListener[] listeners;
FeatureEvent event;
Map
*
*
*
* @param typeName typeName being modified
* @param transaction Transaction used for change
* @param bounds BoundingBox of changes (may be null
if
* unknown)
*/
public void fireFeaturesRemoved(String typeName, Transaction transaction,
ReferencedEnvelope bounds, boolean commit ) {
if (commit) {
fireCommit(typeName, transaction, FeatureEvent.FEATURES_REMOVED, bounds );
} else {
fireEvent(typeName, transaction, FeatureEvent.FEATURES_REMOVED, bounds );
}
}
}