Ignore:
Timestamp:
2007-09-05T23:55:17+02:00 (18 years ago)
Author:
frederik
Message:

initial changeset for 0.5 functionality. this version works with 0.5 as specified but there are some minor bugs to be fixed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/core_0.5/src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java

    r4437 r4464  
    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;
    2827import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
     
    3837 * @see #deleteWithReferences(OsmPrimitive)
    3938 *
    40  * Pressing Alt will select the way instead of a segment, as usual.
    41  *
    4239 * If the user did not press Ctrl and the object has any references, the user
    4340 * is informed and nothing is deleted.
     
    5754                super(tr("Delete"),
    5855                                "delete",
    59                                 tr("Delete nodes, streets or segments."),
     56                                tr("Delete nodes or ways."),
    6057                                KeyEvent.VK_D,
    6158                                mapFrame,
     
    9289                        return;
    9390               
    94                 OsmPrimitive sel = Main.map.mapView.getNearest(e.getPoint(), (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0);
     91                OsmPrimitive sel = Main.map.mapView.getNearest(e.getPoint());
    9592                if (sel == null)
    9693                        return;
     
    105102
    106103        /**
    107          * Delete the primitives and everything they references.
     104         * Delete the primitives and everything they reference.
    108105         *
    109          * If a node is deleted, the node and all segments, ways and areas
     106         * If a node is deleted, the node and all ways and relations
    110107         * the node is part of are deleted as well.
    111108         *
    112          * If a segment is deleted, all ways the segment is part of
    113          * are deleted as well. No nodes are deleted.
     109         * If a way is deleted, all relations the way is member of are also deleted.
    114110         *
    115          * If a way is deleted, only the way and no segments or nodes are
    116          * deleted.
    117          *
    118          * If an area is deleted, only the area gets deleted.
     111         * If a way is deleted, only the way and no nodes are deleted.
    119112         *
    120113         * @param selection The list of all object to be deleted.
     
    134127         * inform the user and do not delete.
    135128         *
    136          * If deleting a node which is part of exactly two segments, and both segments
    137          * have no conflicting keys, join them and remove the node.
    138          * If the two segments are part of the same way, remove the deleted segment
    139          * from the way.
     129         * If a node is to be deleted which is in the middle of exactly one way,
     130         * the node is removed from the way's node list and after that removed
     131         * itself.
    140132         *
    141133         * @param selection The objects to delete.
     
    149141                        if (!selection.containsAll(v.data)) {
    150142                                if (osm instanceof Node && joinIfPossible) {
    151                                         String reason = deleteNodeAndJoinSegment((Node)osm);
     143                                        String reason = deleteNodeAndJoinWay((Node)osm);
    152144                                        if (reason != null && msgBox) {
    153145                                                JOptionPane.showMessageDialog(Main.parent,tr("Cannot delete node.")+" "+reason);
     
    167159        }
    168160
    169         private String deleteNodeAndJoinSegment(Node n) {
    170                 ArrayList<Segment> segs = new ArrayList<Segment>(2);
    171                 for (Segment s : Main.ds.segments) {
    172                         if (!s.deleted && (s.from == n || s.to == n)) {
    173                                 if (segs.size() > 1)
    174                                         return tr("Used by more than two segments.");
    175                                 segs.add(s);
    176                         }
    177                 }
    178                 if (segs.size() != 2)
    179                         return tr("Used by only one segment.");
    180                 Segment seg1 = segs.get(0);
    181                 Segment seg2 = segs.get(1);
    182                 if (seg1.from == seg2.to) {
    183                         Segment s = seg1;
    184                         seg1 = seg2;
    185                         seg2 = s;
    186                 }
    187                 if (seg1.from == seg2.from || seg1.to == seg2.to)
    188                         return tr("Wrong direction of segments.");
    189                 for (Entry<String, String> e : seg1.entrySet())
    190                         if (seg2.keySet().contains(e.getKey()) && !seg2.get(e.getKey()).equals(e.getValue()))
    191                                 return tr("Conflicting keys");
    192                 ArrayList<Way> ways = new ArrayList<Way>(2);
     161        private String deleteNodeAndJoinWay(Node n) {
     162                ArrayList<Way> ways = new ArrayList<Way>(1);
    193163                for (Way w : Main.ds.ways) {
    194                         if (w.deleted)
    195                                 continue;
    196                         if ((w.segments.contains(seg1) && !w.segments.contains(seg2)) || (w.segments.contains(seg2) && !w.segments.contains(seg1)))
    197                                 return tr("Segments are part of different ways.");
    198                         if (w.segments.contains(seg1) && w.segments.contains(seg2))
     164                        if (!w.deleted && w.nodes.contains(n)) {
    199165                                ways.add(w);
    200166                }
    201                 Segment s = new Segment(seg1);
    202                 s.to = seg2.to;
    203                 if (s.keys == null)
    204                         s.keys = seg2.keys;
    205                 else if (seg2.keys != null)
    206                         s.keys.putAll(seg2.keys);
     167                }
     168
     169                if (ways.size() > 1)
     170                        return tr("Used by more than one way.");
     171                Way w = ways.get(0);
     172
     173                int i = w.nodes.indexOf(n);
     174                if (w.nodes.lastIndexOf(n) != i)
     175                        return tr("Occurs more than once in the same way.");
     176                if (i == 0 || i == w.nodes.size() - 1)
     177                        return tr("Is at the end of a way");
     178
     179                Way wnew = new Way(w);
     180                wnew.nodes.remove(i);
     181
    207182                Collection<Command> cmds = new LinkedList<Command>();
    208                 for (Way w : ways) {
    209                         Way copy = new Way(w);
    210                         copy.segments.remove(seg2);
    211                         cmds.add(new ChangeCommand(w, copy));
    212                 }
    213                 cmds.add(new ChangeCommand(seg1, s));
    214                 cmds.add(new DeleteCommand(Arrays.asList(new OsmPrimitive[]{n, seg2})));
     183                cmds.add(new ChangeCommand(w, wnew));
     184                cmds.add(new DeleteCommand(Collections.singleton(n)));
    215185                Main.main.undoRedo.add(new SequenceCommand(tr("Delete Node"), cmds));
    216186                return null;
Note: See TracChangeset for help on using the changeset viewer.