001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.feature.collection;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.geotools.data.collection.ResourceCollection;
024: import org.geotools.feature.CollectionEvent;
025: import org.geotools.feature.CollectionListener;
026: import org.geotools.feature.DefaultFeatureType;
027: import org.geotools.feature.Feature;
028: import org.geotools.feature.FeatureCollection;
029: import org.geotools.feature.FeatureType;
030: import org.geotools.feature.FeatureTypes;
031: import org.geotools.feature.type.FeatureAttributeType;
032:
033: /**
034: * This is *not* a Feature - it is a Delegate used by FeatureCollection
035: * implementations as "mix-in", provides implementation of featureCollection
036: * events, featureType, and attribute access.
037: * <p>
038: * To use cut&paste the following code exactly:<pre>
039: * <code>
040: *
041: * </code>
042: * </p>
043: * <p>
044: * On the bright side this means we can "fix" all the FeatureCollection implementations
045: * in one fell-swoop.
046: * </p>
047: *
048: * @author Jody Garnett, Refractions Reserach, Inc.
049: * @since GeoTools 2.2
050: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/collection/BaseFeatureState.java $
051: */
052: public class BaseFeatureState extends FeatureState {
053: //final ResourceCollection collection;
054: final FeatureType featureType;
055: final FeatureType schema;
056: String id;
057:
058: /** Internal listener storage list */
059: private List listeners = new ArrayList(2);
060:
061: /**
062: * Construct a fake FeatureType of this FeatureCollection.
063: * <p>
064: * Unless a FeatureType was provided during consturction (or this method is
065: * overriden) a FeatureType will be generated based on getSchmea according
066: * to the following assumptions:
067: * <ul>
068: * <li>FeatureType is gml:AbstractFeatureCollectionType
069: * <li>first attribute is getSchema.typeName
070: * <li>the attribute FeatureType the same as returned by getSchema()
071: * </ul>
072: * </p>
073: *
074: */
075: public static FeatureType featureType(FeatureType schema) {
076: List ats = new LinkedList();
077: ats.add(new FeatureAttributeType(schema.getTypeName(), schema,
078: false));
079: return new DefaultFeatureType("AbstractFeatureColletionType",
080: FeatureTypes.DEFAULT_NAMESPACE, ats, new LinkedList(),
081: null);
082: }
083:
084: public BaseFeatureState(ResourceCollection collection,
085: FeatureType schema) {
086: this (collection, featureType(schema), schema);
087: }
088:
089: public BaseFeatureState(ResourceCollection collection,
090: FeatureType featureType, FeatureType schema) {
091: super (collection);
092: //this.collection = collection;
093: this .featureType = featureType;
094: this .schema = schema;
095: }
096:
097: //
098: // FeatureCollection Event Support
099: //
100:
101: /**
102: * Adds a listener for collection events.
103: *
104: * @param listener The listener to add
105: */
106: public void addListener(CollectionListener listener) {
107: listeners.add(listener);
108: }
109:
110: /**
111: * Removes a listener for collection events.
112: *
113: * @param listener The listener to remove
114: */
115: public void removeListener(CollectionListener listener) {
116: listeners.remove(listener);
117: }
118:
119: /**
120: * To let listeners know that something has changed.
121: */
122: protected void fireChange(Feature[] features, int type) {
123: bounds = null; // must recalculate bounds
124:
125: CollectionEvent cEvent = new CollectionEvent(
126: (FeatureCollection) data, features, type);
127:
128: for (int i = 0, ii = listeners.size(); i < ii; i++) {
129: ((CollectionListener) listeners.get(i))
130: .collectionChanged(cEvent);
131: }
132: }
133:
134: protected void fireChange(Feature feature, int type) {
135: fireChange(new Feature[] { feature }, type);
136: }
137:
138: protected void fireChange(Collection coll, int type) {
139: Feature[] features = new Feature[coll.size()];
140: features = (Feature[]) coll.toArray(features);
141: fireChange(features, type);
142: }
143:
144: //
145: // Feature Methods
146: //
147: public FeatureType getFeatureType() {
148: return featureType;
149: }
150:
151: public FeatureType getChildFeatureType() {
152: return schema;
153: }
154:
155: public String getId() {
156: return id;
157: }
158:
159: public void setId(String id) {
160: this.id = id;
161: }
162: }
|