1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others
|
---|
2 | package cadastre_fr;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.Graphics2D;
|
---|
9 | import java.awt.Toolkit;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.awt.event.MouseListener;
|
---|
13 | import java.awt.event.MouseMotionListener;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
19 | import org.openstreetmap.josm.gui.MapView;
|
---|
20 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
21 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
22 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
23 |
|
---|
24 | public class WMSAdjustAction extends MapMode implements
|
---|
25 | MouseListener, MouseMotionListener{
|
---|
26 |
|
---|
27 | private static final long serialVersionUID = 1L;
|
---|
28 | private WMSLayer modifiedLayer = null;
|
---|
29 | private boolean rasterMoved;
|
---|
30 | private EastNorth prevEastNorth;
|
---|
31 | enum Mode { moveXY, moveZ, rotate}
|
---|
32 | private static Mode mode = null;
|
---|
33 | private static EastNorth[] croppedRaster = new EastNorth[5];;
|
---|
34 |
|
---|
35 | public WMSAdjustAction(MapFrame mapFrame) {
|
---|
36 | super(tr("Adjust WMS"), "adjustxywms",
|
---|
37 | tr("Adjust the position of the WMS layer (saved for raster images only)"), mapFrame,
|
---|
38 | ImageProvider.getCursor("normal", "move"));
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override public void enterMode() {
|
---|
42 | if (Main.map != null) {
|
---|
43 | if (Main.map.mapView.getActiveLayer() instanceof WMSLayer) {
|
---|
44 | modifiedLayer = (WMSLayer)Main.map.mapView.getActiveLayer();
|
---|
45 | super.enterMode();
|
---|
46 | Main.map.mapView.addMouseListener(this);
|
---|
47 | Main.map.mapView.addMouseMotionListener(this);
|
---|
48 | rasterMoved = false;
|
---|
49 | modifiedLayer.adjustModeEnabled = true;
|
---|
50 | } else {
|
---|
51 | // JOptionPane.showMessageDialog(Main.parent,tr("This mode works only if active layer is\n"
|
---|
52 | // +"a cadastre layer"));
|
---|
53 | exitMode();
|
---|
54 | Main.map.selectMapMode((MapMode)Main.map.getDefaultButtonAction());
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override public void exitMode() {
|
---|
60 | super.exitMode();
|
---|
61 | Main.map.mapView.removeMouseListener(this);
|
---|
62 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
63 | if (rasterMoved && CacheControl.cacheEnabled && modifiedLayer.isRaster()) {
|
---|
64 | int reply = JOptionPane.showConfirmDialog(null,
|
---|
65 | "Save the changes in cache ?",
|
---|
66 | "Update cache",
|
---|
67 | JOptionPane.YES_NO_OPTION);
|
---|
68 | if (reply == JOptionPane.OK_OPTION) {
|
---|
69 | saveModifiedLayers();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | rasterMoved = false;
|
---|
73 | if (modifiedLayer != null) {
|
---|
74 | modifiedLayer.adjustModeEnabled = false;
|
---|
75 | modifiedLayer = null;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void mousePressed(MouseEvent e) {
|
---|
81 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
82 | return;
|
---|
83 | requestFocusInMapView();
|
---|
84 | boolean ctrl = (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
|
---|
85 | // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
---|
86 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
87 | if (shift && !ctrl && modifiedLayer.isRaster())
|
---|
88 | mode = Mode.moveZ;
|
---|
89 | else if (shift && ctrl && modifiedLayer.isRaster())
|
---|
90 | mode = Mode.rotate;
|
---|
91 | else
|
---|
92 | mode = Mode.moveXY;
|
---|
93 | rasterMoved = true;
|
---|
94 | prevEastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
---|
95 | Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override public void mouseDragged(MouseEvent e) {
|
---|
99 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
---|
100 | if (mode == Mode.rotate) {
|
---|
101 | rotateFrameOnly(prevEastNorth, newEastNorth);
|
---|
102 | } else {
|
---|
103 | if (mode == Mode.moveXY) {
|
---|
104 | displace(prevEastNorth, newEastNorth);
|
---|
105 | } else if (mode == Mode.moveZ) {
|
---|
106 | resize(newEastNorth);
|
---|
107 | }
|
---|
108 | prevEastNorth = newEastNorth;
|
---|
109 | }
|
---|
110 | Main.map.mapView.repaint();
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static void paintAdjustFrames(Graphics2D g, final MapView mv) {
|
---|
114 | if (mode == Mode.rotate && croppedRaster != null) {
|
---|
115 | g.setColor(Color.red);
|
---|
116 | for (int i=0; i<4; i++)
|
---|
117 | g.drawLine(mv.getPoint(croppedRaster[i]).x,
|
---|
118 | mv.getPoint(croppedRaster[i]).y,
|
---|
119 | mv.getPoint(croppedRaster[i+1]).x,
|
---|
120 | mv.getPoint(croppedRaster[i+1]).y);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | private void displace(EastNorth start, EastNorth end) {
|
---|
125 | modifiedLayer.displace(end.east()-start.east(), end.north()-start.north());
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void resize(EastNorth newEastNorth) {
|
---|
129 | EastNorth center = modifiedLayer.getRasterCenter();
|
---|
130 | double dPrev = prevEastNorth.distance(center.east(), center.north());
|
---|
131 | double dNew = newEastNorth.distance(center.east(), center.north());
|
---|
132 | modifiedLayer.resize(center, dNew/dPrev);
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void rotate(EastNorth start, EastNorth end) {
|
---|
136 | EastNorth pivot = modifiedLayer.getRasterCenter();
|
---|
137 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
---|
138 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
---|
139 | double rotationAngle = endAngle - startAngle;
|
---|
140 | modifiedLayer.rotate(pivot, rotationAngle);
|
---|
141 | }
|
---|
142 |
|
---|
143 | private void rotateFrameOnly(EastNorth start, EastNorth end) {
|
---|
144 | if (start != null && end != null) {
|
---|
145 | EastNorth pivot = modifiedLayer.getRasterCenter();
|
---|
146 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
---|
147 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
---|
148 | double rotationAngle = endAngle - startAngle;
|
---|
149 | if (modifiedLayer.getImage(0).orgCroppedRaster != null) {
|
---|
150 | for (int i=0; i<4; i++) {
|
---|
151 | croppedRaster[i] = modifiedLayer.getImage(0).orgCroppedRaster[i].rotate(pivot, rotationAngle);
|
---|
152 | }
|
---|
153 | croppedRaster[4] = croppedRaster[0];
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override public void mouseReleased(MouseEvent e) {
|
---|
159 | //Main.map.mapView.repaint();
|
---|
160 | if (mode == Mode.rotate) {
|
---|
161 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
---|
162 | rotate(prevEastNorth, newEastNorth);
|
---|
163 | Main.map.mapView.repaint();
|
---|
164 | }
|
---|
165 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
---|
166 | prevEastNorth = null;
|
---|
167 | mode = null;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void mouseEntered(MouseEvent e) {
|
---|
171 | }
|
---|
172 | public void mouseExited(MouseEvent e) {
|
---|
173 | }
|
---|
174 | public void mouseMoved(MouseEvent e) {
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override public void mouseClicked(MouseEvent e) {
|
---|
178 | }
|
---|
179 |
|
---|
180 | private void saveModifiedLayers() {
|
---|
181 | modifiedLayer.grabThread.saveNewCache();
|
---|
182 | }
|
---|
183 | }
|
---|