| 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 | GeorefImage selectedImage;
|
|---|
| 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"), mapFrame,
|
|---|
| 36 | ImageProvider.getCursor("normal", "move"));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | @Override public void enterMode() {
|
|---|
| 40 | super.enterMode();
|
|---|
| 41 | Main.map.mapView.addMouseListener(this);
|
|---|
| 42 | Main.map.mapView.addMouseMotionListener(this);
|
|---|
| 43 | rasterMoved = false;
|
|---|
| 44 | /*/ FOR TEST
|
|---|
| 45 | for (Layer layer : Main.map.mapView.getAllLayers()) {
|
|---|
| 46 | if (layer.visible && layer instanceof WMSLayer) {
|
|---|
| 47 | WMSLayer wmsLayer = (WMSLayer)layer;
|
|---|
| 48 | wmsLayer.rotate(Math.PI/4);
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | Main.map.mapView.repaint();*/
|
|---|
| 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 | }
|
|---|
| 69 |
|
|---|
| 70 | @Override
|
|---|
| 71 | public void mousePressed(MouseEvent e) {
|
|---|
| 72 | if (e.getButton() != MouseEvent.BUTTON1)
|
|---|
| 73 | return;
|
|---|
| 74 | boolean ctrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
|---|
| 75 | // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
|---|
| 76 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
|---|
| 77 | if (shift)
|
|---|
| 78 | mode = Mode.moveZ;
|
|---|
| 79 | else if (ctrl)
|
|---|
| 80 | mode = Mode.rotate;
|
|---|
| 81 | else
|
|---|
| 82 | mode = Mode.moveXY;
|
|---|
| 83 | for (Layer layer : Main.map.mapView.getAllLayers()) {
|
|---|
| 84 | if (layer.visible && layer instanceof WMSLayer) {
|
|---|
| 85 | prevEastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
|---|
| 86 | selectedLayer = ((WMSLayer) layer);
|
|---|
| 87 | selectedImage = selectedLayer.findImage(prevEastNorth);
|
|---|
| 88 | if (selectedImage != null) {
|
|---|
| 89 | Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | @Override public void mouseDragged(MouseEvent e) {
|
|---|
| 96 | if(selectedImage != null && (mode == Mode.moveXY || mode == Mode.moveZ || mode == Mode.rotate)) {
|
|---|
| 97 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
|---|
| 98 | if (mode == Mode.moveXY) {
|
|---|
| 99 | displace(prevEastNorth, newEastNorth);
|
|---|
| 100 | } else if (mode == Mode.moveZ) {
|
|---|
| 101 | resize(newEastNorth);
|
|---|
| 102 | } else if (mode == Mode.rotate) {
|
|---|
| 103 | rotate(prevEastNorth, newEastNorth);
|
|---|
| 104 | }
|
|---|
| 105 | rasterMoved = true;
|
|---|
| 106 | if (!modifiedLayers.contains(selectedLayer))
|
|---|
| 107 | modifiedLayers.add(selectedLayer);
|
|---|
| 108 | Main.map.mapView.repaint();
|
|---|
| 109 | prevEastNorth = newEastNorth;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | private void displace(EastNorth start, EastNorth end) {
|
|---|
| 114 | selectedLayer.displace(end.east()-start.east(), end.north()-start.north());
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | private void resize(EastNorth newEastNorth) {
|
|---|
| 118 | double dPrev = prevEastNorth.distance(selectedLayer.getRasterCenter().east(), selectedLayer.getRasterCenter().north());
|
|---|
| 119 | double dNew = newEastNorth.distance(selectedLayer.getRasterCenter().east(), selectedLayer.getRasterCenter().north());
|
|---|
| 120 | selectedLayer.resize(1 - dNew/dPrev);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | private void rotate(EastNorth start, EastNorth end) {
|
|---|
| 124 | EastNorth pivot = selectedLayer.getRasterCenter();
|
|---|
| 125 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
|---|
| 126 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
|---|
| 127 | double rotationAngle = endAngle - startAngle;
|
|---|
| 128 | selectedLayer.rotate(rotationAngle);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | @Override public void mouseReleased(MouseEvent e) {
|
|---|
| 132 | //Main.map.mapView.repaint();
|
|---|
| 133 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
|---|
| 134 | selectedImage = null;
|
|---|
| 135 | prevEastNorth = null;
|
|---|
| 136 | selectedLayer = null;
|
|---|
| 137 | mode = null;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public void mouseEntered(MouseEvent e) {
|
|---|
| 141 | }
|
|---|
| 142 | public void mouseExited(MouseEvent e) {
|
|---|
| 143 | }
|
|---|
| 144 | public void mouseMoved(MouseEvent e) {
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | @Override public void mouseClicked(MouseEvent e) {
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | private void saveModifiedLayers() {
|
|---|
| 151 | for (WMSLayer wmsLayer : modifiedLayers) {
|
|---|
| 152 | wmsLayer.saveNewCache();
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|