1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.AWTEvent;
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.Toolkit;
|
---|
11 | import java.awt.event.AWTEventListener;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.KeyEvent;
|
---|
14 | import java.awt.event.MouseEvent;
|
---|
15 | import java.awt.event.MouseListener;
|
---|
16 | import java.awt.event.MouseMotionListener;
|
---|
17 | import java.beans.PropertyChangeEvent;
|
---|
18 | import java.beans.PropertyChangeListener;
|
---|
19 | import java.text.DecimalFormat;
|
---|
20 |
|
---|
21 | import javax.swing.JFormattedTextField;
|
---|
22 | import javax.swing.JLabel;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JTextField;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
28 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
29 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
---|
30 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
31 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
32 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
33 | import org.openstreetmap.josm.tools.GBC;
|
---|
34 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
35 |
|
---|
36 |
|
---|
37 | public class ImageryAdjustAction extends MapMode implements MouseListener, MouseMotionListener, AWTEventListener{
|
---|
38 | static ImageryOffsetDialog offsetDialog;
|
---|
39 | static Cursor cursor = ImageProvider.getCursor("normal", "move");
|
---|
40 |
|
---|
41 | double oldDx, oldDy;
|
---|
42 | boolean mouseDown;
|
---|
43 | EastNorth prevEastNorth;
|
---|
44 | private ImageryLayer layer;
|
---|
45 | private MapMode oldMapMode;
|
---|
46 |
|
---|
47 | public ImageryAdjustAction(ImageryLayer layer) {
|
---|
48 | super(tr("New offset"), "adjustimg",
|
---|
49 | tr("Adjust the position of this imagery layer"), Main.map,
|
---|
50 | cursor);
|
---|
51 | this.layer = layer;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void enterMode() {
|
---|
55 | super.enterMode();
|
---|
56 | if (layer == null)
|
---|
57 | return;
|
---|
58 | if (!layer.isVisible()) {
|
---|
59 | layer.setVisible(true);
|
---|
60 | }
|
---|
61 | Main.map.mapView.addMouseListener(this);
|
---|
62 | Main.map.mapView.addMouseMotionListener(this);
|
---|
63 | oldDx = layer.getDx();
|
---|
64 | oldDy = layer.getDy();
|
---|
65 | try {
|
---|
66 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
67 | } catch (SecurityException ex) {
|
---|
68 | }
|
---|
69 | offsetDialog = new ImageryOffsetDialog();
|
---|
70 | offsetDialog.setVisible(true);
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override public void exitMode() {
|
---|
74 | super.exitMode();
|
---|
75 | if (offsetDialog != null) {
|
---|
76 | layer.setOffset(oldDx, oldDy);
|
---|
77 | offsetDialog.setVisible(false);
|
---|
78 | offsetDialog = null;
|
---|
79 | }
|
---|
80 | try {
|
---|
81 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
82 | } catch (SecurityException ex) {
|
---|
83 | }
|
---|
84 | Main.map.mapView.removeMouseListener(this);
|
---|
85 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public void eventDispatched(AWTEvent event) {
|
---|
90 | if (!(event instanceof KeyEvent)) return;
|
---|
91 | if (event.getID() != KeyEvent.KEY_PRESSED) return;
|
---|
92 | if (layer == null) return;
|
---|
93 | KeyEvent kev = (KeyEvent)event;
|
---|
94 | double dx = 0, dy = 0;
|
---|
95 | switch (kev.getKeyCode()) {
|
---|
96 | case KeyEvent.VK_UP : dy = +1; break;
|
---|
97 | case KeyEvent.VK_DOWN : dy = -1; break;
|
---|
98 | case KeyEvent.VK_LEFT : dx = -1; break;
|
---|
99 | case KeyEvent.VK_RIGHT : dx = +1; break;
|
---|
100 | }
|
---|
101 | if (dx != 0 || dy != 0) {
|
---|
102 | double ppd = layer.getPPD();
|
---|
103 | layer.displace(dx / ppd, dy / ppd);
|
---|
104 | if (offsetDialog != null) {
|
---|
105 | offsetDialog.updateOffset();
|
---|
106 | }
|
---|
107 | kev.consume();
|
---|
108 | Main.map.repaint();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override public void mousePressed(MouseEvent e) {
|
---|
113 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
114 | return;
|
---|
115 |
|
---|
116 | if (layer.isVisible()) {
|
---|
117 | prevEastNorth=Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
---|
118 | Main.map.mapView.setCursor
|
---|
119 | (Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override public void mouseDragged(MouseEvent e) {
|
---|
124 | if (layer == null || prevEastNorth == null) return;
|
---|
125 | EastNorth eastNorth =
|
---|
126 | Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
---|
127 | double dx = layer.getDx()+eastNorth.east()-prevEastNorth.east();
|
---|
128 | double dy = layer.getDy()+eastNorth.north()-prevEastNorth.north();
|
---|
129 | layer.setOffset(dx, dy);
|
---|
130 | if (offsetDialog != null) {
|
---|
131 | offsetDialog.updateOffset();
|
---|
132 | }
|
---|
133 | Main.map.repaint();
|
---|
134 | prevEastNorth = eastNorth;
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override public void mouseReleased(MouseEvent e) {
|
---|
138 | Main.map.mapView.repaint();
|
---|
139 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
---|
140 | prevEastNorth = null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void actionPerformed(ActionEvent e) {
|
---|
145 | if (offsetDialog != null || layer == null || Main.map == null)
|
---|
146 | return;
|
---|
147 | oldMapMode = Main.map.mapMode;
|
---|
148 | super.actionPerformed(e);
|
---|
149 | }
|
---|
150 |
|
---|
151 | class ImageryOffsetDialog extends ExtendedDialog implements PropertyChangeListener {
|
---|
152 | public final JFormattedTextField easting = new JFormattedTextField(new DecimalFormat("0.00000E0"));
|
---|
153 | public final JFormattedTextField northing = new JFormattedTextField(new DecimalFormat("0.00000E0"));
|
---|
154 | JTextField tBookmarkName = new JTextField();
|
---|
155 | private boolean ignoreListener;
|
---|
156 | public ImageryOffsetDialog() {
|
---|
157 | super(Main.parent,
|
---|
158 | tr("Adjust imagery offset"),
|
---|
159 | new String[] { tr("OK"),tr("Cancel") },
|
---|
160 | false);
|
---|
161 | setButtonIcons(new String[] { "ok", "cancel" });
|
---|
162 | contentInsets = new Insets(10, 15, 5, 15);
|
---|
163 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
164 | pnl.add(new JMultilineLabel(tr("Use arrow keys or drag the imagery layer with mouse to adjust the imagery offset.\n" +
|
---|
165 | "You can also enter east and north offset in the {0} coordinates.\n" +
|
---|
166 | "If you want to save the offset as bookmark, enter the bookmark name below",Main.proj.toString())), GBC.eop());
|
---|
167 | pnl.add(new JLabel(tr("Easting") + ": "),GBC.std());
|
---|
168 | pnl.add(easting,GBC.std().fill(GBC.HORIZONTAL).insets(0, 0, 5, 0));
|
---|
169 | pnl.add(new JLabel(tr("Northing") + ": "),GBC.std());
|
---|
170 | pnl.add(northing,GBC.eol().fill(GBC.HORIZONTAL));
|
---|
171 | pnl.add(new JLabel(tr("Bookmark name: ")),GBC.eol().insets(0,5,0,0));
|
---|
172 | pnl.add(tBookmarkName,GBC.eol().fill(GBC.HORIZONTAL));
|
---|
173 | easting.setColumns(8);
|
---|
174 | northing.setColumns(8);
|
---|
175 | easting.setValue(layer.getDx());
|
---|
176 | northing.setValue(layer.getDy());
|
---|
177 | easting.addPropertyChangeListener("value",this);
|
---|
178 | northing.addPropertyChangeListener("value",this);
|
---|
179 | setContent(pnl);
|
---|
180 | setupDialog();
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
185 | if (ignoreListener) return;
|
---|
186 | layer.setOffset(((Number)easting.getValue()).doubleValue(), ((Number)northing.getValue()).doubleValue());
|
---|
187 | Main.map.repaint();
|
---|
188 | }
|
---|
189 |
|
---|
190 | public void updateOffset() {
|
---|
191 | ignoreListener = true;
|
---|
192 | easting.setValue(layer.getDx());
|
---|
193 | northing.setValue(layer.getDy());
|
---|
194 | ignoreListener = false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | private boolean confirmOverwriteBookmark() {
|
---|
198 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
199 | Main.parent,
|
---|
200 | tr("Overwrite"),
|
---|
201 | new String[] {tr("Overwrite"), tr("Cancel")}
|
---|
202 | ) {{
|
---|
203 | contentInsets = new Insets(10, 15, 10, 15);
|
---|
204 | }};
|
---|
205 | dialog.setContent(tr("Offset bookmark already exists. Overwrite?"));
|
---|
206 | dialog.setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
---|
207 | dialog.setupDialog();
|
---|
208 | dialog.setVisible(true);
|
---|
209 | return dialog.getValue() == 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | @Override
|
---|
213 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
214 | if (buttonIndex != 1 && tBookmarkName.getText() != null && !"".equals(tBookmarkName.getText()) &&
|
---|
215 | OffsetBookmark.getBookmarkByName(layer, tBookmarkName.getText()) != null) {
|
---|
216 | if (!confirmOverwriteBookmark()) return;
|
---|
217 | }
|
---|
218 | super.buttonAction(buttonIndex, evt);
|
---|
219 | offsetDialog = null;
|
---|
220 | if (buttonIndex == 1) {
|
---|
221 | layer.setOffset(oldDx, oldDy);
|
---|
222 | } else if (tBookmarkName.getText() != null && !"".equals(tBookmarkName.getText())) {
|
---|
223 | OffsetBookmark.bookmarkOffset(tBookmarkName.getText(), layer);
|
---|
224 | }
|
---|
225 | Main.main.menu.imageryMenu.refreshOffsetMenu();
|
---|
226 | if (Main.map == null) return;
|
---|
227 | if (oldMapMode != null) {
|
---|
228 | Main.map.selectMapMode(oldMapMode);
|
---|
229 | oldMapMode = null;
|
---|
230 | } else {
|
---|
231 | Main.map.selectSelectTool(false);
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|