svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / PolylineBehavior.java @ 43510
History | View | Annotate | Download (6.78 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontrol.tools.Behavior; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.event.MouseEvent; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.GeometryLocator; |
33 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
34 |
|
35 |
import org.gvsig.fmap.geom.primitive.Line; |
36 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
37 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
38 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
39 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener; |
40 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
41 |
|
42 |
|
43 |
|
44 |
/**
|
45 |
* <p>Behavior that allows user to draw a polyline by its vertexes on the image of the associated
|
46 |
* <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
|
47 |
*
|
48 |
* @author Vicente Caballero Navarro
|
49 |
* @author Pablo Piqueras Bartolom?
|
50 |
*/
|
51 |
public class PolylineBehavior extends Behavior { |
52 |
/**
|
53 |
* The abscissa coordinate of all vertexes of the polyline.
|
54 |
*/
|
55 |
protected List<Double> arrayX = new ArrayList<>(); |
56 |
|
57 |
/**
|
58 |
* The ordinate coordinate of all vertexes of the polyline.
|
59 |
*/
|
60 |
protected List<Double> arrayY = new ArrayList<>(); |
61 |
|
62 |
/**
|
63 |
* Determines if user is setting the vertexes (with one click of the button 1 of the mouse), or not.
|
64 |
*/
|
65 |
protected boolean isClicked = false; |
66 |
|
67 |
/**
|
68 |
* Tool listener used to work with the <code>MapControl</code> object.
|
69 |
*
|
70 |
* @see #getListener()
|
71 |
* @see #setListener(ToolListener)
|
72 |
*/
|
73 |
protected PolylineListener listener;
|
74 |
|
75 |
/**
|
76 |
* <p>Creates a new behavior for drawing a polyline by its vertexes.</p>
|
77 |
*
|
78 |
* @param mli listener used to permit this object to work with the associated <code>MapControl</code>
|
79 |
*/
|
80 |
public PolylineBehavior(PolylineListener mli) {
|
81 |
super(Behavior.BUTTON_LEFT);
|
82 |
listener = mli; |
83 |
} |
84 |
|
85 |
/*
|
86 |
* (non-Javadoc)
|
87 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
88 |
*/
|
89 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
90 |
mapControlDrawer.setColor(Color.black);
|
91 |
|
92 |
if (!arrayX.isEmpty()) {
|
93 |
drawPolyLine(mapControlDrawer); |
94 |
} |
95 |
} |
96 |
|
97 |
/*
|
98 |
* (non-Javadoc)
|
99 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
100 |
*/
|
101 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
102 |
if( !isMyButton(e) ) {
|
103 |
return;
|
104 |
} |
105 |
if (e.getClickCount() == 2) { |
106 |
listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
107 |
|
108 |
arrayX.clear(); |
109 |
arrayY.clear(); |
110 |
|
111 |
isClicked = false;
|
112 |
} else {
|
113 |
isClicked = true;
|
114 |
Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
|
115 |
addPoint(point); |
116 |
|
117 |
if (arrayX.size() < 2) { |
118 |
addPoint(point); |
119 |
} |
120 |
|
121 |
listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
122 |
} |
123 |
} |
124 |
|
125 |
/*
|
126 |
* (non-Javadoc)
|
127 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
128 |
*/
|
129 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
130 |
mouseMoved(e); |
131 |
} |
132 |
|
133 |
/**
|
134 |
* <p>Changes the last point added of the polyline.</p>
|
135 |
*
|
136 |
* @param p a point which will replace the last added to the polyline
|
137 |
*/
|
138 |
protected void changeLastPoint(Point2D p) { |
139 |
if (arrayX.size() > 0) { |
140 |
arrayX.set(arrayX.size() - 1, new Double(p.getX())); |
141 |
arrayY.set(arrayY.size() - 1, new Double(p.getY())); |
142 |
} |
143 |
} |
144 |
|
145 |
/*
|
146 |
* (non-Javadoc)
|
147 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
|
148 |
*/
|
149 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
150 |
if (isClicked) {
|
151 |
//System.err.println("moved despues de click");
|
152 |
changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
153 |
|
154 |
MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e); |
155 |
|
156 |
listener.points(event); |
157 |
getMapControl().repaint(); |
158 |
} |
159 |
} |
160 |
|
161 |
/**
|
162 |
* <p>Draws the polyline in the <code>Graphics2D</code> of the associated <code>MapControl</code>.</p>
|
163 |
* @param mapControlDrawer
|
164 |
*/
|
165 |
protected void drawPolyLine(MapControlDrawer mapControlDrawer) { |
166 |
|
167 |
// GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, arrayX.size());
|
168 |
//
|
169 |
// polyline.moveTo(((Double) arrayX.get(0)).doubleValue(),
|
170 |
// ((Double) arrayY.get(0)).doubleValue());
|
171 |
//
|
172 |
// for (int index = 0; index < arrayX.size(); index++) {
|
173 |
// polyline.lineTo(((Double) arrayX.get(index)).doubleValue(),((Double) arrayY.get(index)).doubleValue());
|
174 |
// }
|
175 |
try {
|
176 |
Line line = geomManager.createLine(Geometry.SUBTYPES.GEOM2D);
|
177 |
for (int index = 0; index < arrayX.size(); index++) { |
178 |
line.addVertex(arrayX.get(index),arrayY.get(index)); |
179 |
} |
180 |
mapControlDrawer.draw(line); |
181 |
} catch (Exception ex) { |
182 |
LOG.warn("Can't draw polyline",ex);
|
183 |
} |
184 |
} |
185 |
|
186 |
/**
|
187 |
* <p>Adds a new point to the polyline.</p>
|
188 |
*
|
189 |
* @param p a new point to the polyline
|
190 |
*/
|
191 |
protected void addPoint(Point2D p) { |
192 |
arrayX.add(new Double(p.getX())); |
193 |
arrayY.add(new Double(p.getY())); |
194 |
} |
195 |
|
196 |
/**
|
197 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
198 |
*
|
199 |
* @param listener a <code>PolylineListener</code> object for this behavior
|
200 |
*/
|
201 |
public void setListener(ToolListener listener) { |
202 |
this.listener = (PolylineListener) listener;
|
203 |
} |
204 |
|
205 |
/*
|
206 |
* (non-Javadoc)
|
207 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
208 |
*/
|
209 |
public ToolListener getListener() {
|
210 |
return listener;
|
211 |
} |
212 |
} |