001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
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: */
017: package net.refractions.udig.project.ui.internal.commands.draw;
018:
019: import java.awt.Color;
020: import java.awt.Rectangle;
021: import java.util.List;
022:
023: import net.refractions.udig.project.internal.EditManager;
024: import net.refractions.udig.project.internal.ProjectPackage;
025: import net.refractions.udig.project.internal.render.ViewportModel;
026: import net.refractions.udig.project.render.IViewportModel;
027: import net.refractions.udig.project.ui.commands.AbstractDrawCommand;
028: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
029: import net.refractions.udig.ui.Drawing;
030:
031: import org.eclipse.core.runtime.IProgressMonitor;
032: import org.eclipse.emf.common.notify.Adapter;
033: import org.eclipse.emf.common.notify.Notification;
034: import org.eclipse.emf.common.notify.impl.AdapterImpl;
035: import org.geotools.feature.Feature;
036: import org.geotools.geometry.jts.JTS;
037: import org.geotools.styling.Symbolizer;
038: import org.opengis.referencing.operation.MathTransform;
039: import org.opengis.referencing.operation.TransformException;
040:
041: import com.vividsolutions.jts.geom.Envelope;
042: import com.vividsolutions.jts.geom.LineString;
043: import com.vividsolutions.jts.geom.MultiPoint;
044: import com.vividsolutions.jts.geom.Point;
045:
046: /**
047: * Draws the currently edited feature on the screen.
048: *
049: * @author jeichar
050: * @since 0.3
051: */
052: public class DrawEditFeatureCommand extends AbstractDrawCommand {
053: // private static Symbolizer[] symbs = getSymbolizers();
054:
055: ViewportModel model;
056:
057: Drawing drawing = Drawing.create();
058: Adapter editListener = new AdapterImpl() {
059: /**
060: * @see org.eclipse.emf.common.notify.impl.AdapterImpl#notifyChanged(org.eclipse.emf.common.notify.Notification)
061: */
062: public void notifyChanged(Notification msg) {
063: if (msg.getFeatureID(EditManager.class) == ProjectPackage.EDIT_MANAGER__EDIT_FEATURE) {
064: ((ViewportPane) model.getRenderManagerInternal()
065: .getMapDisplay()).repaint();
066: }
067:
068: }
069: };
070:
071: private boolean doKnots = false;
072:
073: private MathTransform mt;
074:
075: /**
076: * Creates a new instance of DrawFeatureCommand
077: *
078: * @param model The viewportmodel that the command uses to determine how the victim should be
079: * drawn.
080: */
081: public DrawEditFeatureCommand(IViewportModel model) {
082: this .model = (ViewportModel) model;
083: }
084:
085: /**
086: * @see net.refractions.udig.project.internal.command.MapCommand#open()
087: */
088: public void run(IProgressMonitor monitor) {
089: Feature feature = model.getMapInternal().getEditManager()
090: .getEditFeature();
091: if (feature == null)
092: return;
093:
094: @SuppressWarnings("unchecked") List<Adapter> list = model.getMapInternal().getEditManagerInternal().eAdapters(); //$NON-NLS-1$
095: if (!list.contains(editListener))
096: list.add(editListener);
097: MathTransform mt = null;
098: mt = getMathTransform();
099:
100: Symbolizer[] symbs = null;
101: if (feature.getDefaultGeometry() instanceof Point
102: || feature.getDefaultGeometry() instanceof MultiPoint)
103: symbs = Drawing.getSymbolizers(Point.class, Color.RED);
104: else
105: symbs = Drawing.getSymbolizers(LineString.class, Color.RED);
106: drawing.drawFeature(graphics, feature, model
107: .worldToScreenTransform(), doKnots, symbs, mt);
108: }
109:
110: /**
111: *
112: * @return
113: */
114: private MathTransform getMathTransform() {
115: if (mt == null)
116: try {
117: mt = model.getMapInternal().getEditManagerInternal()
118: .getEditLayerInternal().layerToMapTransform();
119: } catch (Exception e) {
120: mt = null;
121: }
122: return mt;
123: }
124:
125: /**
126: * If doKnots is set to true the edit features will be drawn with vertex knots.
127: */
128: public void setDrawKnots(boolean doKnots) {
129: this .doKnots = doKnots;
130: }
131:
132: /**
133: * @see net.refractions.udig.project.ui.commands.AbstractDrawCommand#setValid(boolean)
134: */
135: @SuppressWarnings("unchecked")//$NON-NLS-1$
136: public void setValid(boolean valid) {
137: super .setValid(valid);
138: if (!valid) {
139: List<Adapter> adapters = model.getMapInternal()
140: .getEditManagerInternal().eAdapters();
141: adapters.remove(editListener);
142: }
143: }
144:
145: public Rectangle getValidArea() {
146: Feature feature = getMap().getEditManager().getEditFeature();
147: if (feature != null) {
148: try {
149: Envelope bounds = JTS.transform(feature.getBounds(),
150: getMathTransform());
151: double[] points = new double[] { bounds.getMinX(),
152: bounds.getMinY(), bounds.getMaxX(),
153: bounds.getMaxY() };
154: getMap().getViewportModel().worldToScreenTransform()
155: .transform(points, 0, points, 0, 2);
156: return new Rectangle((int) points[0], (int) points[1],
157: (int) Math.abs(points[2] - points[0]),
158: (int) Math.abs(points[3] - points[1]));
159: } catch (TransformException e) {
160: return null;
161: }
162: }
163: return null;
164: }
165:
166: }
|