001: package net.refractions.udig.project.internal.impl;
002:
003: import java.io.IOException;
004: import java.util.Set;
005:
006: import net.refractions.udig.project.ILayer;
007: import net.refractions.udig.project.ProjectBlackboardConstants;
008: import net.refractions.udig.project.internal.EditManager;
009: import net.refractions.udig.project.internal.Messages;
010: import net.refractions.udig.project.internal.ProjectPlugin;
011:
012: import org.geotools.data.DataStore;
013: import org.geotools.data.FeatureListener;
014: import org.geotools.data.FeatureReader;
015: import org.geotools.data.FeatureSource;
016: import org.geotools.data.FeatureStore;
017: import org.geotools.data.Query;
018: import org.geotools.data.Transaction;
019: import org.geotools.feature.AttributeType;
020: import org.geotools.feature.FeatureCollection;
021: import org.geotools.feature.FeatureType;
022: import org.geotools.filter.Filter;
023:
024: import com.vividsolutions.jts.geom.Envelope;
025:
026: /**
027: * A FeatureStore decorator that does not allow the transaction to be set more than once. (Only if
028: * the current transaction is "AUTO_COMMIT" can the transaction be set)
029: *
030: * @author jones
031: * @since 1.0.0
032: */
033: public class UDIGFeatureStore implements FeatureStore {
034:
035: FeatureStore wrapped;
036: ILayer layer;
037:
038: /**
039: * Create a new FeatureStore decorator that does not allow the transaction to be set more than
040: * once. (Only if the current transaction is "AUTO_COMMIT" can the transaction be set)
041: *
042: * @param store the feature store that will be decorated
043: * @param layer TODO
044: */
045: public UDIGFeatureStore(FeatureStore store, ILayer layer) {
046: wrapped = store;
047: this .layer = layer;
048:
049: }
050:
051: @SuppressWarnings("deprecation")
052: public Set addFeatures(FeatureReader arg0) throws IOException {
053: setTransactionInternal();
054: return wrapped.addFeatures(arg0);
055: }
056:
057: public void removeFeatures(Filter arg0) throws IOException {
058: setTransactionInternal();
059: wrapped.removeFeatures(arg0);
060: }
061:
062: public void modifyFeatures(AttributeType[] arg0, Object[] arg1,
063: Filter arg2) throws IOException {
064: setTransactionInternal();
065: wrapped.modifyFeatures(arg0, arg1, arg2);
066: }
067:
068: public void modifyFeatures(AttributeType arg0, Object arg1,
069: Filter arg2) throws IOException {
070: setTransactionInternal();
071: wrapped.modifyFeatures(arg0, arg1, arg2);
072: }
073:
074: public void setFeatures(FeatureReader arg0) throws IOException {
075: setTransactionInternal();
076: wrapped.setFeatures(arg0);
077: }
078:
079: public void setTransaction(Transaction arg0) {
080: throw new IllegalArgumentException(Messages.UDIGFeatureStore_0
081: + Messages.UDIGFeatureStore_1);
082: // if (wrapped.getTransaction() != Transaction.AUTO_COMMIT
083: // || !(arg0 instanceof UDIGTransaction))
084: // throw new IllegalArgumentException(Messages.UDIGFeatureStore_0 +
085: // Messages.UDIGFeatureStore_1);
086: // wrapped.setTransaction(arg0);
087: }
088:
089: protected void editComplete() {
090: wrapped.setTransaction(Transaction.AUTO_COMMIT);
091: }
092:
093: private void setTransactionInternal() {
094: if (!layer
095: .isApplicable(ProjectBlackboardConstants.LAYER__EDIT_APPLICABILITY)) {
096: ProjectPlugin
097: .log(
098: "Attempted to open a transaction on a non-editable layer (Aborted)", //$NON-NLS-1$
099: new Exception());
100: }
101: if (wrapped.getTransaction() == Transaction.AUTO_COMMIT) {
102: Transaction transaction = ((EditManager) layer.getMap()
103: .getEditManager()).getTransaction();
104: wrapped.setTransaction(transaction);
105: }
106: }
107:
108: /**
109: * Vitalus: Think out how to provide for developers the opportunity
110: * to use its own FeatureStore wrapper, not UDIGFeatureStore.
111: */
112: public void startTransaction() {
113: if (wrapped.getTransaction() == Transaction.AUTO_COMMIT) {
114: Transaction transaction = ((EditManager) layer.getMap()
115: .getEditManager()).getTransaction();
116: wrapped.setTransaction(transaction);
117: }
118: }
119:
120: public Transaction getTransaction() {
121: return wrapped.getTransaction();
122: }
123:
124: public DataStore getDataStore() {
125: return wrapped.getDataStore();
126: }
127:
128: public void addFeatureListener(FeatureListener arg0) {
129: wrapped.addFeatureListener(arg0);
130: }
131:
132: public void removeFeatureListener(FeatureListener arg0) {
133: wrapped.removeFeatureListener(arg0);
134: }
135:
136: public FeatureCollection getFeatures(Query arg0) throws IOException {
137: return wrapped.getFeatures(arg0);
138: }
139:
140: public FeatureCollection getFeatures(Filter arg0)
141: throws IOException {
142: return wrapped.getFeatures(arg0);
143: }
144:
145: public FeatureCollection getFeatures() throws IOException {
146: return wrapped.getFeatures();
147: }
148:
149: public FeatureType getSchema() {
150: return wrapped.getSchema();
151: }
152:
153: public Envelope getBounds() throws IOException {
154: return wrapped.getBounds();
155: }
156:
157: public Envelope getBounds(Query arg0) throws IOException {
158: return wrapped.getBounds(arg0);
159: }
160:
161: public int getCount(Query arg0) throws IOException {
162: return wrapped.getCount(arg0);
163: }
164:
165: public Set addFeatures(FeatureCollection arg0) throws IOException {
166: setTransactionInternal();
167: return wrapped.addFeatures(arg0);
168: }
169:
170: public boolean sameSource(FeatureSource source) {
171: return source == wrapped || source == this;
172: }
173: }
|