Revision 23163 trunk/extensions/extTopology/src/org/gvsig/fmap/tools/behavior/VectorBehavior.java
VectorBehavior.java | ||
---|---|---|
59 | 59 |
import java.awt.geom.Point2D; |
60 | 60 |
import java.awt.geom.Rectangle2D; |
61 | 61 |
import java.awt.image.BufferedImage; |
62 |
import java.util.List; |
|
62 | 63 |
|
63 | 64 |
import org.gvsig.fmap.core.FGeometryUtil; |
65 |
import org.gvsig.fmap.core.ShapePointExtractor; |
|
64 | 66 |
import org.gvsig.fmap.tools.listeners.VectorListener; |
65 | 67 |
|
68 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
|
69 |
import com.iver.andami.PluginServices; |
|
66 | 70 |
import com.iver.cit.gvsig.fmap.ViewPort; |
67 | 71 |
import com.iver.cit.gvsig.fmap.core.FPolyline2D; |
68 | 72 |
import com.iver.cit.gvsig.fmap.core.FShape; |
69 | 73 |
import com.iver.cit.gvsig.fmap.core.GeneralPathX; |
74 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
|
75 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
|
76 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
|
77 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
|
70 | 78 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
71 | 79 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
72 | 80 |
import com.iver.cit.gvsig.fmap.tools.Events.MoveEvent; |
73 | 81 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
82 |
import com.iver.cit.gvsig.project.documents.view.snapping.SnappingVisitor; |
|
83 |
import com.iver.cit.gvsig.project.documents.view.snapping.snappers.NearestPointSnapper; |
|
74 | 84 |
|
75 | 85 |
/** |
76 | 86 |
* Behavior to digitize two points vectors in mapcontrol |
77 | 87 |
* @author azabala |
78 | 88 |
*/ |
79 | 89 |
public class VectorBehavior extends Behavior { |
90 |
|
|
91 |
NearestPointSnapper snapper = new NearestPointSnapper(); |
|
92 |
PointSnapper pointSnapper = new PointSnapper(); |
|
93 |
|
|
94 |
FLyrVect snappingLyr = null; |
|
95 |
|
|
96 |
|
|
97 |
private boolean isZooming = false; |
|
80 | 98 |
private Point2D m_FirstPoint; |
81 | 99 |
private Point2D m_LastPoint; |
82 | 100 |
private VectorListener listener; |
... | ... | |
90 | 108 |
BufferedImage.TYPE_INT_ARGB); |
91 | 109 |
protected static Rectangle rect = new Rectangle(0, 0, 1, 1); |
92 | 110 |
|
111 |
|
|
112 |
class PointSnapper extends NearestPointSnapper { |
|
93 | 113 |
|
114 |
public Point2D getSnapPoint(Point2D queryPoint, IGeometry geomToSnap, |
|
115 |
double tolerance, Point2D lastPointEntered) { |
|
116 |
|
|
117 |
Point2D solution = null; |
|
118 |
double minDistance = tolerance; |
|
119 |
|
|
120 |
List<Point2D[]> pointsParts = |
|
121 |
ShapePointExtractor.extractPoints(geomToSnap); |
|
122 |
for(int i = 0; i < pointsParts.size(); i++){ |
|
123 |
Point2D[] points = pointsParts.get(i); |
|
124 |
for(int j = 0; j < points.length; j++){ |
|
125 |
Point2D point = points[j]; |
|
126 |
double dist = point.distance(queryPoint); |
|
127 |
if(dist <= minDistance){ |
|
128 |
solution = point; |
|
129 |
minDistance = dist; |
|
130 |
}//if |
|
131 |
}//for j |
|
132 |
}//for i |
|
133 |
return solution; |
|
134 |
} |
|
94 | 135 |
|
136 |
public String getToolTipText() { |
|
137 |
return PluginServices.getText(this, "nearest_point_for_point_layers"); |
|
138 |
} |
|
139 |
|
|
140 |
} |
|
141 |
|
|
95 | 142 |
|
96 | 143 |
/** |
97 | 144 |
* Crea un nuevo RectangleBehavior. |
98 | 145 |
* |
99 | 146 |
* @param zili listener. |
100 | 147 |
*/ |
101 |
public VectorBehavior(VectorListener zili) { |
|
148 |
public VectorBehavior(VectorListener zili, FLyrVect snappingLyr) {
|
|
102 | 149 |
listener = zili; |
103 |
|
|
150 |
this.snappingLyr = snappingLyr; |
|
104 | 151 |
Graphics2D g2 = img.createGraphics(); |
105 | 152 |
drawInsideRectangle(g2, g2.getTransform(), rect); |
106 | 153 |
rgb = img.getRGB(0, 0); |
... | ... | |
142 | 189 |
|
143 | 190 |
|
144 | 191 |
// Dibujamos el actual |
145 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
|
192 |
if ((m_FirstPoint != null) && (m_LastPoint != null) && !isZooming) {
|
|
146 | 193 |
GeneralPathX line = new GeneralPathX(); |
147 | 194 |
line.moveTo(m_FirstPoint.getX(), m_FirstPoint.getY()); |
148 | 195 |
line.lineTo(m_LastPoint.getX(), m_LastPoint.getY()); |
... | ... | |
158 | 205 |
* @param e MouseEvent |
159 | 206 |
*/ |
160 | 207 |
public void mousePressed(MouseEvent e) { |
161 |
// if (e.getButton() == MouseEvent.BUTTON1) { |
|
208 |
|
|
209 |
int modifiers = e.getModifiersEx(); |
|
210 |
int ctrlDownMask = modifiers & MouseEvent.CTRL_DOWN_MASK; |
|
211 |
if(ctrlDownMask == MouseEvent.CTRL_DOWN_MASK ){ |
|
212 |
isZooming = true; |
|
213 |
} |
|
162 | 214 |
m_FirstPoint = e.getPoint(); |
163 | 215 |
getMapControl().repaint(); |
164 |
// } |
|
165 |
if (listener.cancelDrawing()) { |
|
166 |
getMapControl().cancelDrawing(); |
|
167 |
getMapControl().repaint(); |
|
168 |
} |
|
216 |
|
|
217 |
if (listener.cancelDrawing()) {
|
|
218 |
getMapControl().cancelDrawing();
|
|
219 |
getMapControl().repaint();
|
|
220 |
}
|
|
169 | 221 |
} |
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
170 | 227 |
|
171 | 228 |
/** |
172 | 229 |
* Reimplementaci?n del m?todo mouseReleased de Behavior. |
... | ... | |
176 | 233 |
* @throws BehaviorException Excepci?n lanzada cuando el Behavior. |
177 | 234 |
*/ |
178 | 235 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
179 |
if (m_FirstPoint == null) return; |
|
236 |
if (m_FirstPoint == null) { |
|
237 |
isZooming = false; |
|
238 |
return; |
|
239 |
} |
|
180 | 240 |
Point2D p1; |
181 | 241 |
Point2D p2; |
182 | 242 |
Point pScreen = e.getPoint(); |
183 | 243 |
|
184 | 244 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
245 |
p1 = vp.toMapPoint(m_FirstPoint); |
|
246 |
|
|
247 |
|
|
248 |
//we snap to the nearest point of the adjusting layer |
|
249 |
int pixelTolerance = 8; |
|
250 |
double mapTolerance = vp.toMapDistance(pixelTolerance); |
|
251 |
|
|
252 |
double minDist = mapTolerance; |
|
253 |
Rectangle2D r = new Rectangle2D.Double(p1.getX() - mapTolerance / 2, |
|
254 |
p1.getY() - mapTolerance / 2, mapTolerance, mapTolerance); |
|
185 | 255 |
|
186 |
p1 = vp.toMapPoint(m_FirstPoint); |
|
256 |
if (snappingLyr.isVisible()){ |
|
257 |
try { |
|
258 |
IFeatureIterator iterator = snappingLyr.getSource().getFeatureIterator(r, null, null, false); |
|
259 |
SnappingVisitor snapVisitor = |
|
260 |
new SnappingVisitor(snapper, p1, mapTolerance, null); |
|
261 |
SnappingVisitor snapVisitor2 = |
|
262 |
new SnappingVisitor(pointSnapper, p1, mapTolerance, null); |
|
263 |
|
|
264 |
while(iterator.hasNext()){ |
|
265 |
IFeature feature = iterator.next(); |
|
266 |
IGeometry geo = feature.getGeometry(); |
|
267 |
snapVisitor.visitItem(geo); |
|
268 |
snapVisitor2.visitItem(geo); |
|
269 |
} |
|
270 |
Point2D theSnappedPoint = snapVisitor.getSnapPoint(); |
|
271 |
if(theSnappedPoint == null) |
|
272 |
theSnappedPoint = snapVisitor2.getSnapPoint(); |
|
273 |
|
|
274 |
if (theSnappedPoint != null) { |
|
275 |
p1.setLocation(theSnappedPoint); |
|
276 |
} |
|
277 |
} catch (ReadDriverException e1) { |
|
278 |
e1.printStackTrace(); |
|
279 |
throw new BehaviorException("Error de driver intentando aplicar snap", e1); |
|
280 |
} |
|
281 |
}//isVisible |
|
282 |
|
|
283 |
|
|
187 | 284 |
p2 = vp.toMapPoint(pScreen); |
285 |
// Rectangle2D.Double rectangle = new Rectangle2D.Double(); |
|
286 |
// rectangle.setFrameFromDiagonal(p1, p2); |
|
188 | 287 |
|
189 |
// if (e.getButton() == MouseEvent.BUTTON1) { |
|
190 |
// Fijamos el nuevo extent |
|
191 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
|
192 |
r.setFrameFromDiagonal(p1, p2); |
|
288 |
// MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e); |
|
289 |
MoveEvent event = new MoveEvent(p1, p2, e); |
|
290 |
listener.vector(event); |
|
193 | 291 |
|
194 |
MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e); |
|
195 |
listener.vector(event); |
|
196 |
// } |
|
197 | 292 |
|
198 | 293 |
m_FirstPoint = null; |
199 | 294 |
m_LastPoint = null; |
295 |
isZooming = false; |
|
200 | 296 |
} |
201 | 297 |
|
202 | 298 |
/** |
... | ... | |
229 | 325 |
|
230 | 326 |
} |
231 | 327 |
|
328 |
public FLyrVect getSnappingLyr() { |
|
329 |
return snappingLyr; |
|
330 |
} |
|
331 |
|
|
332 |
public void setSnappingLyr(FLyrVect snappingLyr) { |
|
333 |
this.snappingLyr = snappingLyr; |
|
334 |
} |
|
335 |
|
|
232 | 336 |
} |
233 | 337 |
|
Also available in: Unified diff