Changeset 33191 in osm for applications/editors/josm
- Timestamp:
- 2017-03-15T10:41:40+01:00 (8 years ago)
- Location:
- applications/editors/josm/plugins/pt_assistant
- Files:
-
- 5 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/PTAssistantPlugin.java
r33055 r33191 7 7 import org.openstreetmap.josm.Main; 8 8 import org.openstreetmap.josm.data.validation.OsmValidator; 9 import org.openstreetmap.josm.gui.IconToggleButton; 9 10 import org.openstreetmap.josm.gui.MainMenu; 10 11 import org.openstreetmap.josm.gui.MapFrame; … … 32 33 private static PTRouteSegment lastFix; 33 34 34 /* item of the Tools menu for adding stop_positions */35 private JMenuItem addStopPositionMenu;36 37 35 /* item of the Tools menu for repeating the last fix */ 38 36 private static JMenuItem repeatLastFixMenu; … … 50 48 OsmValidator.addTest(PTAssistantValidatorTest.class); 51 49 52 AddStopPositionAction addStopPositionAction = new AddStopPositionAction();53 addStopPositionMenu = MainMenu.add(Main.main.menu.toolsMenu, addStopPositionAction, false);54 50 RepeatLastFixAction repeatLastFixAction = new RepeatLastFixAction(); 55 51 repeatLastFixMenu = MainMenu.add(Main.main.menu.toolsMenu, repeatLastFixAction, false); 56 57 52 } 58 53 … … 63 58 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 64 59 if (oldFrame == null && newFrame != null) { 65 addStopPositionMenu.setEnabled(true);66 60 repeatLastFixMenu.setEnabled(false); 61 Main.map.addMapMode(new IconToggleButton(new AddStopPositionAction(Main.map))); 67 62 } else if (oldFrame != null && newFrame == null) { 68 addStopPositionMenu.setEnabled(false);69 63 repeatLastFixMenu.setEnabled(false); 70 64 } -
applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/AddStopPositionAction.java
r33055 r33191 1 // License: GPL. For details, see LICENSE file.2 1 package org.openstreetmap.josm.plugins.pt_assistant.actions; 3 2 4 3 import static org.openstreetmap.josm.tools.I18n.tr; 5 4 6 import java.awt. event.ActionEvent;5 import java.awt.Cursor; 7 6 import java.awt.event.KeyEvent; 8 import java.awt.event.MouseAdapter;9 7 import java.awt.event.MouseEvent; 8 import java.util.HashSet; 9 import java.util.List; 10 import java.util.Set; 10 11 11 12 import org.openstreetmap.josm.Main; 12 13 import org.openstreetmap.josm.actions.JoinNodeWayAction; 13 import org.openstreetmap.josm.actions.JosmAction;14 14 import org.openstreetmap.josm.actions.SplitWayAction; 15 import org.openstreetmap.josm.data.coor.LatLon; 15 import org.openstreetmap.josm.actions.mapmode.MapMode; 16 import org.openstreetmap.josm.command.AddCommand; 17 import org.openstreetmap.josm.command.ChangeCommand; 18 import org.openstreetmap.josm.data.osm.DataSet; 16 19 import org.openstreetmap.josm.data.osm.Node; 17 import org.openstreetmap.josm.gui.layer.Layer; 18 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 20 import org.openstreetmap.josm.data.osm.OsmPrimitive; 21 import org.openstreetmap.josm.data.osm.WaySegment; 22 import org.openstreetmap.josm.gui.MapFrame; 19 23 import org.openstreetmap.josm.tools.ImageProvider; 20 24 import org.openstreetmap.josm.tools.Shortcut; 21 25 22 /** 23 * Action to add stop position and split the relevant way 24 * 25 * @author darya 26 * 26 /* 27 * The AddStopPositionAction is a mapmode that allows users to add 28 * new stop_positions or to convert already existing nodes. 27 29 */ 28 public class AddStopPositionAction extends JosmAction { 30 @SuppressWarnings("serial") 31 public class AddStopPositionAction extends MapMode { 29 32 30 /** 31 * 32 */ 33 private static final long serialVersionUID = -5140181388906670207L; 33 private transient Set<OsmPrimitive> newHighlights = new HashSet<>(); 34 private transient Set<OsmPrimitive> oldHighlights = new HashSet<>(); 34 35 35 public AddStopPositionAction() { 36 super(tr("Add stop position"), new ImageProvider("presets/transport", "bus.svg"), tr("Add stop position"), 37 Shortcut.registerShortcut("Add stop position", tr("Add stop position"), KeyEvent.VK_T, Shortcut.NONE), 38 false, "addStopPosition", false); 36 private final Cursor cursorJoinNode; 37 private final Cursor cursorJoinWay; 39 38 39 public AddStopPositionAction(MapFrame mapFrame) { 40 super(tr("Add stop position"), 41 "bus", 42 tr("Add stop position"), 43 Shortcut.registerShortcut("mapmode:stop_position", 44 tr("Mode: {0}", tr("Add stop position")), 45 KeyEvent.VK_T, Shortcut.DIRECT), 46 mapFrame, 47 getCursor()); 48 49 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode"); 50 cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway"); 51 } 52 53 private static Cursor getCursor() { 54 try { 55 return ImageProvider.getCursor("crosshair", "bus"); 56 } catch (Exception e) { 57 Main.error(e); 58 } 59 return Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); 40 60 } 41 61 42 /**43 * Actions that add the new node, set tags and update the map frame.44 */45 62 @Override 46 public void actionPerformed(ActionEvent e) { 63 public void enterMode() { 64 super.enterMode(); 65 Main.map.mapView.addMouseListener(this); 66 Main.map.mapView.addMouseMotionListener(this); 67 } 47 68 48 if (!isEnabled() || !Main.isDisplayingMapView()) { 49 return; 69 @Override 70 public void exitMode() { 71 super.exitMode(); 72 Main.map.mapView.removeMouseListener(this); 73 Main.map.mapView.removeMouseMotionListener(this); 74 } 75 76 @Override 77 public void mouseMoved (MouseEvent e) { 78 79 //while the mouse is moving, surroundings are checked 80 //if anything is found, it will be highlighted. 81 //priority is given to nodes 82 Cursor newCurs = getCursor(); 83 84 Node n = Main.map.mapView.getNearestNode(e.getPoint(), OsmPrimitive::isUsable); 85 if(n != null) { 86 newHighlights.add(n); 87 newCurs = cursorJoinNode; 88 } else { 89 List<WaySegment> wss = 90 Main.map.mapView.getNearestWaySegments(e.getPoint(), OsmPrimitive::isSelectable); 91 92 if(wss.size() > 0) { 93 for(WaySegment ws : wss) { 94 newHighlights.add(ws.way); 95 } 96 newCurs = cursorJoinWay; 97 } 98 } 99 100 Main.map.mapView.setCursor(newCurs); 101 updateHighlights(); 102 } 103 104 @Override 105 public void mouseClicked (MouseEvent e) { 106 107 Boolean newNode = false; 108 Node newStopPos; 109 110 //check if the user as selected an existing node, or a new one 111 Node n = Main.map.mapView.getNearestNode(e.getPoint(), OsmPrimitive::isUsable); 112 if (n == null) { 113 newNode = true; 114 newStopPos = new Node(Main.map.mapView.getLatLon(e.getX(), e.getY())); 115 } else { 116 newStopPos = new Node(n); 117 clearNodeTags(newStopPos); 50 118 } 51 119 52 final ActionEvent actionEventParameter = e; 120 //add the tags of the stop position 121 newStopPos.put("bus", "yes"); 122 newStopPos.put("public_transport", "stop_position"); 53 123 54 Main.map.mapView.addMouseListener(new MouseAdapter() { 124 if(newNode) { 125 Main.main.undoRedo.add(new AddCommand(newStopPos)); 126 } else { 127 Main.main.undoRedo.add(new ChangeCommand(n, newStopPos)); 128 } 55 129 56 LatLon clickPosition; 130 DataSet ds = Main.getLayerManager().getEditLayer().data; 131 ds.setSelected(newStopPos); 57 132 58 @Override 59 public void mouseClicked(MouseEvent e) { 133 //join the node to the way only if the node is new 134 if(newNode) { 135 JoinNodeWayAction joinNodeWayAction = JoinNodeWayAction.createJoinNodeToWayAction(); 136 joinNodeWayAction.actionPerformed(null); 137 } 60 138 61 if (clickPosition == null) { 62 clickPosition = Main.map.mapView.getLatLon(e.getX(), e.getY()); 139 // split the way in any case 140 SplitWayAction splitWayAction = new SplitWayAction(); 141 splitWayAction.actionPerformed(null); 142 } 63 143 64 Layer activeLayer = Main.getLayerManager().getActiveLayer(); 144 private void clearNodeTags(Node newStopPos) { 145 for(String key : newStopPos.keySet()) { 146 newStopPos.put(key, null); 147 } 65 148 66 if (activeLayer instanceof OsmDataLayer) { 67 OsmDataLayer osmDataLayer = (OsmDataLayer) activeLayer; 68 Node newNode = new Node(clickPosition); 69 newNode.put("bus", "yes"); 70 newNode.put("public_transport", "stop_position"); 71 osmDataLayer.data.addPrimitive(newNode); 72 osmDataLayer.data.setSelected(newNode); 73 Main.map.mapView.repaint(); 149 } 74 150 75 // make the stop position node part of the way: 76 JoinNodeWayAction joinNodeWayAction = JoinNodeWayAction.createJoinNodeToWayAction(); 77 joinNodeWayAction.actionPerformed(actionEventParameter); 151 //turn off what has been highlighted on last mouse move and highlight what has to be highlighted now 152 private void updateHighlights() 153 { 154 if(oldHighlights.size() > 0 || newHighlights.size() > 0) { 78 155 79 // split the way:80 SplitWayAction splitWayAction = new SplitWayAction();81 splitWayAction.actionPerformed(actionEventParameter);156 for(OsmPrimitive osm : oldHighlights) { 157 osm.setHighlighted(false); 158 } 82 159 83 } 160 for(OsmPrimitive osm : newHighlights) { 161 osm.setHighlighted(true); 162 } 84 163 85 Main.map.mapView.removeMouseListener(this); 86 Main.map.mapView.removeMouseMotionListener(this); 164 Main.getLayerManager().getEditLayer().invalidate(); 87 165 88 }89 }90 });91 166 oldHighlights.clear(); 167 oldHighlights.addAll(newHighlights); 168 newHighlights.clear(); 169 } 92 170 } 93 171
Note:
See TracChangeset
for help on using the changeset viewer.