Revision 44246 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/tools/Events/MeasureEvent.java

View differences:

MeasureEvent.java
24 24
package org.gvsig.fmap.mapcontrol.tools.Events;
25 25

  
26 26
import java.awt.event.MouseEvent;
27
import java.awt.geom.Point2D;
28
import java.util.logging.Level;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryException;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.GeometryManager;
33
import org.gvsig.fmap.geom.exception.CreateGeometryException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationException;
35
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
36
import org.gvsig.fmap.geom.primitive.Circle;
37
import org.gvsig.fmap.geom.primitive.Circumference;
38
import org.gvsig.fmap.geom.primitive.Ellipse;
27 39

  
28 40
import org.gvsig.fmap.geom.primitive.GeneralPathX;
41
import org.gvsig.fmap.geom.primitive.PeriEllipse;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.mapcontext.ViewPort;
44
import org.gvsig.fmap.mapcontext.impl.DefaultMapContextManager;
45
import org.slf4j.Logger;
46
import org.slf4j.LoggerFactory;
29 47

  
30 48
/**
31
 * <p><code>MeasureEvent</code> is used to notify the selection of a polyline.</p>
49
 * <p>
50
 * <code>MeasureEvent</code> is used to notify the selection of a polyline.</p>
32 51
 *
33
 * <p>Stores the information about the 2D vertexes and the {@link GeneralPathX GeneralPathX}
34
 *  between them.</p>
52
 * <p>
53
 * Stores the information about the 2D vertexes and the
54
 * {@link GeneralPathX GeneralPathX} between them.</p>
35 55
 *
36 56
 * @author Vicente Caballero Navarro
37 57
 */
38 58
public class MeasureEvent {
39
	/**
40
	 * The polyline, broken down in straight lines, and quadratic and cubic (B?zier) curves.
41
	 */
42
	private GeneralPathX gp;
43 59

  
44
	/**
45
	 * Vector with the X coordinates.
46
	 */
47
	private Double[] x;
60
    /**
61
     * The polyline, broken down in straight lines, and quadratic and cubic
62
     * (B?zier) curves.
63
     */
64
    private GeneralPathX gp;
48 65

  
49
	/**
50
	 * Vector with the Y coordinates.
51
	 */
52
	private Double[] y;
66
    /**
67
     * Vector with the X coordinates.
68
     */
69
    private Double[] x;
53 70

  
54
	/**
55
	 * Mouse event that has been the cause of creating this event.
56
	 */
57
	private MouseEvent event;
71
    /**
72
     * Vector with the Y coordinates.
73
     */
74
    private Double[] y;
58 75

  
59
	/**
60
	 * <p>Creates a new <code>MeasureEvent</code> with all necessary data.</p>
61
	 * <p>The general path is calculated according the enclosed regions of the path alternate between interior
62
	 *  and exterior areas are traversed from the outside of the path towards a point inside the region.</p>
63
	 *
64
	 * @param x vector with the X coordinates
65
	 * @param y vector with the Y coordinates
66
	 * @param e event that has been the cause of creating this one
67
	 */
68
	public MeasureEvent(Double[] x, Double[] y, MouseEvent e) {
69
		this.x = x;
70
		this.y = y;
71
		this.event = e;
72
		gp = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, x.length);
73
		gp.moveTo(x[0].doubleValue(), y[0].doubleValue());
76
    /**
77
     * Mouse event that has been the cause of creating this event.
78
     */
79
    private MouseEvent event;
74 80

  
75
		for (int index = 1; index < x.length; index++) {
76
			gp.lineTo(x[index].doubleValue(), y[index].doubleValue());
77
		}
78
	}
81
    private static final Logger logger = LoggerFactory
82
            .getLogger(MeasureEvent.class);
79 83

  
80
	/**
81
	 * <p>Gets the {@link GeneralPathX GeneralPathX} of the measurement.</p>
82
	 *
83
	 * @see GeneralPathX
84
	 *
85
	 * @return geometric path constructed from straight lines, and quadratic and cubic (B?zier) curves
86
	 */
87
	public GeneralPathX getGP() {
88
		return gp;
89
	}
