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 @ 41971
History | View | Annotate | Download (6.55 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 |
|
31 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
32 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
33 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
34 |
import org.gvsig.fmap.mapcontrol.tools.Events.MeasureEvent; |
35 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PolylineListener; |
36 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
37 |
|
38 |
|
39 |
|
40 |
/**
|
41 |
* <p>Behavior that allows user to draw a polyline by its vertexes on the image of the associated
|
42 |
* <code>MapControl</code> using a {@link PolylineListener PolylineListener}.</p>
|
43 |
*
|
44 |
* @author Vicente Caballero Navarro
|
45 |
* @author Pablo Piqueras Bartolom?
|
46 |
*/
|
47 |
public class PolylineBehavior extends Behavior { |
48 |
/**
|
49 |
* The abscissa coordinate of all vertexes of the polyline.
|
50 |
*/
|
51 |
protected ArrayList arrayX = new ArrayList(); //<Double> |
52 |
|
53 |
/**
|
54 |
* The ordinate coordinate of all vertexes of the polyline.
|
55 |
*/
|
56 |
protected ArrayList arrayY = new ArrayList(); //<Double> |
57 |
|
58 |
/**
|
59 |
* Determines if user is setting the vertexes (with one click of the button 1 of the mouse), or not.
|
60 |
*/
|
61 |
protected boolean isClicked = false; |
62 |
|
63 |
/**
|
64 |
* Tool listener used to work with the <code>MapControl</code> object.
|
65 |
*
|
66 |
* @see #getListener()
|
67 |
* @see #setListener(ToolListener)
|
68 |
*/
|
69 |
protected PolylineListener listener;
|
70 |
|
71 |
/**
|
72 |
* <p>Creates a new behavior for drawing a polyline by its vertexes.</p>
|
73 |
*
|
74 |
* @param mli listener used to permit this object to work with the associated <code>MapControl</code>
|
75 |
*/
|
76 |
public PolylineBehavior(PolylineListener mli) {
|
77 |
listener = mli; |
78 |
} |
79 |
|
80 |
/*
|
81 |
* (non-Javadoc)
|
82 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
83 |
*/
|
84 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
85 |
mapControlDrawer.drawImage(getMapControl().getImage(), 0, 0); |
86 |
mapControlDrawer.setColor(Color.black);
|
87 |
|
88 |
if (!arrayX.isEmpty()) {
|
89 |
drawPolyLine(mapControlDrawer); |
90 |
} |
91 |
} |
92 |
|
93 |
/*
|
94 |
* (non-Javadoc)
|
95 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
96 |
*/
|
97 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
98 |
if( !isMyButton(e) ) {
|
99 |
return;
|
100 |
} |
101 |
if (e.getClickCount() == 2) { |
102 |
listener.polylineFinished(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
103 |
|
104 |
arrayX.clear(); |
105 |
arrayY.clear(); |
106 |
|
107 |
isClicked = false;
|
108 |
} else {
|
109 |
//System.err.println("simpleclick");
|
110 |
isClicked = true;
|
111 |
Point2D point = getMapControl().getViewPort().toMapPoint(e.getPoint());
|
112 |
addPoint(point); |
113 |
|
114 |
if (arrayX.size() < 2) { |
115 |
addPoint(point); |
116 |
} |
117 |
|
118 |
listener.pointFixed(new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e)); |
119 |
} |
120 |
} |
121 |
|
122 |
/*
|
123 |
* (non-Javadoc)
|
124 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
125 |
*/
|
126 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
127 |
if( !isMyButton(e) ) {
|
128 |
return;
|
129 |
} |
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( !isMyButton(e) ) {
|
151 |
return;
|
152 |
} |
153 |
//System.err.println("moved antes de click");
|
154 |
if (isClicked) {
|
155 |
//System.err.println("moved despues de click");
|
156 |
changeLastPoint(getMapControl().getViewPort().toMapPoint(e.getPoint())); |
157 |
|
158 |
MeasureEvent event = new MeasureEvent((Double[]) arrayX.toArray(new Double[arrayX.size()]), (Double[]) arrayY.toArray(new Double[arrayY.size()]), e); |
159 |
|
160 |
listener.points(event); |
161 |
getMapControl().repaint(); |
162 |
} |
163 |
} |
164 |
|
165 |
/**
|
166 |
* <p>Draws the polyline in the <code>Graphics2D</code> of the associated <code>MapControl</code>.</p>
|
167 |
* @param mapControlDrawer
|
168 |
*/
|
169 |
protected void drawPolyLine(MapControlDrawer mapControlDrawer) { |
170 |
GeneralPathX polyline = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD, arrayX.size());
|
171 |
|
172 |
polyline.moveTo(((Double) arrayX.get(0)).doubleValue(), |
173 |
((Double) arrayY.get(0)).doubleValue()); |
174 |
|
175 |
for (int index = 0; index < arrayX.size(); index++) { |
176 |
polyline.lineTo(((Double) arrayX.get(index)).doubleValue(),((Double) arrayY.get(index)).doubleValue()); |
177 |
} |
178 |
|
179 |
mapControlDrawer.draw(createLineString(polyline)); |
180 |
} |
181 |
|
182 |
/**
|
183 |
* <p>Adds a new point to the polyline.</p>
|
184 |
*
|
185 |
* @param p a new point to the polyline
|
186 |
*/
|
187 |
protected void addPoint(Point2D p) { |
188 |
arrayX.add(new Double(p.getX())); |
189 |
arrayY.add(new Double(p.getY())); |
190 |
} |
191 |
|
192 |
/**
|
193 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
194 |
*
|
195 |
* @param listener a <code>PolylineListener</code> object for this behavior
|
196 |
*/
|
197 |
public void setListener(ToolListener listener) { |
198 |
this.listener = (PolylineListener) listener;
|
199 |
} |
200 |
|
201 |
/*
|
202 |
* (non-Javadoc)
|
203 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
204 |
*/
|
205 |
public ToolListener getListener() {
|
206 |
return listener;
|
207 |
} |
208 |
} |