| 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 | boolean ctrl = (e.getModifiers() & Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
|
|---|
| 84 | // boolean alt = (e.getModifiers() & ActionEvent.ALT_MASK) != 0;
|
|---|
| 85 | boolean shift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
|---|
| 86 | if (shift && !ctrl && modifiedLayer.isRaster())
|
|---|
| 87 | mode = Mode.moveZ;
|
|---|
| 88 | else if (shift && ctrl && modifiedLayer.isRaster())
|
|---|
| 89 | mode = Mode.rotate;
|
|---|
| 90 | else
|
|---|
| 91 | mode = Mode.moveXY;
|
|---|
| 92 | rasterMoved = true;
|
|---|
| 93 | prevEastNorth = Main.map.mapView.getEastNorth(e.getX(), e.getY());
|
|---|
| 94 | Main.map.mapView.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override public void mouseDragged(MouseEvent e) {
|
|---|
| 98 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
|---|
| 99 | if (mode == Mode.rotate) {
|
|---|
| 100 | rotateFrameOnly(prevEastNorth, newEastNorth);
|
|---|
| 101 | } else {
|
|---|
| 102 | if (mode == Mode.moveXY) {
|
|---|
| 103 | displace(prevEastNorth, newEastNorth);
|
|---|
| 104 | } else if (mode == Mode.moveZ) {
|
|---|
| 105 | resize(newEastNorth);
|
|---|
| 106 | }
|
|---|
| 107 | prevEastNorth = newEastNorth;
|
|---|
| 108 | }
|
|---|
| 109 | Main.map.mapView.repaint();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | public static void paintAdjustFrames(Graphics2D g, final MapView mv) {
|
|---|
| 113 | if (mode == Mode.rotate && croppedRaster != null) {
|
|---|
| 114 | g.setColor(Color.red);
|
|---|
| 115 | for (int i=0; i<4; i++)
|
|---|
| 116 | g.drawLine(mv.getPoint(croppedRaster[i]).x,
|
|---|
| 117 | mv.getPoint(croppedRaster[i]).y,
|
|---|
| 118 | mv.getPoint(croppedRaster[i+1]).x,
|
|---|
| 119 | mv.getPoint(croppedRaster[i+1]).y);
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | private void displace(EastNorth start, EastNorth end) {
|
|---|
| 124 | modifiedLayer.displace(end.east()-start.east(), end.north()-start.north());
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | private void resize(EastNorth newEastNorth) {
|
|---|
| 128 | EastNorth center = modifiedLayer.getRasterCenter();
|
|---|
| 129 | double dPrev = prevEastNorth.distance(center.east(), center.north());
|
|---|
| 130 | double dNew = newEastNorth.distance(center.east(), center.north());
|
|---|
| 131 | modifiedLayer.resize(center, dNew/dPrev);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | private void rotate(EastNorth start, EastNorth end) {
|
|---|
| 135 | EastNorth pivot = modifiedLayer.getRasterCenter();
|
|---|
| 136 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
|---|
| 137 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
|---|
| 138 | double rotationAngle = endAngle - startAngle;
|
|---|
| 139 | modifiedLayer.rotate(pivot, rotationAngle);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | private void rotateFrameOnly(EastNorth start, EastNorth end) {
|
|---|
| 143 | if (start != null && end != null) {
|
|---|
| 144 | EastNorth pivot = modifiedLayer.getRasterCenter();
|
|---|
| 145 | double startAngle = Math.atan2(start.east()-pivot.east(), start.north()-pivot.north());
|
|---|
| 146 | double endAngle = Math.atan2(end.east()-pivot.east(), end.north()-pivot.north());
|
|---|
| 147 | double rotationAngle = endAngle - startAngle;
|
|---|
| 148 | if (modifiedLayer.getImage(0).orgCroppedRaster != null) {
|
|---|
| 149 | for (int i=0; i<4; i++) {
|
|---|
| 150 | croppedRaster[i] = modifiedLayer.getImage(0).orgCroppedRaster[i].rotate(pivot, rotationAngle);
|
|---|
| 151 | }
|
|---|
| 152 | croppedRaster[4] = croppedRaster[0];
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | @Override public void mouseReleased(MouseEvent e) {
|
|---|
| 158 | //Main.map.mapView.repaint();
|
|---|
| 159 | if (mode == Mode.rotate) {
|
|---|
| 160 | EastNorth newEastNorth = Main.map.mapView.getEastNorth(e.getX(),e.getY());
|
|---|
| 161 | rotate(prevEastNorth, newEastNorth);
|
|---|
| 162 | Main.map.mapView.repaint();
|
|---|
| 163 | }
|
|---|
| 164 | Main.map.mapView.setCursor(Cursor.getDefaultCursor());
|
|---|
| 165 | prevEastNorth = null;
|
|---|
| 166 | mode = null;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | public void mouseEntered(MouseEvent e) {
|
|---|
| 170 | }
|
|---|
| 171 | public void mouseExited(MouseEvent e) {
|
|---|
| 172 | }
|
|---|
| 173 | public void mouseMoved(MouseEvent e) {
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | @Override public void mouseClicked(MouseEvent e) {
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | private void saveModifiedLayers() {
|
|---|
| 180 | modifiedLayer.grabThread.saveNewCache();
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|