84
    /**
85
     * <p>
86
     * Creates a new <code>MeasureEvent</code> with all necessary data.</p>
87
     * <p>
88
     * The general path is calculated according the enclosed regions of the path
89
     * alternate between interior and exterior areas are traversed from the
90
     * outside of the path towards a point inside the region.</p>
91
     *
92
     * @param x vector with the X coordinates
93
     * @param y vector with the Y coordinates
94
     * @param e event that has been the cause of creating this one
95
     */
96
    public MeasureEvent(Double[] x, Double[] y, MouseEvent e) {
97
        this.x = x;
98
        this.y = y;
99
        this.event = e;
100
        gp = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, x.length);
101
        gp.moveTo(x[0].doubleValue(), y[0].doubleValue());
90 102

  
91
	/**
92
	 * <p>Sets the {@link GeneralPathX GeneralPathX} of the measurement.</p>
93
	 *
94
	 * @see GeneralPathX
95
	 *
96
	 * @param gP geometric path constructed from straight lines, and quadratic and cubic (B?zier) curves
97
	 */
98
	public void setGP(GeneralPathX gp) {
99
		this.gp = gp;
100
	}
103
        for (int index = 1; index < x.length; index++) {
104
            gp.lineTo(x[index].doubleValue(), y[index].doubleValue());
105
        }
106
    }
101 107

  
102
	/**
103
	 * <p>Gets a vector with the X coordinates.</p>
104
	 *
105
	 * @return vector with the X coordinates
106
	 */
107
	public Double[] getXs() {
108
		return x;
109
	}
108
    /**
109
     * <p>
110
     * Gets the {@link GeneralPathX GeneralPathX} of the measurement.</p>
111
     *
112
     * @see GeneralPathX
113
     *
114
     * @return geometric path constructed from straight lines, and quadratic and
115
     * cubic (B?zier) curves
116
     */
117
    public GeneralPathX getGP() {
118
        return gp;
119
    }
110 120

  
111
	/**
112
	 * <p>Gets a vector with the Y coordinates.</p>
113
	 *
114
	 * @return vector with the Y coordinates
115
	 */
116
	public Double[] getYs() {
117
		return y;
118
	}
121
    /**
122
     * <p>
123
     * Sets the {@link GeneralPathX GeneralPathX} of the measurement.</p>
124
     *
125
     * @see GeneralPathX
126
     *
127
     * @param gP geometric path constructed from straight lines, and quadratic
128
     * and cubic (B?zier) curves
129
     */
130
    public void setGP(GeneralPathX gp) {
131
        this.gp = gp;
132
    }
119 133

  
120
	/**
121
	 * <p>Gets the event that has been the cause of creating this one.</p>
122
	 *
123
	 * @return mouse event that has been the cause of creating this one
124
	 */
125
	public MouseEvent getEvent() {
126
		return event;
127
	}
134
    /**
135
     * <p>
136
     * Gets a vector with the X coordinates.</p>
137
     *
138
     * @return vector with the X coordinates
139
     */
140
    public Double[] getXs() {
141
        return x;
142
    }
143

  
144
    /**
145
     * <p>
146
     * Gets a vector with the Y coordinates.</p>
147
     *
148
     * @return vector with the Y coordinates
149
     */
150
    public Double[] getYs() {
151
        return y;
152
    }
153

  
154
    /**
155
     * <p>
156
     * Gets the event that has been the cause of creating this one.</p>
157
     *
158
     * @return mouse event that has been the cause of creating this one
159
     */
160
    public MouseEvent getEvent() {
161
        return event;
162
    }
163

  
164
    public Geometry getCircle() {
165
        GeometryManager manager = GeometryLocator.getGeometryManager();
166
        Point center;
167
        Geometry geom = null;
168
        try {
169
            center = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
170
            center.setX(this.getXs()[0]);
171
            center.setY(this.getYs()[0]);
172

  
173
            Point point2;
174
            point2 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
175
            point2.setX(this.getXs()[1]);
176
            point2.setY(this.getYs()[1]);
177

  
178
            double radious = center.distance(point2);
179

  
180
            Circle circle;
181
            circle = (Circle) manager.create(Geometry.TYPES.CIRCLE, Geometry.SUBTYPES.GEOM2D);
182
            circle.setPoints(center, radious);
183
            geom = circle;
184
        } catch (CreateGeometryException e) {
185
            logger.error("Failed_creating_geometry", e);
186
        } catch (GeometryOperationNotSupportedException e) {
187
            logger.error("Operation_not_supported", e);
188
        } catch (GeometryOperationException e) {
189
            logger.error("Failed_performing_the_operation", e);
190
        }
191

  
192
        return geom;
193
    }
