Changeset 306 in josm


Ignore:
Timestamp:
2007-08-21T23:03:00+02:00 (17 years ago)
Author:
framm
Message:
  • modified "add node into segment" function to add an intersection node into two segments at the same time if you click on an intersection.
Location:
src/org/openstreetmap/josm
Files:
2 edited

Legend:

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

    r301 r306  
    99import java.util.ArrayList;
    1010import java.util.Collection;
     11import java.util.Collections;
    1112import java.util.HashMap;
    1213import java.util.LinkedList;
     
    101102                        if (s == null)
    102103                                return;
    103 
    104                         if ((e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) == 0) {
     104                       
     105                        // see if another segment is also near
     106                        Segment other = Main.map.mapView.getNearestSegment(e.getPoint(), Collections.singleton(s));
     107
     108                        if (other == null && (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) == 0) {
    105109                                // moving the new point to the perpendicular point
    106110                                EastNorth A = s.from.eastNorth;
     
    116120                        Collection<Command> cmds = new LinkedList<Command>();
    117121                        cmds.add(c);
    118                         Segment s1 = new Segment(s);
    119                         s1.to = n;
    120                         Segment s2 = new Segment(s.from, s.to);
    121                         s2.from = n;
    122                         if (s.keys != null)
    123                                 s2.keys = new HashMap<String, String>(s.keys);
    124 
    125                         cmds.add(new ChangeCommand(s, s1));
    126                         cmds.add(new AddCommand(s2));
    127 
    128                         // Add the segment to every way
    129                         for (Way wold : Main.ds.ways) {
    130                                 if (wold.segments.contains(s)) {
    131                                         Way wnew = new Way(wold);
    132                                         Collection<Segment> segs = new ArrayList<Segment>(wnew.segments);
    133                                         wnew.segments.clear();
    134                                         for (Segment waySeg : segs) {
    135                                                 wnew.segments.add(waySeg);
    136                                                 if (waySeg == s)
    137                                                         wnew.segments.add(s2);
    138                                         }
    139                                         cmds.add(new ChangeCommand(wold, wnew));
    140                                 }
    141                         }
    142 
    143                         c = new SequenceCommand(tr("Add node into segment"), cmds);
     122                       
     123                        // split the first segment
     124                        splitSegmentAtNode(s, n, cmds);
     125                       
     126                        // if a second segment was found, split that as well
     127                        if (other != null) splitSegmentAtNode(other, n, cmds);
     128
     129                        c = new SequenceCommand(tr((other == null) ?
     130                                "Add node into segment" : "Add common node into two segments"), cmds);
    144131                }
    145132
     
    194181                return way;
    195182        }
     183       
     184        private void splitSegmentAtNode(Segment s, Node n, Collection<Command> cmds) {
     185                Segment s1 = new Segment(s);
     186                s1.to = n;
     187                Segment s2 = new Segment(s.from, s.to);
     188                s2.from = n;
     189                if (s.keys != null)
     190                        s2.keys = new HashMap<String, String>(s.keys);
     191
     192                cmds.add(new ChangeCommand(s, s1));
     193                cmds.add(new AddCommand(s2));
     194
     195                // Add the segment to every way
     196                for (Way wold : Main.ds.ways) {
     197                        if (wold.segments.contains(s)) {
     198                                Way wnew = new Way(wold);
     199                                Collection<Segment> segs = new ArrayList<Segment>(wnew.segments);
     200                                wnew.segments.clear();
     201                                for (Segment waySeg : segs) {
     202                                        wnew.segments.add(waySeg);
     203                                        if (waySeg == s)
     204                                                wnew.segments.add(s2);
     205                                }
     206                                cmds.add(new ChangeCommand(wold, wnew));
     207                        }
     208                }
     209        }
    196210}
  • src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r298 r306  
    44import java.awt.Point;
    55import java.util.Collection;
     6import java.util.Collections;
    67import java.util.HashSet;
     8import java.util.List;
    79
    810import javax.swing.JComponent;
     
    172174
    173175        /**
    174          * @return the nearest segment to the screen point given.
     176         * @return the nearest segment to the screen point given
     177         *
     178         * @param p the point for which to search the nearest segment.
    175179         */
    176180        public final Segment getNearestSegment(Point p) {
     181                List<Segment> e = Collections.emptyList();
     182                return getNearestSegment(p, e);
     183        }
     184       
     185        /**
     186         * @return the nearest segment to the screen point given that is not
     187         * in ignoreThis.
     188         *
     189         * @param p the point for which to search the nearest segment.
     190         * @param ignore a collection of segments which are not to be returned. Must not be null.
     191         */
     192        public final Segment getNearestSegment(Point p, Collection<Segment> ignore) {
    177193                Segment minPrimitive = null;
    178194                double minDistanceSq = Double.MAX_VALUE;
    179195                // segments
    180196                for (Segment ls : Main.ds.segments) {
    181                         if (ls.deleted || ls.incomplete)
     197                        if (ls.deleted || ls.incomplete || ignore.contains(ls))
    182198                                continue;
    183199                        Point A = getPoint(ls.from.eastNorth);
Note: See TracChangeset for help on using the changeset viewer.