Ignore:
Timestamp:
2017-03-15T10:41:40+01:00 (7 years ago)
Author:
giackserva
Message:

add stop position is now a mapmode

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  
    77import org.openstreetmap.josm.Main;
    88import org.openstreetmap.josm.data.validation.OsmValidator;
     9import org.openstreetmap.josm.gui.IconToggleButton;
    910import org.openstreetmap.josm.gui.MainMenu;
    1011import org.openstreetmap.josm.gui.MapFrame;
     
    3233    private static PTRouteSegment lastFix;
    3334
    34     /* item of the Tools menu for adding stop_positions */
    35     private JMenuItem addStopPositionMenu;
    36 
    3735    /* item of the Tools menu for repeating the last fix */
    3836    private static JMenuItem repeatLastFixMenu;
     
    5048        OsmValidator.addTest(PTAssistantValidatorTest.class);
    5149
    52         AddStopPositionAction addStopPositionAction = new AddStopPositionAction();
    53         addStopPositionMenu = MainMenu.add(Main.main.menu.toolsMenu, addStopPositionAction, false);
    5450        RepeatLastFixAction repeatLastFixAction = new RepeatLastFixAction();
    5551        repeatLastFixMenu = MainMenu.add(Main.main.menu.toolsMenu, repeatLastFixAction, false);
    56 
    5752    }
    5853
     
    6358    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    6459        if (oldFrame == null && newFrame != null) {
    65             addStopPositionMenu.setEnabled(true);
    6660            repeatLastFixMenu.setEnabled(false);
     61            Main.map.addMapMode(new IconToggleButton(new AddStopPositionAction(Main.map)));
    6762        } else if (oldFrame != null && newFrame == null) {
    68             addStopPositionMenu.setEnabled(false);
    6963            repeatLastFixMenu.setEnabled(false);
    7064        }
  • 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.
    21package org.openstreetmap.josm.plugins.pt_assistant.actions;
    32
    43import static org.openstreetmap.josm.tools.I18n.tr;
    54
    6 import java.awt.event.ActionEvent;
     5import java.awt.Cursor;
    76import java.awt.event.KeyEvent;
    8 import java.awt.event.MouseAdapter;
    97import java.awt.event.MouseEvent;
     8import java.util.HashSet;
     9import java.util.List;
     10import java.util.Set;
    1011
    1112import org.openstreetmap.josm.Main;
    1213import org.openstreetmap.josm.actions.JoinNodeWayAction;
    13 import org.openstreetmap.josm.actions.JosmAction;
    1414import org.openstreetmap.josm.actions.SplitWayAction;
    15 import org.openstreetmap.josm.data.coor.LatLon;
     15import org.openstreetmap.josm.actions.mapmode.MapMode;
     16import org.openstreetmap.josm.command.AddCommand;
     17import org.openstreetmap.josm.command.ChangeCommand;
     18import org.openstreetmap.josm.data.osm.DataSet;
    1619import org.openstreetmap.josm.data.osm.Node;
    17 import org.openstreetmap.josm.gui.layer.Layer;
    18 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     20import org.openstreetmap.josm.data.osm.OsmPrimitive;
     21import org.openstreetmap.josm.data.osm.WaySegment;
     22import org.openstreetmap.josm.gui.MapFrame;
    1923import org.openstreetmap.josm.tools.ImageProvider;
    2024import org.openstreetmap.josm.tools.Shortcut;
    2125
    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.
    2729 */
    28 public class AddStopPositionAction extends JosmAction {
     30@SuppressWarnings("serial")
     31public class AddStopPositionAction extends MapMode {
    2932
    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<>();
    3435
    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;
    3938
     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);
    4060    }
    4161
    42     /**
    43      * Actions that add the new node, set tags and update the map frame.
    44      */
    4562    @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    }
    4768
    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);
    50118        }
    51119
    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");
    53123
    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        }
    55129
    56             LatLon clickPosition;
     130        DataSet ds = Main.getLayerManager().getEditLayer().data;
     131        ds.setSelected(newStopPos);
    57132
    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        }
    60138
    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    }
    63143
    64                     Layer activeLayer = Main.getLayerManager().getActiveLayer();
     144    private void clearNodeTags(Node newStopPos) {
     145                for(String key : newStopPos.keySet()) {
     146                        newStopPos.put(key, null);
     147                }
    65148
    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        }
    74150
    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) {
    78155
    79                         // split the way:
    80                         SplitWayAction splitWayAction = new SplitWayAction();
    81                         splitWayAction.actionPerformed(actionEventParameter);
     156                for(OsmPrimitive osm : oldHighlights) {
     157                        osm.setHighlighted(false);
     158                }
    82159
    83                     }
     160                for(OsmPrimitive osm : newHighlights) {
     161                        osm.setHighlighted(true);
     162                }
    84163
    85                     Main.map.mapView.removeMouseListener(this);
    86                     Main.map.mapView.removeMouseMotionListener(this);
     164                Main.getLayerManager().getEditLayer().invalidate();
    87165
    88                 }
    89             }
    90         });
    91 
     166                oldHighlights.clear();
     167                oldHighlights.addAll(newHighlights);
     168                newHighlights.clear();
     169        }
    92170    }
    93171
Note: See TracChangeset for help on using the changeset viewer.