1 |
alfonx |
639 |
package skrueger.geotools; |
2 |
|
|
|
3 |
|
|
import java.awt.Point; |
4 |
alfonx |
644 |
import java.awt.Rectangle; |
5 |
alfonx |
639 |
import java.awt.event.MouseEvent; |
6 |
|
|
import java.awt.event.MouseWheelEvent; |
7 |
|
|
|
8 |
|
|
import org.opengis.geometry.DirectPosition; |
9 |
|
|
|
10 |
alfonx |
644 |
import com.vividsolutions.jts.geom.Coordinate; |
11 |
|
|
import com.vividsolutions.jts.geom.Envelope; |
12 |
|
|
|
13 |
alfonx |
646 |
public abstract class XMapPaneAction_Zoom implements XMapPaneAction { |
14 |
alfonx |
644 |
|
15 |
alfonx |
648 |
public static class In extends XMapPaneAction_Zoom { |
16 |
alfonx |
646 |
|
17 |
alfonx |
648 |
@Override |
18 |
|
|
public void performClick(XMapPane mapPane, MouseEvent ev, |
19 |
|
|
DirectPosition coord) { |
20 |
alfonx |
644 |
|
21 |
alfonx |
648 |
mapPane.zoomTo(ev.getPoint(), 1 / 2.); |
22 |
|
|
} |
23 |
alfonx |
639 |
|
24 |
alfonx |
648 |
@Override |
25 |
|
|
public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt, |
26 |
|
|
DirectPosition coord) { |
27 |
alfonx |
639 |
|
28 |
alfonx |
648 |
final int units = wheelEvt.getUnitsToScroll(); |
29 |
|
|
if (units > 0) |
30 |
|
|
mapPane.zoomTo(wheelEvt.getPoint(), 1. + .11 * units); |
31 |
|
|
else |
32 |
|
|
mapPane.zoomTo(wheelEvt.getPoint(), 2. / -units); |
33 |
|
|
} |
34 |
alfonx |
639 |
|
35 |
alfonx |
648 |
@Override |
36 |
|
|
public void performDragging(XMapPane mapPane, MouseEvent ev, |
37 |
|
|
Point dragStartPos, Point dragLastPos, |
38 |
|
|
DirectPosition startCoord, DirectPosition endCoord) { |
39 |
alfonx |
644 |
|
40 |
alfonx |
648 |
if (dragLastPos != null) |
41 |
|
|
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
42 |
|
|
dragLastPos); |
43 |
alfonx |
644 |
|
44 |
alfonx |
648 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev |
45 |
|
|
.getPoint()); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
@Override |
49 |
|
|
public void performDragged(XMapPane mapPane, MouseEvent ev, |
50 |
|
|
Point dragStartPos, Point dragLastPos, |
51 |
|
|
DirectPosition startCoord, DirectPosition endCoord) { |
52 |
|
|
|
53 |
|
|
if (dragLastPos != null) |
54 |
|
|
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
55 |
|
|
dragLastPos); |
56 |
|
|
|
57 |
|
|
// If this is similar to a click, let mouseClicked handle it! |
58 |
|
|
if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev |
59 |
|
|
.getPoint().y |
60 |
|
|
- dragStartPos.y)) < 160) { |
61 |
|
|
// performClick(mapPane, ev, coord) |
62 |
|
|
return; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
final Rectangle bounds = mapPane.getBounds(); |
66 |
|
|
|
67 |
|
|
Envelope mapArea = mapPane.getMapArea(); |
68 |
|
|
|
69 |
|
|
// Replace with transform and translate |
70 |
|
|
final double mapWidth = mapArea.getWidth(); |
71 |
|
|
final double mapHeight = mapArea.getHeight(); |
72 |
|
|
|
73 |
|
|
final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width) |
74 |
|
|
+ mapArea.getMinX(); |
75 |
|
|
final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height) |
76 |
|
|
+ mapArea.getMinY(); |
77 |
|
|
final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width) |
78 |
|
|
+ mapArea.getMinX(); |
79 |
|
|
final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height) |
80 |
|
|
+ mapArea.getMinY(); |
81 |
|
|
|
82 |
|
|
final double left = Math.min(startX, endX); |
83 |
|
|
final double right = Math.max(startX, endX); |
84 |
|
|
final double bottom = Math.min(startY, endY); |
85 |
|
|
final double top = Math.max(startY, endY); |
86 |
|
|
final Coordinate ll = new Coordinate(left, bottom); |
87 |
|
|
final Coordinate ur = new Coordinate(right, top); |
88 |
|
|
|
89 |
|
|
mapPane.setMapArea(new Envelope(ll, ur)); |
90 |
|
|
|
91 |
|
|
} |
92 |
alfonx |
644 |
} |
93 |
|
|
|
94 |
alfonx |
648 |
public static class Out extends XMapPaneAction_Zoom { |
95 |
alfonx |
639 |
|
96 |
alfonx |
648 |
@Override |
97 |
|
|
public void performClick(XMapPane mapPane, MouseEvent ev, |
98 |
|
|
DirectPosition coord) { |
99 |
alfonx |
644 |
|
100 |
alfonx |
648 |
mapPane.zoomTo(ev.getPoint(), 2.); |
101 |
alfonx |
644 |
} |
102 |
|
|
|
103 |
alfonx |
648 |
@Override |
104 |
|
|
public void performWheel(XMapPane mapPane, MouseWheelEvent wheelEvt, |
105 |
|
|
DirectPosition coord) { |
106 |
alfonx |
644 |
|
107 |
alfonx |
648 |
final int units = wheelEvt.getUnitsToScroll(); |
108 |
|
|
if (units <= 0) |
109 |
|
|
mapPane.zoomTo(wheelEvt.getPoint(), 1. + .11 * units); |
110 |
|
|
else |
111 |
|
|
mapPane.zoomTo(wheelEvt.getPoint(), 2. / -units); |
112 |
|
|
} |
113 |
alfonx |
644 |
|
114 |
alfonx |
648 |
@Override |
115 |
|
|
public void performDragging(XMapPane mapPane, MouseEvent ev, |
116 |
|
|
Point dragStartPos, Point dragLastPos, |
117 |
|
|
DirectPosition startCoord, DirectPosition endCoord) { |
118 |
alfonx |
644 |
|
119 |
alfonx |
648 |
if (dragLastPos != null) |
120 |
|
|
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
121 |
|
|
dragLastPos); |
122 |
alfonx |
644 |
|
123 |
alfonx |
648 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev |
124 |
|
|
.getPoint()); |
125 |
|
|
} |
126 |
alfonx |
644 |
|
127 |
alfonx |
648 |
@Override |
128 |
|
|
public void performDragged(XMapPane mapPane, MouseEvent ev, |
129 |
|
|
Point dragStartPos, Point dragLastPos, |
130 |
|
|
DirectPosition startCoord, DirectPosition endCoord) { |
131 |
alfonx |
644 |
|
132 |
alfonx |
648 |
if (dragLastPos != null) |
133 |
|
|
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
134 |
|
|
dragLastPos); |
135 |
alfonx |
639 |
|
136 |
alfonx |
648 |
// If this is similar to a click, let mouseClicked handle it! |
137 |
|
|
if ((Math.abs(dragStartPos.x - ev.getPoint().x) * Math.abs(ev |
138 |
|
|
.getPoint().y |
139 |
|
|
- dragStartPos.y)) < 160) { |
140 |
|
|
// performClick(mapPane, ev, coord) |
141 |
|
|
return; |
142 |
|
|
} |
143 |
alfonx |
646 |
|
144 |
alfonx |
648 |
final Rectangle bounds = mapPane.getBounds(); |
145 |
|
|
|
146 |
|
|
Envelope mapArea = mapPane.getMapArea(); |
147 |
|
|
|
148 |
|
|
// Replace with transform and translate |
149 |
|
|
final double mapWidth = mapArea.getWidth(); |
150 |
|
|
final double mapHeight = mapArea.getHeight(); |
151 |
|
|
|
152 |
|
|
final double startX = ((dragStartPos.x * mapWidth) / (double) bounds.width) |
153 |
|
|
+ mapArea.getMinX(); |
154 |
|
|
final double startY = (((bounds.getHeight() - dragStartPos.y) * mapHeight) / (double) bounds.height) |
155 |
|
|
+ mapArea.getMinY(); |
156 |
|
|
final double endX = ((ev.getPoint().x * mapWidth) / (double) bounds.width) |
157 |
|
|
+ mapArea.getMinX(); |
158 |
|
|
final double endY = (((bounds.getHeight() - ev.getPoint().y) * mapHeight) / (double) bounds.height) |
159 |
|
|
+ mapArea.getMinY(); |
160 |
|
|
|
161 |
|
|
// make the dragged rectangle in screen coords the new map size? |
162 |
|
|
final double left = Math.min(startX, endX); |
163 |
|
|
final double right = Math.max(startX, endX); |
164 |
|
|
final double bottom = Math.min(startY, endY); |
165 |
|
|
final double top = Math.max(startY, endY); |
166 |
|
|
final double nWidth = (mapWidth * mapWidth) / (right - left); |
167 |
|
|
final double nHeight = (mapHeight * mapHeight) / (top - bottom); |
168 |
|
|
final double deltaX1 = left - mapArea.getMinX(); |
169 |
|
|
final double nDeltaX1 = (deltaX1 * nWidth) / mapWidth; |
170 |
|
|
final double deltaY1 = bottom - mapArea.getMinY(); |
171 |
|
|
final double nDeltaY1 = (deltaY1 * nHeight) / mapHeight; |
172 |
|
|
final Coordinate ll = new Coordinate(mapArea.getMinX() - nDeltaX1, |
173 |
|
|
mapArea.getMinY() - nDeltaY1); |
174 |
|
|
final double deltaX2 = mapArea.getMaxX() - right; |
175 |
|
|
final double nDeltaX2 = (deltaX2 * nWidth) / mapWidth; |
176 |
|
|
final double deltaY2 = mapArea.getMaxY() - top; |
177 |
|
|
final double nDeltaY2 = (deltaY2 * nHeight) / mapHeight; |
178 |
|
|
final Coordinate ur = new Coordinate(mapArea.getMaxX() + nDeltaX2, |
179 |
|
|
mapArea.getMaxY() + nDeltaY2); |
180 |
|
|
|
181 |
|
|
mapPane.setMapArea(new Envelope(ll, ur)); |
182 |
|
|
|
183 |
|
|
} |
184 |
|
|
} |
185 |
alfonx |
646 |
} |