194

  
195
    public Geometry getCircumference() {
196
        GeometryManager manager = GeometryLocator.getGeometryManager();
197
        Point center;
198
        Geometry geom = null;
199
        try {
200
            center = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
201
            center.setX(this.getXs()[0]);
202
            center.setY(this.getYs()[0]);
203

  
204
            Point point2;
205
            point2 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
206
            point2.setX(this.getXs()[1]);
207
            point2.setY(this.getYs()[1]);
208

  
209
            double radious = center.distance(point2);
210

  
211
            Circumference circle;
212
            circle = (Circumference) manager.create(Geometry.TYPES.CIRCUMFERENCE, Geometry.SUBTYPES.GEOM2D);
213
            circle.setPoints(center, radious);
214
            geom = circle;
215
        } catch (CreateGeometryException e) {
216
            logger.error("Failed_creating_geometry", e);
217
        } catch (GeometryOperationNotSupportedException e) {
218
            logger.error("Operation_not_supported", e);
219
        } catch (GeometryOperationException e) {
220
            logger.error("Failed_performing_the_operation", e);
221
        }
222

  
223
        return geom;
224
    }
225

  
226
    public Geometry getEllipse() {
227
        Geometry geom = null;
228
        try {
229
            GeometryManager manager = GeometryLocator.getGeometryManager();
230
            Point point1 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
231
            point1.setX(this.getXs()[0]);
232
            point1.setY(this.getYs()[0]);
233
            Point point2 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
234
            point2.setX(this.getXs()[1]);
235
            point2.setY(this.getYs()[1]);
236
            Point point3 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
237
            point3.setX(this.getXs()[2]);
238
            point3.setY(this.getYs()[2]);
239
            double radious = point2.distance(point3);
240

  
241
            Ellipse ellipse;
242
            ellipse = (Ellipse) manager.create(Geometry.TYPES.ELLIPSE, Geometry.SUBTYPES.GEOM2D);
243
            ellipse.setPoints(point1, point2, radious);
244
            geom = ellipse;
245
        } catch (CreateGeometryException e) {
246
            logger.error("Failed_creating_geometry", e);
247
        } catch (GeometryOperationNotSupportedException e) {
248
            logger.error("Operation_not_supported", e);
249
        } catch (GeometryOperationException e) {
250
            logger.error("Failed_performing_the_operation", e);
251
        }
252
        return geom;
253
    }
254
    
255
    public Geometry getPeriEllipse() {
256
        Geometry geom = null;
257
        try {
258
            GeometryManager manager = GeometryLocator.getGeometryManager();
259
            Point point1 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
260
            point1.setX(this.getXs()[0]);
261
            point1.setY(this.getYs()[0]);
262
            Point point2 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
263
            point2.setX(this.getXs()[1]);
264
            point2.setY(this.getYs()[1]);
265
            Point point3 = (Point) manager.create(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
266
            point3.setX(this.getXs()[2]);
267
            point3.setY(this.getYs()[2]);
268
            double radious = point2.distance(point3);
269

  
270
            PeriEllipse ellipse;
271
            ellipse = (PeriEllipse) manager.create(Geometry.TYPES.PERIELLIPSE, Geometry.SUBTYPES.GEOM2D);
272
            ellipse.setPoints(point1, point2, radious);
273
            geom = ellipse.toLines();
274
        } catch (CreateGeometryException e) {
275
            logger.error("Failed_creating_geometry", e);
276
        } catch (GeometryOperationNotSupportedException e) {
277
            logger.error("Operation_not_supported", e);
278
        } catch (GeometryOperationException e) {
279
            logger.error("Failed_performing_the_operation", e);
280
        } catch (GeometryException e) {
281
           logger.error("Can't create line geometry from ellipse", e);
282
        }
283
        return geom;
284
    }
128 285
}

Also available in: Unified diff