1 |
alfonx |
651 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
|
|
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
|
|
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
|
* Stefan A. Krüger - additional utility classes |
29 |
|
|
******************************************************************************/ |
30 |
|
|
package skrueger.geotools; |
31 |
|
|
|
32 |
|
|
import java.awt.Cursor; |
33 |
|
|
import java.awt.Point; |
34 |
|
|
import java.awt.Rectangle; |
35 |
|
|
import java.awt.event.MouseEvent; |
36 |
|
|
import java.awt.event.MouseWheelEvent; |
37 |
|
|
|
38 |
|
|
import org.opengis.geometry.DirectPosition; |
39 |
|
|
|
40 |
|
|
import schmitzm.swing.SwingUtil; |
41 |
|
|
|
42 |
|
|
import com.vividsolutions.jts.geom.Envelope; |
43 |
|
|
|
44 |
|
|
public class XMapPaneAction_Pan implements XMapPaneAction { |
45 |
alfonx |
653 |
|
46 |
|
|
public static enum Direction { |
47 |
|
|
UP, DOWN, RIGHT, LEFT |
48 |
|
|
} |
49 |
|
|
|
50 |
alfonx |
651 |
/** |
51 |
alfonx |
653 |
* If this action is triggered by keyboard, defines the amount of panning in |
52 |
|
|
* screen pixels |
53 |
|
|
**/ |
54 |
|
|
private static final int KEYBOARD_PAN_LENGTH = 15; |
55 |
|
|
|
56 |
|
|
/** |
57 |
alfonx |
651 |
* This variable can be used to backup the active cursor of the mapPane, if |
58 |
|
|
* the actions changes the cursor during dragging |
59 |
|
|
*/ |
60 |
|
|
public Cursor backupCursor = null; |
61 |
alfonx |
653 |
|
62 |
alfonx |
651 |
/** |
63 |
alfonx |
653 |
* Performs a pan action. During panning, the displacement is stored in |
64 |
|
|
* {@link #imageOrigin} object. Calling {@link #performPan()} will reset the |
65 |
|
|
* offset and call {@link #setMapArea(Envelope)}. |
66 |
alfonx |
651 |
*/ |
67 |
|
|
@Override |
68 |
|
|
public void performDragged(XMapPane mapPane, MouseEvent ev, |
69 |
|
|
Point dragStartPos, Point dragLastPos, DirectPosition startCoord, |
70 |
|
|
DirectPosition endCoord) { |
71 |
|
|
|
72 |
alfonx |
653 |
Point translation = mapPane.getImageOrigin(); |
73 |
alfonx |
651 |
|
74 |
alfonx |
653 |
doPan(mapPane, translation); |
75 |
|
|
} |
76 |
alfonx |
651 |
|
77 |
alfonx |
653 |
private void doPan(XMapPane mapPane, Point translation) { |
78 |
|
|
|
79 |
|
|
final Rectangle winBounds = mapPane.getVisibleRect(); |
80 |
|
|
winBounds.translate(-translation.x, -translation.y); |
81 |
alfonx |
651 |
final Envelope newMapArea = mapPane.tranformWindowToGeo(winBounds.x, |
82 |
|
|
winBounds.y, winBounds.x + winBounds.width, winBounds.y |
83 |
|
|
+ winBounds.height); |
84 |
|
|
|
85 |
alfonx |
653 |
translation.x = 0; |
86 |
|
|
translation.y = 0; |
87 |
alfonx |
651 |
|
88 |
|
|
if (!mapPane.setMapArea(newMapArea)) { |
89 |
|
|
/** |
90 |
|
|
* If setMapArea returns true, the finalImage is updated anyways. |
91 |
|
|
* This if-case exists to ensure that we repaint a correct image |
92 |
|
|
* even if the new panning area has been denied. |
93 |
|
|
*/ |
94 |
|
|
mapPane.updateFinalImage(); |
95 |
|
|
mapPane.repaint(); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
if (mapPane.getCursor() == SwingUtil.PANNING_CURSOR) { |
99 |
|
|
mapPane.setCursor(backupCursor); |
100 |
|
|
} |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
/** |
104 |
|
|
* TODO: Stop dragging if the expected map area would not be valid. |
105 |
|
|
*/ |
106 |
|
|
@Override |
107 |
|
|
public void performDragging(XMapPane mapPane, MouseEvent ev, |
108 |
|
|
Point dragStartPos, Point dragLastPos, DirectPosition startCoord, |
109 |
|
|
DirectPosition endCoord) { |
110 |
|
|
|
111 |
|
|
if (mapPane.getCursor() != SwingUtil.PANNING_CURSOR) { |
112 |
|
|
backupCursor = mapPane.getCursor(); |
113 |
|
|
mapPane.setCursor(SwingUtil.PANNING_CURSOR); |
114 |
|
|
} |
115 |
|
|
|
116 |
alfonx |
653 |
// Variables for the translation in screen pixels |
117 |
alfonx |
651 |
int dX; |
118 |
|
|
int dY; |
119 |
|
|
|
120 |
|
|
if (dragLastPos == null) { |
121 |
|
|
dX = ev.getPoint().x - dragStartPos.x; |
122 |
|
|
dY = ev.getPoint().y - dragStartPos.y; |
123 |
|
|
} else { |
124 |
|
|
dX = ev.getPoint().x - dragLastPos.x; |
125 |
|
|
dY = ev.getPoint().y - dragLastPos.y; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
if (dX != 0 && dY != 0) |
129 |
|
|
mapPane.pan(dX, dY); |
130 |
|
|
|
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
@Override |
134 |
|
|
public void performWheel(XMapPane mapPane, MouseWheelEvent ev, |
135 |
|
|
DirectPosition coord) { |
136 |
|
|
} |
137 |
alfonx |
653 |
|
138 |
alfonx |
651 |
@Override |
139 |
|
|
public void performClick(XMapPane mapPane, MouseEvent ev, |
140 |
|
|
DirectPosition coord) { |
141 |
|
|
} |
142 |
|
|
|
143 |
alfonx |
653 |
@Override |
144 |
|
|
public void performKeyboard(XMapPane mapPane, Object param) { |
145 |
|
|
if (param == null) |
146 |
|
|
return; |
147 |
alfonx |
651 |
|
148 |
alfonx |
653 |
if (!(param instanceof Direction)) { |
149 |
|
|
throw new IllegalArgumentException("param = " + param |
150 |
|
|
+ " is of expected type XMapPaneAction_Pan.Direction"); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
// Variables for the translation in screen pixels |
154 |
|
|
int dX = 0; |
155 |
|
|
int dY = 0; |
156 |
|
|
|
157 |
|
|
switch ((Direction) param) { |
158 |
|
|
case DOWN: |
159 |
|
|
dY = KEYBOARD_PAN_LENGTH; |
160 |
|
|
break; |
161 |
|
|
case UP: |
162 |
|
|
dY = -KEYBOARD_PAN_LENGTH; |
163 |
|
|
break; |
164 |
|
|
case LEFT: |
165 |
|
|
dX = -KEYBOARD_PAN_LENGTH; |
166 |
|
|
break; |
167 |
|
|
case RIGHT: |
168 |
|
|
dX = KEYBOARD_PAN_LENGTH; |
169 |
|
|
break; |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
doPan(mapPane, new Point(dX, dY)); |
173 |
|
|
} |
174 |
|
|
|
175 |
alfonx |
651 |
} |