Changeset 170 in josm


Ignore:
Timestamp:
2006-11-28T20:30:37+01:00 (18 years ago)
Author:
imi
Message:
  • added new mapmode "Add node and connect"
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/mapmode/AddNodeAction.java

    r155 r170  
    2121import org.openstreetmap.josm.data.coor.EastNorth;
    2222import org.openstreetmap.josm.data.osm.Node;
     23import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2324import org.openstreetmap.josm.data.osm.Segment;
    2425import org.openstreetmap.josm.data.osm.Way;
     
    3839public class AddNodeAction extends MapMode {
    3940
    40         enum Mode {node, nodesegment}
     41        enum Mode {node, nodesegment, autonode}
    4142        private final Mode mode;
    4243
     
    4748                        actions.add(new AddNodeAction(mf,tr("Add node"), Mode.node, tr("Add a new node to the map")));
    4849                        actions.add(new AddNodeAction(mf, tr("Add node into segment"), Mode.nodesegment,tr( "Add a node into an existing segment")));
     50                        actions.add(new AddNodeAction(mf, tr("Add node and connect"), Mode.autonode,tr( "Add a node and connect it to the selected (previous added) node")));
    4951                        setCurrent(0);
    5052                }
     
    132134                        }
    133135
    134                         c = new SequenceCommand(tr("Add Node into Segment"), cmds);
     136                        c = new SequenceCommand(tr("Add node into segment"), cmds);
    135137                }
     138
     139                if (mode == Mode.autonode) {
     140                        Collection<OsmPrimitive> selection = Main.ds.getSelected();
     141                        if (selection.size() == 1 && selection.iterator().next() instanceof Node) {
     142                                Node n1 = (Node)selection.iterator().next();
     143                                Collection<Command> cmds = new LinkedList<Command>();
     144                                Segment s = new Segment(n1, n);
     145                                cmds.add(c);                           
     146                                cmds.add(new AddCommand(s));                   
     147
     148                                Way way = getWayForNode(n1);
     149                                if (way != null) {
     150                                        Way newWay = new Way(way);
     151                                        if (way.segments.get(0).from == n1)
     152                                                newWay.segments.add(0, s);
     153                                        else
     154                                                newWay.segments.add(s);
     155                                        cmds.add(new ChangeCommand(way, newWay));
     156                                }
     157
     158                                c = new SequenceCommand(tr("Add node and connect"), cmds);
     159                        }       
     160                }               
     161       
    136162                Main.main.editLayer().add(c);
    137163                Main.ds.setSelected(n);
    138164                Main.map.mapView.repaint();
    139165        }
     166       
     167        /**
     168         * @return If the node is part of exactly one way, return this.
     169         *      <code>null</code> otherwise.
     170         */
     171        private Way getWayForNode(Node n) {
     172                Way way = null;
     173                for (Way w : Main.ds.ways) {
     174                        for (Segment s : w.segments) {
     175                                if (s.from == n || s.to == n) {
     176                                        if (way != null)
     177                                                return null;
     178                                        if (s.from == s.to)
     179                                                return null;
     180                                        way = w;
     181                                }
     182                        }
     183                }
     184                return way;
     185        }
    140186}
Note: See TracChangeset for help on using the changeset viewer.