001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal.commands.edit;
010:
011: import java.util.Iterator;
012:
013: import net.refractions.udig.core.internal.GeometryBuilder;
014: import net.refractions.udig.project.ILayer;
015: import net.refractions.udig.project.command.MapCommand;
016: import net.refractions.udig.project.command.UndoableMapCommand;
017: import net.refractions.udig.project.internal.Layer;
018: import net.refractions.udig.project.internal.Messages;
019:
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.jface.dialogs.MessageDialog;
022: import org.eclipse.swt.widgets.Display;
023: import org.geotools.data.FeatureStore;
024: import org.geotools.data.Transaction;
025: import org.geotools.feature.AttributeType;
026: import org.geotools.feature.Feature;
027: import org.geotools.feature.FeatureType;
028: import org.geotools.filter.FilterFactoryFinder;
029: import org.geotools.referencing.CRS;
030: import org.opengis.referencing.operation.MathTransform;
031: import org.opengis.util.CodeList;
032:
033: import com.vividsolutions.jts.geom.Coordinate;
034: import com.vividsolutions.jts.geom.Geometry;
035:
036: /**
037: * Creates a new feature in the current edit layer.
038: *
039: * @author jones
040: * @since 0.3
041: */
042: public class CreateFeatureCommand extends AbstractEditCommand implements
043: UndoableMapCommand {
044:
045: private Coordinate[] coordinates;
046:
047: String fid;
048:
049: /**
050: * Construct <code>CreateFeatureCommand</code>.
051: *
052: * @param coordinates Coordinates in Map coordinates.
053: */
054: public CreateFeatureCommand(Coordinate[] coordinates) {
055: int i = 0;
056: if (coordinates != null)
057: i = coordinates.length;
058: Coordinate[] c = new Coordinate[i];
059: if (coordinates != null)
060: System.arraycopy(coordinates, 0, c, 0, c.length);
061: this .coordinates = c;
062: }
063:
064: /**
065: * @see net.refractions.udig.project.command.MapCommand#run()
066: */
067: @SuppressWarnings("unchecked")
068: public void run(IProgressMonitor monitor) throws Exception {
069: ILayer editLayer = getMap().getEditManager().getEditLayer();
070:
071: editLayer = findEditLayer();
072:
073: if (editLayer == null) {
074: MessageDialog.openError(Display.getDefault()
075: .getActiveShell(),
076: Messages.CreateFeatureCommand_error_title,
077: Messages.CreateFeatureCommand_error_message);
078: return;
079: }
080: FeatureStore store = editLayer.getResource(FeatureStore.class,
081: null);
082: transform();
083: if (store.getTransaction() == Transaction.AUTO_COMMIT)
084: throw new Exception(
085: "Error transaction has not been started"); //$NON-NLS-1$
086: final FeatureType type = store.getSchema();
087: Object[] attrs = new Object[type.getAttributeCount()];
088: for (int i = 0; i < attrs.length; i++) {
089: attrs[i] = setDefaultValue(type.getAttributeType(i));
090: }
091: final Feature newFeature = type.create(attrs);
092: Class geomType = type.getDefaultGeometry().getType();
093:
094: Geometry geom = GeometryBuilder.create().safeCreateGeometry(
095: geomType, coordinates);
096: newFeature.setDefaultGeometry(geom);
097:
098: fid = newFeature.getID();
099: map.getEditManagerInternal().addFeature(newFeature,
100: (Layer) editLayer);
101: }
102:
103: /**
104: * @param object
105: * @param object2
106: */
107: private Object setDefaultValue(AttributeType type) {
108: if (type.createDefaultValue() != null)
109: return type.createDefaultValue();
110: if (Boolean.class.isAssignableFrom(type.getType())
111: || boolean.class.isAssignableFrom(type.getType()))
112: return Boolean.FALSE;
113: if (String.class.isAssignableFrom(type.getType()))
114: return ""; //$NON-NLS-1$
115: if (Integer.class.isAssignableFrom(type.getType()))
116: return Integer.valueOf(0);
117: if (Double.class.isAssignableFrom(type.getType()))
118: return Double.valueOf(0);
119: if (Float.class.isAssignableFrom(type.getType()))
120: return Float.valueOf(0);
121: if (CodeList.class.isAssignableFrom(type.getType())) {
122: return type.createDefaultValue();
123: }
124: return null;
125: }
126:
127: /**
128: * TODO summary sentence for findEditLayer ...
129: */
130: private Layer findEditLayer() {
131: Layer layer = null;
132: if (map.getEditManagerInternal().getEditLayerInternal() != null)
133: return map.getEditManagerInternal().getEditLayerInternal();
134: for (Iterator iter = map.getContextModel().getLayers()
135: .iterator(); iter.hasNext();) {
136: layer = (Layer) iter.next();
137: if (layer.isType(FeatureStore.class)
138: && layer.isSelectable() && layer.isVisible())
139: break;
140: }
141: return layer;
142: }
143:
144: /**
145: * Transforms coordinates into the layer CRS if nessecary
146: *
147: * @throws Exception
148: */
149: private void transform() throws Exception {
150: ILayer editLayer = getMap().getEditManager().getEditLayer();
151: if (map.getViewportModel().getCRS().equals(
152: editLayer.getCRS(null)))
153: return;
154: MathTransform mt = CRS.transform(map.getViewportModel()
155: .getCRS(), editLayer.getCRS(null), true);
156: if (mt == null || mt.isIdentity())
157: return;
158: double[] coords = new double[coordinates.length * 2];
159: for (int i = 0; i < coordinates.length; i++) {
160: coords[i * 2] = coordinates[i].x;
161: coords[i * 2 + 1] = coordinates[i].y;
162: }
163: mt.transform(coords, 0, coords, 0, coordinates.length);
164: for (int i = 0; i < coordinates.length; i++) {
165: coordinates[i].x = coords[i * 2];
166: coordinates[i].y = coords[i * 2 + 1];
167: }
168: }
169:
170: public MapCommand copy() {
171: return new CreateFeatureCommand(coordinates);
172: }
173:
174: /**
175: * @see net.refractions.udig.project.command.MapCommand#getName()
176: */
177: public String getName() {
178: return Messages.CreateFeatureCommand_createFeature;
179: }
180:
181: /**
182: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
183: */
184: public void rollback(IProgressMonitor monitor) throws Exception {
185: ILayer editLayer = getMap().getEditManager().getEditLayer();
186: editLayer.getResource(FeatureStore.class, null).removeFeatures(
187: FilterFactoryFinder.createFilterFactory()
188: .createFidFilter(fid));
189: }
190:
191: }
|