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.enablement;
16:
17: import java.io.IOException;
18: import java.util.Map;
19: import java.util.WeakHashMap;
20:
21: import net.refractions.udig.project.ILayer;
22: import net.refractions.udig.project.command.UndoableMapCommand;
23: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
24: import net.refractions.udig.tools.edit.EditPlugin;
25: import net.refractions.udig.tools.edit.EditState;
26: import net.refractions.udig.tools.edit.EditToolHandler;
27: import net.refractions.udig.tools.edit.EnablementBehaviour;
28: import net.refractions.udig.tools.edit.EventBehaviour;
29: import net.refractions.udig.tools.edit.EventType;
30:
31: import org.opengis.referencing.operation.MathTransform;
32:
33: import com.vividsolutions.jts.geom.Coordinate;
34: import com.vividsolutions.jts.geom.Envelope;
35:
36: /**
37: * Sets the EditState to illegal if the Mouse moves into an area of the map where the layer is no
38: * longer valid as according to its CRS. In addition puts a warning on the StatusLine.
39: *
40: * @author Jesse
41: * @since 1.1.0
42: */
43: public class WithinLegalLayerBoundsBehaviour implements
44: EnablementBehaviour {
45:
46: Map<ILayer, Envelope> cache = new WeakHashMap<ILayer, Envelope>();
47: private EditState previousState = EditState.NONE;
48:
49: private boolean canTransformTo(ILayer selectedLayer,
50: Coordinate world) {
51: MathTransform t;
52: try {
53: t = selectedLayer.mapToLayerTransform();
54: } catch (IOException e1) {
55: throw (RuntimeException) new RuntimeException()
56: .initCause(e1);
57: }
58: try {
59: double[] dest = new double[2];
60: t.transform(new double[] { world.x, world.y }, 0, dest, 0,
61: 1);
62: if (Double.isNaN(dest[0]) || Double.isNaN(dest[1])
63: || Double.isInfinite(dest[0])
64: || Double.isInfinite(dest[1]))
65: return false;
66: return true;
67: } catch (Throwable throwable) {
68: return false;
69: }
70: }
71:
72: public String isEnabled(EditToolHandler handler, MapMouseEvent e,
73: EventType eventType) {
74: if (eventType != EventType.MOVED
75: && eventType != EventType.ENTERED)
76: return null;
77: ILayer editLayer = handler.getEditLayer();
78: Envelope env = cache.get(editLayer);
79:
80: Coordinate world = handler.getContext().pixelToWorld(e.x, e.y);
81: if (env != null && env.contains(world)) {
82: } else {
83: if (canTransformTo(editLayer, world)) {
84: // ok its at least somewhat legal, we can transform to it so lets expand the legal
85: // envelope.
86: if (env == null) {
87: env = new Envelope();
88: }
89: env.expandToInclude(world);
90: } else
91: return "Cannot edit. Either in a bad projection for editing layer or out of the bounds for the layer";
92: }
93: return null;
94:
95: }
96:
97: }
|