Changeset 29595 in osm for applications/editors/josm/plugins/CommandLine/src
- Timestamp:
- 2013-05-14T22:49:46+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/CommandLine/src/CommandLine/LengthAction.java
r29505 r29595 38 38 39 39 public class LengthAction extends MapMode implements MapViewPaintable, AWTEventListener { 40 private CommandLine parentPlugin; 41 final private Cursor cursorCrosshair; 42 final private Cursor cursorJoinNode; 43 private Cursor currentCursor; 44 private Color selectedColor; 45 private Point drawStartPos; 46 private Point drawEndPos; 47 private LatLon startCoor; 48 private LatLon endCoor; 49 private Point mousePos; 50 private Node nearestNode; 51 private boolean drawing; 52 53 public LengthAction(MapFrame mapFrame, CommandLine parentPlugin) { 54 super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("crosshair", null)); 55 this.parentPlugin = parentPlugin; 56 selectedColor = Main.pref.getColor(marktr("selected"), Color.red); 57 cursorCrosshair = ImageProvider.getCursor("crosshair", null); 58 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode"); 59 currentCursor = cursorCrosshair; 60 nearestNode = null; 61 } 62 63 @Override 64 public void enterMode() { 65 super.enterMode(); 66 Main.map.mapView.addMouseListener(this); 67 Main.map.mapView.addMouseMotionListener(this); 68 Main.map.mapView.addTemporaryLayer(this); 69 try { 70 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK); 71 } catch (SecurityException ex) { 72 } 73 } 74 75 @Override 76 public void exitMode() { 77 super.exitMode(); 78 Main.map.mapView.removeMouseListener(this); 79 Main.map.mapView.removeMouseMotionListener(this); 80 Main.map.mapView.removeTemporaryLayer(this); 81 try { 82 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 83 } catch (SecurityException ex) { 84 } 85 if (drawing) 86 Main.map.mapView.repaint(); 87 } 88 89 public void cancelDrawing() { 90 if (Main.map == null || Main.map.mapView == null) 91 return; 92 Main.map.statusLine.setHeading(-1); 93 Main.map.statusLine.setAngle(-1); 94 updateStatusLine(); 95 parentPlugin.abortInput(); 96 } 97 98 public void eventDispatched(AWTEvent arg0) { 99 if (!(arg0 instanceof KeyEvent)) 100 return; 101 KeyEvent ev = (KeyEvent) arg0; 102 if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) { 103 if (drawing) 104 ev.consume(); 105 cancelDrawing(); 106 } 107 } 108 109 private void processMouseEvent(MouseEvent e) { 110 if (e != null) { 111 mousePos = e.getPoint(); 112 } 113 } 114 115 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 116 if (!drawing) 117 return; 118 119 g.setColor(selectedColor); 120 g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 121 122 GeneralPath b = new GeneralPath(); 123 Point pp1 = drawStartPos; 124 Point pp2 = drawEndPos; 125 126 b.moveTo(pp1.x, pp1.y); 127 b.lineTo(pp2.x, pp2.y); 128 g.draw(b); 129 130 g.setStroke(new BasicStroke(1)); 131 } 132 133 private void drawingStart(MouseEvent e) { 134 mousePos = e.getPoint(); 135 if (nearestNode != null) { 136 drawStartPos = Main.map.mapView.getPoint(nearestNode.getCoor()); 137 } else { 138 drawStartPos = mousePos; 139 } 140 drawEndPos = drawStartPos; 141 startCoor = Main.map.mapView.getLatLon(drawStartPos.x, drawStartPos.y); 142 endCoor = startCoor; 143 drawing = true; 144 updateStatusLine(); 145 } 146 147 private void drawingFinish() { 148 parentPlugin.loadParameter(String.valueOf(startCoor.greatCircleDistance(endCoor)), true); 149 drawStartPos = null; 150 drawing = false; 151 exitMode(); 152 } 153 154 @Override 155 public void mousePressed(MouseEvent e) { 156 if (e.getButton() == MouseEvent.BUTTON1) { 157 if (!Main.map.mapView.isActiveLayerDrawable()) 158 return; 159 drawingStart(e); 160 } 161 else 162 drawing = false; 163 } 164 165 @Override 166 public void mouseReleased(MouseEvent e) { 167 if (e.getButton() != MouseEvent.BUTTON1) 168 return; 169 if (!Main.map.mapView.isActiveLayerDrawable()) 170 return; 171 boolean dragged = true; 172 if (drawStartPos != null) 173 dragged = drawEndPos.distance(drawStartPos) > 10; 174 if (drawing && dragged) 175 drawingFinish(); 176 drawing = false; 177 } 178 179 @Override 180 public void mouseDragged(MouseEvent e) { 181 processMouseEvent(e); 182 updCursor(); 183 if (nearestNode != null) 184 drawEndPos = Main.map.mapView.getPoint(nearestNode.getCoor()); 185 else 186 drawEndPos = mousePos; 187 endCoor = Main.map.mapView.getLatLon(drawEndPos.x, drawEndPos.y); 188 if (drawing) { 189 Main.map.statusLine.setDist(startCoor.greatCircleDistance(endCoor)); 190 Main.map.mapView.repaint(); 191 } 192 } 193 194 @Override 195 public void mouseMoved(MouseEvent e) { 196 if (!Main.map.mapView.isActiveLayerDrawable()) 197 return; 198 processMouseEvent(e); 199 updCursor(); 200 if (drawing) 201 Main.map.mapView.repaint(); 202 } 203 204 @Override 205 public String getModeHelpText() { 206 if (drawing) 207 return tr("Point on the start"); 208 else 209 return tr("Point on the end"); 210 } 211 212 @Override 213 public boolean layerIsSupported(Layer l) { 214 return l instanceof OsmDataLayer; 215 } 216 217 private void updCursor() { 218 if (mousePos != null) { 219 if (!Main.isDisplayingMapView()) 220 return; 221 nearestNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate); 222 if (nearestNode != null) { 223 setCursor(cursorJoinNode); 224 } 225 else { 226 setCursor(cursorCrosshair); 227 } 228 } 229 } 230 231 private void setCursor(final Cursor c) { 232 if (currentCursor.equals(c)) 233 return; 234 try { 235 // We invoke this to prevent strange things from happening 236 EventQueue.invokeLater(new Runnable() { 237 public void run() { 238 // Don't change cursor when mode has changed already 239 if (!(Main.map.mapMode instanceof LengthAction)) 240 return; 241 Main.map.mapView.setCursor(c); 242 } 243 }); 244 currentCursor = c; 245 } catch (Exception e) { 246 } 247 } 40 private final CommandLine parentPlugin; 41 final private Cursor cursorCrosshair; 42 final private Cursor cursorJoinNode; 43 private Cursor currentCursor; 44 private final Color selectedColor; 45 private Point drawStartPos; 46 private Point drawEndPos; 47 private LatLon startCoor; 48 private LatLon endCoor; 49 private Point mousePos; 50 private Node nearestNode; 51 private boolean drawing; 52 53 public LengthAction(MapFrame mapFrame, CommandLine parentPlugin) { 54 super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("crosshair", null)); 55 this.parentPlugin = parentPlugin; 56 selectedColor = Main.pref.getColor(marktr("selected"), Color.red); 57 cursorCrosshair = ImageProvider.getCursor("crosshair", null); 58 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode"); 59 currentCursor = cursorCrosshair; 60 nearestNode = null; 61 } 62 63 @Override 64 public void enterMode() { 65 super.enterMode(); 66 Main.map.mapView.addMouseListener(this); 67 Main.map.mapView.addMouseMotionListener(this); 68 Main.map.mapView.addTemporaryLayer(this); 69 try { 70 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK); 71 } catch (SecurityException ex) { 72 } 73 } 74 75 @Override 76 public void exitMode() { 77 super.exitMode(); 78 Main.map.mapView.removeMouseListener(this); 79 Main.map.mapView.removeMouseMotionListener(this); 80 Main.map.mapView.removeTemporaryLayer(this); 81 try { 82 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 83 } catch (SecurityException ex) { 84 } 85 if (drawing) 86 Main.map.mapView.repaint(); 87 } 88 89 public void cancelDrawing() { 90 if (Main.map == null || Main.map.mapView == null) 91 return; 92 Main.map.statusLine.setHeading(-1); 93 Main.map.statusLine.setAngle(-1); 94 updateStatusLine(); 95 parentPlugin.abortInput(); 96 } 97 98 @Override 99 public void eventDispatched(AWTEvent arg0) { 100 if (!(arg0 instanceof KeyEvent)) 101 return; 102 KeyEvent ev = (KeyEvent) arg0; 103 if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) { 104 if (drawing) 105 ev.consume(); 106 cancelDrawing(); 107 } 108 } 109 110 private void processMouseEvent(MouseEvent e) { 111 if (e != null) { 112 mousePos = e.getPoint(); 113 } 114 } 115 116 @Override 117 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 118 if (!drawing) 119 return; 120 121 g.setColor(selectedColor); 122 g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 123 124 GeneralPath b = new GeneralPath(); 125 Point pp1 = drawStartPos; 126 Point pp2 = drawEndPos; 127 128 b.moveTo(pp1.x, pp1.y); 129 b.lineTo(pp2.x, pp2.y); 130 g.draw(b); 131 132 g.setStroke(new BasicStroke(1)); 133 } 134 135 private void drawingStart(MouseEvent e) { 136 mousePos = e.getPoint(); 137 if (nearestNode != null) { 138 drawStartPos = Main.map.mapView.getPoint(nearestNode.getCoor()); 139 } else { 140 drawStartPos = mousePos; 141 } 142 drawEndPos = drawStartPos; 143 startCoor = Main.map.mapView.getLatLon(drawStartPos.x, drawStartPos.y); 144 endCoor = startCoor; 145 drawing = true; 146 updateStatusLine(); 147 } 148 149 private void drawingFinish() { 150 parentPlugin.loadParameter(String.valueOf(startCoor.greatCircleDistance(endCoor)), true); 151 drawStartPos = null; 152 drawing = false; 153 exitMode(); 154 } 155 156 @Override 157 public void mousePressed(MouseEvent e) { 158 if (e.getButton() == MouseEvent.BUTTON1) { 159 if (!Main.map.mapView.isActiveLayerDrawable()) 160 return; 161 requestFocusInMapView(); 162 drawingStart(e); 163 } 164 else 165 drawing = false; 166 } 167 168 @Override 169 public void mouseReleased(MouseEvent e) { 170 if (e.getButton() != MouseEvent.BUTTON1) 171 return; 172 if (!Main.map.mapView.isActiveLayerDrawable()) 173 return; 174 boolean dragged = true; 175 if (drawStartPos != null) 176 dragged = drawEndPos.distance(drawStartPos) > 10; 177 if (drawing && dragged) 178 drawingFinish(); 179 drawing = false; 180 } 181 182 @Override 183 public void mouseDragged(MouseEvent e) { 184 processMouseEvent(e); 185 updCursor(); 186 if (nearestNode != null) 187 drawEndPos = Main.map.mapView.getPoint(nearestNode.getCoor()); 188 else 189 drawEndPos = mousePos; 190 endCoor = Main.map.mapView.getLatLon(drawEndPos.x, drawEndPos.y); 191 if (drawing) { 192 Main.map.statusLine.setDist(startCoor.greatCircleDistance(endCoor)); 193 Main.map.mapView.repaint(); 194 } 195 } 196 197 @Override 198 public void mouseMoved(MouseEvent e) { 199 if (!Main.map.mapView.isActiveLayerDrawable()) 200 return; 201 processMouseEvent(e); 202 updCursor(); 203 if (drawing) 204 Main.map.mapView.repaint(); 205 } 206 207 @Override 208 public String getModeHelpText() { 209 if (drawing) 210 return tr("Point on the start"); 211 else 212 return tr("Point on the end"); 213 } 214 215 @Override 216 public boolean layerIsSupported(Layer l) { 217 return l instanceof OsmDataLayer; 218 } 219 220 private void updCursor() { 221 if (mousePos != null) { 222 if (!Main.isDisplayingMapView()) 223 return; 224 nearestNode = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate); 225 if (nearestNode != null) { 226 setCursor(cursorJoinNode); 227 } 228 else { 229 setCursor(cursorCrosshair); 230 } 231 } 232 } 233 234 private void setCursor(final Cursor c) { 235 if (currentCursor.equals(c)) 236 return; 237 try { 238 // We invoke this to prevent strange things from happening 239 EventQueue.invokeLater(new Runnable() { 240 @Override 241 public void run() { 242 // Don't change cursor when mode has changed already 243 if (!(Main.map.mapMode instanceof LengthAction)) 244 return; 245 Main.map.mapView.setCursor(c); 246 } 247 }); 248 currentCursor = c; 249 } catch (Exception e) { 250 } 251 } 248 252 }
Note:
See TracChangeset
for help on using the changeset viewer.