Ignore:
Timestamp:
2007-09-24T01:36:24+02:00 (17 years ago)
Author:
framm
Message:

This commit is a manual merge of all changes that have been made to
the intermediate "core_0.5" branch on the main OSM repository,
bevore JOSM was moved to openstreetmap.de.

Changes incorporated here:

r4464@svn.openstreetmap.org
r4466@svn.openstreetmap.org
r4468@svn.openstreetmap.org
r4469@svn.openstreetmap.org
r4479@svn.openstreetmap.org

File:
1 edited

Legend:

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

    r314 r329  
    2424import org.openstreetmap.josm.data.osm.Node;
    2525import org.openstreetmap.josm.data.osm.OsmPrimitive;
    26 import org.openstreetmap.josm.data.osm.Segment;
    2726import org.openstreetmap.josm.data.osm.Way;
     27import org.openstreetmap.josm.data.osm.WaySegment;
    2828import org.openstreetmap.josm.gui.MapFrame;
    2929import org.openstreetmap.josm.tools.ImageProvider;
     
    3333 * and there is it. Nothing more, nothing less.
    3434 *
     35 * FIXME: "nothing more, nothing less" is a bit out-of-date
     36 *
    3537 * Newly created nodes are selected. Shift modifier does not cancel the old
    3638 * selection as usual.
     
    4143public class AddNodeAction extends MapMode {
    4244
    43         enum Mode {node, nodesegment, autonode}
     45        enum Mode {node, nodeway, autonode}
    4446        private final Mode mode;
    4547
     
    4951                        putValue("help", "Action/AddNode");
    5052                        actions.add(new AddNodeAction(mf,tr("Add node"), Mode.node, tr("Add a new node to the map")));
    51                         actions.add(new AddNodeAction(mf, tr("Add node into segment"), Mode.nodesegment,tr( "Add a node into an existing segment")));
    52                         actions.add(new AddNodeAction(mf, tr("Add node and connect"), Mode.autonode,tr( "Add a node and connect it to the selected node (with CTRL: add node into segment; with SHIFT: re-use existing node)")));
     53                        actions.add(new AddNodeAction(mf, tr("Add node into way"), Mode.nodeway,tr( "Add a node into an existing way")));
     54                        actions.add(new AddNodeAction(mf, tr("Add node and connect"), Mode.autonode,tr( "Add a node and connect it to the selected node (with CTRL: add node into way; with SHIFT: re-use existing node)")));
    5355                        setCurrent(0);
    5456                }
     
    8385         * position.
    8486         *
    85          * If in nodesegment mode, add the node to the line segment by splitting the
    86          * segment. The new created segment will be inserted in every way the segment
    87          * was part of.
     87         * If in nodeway mode, insert the node into the way.
    8888         */
    8989        @Override public void mouseClicked(MouseEvent e) {
     
    9898
    9999                Command c = new AddCommand(n);
    100                 if (mode == Mode.nodesegment) {
    101                         Segment s = Main.map.mapView.getNearestSegment(e.getPoint());
    102                         if (s == null)
     100                if (mode == Mode.nodeway) {
     101                        WaySegment ws = Main.map.mapView.getNearestWaySegment(e.getPoint());
     102                        if (ws == null)
    103103                                return;
    104104                       
    105105                        // see if another segment is also near
    106                         Segment other = Main.map.mapView.getNearestSegment(e.getPoint(), Collections.singleton(s));
     106                        WaySegment other = Main.map.mapView.getNearestWaySegment(e.getPoint(),
     107                                Collections.singleton(ws));
     108
     109                        Node n1 = ws.way.nodes.get(ws.lowerIndex),
     110                                n2 = ws.way.nodes.get(ws.lowerIndex + 1);
    107111
    108112                        if (other == null && (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) == 0) {
    109113                                // moving the new point to the perpendicular point
    110                                 // FIXME: when two segments are split, should move the new point to the
     114                                // FIXME: when two way segments are split, should move the new point to the
    111115                                // intersection point!
    112                                 EastNorth A = s.from.eastNorth;
    113                                 EastNorth B = s.to.eastNorth;
     116                                EastNorth A = n1.eastNorth;
     117                                EastNorth B = n2.eastNorth;
    114118                                double ab = A.distance(B);
    115119                                double nb = n.eastNorth.distance(B);
     
    124128                       
    125129                        // split the first segment
    126                         splitSegmentAtNode(s, n, cmds);
     130                        splitWaySegmentAtNode(ws, n, cmds);
    127131                       
    128132                        // if a second segment was found, split that as well
    129                         if (other != null) splitSegmentAtNode(other, n, cmds);
     133                        if (other != null) splitWaySegmentAtNode(other, n, cmds);
    130134
    131135                        c = new SequenceCommand(tr((other == null) ?
    132                                 "Add node into segment" : "Add common node into two segments"), cmds);
     136                                "Add node into way" : "Add common node into two ways"), cmds);
    133137                }
    134138
     
    136140                if (mode == Mode.autonode) {
    137141
    138                         Segment insertInto = null;
     142                        WaySegment insertInto = null;
    139143                        Node reuseNode = null;
    140144                       
    141                         // If CTRL is held, insert the node into a potentially existing segment
     145                        // If CTRL is held, insert the node into a potentially existing way segment
    142146                        if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) != 0) {
    143                                 insertInto = Main.map.mapView.getNearestSegment(e.getPoint());
     147                                insertInto = Main.map.mapView.getNearestWaySegment(e.getPoint());
     148                                if (insertInto == null) System.err.println("Couldn't find nearby way segment");
    144149                                if (insertInto == null)
    145150                                        return;
     
    150155                        // continuation of the "add node and connect" stuff)
    151156                        else if ((e.getModifiersEx() & MouseEvent.SHIFT_DOWN_MASK) != 0) {
    152                                 OsmPrimitive clicked = Main.map.mapView.getNearest(e.getPoint(), false);
     157                                OsmPrimitive clicked = Main.map.mapView.getNearest(e.getPoint());
    153158                                if (clicked == null || !(clicked instanceof Node))
    154159                                        return;
     
    159164                        if (selection.size() == 1 && selection.iterator().next() instanceof Node) {
    160165                                Node n1 = (Node)selection.iterator().next();
     166
    161167                                Collection<Command> cmds = new LinkedList<Command>();
    162168                               
    163169                                if (reuseNode != null) {
    164170                                        // in re-use node mode, n1 must not be identical to clicked node
     171                                        if (n1 == reuseNode) System.err.println("n1 == reuseNode");
    165172                                        if (n1 == reuseNode) return;
    166173                                        // replace newly created node with existing node
     
    171178                                }
    172179                               
    173                                 Segment s = new Segment(n1, n);
    174                                
     180                                /* Keep track of the way we change, it might be the same into
     181                                 * which we insert the node.
     182                                 */
     183                                Way newInsertInto = null;
    175184                                if (insertInto != null)
    176                                         splitSegmentAtNode(insertInto, n, cmds);
    177                                
    178                                 cmds.add(new AddCommand(s));                   
     185                                        newInsertInto = splitWaySegmentAtNode(insertInto, n, cmds);
    179186
    180187                                Way way = getWayForNode(n1);
    181                                 if (way != null) {
    182                                         Way newWay = new Way(way);
    183                                         if (way.segments.get(0).from == n1) {
    184                                                 Node tmp = s.from;
    185                                                 s.from = s.to;
    186                                                 s.to = tmp;
    187                                                 newWay.segments.add(0, s);
    188                                         } else
    189                                                 newWay.segments.add(s);
    190                                         cmds.add(new ChangeCommand(way, newWay));
    191                                 }
    192 
    193                                 c = new SequenceCommand(tr((insertInto == null) ? "Add node and connect" : "Add node into segment and connect"), cmds);
     188                                if (way == null) {
     189                                        way = new Way();
     190                                        way.nodes.add(n1);
     191                                        cmds.add(new AddCommand(way));
     192                                } else {
     193                                        if (insertInto != null && way == insertInto.way) {
     194                                                way = newInsertInto;
     195                                        } else {
     196                                                Way wnew = new Way(way);
     197                                                cmds.add(new ChangeCommand(way, wnew));
     198                                                way = wnew;
     199                                        }
     200                                }
     201
     202                                if (way.nodes.get(way.nodes.size() - 1) == n1) {
     203                                        way.nodes.add(n);
     204                                } else {
     205                                        way.nodes.add(0, n);
     206                                }
     207
     208                                c = new SequenceCommand(tr((insertInto == null) ? "Add node and connect" : "Add node into way and connect"), cmds);
    194209                        }       
    195210                }               
     
    201216       
    202217        /**
    203          * @return If the node is part of exactly one way, return this.
     218         * @return If the node is the end of exactly one way, return this.
    204219         *      <code>null</code> otherwise.
    205220         */
     
    207222                Way way = null;
    208223                for (Way w : Main.ds.ways) {
    209                         for (Segment s : w.segments) {
    210                                 if (s.from == n || s.to == n) {
     224                        int i = w.nodes.indexOf(n);
     225                        if (i == -1) continue;
     226                        if (i == 0 || i == w.nodes.size() - 1) {
    211227                                        if (way != null)
    212228                                                return null;
    213                                         if (s.from == s.to)
    214                                                 return null;
    215229                                        way = w;
    216230                                }
    217231                        }
    218                 }
    219232                return way;
    220233        }
    221234       
    222         private void splitSegmentAtNode(Segment s, Node n, Collection<Command> cmds) {
    223                 Segment s1 = new Segment(s);
    224                 s1.to = n;
    225                 Segment s2 = new Segment(s.from, s.to);
    226                 s2.from = n;
    227                 if (s.keys != null)
    228                         s2.keys = new HashMap<String, String>(s.keys);
    229 
    230                 cmds.add(new ChangeCommand(s, s1));
    231                 cmds.add(new AddCommand(s2));
    232 
    233                 // Add the segment to every way
    234                 for (Way wold : Main.ds.ways) {
    235                         if (wold.segments.contains(s)) {
    236                                 Way wnew = new Way(wold);
    237                                 Collection<Segment> segs = new ArrayList<Segment>(wnew.segments);
    238                                 wnew.segments.clear();
    239                                 for (Segment waySeg : segs) {
    240                                         wnew.segments.add(waySeg);
    241                                         if (waySeg == s)
    242                                                 wnew.segments.add(s2);
    243                                 }
    244                                 cmds.add(new ChangeCommand(wold, wnew));
    245                         }
    246                 }
     235        private Way splitWaySegmentAtNode(WaySegment ws, Node n, Collection<Command> cmds) {
     236                Way wnew = new Way(ws.way);
     237                wnew.nodes.add(ws.lowerIndex + 1, n);
     238                cmds.add(new ChangeCommand(ws.way, wnew));
     239                return wnew;
    247240        }
    248241}
Note: See TracChangeset for help on using the changeset viewer.