### Eclipse Workspace Patch 1.0
#P JOSM
|
|
|
4 | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
5 | 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
6 | 6 | |
| 7 | import java.awt.AWTEvent; |
7 | 8 | import java.awt.BasicStroke; |
8 | 9 | import java.awt.Color; |
9 | 10 | import java.awt.Cursor; |
10 | 11 | import java.awt.Graphics2D; |
11 | 12 | import java.awt.Point; |
12 | 13 | import java.awt.Rectangle; |
| 14 | import java.awt.Toolkit; |
| 15 | import java.awt.event.AWTEventListener; |
13 | 16 | import java.awt.event.ActionEvent; |
| 17 | import java.awt.event.InputEvent; |
14 | 18 | import java.awt.event.KeyEvent; |
15 | 19 | import java.awt.event.MouseEvent; |
16 | 20 | import java.awt.geom.AffineTransform; |
… |
… |
|
50 | 54 | */ |
51 | 55 | public class ExtrudeAction extends MapMode implements MapViewPaintable { |
52 | 56 | |
53 | | enum Mode { extrude, translate, select } |
| 57 | enum Mode { extrude, translate, select, create_new } |
54 | 58 | |
55 | 59 | private Mode mode = Mode.select; |
56 | 60 | |
… |
… |
|
97 | 101 | */ |
98 | 102 | private MoveCommand moveCommand; |
99 | 103 | |
| 104 | /** The cursor for the 'create_new' mode. */ |
| 105 | private final Cursor cursorCreateNew; |
| 106 | |
100 | 107 | /** |
| 108 | * This listener is used to indicate the 'create_new' mode, if the Alt modifier is pressed. |
| 109 | */ |
| 110 | private final AWTEventListener altKeyListener = new AWTEventListener() { |
| 111 | @Override |
| 112 | public void eventDispatched(AWTEvent e) { |
| 113 | if(Main.map == null || Main.map.mapView == null || !Main.map.mapView.isActiveLayerDrawable()) |
| 114 | return; |
| 115 | if(mode == Mode.select) { |
| 116 | InputEvent ie = (InputEvent) e; |
| 117 | boolean altDown = (ie.getModifiers() & ActionEvent.ALT_MASK) != 0; |
| 118 | Main.map.mapView.setNewCursor(altDown ? cursorCreateNew : cursor, this); |
| 119 | } |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | /** |
101 | 124 | * Create a new SelectAction |
102 | 125 | * @param mapFrame The MapFrame this action belongs to. |
103 | 126 | */ |
… |
… |
|
109 | 132 | putValue("help", ht("/Action/Extrude")); |
110 | 133 | initialMoveDelay = Main.pref.getInteger("edit.initial-move-delay",200); |
111 | 134 | selectedColor = PaintColors.SELECTED.get(); |
| 135 | cursorCreateNew = ImageProvider.getCursor("normal", "rectangle_plus"); |
112 | 136 | } |
113 | 137 | |
114 | 138 | @Override public String getModeHelpText() { |
… |
… |
|
116 | 140 | return tr("Move a segment along its normal, then release the mouse button."); |
117 | 141 | else if (mode == Mode.extrude) |
118 | 142 | return tr("Draw a rectangle of the desired size, then release the mouse button."); |
| 143 | else if (mode == Mode.create_new) |
| 144 | return tr("Draw a rectangle of the desired size, then release the mouse button."); |
119 | 145 | else |
120 | | return tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal."); |
| 146 | return tr("Drag a way segment to make a rectangle. Ctrl-drag to move a segment along its normal, Alt-drag to create a new rectangle."); |
121 | 147 | } |
122 | 148 | |
123 | 149 | @Override public boolean layerIsSupported(Layer l) { |
… |
… |
|
128 | 154 | super.enterMode(); |
129 | 155 | Main.map.mapView.addMouseListener(this); |
130 | 156 | Main.map.mapView.addMouseMotionListener(this); |
| 157 | try { |
| 158 | Toolkit.getDefaultToolkit().addAWTEventListener(altKeyListener, AWTEvent.KEY_EVENT_MASK); |
| 159 | } |
| 160 | catch (SecurityException ex) { /* ignore, don't indicate the create_new mode */ } |
131 | 161 | } |
132 | 162 | |
133 | 163 | @Override public void exitMode() { |
134 | 164 | Main.map.mapView.removeMouseListener(this); |
135 | 165 | Main.map.mapView.removeMouseMotionListener(this); |
136 | 166 | Main.map.mapView.removeTemporaryLayer(this); |
| 167 | try { |
| 168 | Toolkit.getDefaultToolkit().removeAWTEventListener(altKeyListener); |
| 169 | } |
| 170 | catch (SecurityException ex) { /* ignore, don't indicate the create_new mode */ } |
137 | 171 | super.exitMode(); |
138 | 172 | } |
139 | 173 | |
140 | 174 | /** |
141 | 175 | * If the left mouse button is pressed over a segment, switch |
142 | | * to either extrude or translate mode depending on whether Ctrl is held. |
| 176 | * to either extrude, translate or create_new mode depending on whether Ctrl or Alt is held. |
143 | 177 | */ |
144 | 178 | @Override public void mousePressed(MouseEvent e) { |
145 | 179 | if(!Main.map.mapView.isActiveLayerVisible()) |
… |
… |
|
158 | 192 | |
159 | 193 | if ((e.getModifiers() & ActionEvent.CTRL_MASK) != 0) { |
160 | 194 | mode = Mode.translate; |
| 195 | } else if ((e.getModifiers() & ActionEvent.ALT_MASK) != 0) { |
| 196 | mode = Mode.create_new; |
| 197 | // create a new segment and then select and extrude the new segment |
| 198 | getCurrentDataSet().setSelected(selectedSegment.way); |
| 199 | alwaysCreateNodes = true; |
161 | 200 | } else { |
162 | 201 | mode = Mode.extrude; |
163 | 202 | getCurrentDataSet().setSelected(selectedSegment.way); |
… |
… |
|
224 | 263 | if (mode == Mode.select) { |
225 | 264 | // Just sit tight and wait for mouse to be released. |
226 | 265 | } else { |
227 | | //move and extrude mode - move the selected segment |
| 266 | //move, create new and extrude mode - move the selected segment |
228 | 267 | |
229 | 268 | EastNorth initialMouseEn = Main.map.mapView.getEastNorth(initialMousePos.x, initialMousePos.y); |
230 | 269 | EastNorth mouseEn = Main.map.mapView.getEastNorth(e.getPoint().x, e.getPoint().y); |
… |
… |
|
260 | 299 | |
261 | 300 | Main.map.mapView.setNewCursor(Cursor.MOVE_CURSOR, this); |
262 | 301 | |
263 | | if (mode == Mode.extrude) { |
| 302 | if (mode == Mode.extrude || mode == Mode.create_new) { |
264 | 303 | //nothing here |
265 | 304 | } else if (mode == Mode.translate) { |
266 | 305 | //move nodes to new position |
… |
… |
|
292 | 331 | if (mode == Mode.select) { |
293 | 332 | // Nothing to be done |
294 | 333 | } else { |
295 | | if (mode == Mode.extrude) { |
| 334 | if (mode == Mode.create_new) { |
296 | 335 | if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) { |
| 336 | Collection<Command> cmds = new LinkedList<Command>(); |
| 337 | Node third = new Node(newN2en); |
| 338 | Node fourth = new Node(newN1en); |
| 339 | Way wnew = new Way(); |
| 340 | wnew.addNode(selectedSegment.getFirstNode()); |
| 341 | wnew.addNode(selectedSegment.getSecondNode()); |
| 342 | wnew.addNode(third); |
| 343 | wnew.addNode(fourth); |
| 344 | // ... and close the way |
| 345 | wnew.addNode(selectedSegment.getFirstNode()); |
| 346 | // undo support |
| 347 | cmds.add(new AddCommand(third)); |
| 348 | cmds.add(new AddCommand(fourth)); |
| 349 | cmds.add(new AddCommand(wnew)); |
| 350 | Command c = new SequenceCommand(tr("Extrude Way"), cmds); |
| 351 | Main.main.undoRedo.add(c); |
| 352 | getCurrentDataSet().setSelected(wnew); |
| 353 | } |
| 354 | } else if (mode == Mode.extrude) { |
| 355 | if (e.getPoint().distance(initialMousePos) > 10 && newN1en != null) { |
297 | 356 | // create extrusion |
298 | 357 | |
299 | 358 | Collection<Command> cmds = new LinkedList<Command>(); |
… |
… |
|
438 | 497 | Point p3 = mv.getPoint(newN1en); |
439 | 498 | Point p4 = mv.getPoint(newN2en); |
440 | 499 | |
441 | | if (mode == Mode.extrude) { |
| 500 | if (mode == Mode.extrude || mode == Mode.create_new) { |
442 | 501 | // Draw rectangle around new area. |
443 | 502 | GeneralPath b = new GeneralPath(); |
444 | 503 | b.moveTo(p1.x, p1.y); b.lineTo(p3.x, p3.y); |