source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/WMSAdjustAction.java@ 20214

Last change on this file since 20214 was 20211, checked in by pieren, 16 years ago

Fixed image rotation and a null pointer exception in adjust mode (raster maps only).

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