01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.validator;
16:
17: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
18: import net.refractions.udig.tool.edit.internal.Messages;
19: import net.refractions.udig.tools.edit.EditToolHandler;
20: import net.refractions.udig.tools.edit.EventType;
21: import net.refractions.udig.tools.edit.behaviour.IEditValidator;
22: import net.refractions.udig.tools.edit.support.EditUtils;
23: import net.refractions.udig.tools.edit.support.Point;
24: import net.refractions.udig.tools.edit.support.PrimitiveShape;
25:
26: /**
27: * Returns true if:
28: * <ul>
29: * <li>The new vertex does not cause a self intersect</li>
30: * <li>The new vertex does not the hole to intersect with another hole</li>
31: * <li>The new event is within the shell</li>
32: * </ul>
33: * @author Jesse
34: * @since 1.1.0
35: */
36: public class ValidHoleValidator implements IEditValidator {
37:
38: public String isValid(EditToolHandler handler, MapMouseEvent event,
39: EventType type) {
40: PrimitiveShape shell = handler.getCurrentGeom().getShell();
41: PrimitiveShape hole = handler.getCurrentShape();
42:
43: assert hole != shell;
44:
45: // check the new edge (that will be created by event) to see if it intersect with the
46: // rest of the hole
47:
48: Point newPoint = Point.valueOf(event.x, event.y);
49: int lastPointIndex = hole.getNumPoints() - 1;
50: if (hole.getNumPoints() > 2
51: && EditUtils.instance.intersection(hole
52: .getPoint(lastPointIndex), newPoint, hole, 0,
53: lastPointIndex)) {
54: return Messages.ValidHoleValidator_selfIntersection;
55: }
56: if (!shell.contains(newPoint, true))
57: return Messages.ValidHoleValidator_outsideShell;
58:
59: for (PrimitiveShape hole2 : shell.getEditGeom().getHoles()) {
60: if (hole != hole2 && hole2.contains(newPoint, true))
61: return Messages.ValidHoleValidator_holeOverlap;
62: }
63:
64: return null;
65: }
66:
67: }
|