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