1 | package cadastre_fr;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.Cursor;
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.awt.event.MouseListener;
|
---|
9 | import java.awt.event.MouseMotionListener;
|
---|
10 | import java.util.ArrayList;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
16 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
17 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
18 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 |
|
---|
21 | public class WMSAdjustAction extends MapMode implements
|
---|
22 | MouseListener, MouseMotionListener{
|
---|
23 |
|
---|
24 | private static final long serialVersionUID = 1L;
|
---|
25 | private ArrayList<WMSLayer> modifiedLayers = new ArrayList<WMSLayer>();
|
---|
26 | WMSLayer selectedLayer;
|
---|
27 | private boolean rasterMoved;
|
---|
28 | private EastNorth prevEastNorth;
|
---|
29 | enum Mode { moveXY, moveZ, rotate}
|
---|
30 | private Mode mode = null;
|
---|
31 |
|
---|
32 | public WMSAdjustAction(MapFrame mapFrame) {
|
---|
33 | super(tr("Adjust WMS"), "adjustxywms",
|
---|
34 | tr("Adjust the position of the WMS layer (raster images only)"), mapFrame,
|
---|
35 | ImageProvider.getCursor("normal", "move"));
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public void enterMode() {
|
---|
39 | if (Main.map != null) {
|
---|
40 | Layer activeLayer = Main.map.mapView.getActiveLayer();
|
---|
41 | if (activeLayer instanceof WMSLayer && ((WMSLayer)activeLayer).isRaster()) {
|
---|
42 | super.enterMode();
|
---|
43 | Main.map.mapView.addMouseListener(this);
|
---|
44 | Main.map.mapView.addMouseMotionListener(this);
|
---|
45 | rasterMoved = false;
|
---|
46 | selectedLayer = (WMSLayer)activeLayer;
|
---|
47 | } else {
|
---|
48 | JOptionPane.showMessageDialog(Main.parent,tr("This mode works only if active layer is\n"
|
---|
49 | +"a cadastre \"plan image\" (raster image)"));
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override public void exitMode() {
|
---|
55 | super.exitMode();
|
---|
56 | Main.map.mapView.removeMouseListener(this);
|
---|
57 | Main.map.mapView.removeMouseMotionListener(this);
|
---|
58 | if (rasterMoved && CacheControl.cacheEnabled) {
|
---|
59 | int reply = JOptionPane.showConfirmDialog(null,
|
---|
60 | "Save the changes in cache ?",
|
---|
61 | "Update cache",
|
---|
62 | JOptionPane.YES_NO_OPTION);
|
---|
63 | if (reply == JOptionPane.OK_OPTION) {
|
---|
64 | saveModifiedLayers();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | modifiedLayers.clear();
|
---|
68 | selectedLayer = null;
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void mousePressed(MouseEvent e) {
|
---|
73 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
74 | return;
|
---|
75 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
76 | // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
---|
77 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
78 | if (shift)
|
---|
79 | mode = Mode.moveZ;
|
---|
80 | else if (ctrl)
|
---|
81 | mode = Mode.rotate;
|
---|
82 | else
|
---|
83 | mode = Mode.moveXY;
|
---|
84 | rasterMoved = true;
|
---|
85 | prevEastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
---|
86 | Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override public void mouseDragged(MouseEvent e) {
|
---|
90 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
---|
91 | if (mode == Mode.moveXY) {
|
---|
92 | displace(prevEastNorth, newEastNorth);
|
---|
93 | } else if (mode == Mode.moveZ) {
|
---|
94 | resize(newEastNorth);
|
---|
95 | } else if (mode == Mode.rotate) {
|
---|
96 | rotate(prevEastNorth, newEastNorth);
|
---|
97 | }
|
---|
98 | if (!modifiedLayers.contains(selectedLayer))
|
---|
99 | modifiedLayers.add(selectedLayer);
|
---|
100 | Main.map.mapView.repaint();
|
---|
101 | prevEastNorth = newEastNorth;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private void displace(EastNorth start, EastNorth end) {
|
---|
105 | selectedLayer.displace(end.east()-start.east(), end.north()-start.north());
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void resize(EastNorth newEastNorth) {
|
---|
109 | EastNorth center = selectedLayer.getRasterCenter();
|
---|
110 | double dPrev = prevEastNorth.distance(center.east(), center.north());
|
---|
111 | double dNew = newEastNorth.distance(center.east(), center.north());
|
---|
112 | selectedLayer.resize(center, dNew/dPrev);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void rotate(EastNorth start, EastNorth end) {
|
---|
116 | EastNorth pivot = selectedLayer.getRasterCenter();
|
---|
117 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
---|
118 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
---|
119 | double rotationAngle = endAngle - startAngle;
|
---|
120 | selectedLayer.rotate(pivot, rotationAngle);
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override public void mouseReleased(MouseEvent e) {
|
---|
124 | //Main.map.mapView.repaint();
|
---|
125 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
---|
126 | prevEastNorth = null;
|
---|
127 | mode = null;
|
---|
128 | }
|
---|
129 |
|
---|
130 | public void mouseEntered(MouseEvent e) {
|
---|
131 | }
|
---|
132 | public void mouseExited(MouseEvent e) {
|
---|
133 | }
|
---|
134 | public void mouseMoved(MouseEvent e) {
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override public void mouseClicked(MouseEvent e) {
|
---|
138 | }
|
---|
139 |
|
---|
140 | private void saveModifiedLayers() {
|
---|
141 | for (WMSLayer wmsLayer : modifiedLayers) {
|
---|
142 | wmsLayer.saveNewCache();
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|