Changeset 21802 in osm for applications/editors/josm
- Timestamp:
- 2010-06-19T19:33:27+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/buildings_tools/src/buildings_tools/DrawBuildingAction.java
r21801 r21802 37 37 38 38 @SuppressWarnings("serial") 39 public class DrawBuildingAction extends MapMode 40 implements MapViewPaintable, AWTEventListener, SelectionChangedListener { 41 enum Mode {None, Drawing, DrawingWidth, DrawingAngFix} 39 public class DrawBuildingAction extends MapMode 40 implements MapViewPaintable, AWTEventListener, SelectionChangedListener { 41 enum Mode { 42 None, Drawing, DrawingWidth, DrawingAngFix 43 } 44 42 45 final private Cursor cursorCrosshair; 43 46 final private Cursor cursorJoinNode; 44 47 private Cursor currCursor; 45 48 46 49 private Mode mode = Mode.None; 47 50 private Mode nextMode = Mode.None; 48 51 49 52 private Color selectedColor; 50 53 private Point mousePos; 51 54 private Point drawStartPos; 52 55 53 56 Building building = new Building(); 54 57 55 58 public DrawBuildingAction(MapFrame mapFrame) { 56 super(tr("Draw buildings"),"building",tr("Draw buildings"), 59 super(tr("Draw buildings"), "building", tr("Draw buildings"), 57 60 Shortcut.registerShortcut("mapmode:buildings", 58 59 60 mapFrame,getCursor()); 61 61 tr("Mode: {0}", tr("Draw buildings")), 62 KeyEvent.VK_W, Shortcut.GROUP_EDIT), 63 mapFrame, getCursor()); 64 62 65 cursorCrosshair = getCursor(); 63 66 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode"); 64 67 currCursor = cursorCrosshair; 65 68 66 69 selectedColor = Main.pref.getColor(marktr("selected"), Color.red); 67 70 } 71 68 72 private static Cursor getCursor() { 69 73 try { … … 73 77 return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); 74 78 } 79 75 80 /** 76 81 * Displays the given cursor instead of the normal one 77 * @param Cursors One of the available cursors 82 * 83 * @param Cursors 84 * One of the available cursors 78 85 */ 79 86 private void setCursor(final Cursor c) { 80 if(currCursor.equals(c)) 87 if (currCursor.equals(c)) 81 88 return; 82 89 try { … … 85 92 public void run() { 86 93 // Don't change cursor when mode has changed already 87 if(!(Main.map.mapMode instanceof DrawBuildingAction)) 94 if (!(Main.map.mapMode instanceof DrawBuildingAction)) 88 95 return; 89 96 Main.map.mapView.setCursor(c); … … 91 98 }); 92 99 currCursor = c; 93 } catch(Exception e) {} 94 } 100 } catch (Exception e) { 101 } 102 } 103 95 104 private static void showAddrDialog(Way w) { 96 105 AddressDialog dlg = new AddressDialog(); … … 100 109 String tmp; 101 110 tmp = dlg.getHouseNum(); 102 if (tmp!=null&&tmp!="") w.put("addr:housenumber",tmp); 111 if (tmp != null && tmp != "") 112 w.put("addr:housenumber", tmp); 103 113 tmp = dlg.getStreetName(); 104 if (tmp!=null&&tmp!="") w.put("addr:street",tmp); 105 } 106 } 107 108 @Override public void enterMode() { 114 if (tmp != null && tmp != "") 115 w.put("addr:street", tmp); 116 } 117 } 118 119 @Override 120 public void enterMode() { 109 121 super.enterMode(); 110 122 if (getCurrentDataSet() == null) { … … 120 132 try { 121 133 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK); 122 } catch (SecurityException ex) { } 123 } 124 125 @Override public void exitMode() { 134 } catch (SecurityException ex) { 135 } 136 } 137 138 @Override 139 public void exitMode() { 126 140 super.exitMode(); 127 141 Main.map.mapView.removeMouseListener(this); … … 131 145 try { 132 146 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 133 } catch (SecurityException ex) { } 134 if (mode!=Mode.None) Main.map.mapView.repaint(); 147 } catch (SecurityException ex) { 148 } 149 if (mode != Mode.None) 150 Main.map.mapView.repaint(); 135 151 mode = Mode.None; 136 152 } 137 153 138 154 public void cancelDrawing() { 139 155 mode = Mode.None; 140 if(Main.map == null || Main.map.mapView == null) 156 if (Main.map == null || Main.map.mapView == null) 141 157 return; 142 158 Main.map.statusLine.setHeading(-1); 143 144 159 Main.map.statusLine.setAngle(-1); 160 building.reset(); 145 161 Main.map.mapView.repaint(); 146 162 updateStatusLine(); … … 148 164 149 165 public void eventDispatched(AWTEvent arg0) { 150 if (!(arg0 instanceof KeyEvent)) return; 151 KeyEvent ev = (KeyEvent)arg0; 166 if (!(arg0 instanceof KeyEvent)) 167 return; 168 KeyEvent ev = (KeyEvent) arg0; 152 169 if (ev.getKeyCode() == KeyEvent.VK_ESCAPE) 153 170 cancelDrawing(); 154 171 } 155 172 156 173 private EastNorth getPoint(MouseEvent e) { 157 174 Node n; 158 175 if (e.isControlDown()) { 159 176 n = null; 160 } else { 177 } else { 161 178 n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate); 162 179 } … … 167 184 } 168 185 } 169 186 170 187 private Mode modeDrawing(MouseEvent e) { 171 188 EastNorth p = getPoint(e); … … 175 192 } else { 176 193 building.setPlace(p, ToolSettings.getWidth(), 177 ToolSettings.getLenStep(),e.isShiftDown()); 194 ToolSettings.getLenStep(), e.isShiftDown()); 178 195 Main.map.statusLine.setDist(building.getLength()); 179 return this.nextMode = ToolSettings.getWidth() == 0? Mode.DrawingWidth : Mode.None; 196 return this.nextMode = ToolSettings.getWidth() == 0 ? Mode.DrawingWidth : Mode.None; 180 197 } 181 198 } … … 208 225 throw new AssertionError("Invalid drawing mode"); 209 226 } 210 211 public void paint(Graphics2D g, MapView mv,Bounds bbox) 212 { 213 if (mode == Mode.None) return; 214 if (building.getLength() == 0) return; 215 227 228 public void paint(Graphics2D g, MapView mv, Bounds bbox) { 229 if (mode == Mode.None) 230 return; 231 if (building.getLength() == 0) 232 return; 233 216 234 g.setColor(selectedColor); 217 235 g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 218 236 219 237 building.paint(g, mv); 220 238 221 239 g.setStroke(new BasicStroke(1)); 222 240 223 241 } 224 225 private void drawingStart(MouseEvent e) 226 { 242 243 private void drawingStart(MouseEvent e) { 227 244 mousePos = e.getPoint(); 228 245 drawStartPos = mousePos; 229 246 230 247 Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate); 231 248 if (n == null) { … … 247 264 } 248 265 } 249 266 250 267 private void drawingFinish() { 251 268 if (building.getLength() != 0) { … … 257 274 } 258 275 259 @Override public void mousePressed(MouseEvent e) { 260 if (e.getButton() != MouseEvent.BUTTON1) return; 261 if(!Main.map.mapView.isActiveLayerDrawable()) return; 276 @Override 277 public void mousePressed(MouseEvent e) { 278 if (e.getButton() != MouseEvent.BUTTON1) 279 return; 280 if (!Main.map.mapView.isActiveLayerDrawable()) 281 return; 262 282 263 283 if (mode == Mode.None) … … 265 285 } 266 286 267 @Override public void mouseDragged(MouseEvent e) { 287 @Override 288 public void mouseDragged(MouseEvent e) { 268 289 processMouseEvent(e); 269 290 updCursor(); 270 if (mode!=Mode.None) Main.map.mapView.repaint(); 271 } 272 273 @Override public void mouseReleased(MouseEvent e) { 274 if (e.getButton() != MouseEvent.BUTTON1) return; 275 if(!Main.map.mapView.isActiveLayerDrawable()) return; 291 if (mode != Mode.None) 292 Main.map.mapView.repaint(); 293 } 294 295 @Override 296 public void mouseReleased(MouseEvent e) { 297 if (e.getButton() != MouseEvent.BUTTON1) 298 return; 299 if (!Main.map.mapView.isActiveLayerDrawable()) 300 return; 276 301 boolean dragged = true; 277 302 if (drawStartPos != null) … … 279 304 drawStartPos = null; 280 305 281 if (mode == Mode.Drawing && !dragged) return; 282 if (mode == Mode.None) return; 306 if (mode == Mode.Drawing && !dragged) 307 return; 308 if (mode == Mode.None) 309 return; 283 310 284 311 drawingAdvance(e); 285 312 } 286 313 287 314 private void updCursor() { 288 if (mousePos==null) return; 315 if (mousePos == null) 316 return; 289 317 Node n = Main.map.mapView.getNearestNode(mousePos, OsmPrimitive.isUsablePredicate); 290 if (n != null) setCursor(cursorJoinNode); else setCursor(cursorCrosshair); 291 292 } 293 @Override public void mouseMoved(MouseEvent e) { 294 if(!Main.map.mapView.isActiveLayerDrawable()) return; 318 if (n != null) 319 setCursor(cursorJoinNode); 320 else 321 setCursor(cursorCrosshair); 322 323 } 324 325 @Override 326 public void mouseMoved(MouseEvent e) { 327 if (!Main.map.mapView.isActiveLayerDrawable()) 328 return; 295 329 processMouseEvent(e); 296 330 updCursor(); 297 if (mode!=Mode.None) Main.map.mapView.repaint(); 298 } 299 300 @Override public String getModeHelpText() { 301 if (mode==Mode.None) return tr("Point on the corner of the building to start drawing"); 302 if (mode==Mode.Drawing) return tr("Point on opposite end of the building"); 303 if (mode==Mode.DrawingWidth) return tr("Set width of the building"); 331 if (mode != Mode.None) 332 Main.map.mapView.repaint(); 333 } 334 335 @Override 336 public String getModeHelpText() { 337 if (mode == Mode.None) 338 return tr("Point on the corner of the building to start drawing"); 339 if (mode == Mode.Drawing) 340 return tr("Point on opposite end of the building"); 341 if (mode == Mode.DrawingWidth) 342 return tr("Set width of the building"); 304 343 return ""; 305 344 } 306 345 307 @Override public boolean layerIsSupported(Layer l) { 346 @Override 347 public boolean layerIsSupported(Layer l) { 308 348 return l instanceof OsmDataLayer; 309 349 } 310 350 311 351 public void updateConstraint(Collection<? extends OsmPrimitive> newSelection) { 312 352 building.disableAngConstraint(); 313 if (newSelection.size()!=2)return; 314 Object[] arr = newSelection.toArray(); 315 if (!(arr[0] instanceof Node&&arr[1] instanceof Node)) return; 316 EastNorth p1,p2; 317 p1=latlon2eastNorth(((Node)arr[0]).getCoor()); 318 p2=latlon2eastNorth(((Node)arr[1]).getCoor()); 319 building.setAngConstraint(p1.heading(p2)); 320 } 321 353 if (newSelection.size() != 2) 354 return; 355 Object[] arr = newSelection.toArray(); 356 if (!(arr[0] instanceof Node && arr[1] instanceof Node)) 357 return; 358 EastNorth p1, p2; 359 p1 = latlon2eastNorth(((Node) arr[0]).getCoor()); 360 p2 = latlon2eastNorth(((Node) arr[1]).getCoor()); 361 building.setAngConstraint(p1.heading(p2)); 362 } 363 322 364 public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) { 323 365 updateConstraint(newSelection);
Note:
See TracChangeset
for help on using the changeset viewer.