Changeset 30 in josm for src/org/openstreetmap/josm/command


Ignore:
Timestamp:
2005-12-03T14:14:35+01:00 (19 years ago)
Author:
imi
Message:
  • Removed edit layer, combine action, save gpx (integrated in normal save)
  • Simplified and unified shortkeys
  • many small code simplifications
  • added undo
  • broken checkin!
Location:
src/org/openstreetmap/josm/command
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/command/AddCommand.java

    r23 r30  
    33import java.awt.Component;
    44import java.util.Collection;
    5 import java.util.Iterator;
    65
    76import javax.swing.JLabel;
    87
    9 import org.openstreetmap.josm.Main;
     8import org.openstreetmap.josm.data.osm.DataSet;
    109import org.openstreetmap.josm.data.osm.Key;
    1110import org.openstreetmap.josm.data.osm.LineSegment;
     
    2221 * @author imi
    2322 */
    24 public class AddCommand implements Command, Visitor {
     23public class AddCommand implements Command {
    2524
     25        /**
     26         * The dataset this command operates on.
     27         */
     28        DataSet ds;
     29
     30        /**
     31         * Helper that adds the object
     32         * @author imi
     33         */
     34        private final class AddVisitor implements Visitor {
     35                public void visit(Node n) {ds.nodes.add(n);}
     36                public void visit(LineSegment ls) {ds.lineSegments.add(ls);}
     37                public void visit(Track t) {ds.tracks.add(t);}
     38                public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");}
     39        }
     40
     41        /**
     42         * Helper that deletes the object (for undo)
     43         * @author imi
     44         */
     45        private final class RemoveVisitor implements Visitor {
     46                public void visit(Node n) {ds.nodes.remove(n);}
     47                public void visit(LineSegment ls) {ds.lineSegments.remove(ls);}
     48                public void visit(Track t) {ds.tracks.remove(t);}
     49                public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");}
     50        }
     51       
    2652        /**
    2753         * The primitive to add to the dataset.
     
    3258         * Create the command and specify the element to add.
    3359         */
    34         public AddCommand(OsmPrimitive osm) {
     60        public AddCommand(DataSet ds, OsmPrimitive osm) {
     61                this.ds = ds;
    3562                this.osm = osm;
    3663        }
    3764
    3865        public void executeCommand() {
    39                 osm.visit(this);
     66                osm.visit(new AddVisitor());
    4067        }
    41        
     68
     69        public void undoCommand() {
     70                osm.visit(new RemoveVisitor());
     71        }
     72
    4273        public Component commandDescription() {
    4374                SelectionComponentVisitor v = new SelectionComponentVisitor();
     
    4778       
    4879        public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
    49                 if (added != null && !added.contains(osm))
    50                         added.add(osm);
    51         }
    52 
    53         /**
    54          * Add the node to the nodes - list only.
    55          * @param n The node to add.
    56          */
    57         public void visit(Node n) {
    58                 Main.main.ds.nodes.add(n);
    59         }
    60 
    61         /**
    62          * Add the line segment to the list of pending line segments.
    63          * @param ls The line segment to add.
    64          */
    65         public void visit(LineSegment ls) {
    66                 Main.main.ds.pendingLineSegments.add(ls);
    67                 Main.main.ds.addBackReference(ls.start, ls);
    68                 Main.main.ds.addBackReference(ls.end, ls);
    69         }
    70 
    71         /**
    72          * Add the track to the dataset. Remove all line segments that were pending
    73          * from the dataset.
    74          */
    75         public void visit(Track t) {
    76                 Main.main.ds.tracks.add(t);
    77                 for (Iterator<LineSegment> it =  Main.main.ds.pendingLineSegments.iterator(); it.hasNext();)
    78                         if (t.segments.contains(it.next()))
    79                                 it.remove();
    80                 for (LineSegment ls : t.segments) {
    81                         Main.main.ds.addBackReference(ls, t);
    82                         Main.main.ds.addBackReference(ls.start, t);
    83                         Main.main.ds.addBackReference(ls.end, t);
    84                 }
    85         }
    86 
    87         /**
    88          * Add the key to the parent specified by the constructor
    89          */
    90         public void visit(Key k) {
    91                 throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");
     80                added.add(osm);
    9281        }
    9382}
  • src/org/openstreetmap/josm/command/ChangeKeyValueCommand.java

    r24 r30  
    44import java.util.Collection;
    55import java.util.HashMap;
     6import java.util.Iterator;
     7import java.util.LinkedList;
     8import java.util.List;
     9import java.util.Map;
    610
    711import javax.swing.JLabel;
     
    2125         * All primitives, that are affected with this command.
    2226         */
    23         private final Collection<OsmPrimitive> objects;
     27        private final List<OsmPrimitive> objects;
    2428        /**
    2529         * The key that is subject to change.
     
    3236         */
    3337        private final String value;
     38       
     39        /**
     40         * These are the old values of the objects to do a proper undo.
     41         */
     42        private List<Map<Key, String>> oldProperties;
    3443
    3544        public ChangeKeyValueCommand(Collection<OsmPrimitive> objects, Key key, String value) {
    36                 this.objects = objects;
     45                this.objects = new LinkedList<OsmPrimitive>(objects);
    3746                this.key = key;
    3847                this.value = value;
     
    4049       
    4150        public void executeCommand() {
     51                // save old
     52                oldProperties = new LinkedList<Map<Key, String>>();
     53                for (OsmPrimitive osm : objects)
     54                        oldProperties.add(osm.keys == null ? null : new HashMap<Key, String>(osm.keys));
     55                       
    4256                if (value == null) {
    4357                        for (OsmPrimitive osm : objects) {
     
    5771        }
    5872
     73        public void undoCommand() {
     74                Iterator<Map<Key, String>> it = oldProperties.iterator();
     75                for (OsmPrimitive osm : objects)
     76                        osm.keys = it.next();
     77        }
     78
    5979        public Component commandDescription() {
    6080                String objStr = objects.size()+" object" + (objects.size()==1?"":"s");
  • src/org/openstreetmap/josm/command/CombineAndDeleteCommand.java

    r23 r30  
    33import java.awt.Component;
    44import java.util.Collection;
     5import java.util.HashMap;
     6import java.util.Iterator;
     7import java.util.LinkedList;
     8import java.util.List;
    59import java.util.Map;
    610
    711import javax.swing.JLabel;
    812
    9 import org.openstreetmap.josm.Main;
    1013import org.openstreetmap.josm.data.osm.DataSet;
    1114import org.openstreetmap.josm.data.osm.Key;
    1215import org.openstreetmap.josm.data.osm.LineSegment;
    13 import org.openstreetmap.josm.data.osm.Node;
    1416import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1517import org.openstreetmap.josm.data.osm.Track;
     
    1921 * This is a combination of first combining objects to get a node free of
    2022 * references and then delete that node. It is used by the delete action.
     23 *
     24 * The rules is as follow:
     25 * If the node to delete is between exact two line segments, which are
     26 * in a straight (not pointing together), the second line segment is deleted
     27 * and the first now spans to the last node of the second line segment.
     28 *
    2129 * @author imi
    2230 */
     
    2432
    2533        /**
    26          * This class is used as one line segment pair that needs to get combined
    27          * for the node to be deleted.
    28          * @author imi
     34         * The dataset, this command operates on.
    2935         */
    30         public static class LineSegmentCombineEntry {
    31                 public LineSegment first, second;
    32                 public Track track;
    33         }
     36        private DataSet ds;
     37        /**
     38         * This line segment is combined with the second line segment.
     39         * The node that get deleted is the end of this segment.
     40         */
     41        private LineSegment first;
     42        /**
     43         * This line segment is deleted by the combining.
     44         * The node that get deleted is the start of this segment.
     45         */
     46        private LineSegment second;
     47
     48        /**
     49         * The tracks (if any) the line segments are part of.
     50         */
     51        private List<Track> track;
     52
     53       
     54        // stuff for undo
     55
     56        /**
     57         * The old properties of the first line segment (for undo)
     58         */
     59        private Map<Key, String> oldProperties;
     60        /**
     61         * The positions of the second line segment in the tracks (if any track)
     62         */
     63        private List<Integer> lineSegmentTrackPos;
    3464       
    3565        /**
    36          * The node that get deleted
     66         * Create the command and assign the data entries.
     67         * @param ds     The dataset this command operates on.
     68         * @param first  The line segment that remain alive
     69         * @param second The line segment that get deleted
    3770         */
    38         private Node node;
    39         /**
    40          * These line segments are
    41          */
    42         private Collection<LineSegmentCombineEntry> combineLineSegments;
    43         /**
    44          * These tracks are combined
    45          */
    46         private Track firstTrack, secondTrack;
    47         /**
    48          * This line segment is deleted together with the second track. It was the
    49          * first segment of the second track (the other line segments were integrated
    50          * into the first track).
    51          */
    52         private LineSegment firstOfSecond;
    53 
    54         /**
    55          * Create the command and assign the data entries.
    56          */
    57         public CombineAndDeleteCommand(Node nodeToDelete,
    58                         Collection<LineSegmentCombineEntry> combineLineSegments,
    59                         Track firstTrack, Track secondTrack) {
    60                 node = nodeToDelete;
    61                 this.combineLineSegments = combineLineSegments;
    62                 this.firstTrack = firstTrack;
    63                 this.secondTrack = secondTrack;
     71        public CombineAndDeleteCommand(DataSet ds, LineSegment first, LineSegment second) {
     72                this.ds = ds;
     73                this.first = first;
     74                this.second = second;
     75                if (first.end != second.start)
     76                        throw new IllegalArgumentException();
    6477        }
    6578       
    6679        public void executeCommand() {
    67                 // line segments
    68                 DataSet ds = Main.main.ds;
    69                 for (LineSegmentCombineEntry e : combineLineSegments) {
    70                         if (e.first.start == e.second.end) {
    71                                 LineSegment tmp = e.first;
    72                                 e.first = e.second;
    73                                 e.second = tmp;
     80                first.end = second.end;
     81                oldProperties = new HashMap<Key, String>(first.keys);
     82                first.keys = mergeKeys(first.keys, second.keys);
     83
     84                // delete second line segment
     85                for (Track t : ds.tracks) {
     86                        if (t.segments.contains(second)) {
     87                                if (track == null)
     88                                        track = new LinkedList<Track>();
     89                                track.add(t);
    7490                        }
    75                         e.first.end = e.second.end;
    76                         e.first.keys = mergeKeys(e.first.keys, e.second.keys);
    77                         e.track.segments.remove(e.second);
    7891                }
     92                if (track != null) {
     93                        lineSegmentTrackPos = new LinkedList<Integer>();
     94                        for (Track t : track) {
     95                                int i = t.segments.indexOf(second);
     96                                if (i != -1)
     97                                        t.segments.remove(second);
     98                                lineSegmentTrackPos.add(i);
     99                        }
     100                }
     101                ds.lineSegments.remove(second);
    79102               
    80                 // tracks
    81                 if (firstTrack != null && secondTrack != null) {
    82                         if (firstTrack.getStartingNode() == secondTrack.getEndingNode()) {
    83                                 Track t = firstTrack;
    84                                 firstTrack = secondTrack;
    85                                 secondTrack = t;
     103                // delete node
     104                ds.nodes.remove(second.start);
     105        }
     106
     107        public void undoCommand() {
     108                ds.nodes.add(second.start);
     109                ds.lineSegments.add(second);
     110               
     111                if (track != null) {
     112                        Iterator<Track> it = track.iterator();
     113                        for (int i : lineSegmentTrackPos) {
     114                                Track t = it.next();
     115                                if (i != -1)
     116                                        t.segments.add(i, second);
    86117                        }
    87                         // concatenate the line segments.
    88                         LineSegment lastOfFirst = firstTrack.getEndingSegment();
    89                         firstOfSecond = secondTrack.getStartingSegment();
    90                         lastOfFirst.end = firstOfSecond.end;
    91                         lastOfFirst.keys = mergeKeys(lastOfFirst.keys, firstOfSecond.keys);
    92                         secondTrack.segments.remove(firstOfSecond);
    93                         // move the remaining line segments to first track.
    94                         firstTrack.segments.addAll(secondTrack.segments);
    95                         ds.tracks.remove(secondTrack);
    96118                }
    97                 ds.nodes.remove(node);
    98                 ds.rebuildBackReferences();
     119                first.keys = oldProperties;
     120                first.end = second.start;
    99121        }
    100122
     
    115137        public Component commandDescription() {
    116138                SelectionComponentVisitor v = new SelectionComponentVisitor();
    117                 v.visit(node);
     139                v.visit(second.start);
    118140                return new JLabel("Remove "+v.name, v.icon, JLabel.LEADING);
    119141        }
    120142
    121143        public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
    122                 deleted.add(node);
    123                 if (firstTrack != null)
    124                         modified.add(firstTrack);
    125                 if (secondTrack != null)
    126                         deleted.add(secondTrack);
    127                 if (firstOfSecond != null)
    128                         deleted.add(firstOfSecond);
    129                 for (LineSegmentCombineEntry e : combineLineSegments) {
    130                         modified.add(e.first);
    131                         deleted.add(e.second);
    132                         modified.add(e.track);
    133                 }
     144                deleted.add(second);
     145                deleted.add(second.start);
     146                modified.add(first);
     147                if (track != null)
     148                        modified.addAll(track);
    134149        }
    135150
  • src/org/openstreetmap/josm/command/Command.java

    r23 r30  
    1111 * one atomic action on a specific dataset, such as move or delete.
    1212 *
     13 * Remember, that the command must be executable and undoable, even if the
     14 * Main.main.ds has changed, so the command must save the dataset it operates on
     15 * if necessary.
     16 *
    1317 * @author imi
    1418 */
     
    2125
    2226        /**
     27         * Undoes the command.
     28         * It can be assumed, that all objects are in the same state they were before.
     29         * It can also be assumed that executeCommand was called exactly once before.
     30         */
     31        void undoCommand();
     32       
     33        /**
    2334         * Give a description of the command as component to draw
    2435         */
     
    2637       
    2738        /**
    28          * Fill in the changed data this command operates on (for sending to the server).
    29          * Add to the lists, don't clear them. The lists can be <code>null</code>
    30          * in which case they are ignored.
     39         * Fill in the changed data this command operates on.
     40         * Add to the lists, don't clear them.
    3141         *
    32          * @param modified  The modified primitives or <code>null</code>
    33          * @param deleted   The deleted primitives or <code>null</code>
    34          * @param added         The added primitives or <code>null</code>
     42         * @param modified  The modified primitives
     43         * @param deleted   The deleted primitives
     44         * @param added         The added primitives
    3545         */
    3646        void fillModifiedData(Collection<OsmPrimitive> modified,
  • src/org/openstreetmap/josm/command/DeleteCommand.java

    r23 r30  
    33import java.awt.Component;
    44import java.util.Collection;
    5 import java.util.LinkedList;
    65
    76import javax.swing.JLabel;
    87
    9 import org.openstreetmap.josm.Main;
     8import org.openstreetmap.josm.data.osm.DataSet;
    109import org.openstreetmap.josm.data.osm.Key;
    1110import org.openstreetmap.josm.data.osm.LineSegment;
     
    1918 * @author imi
    2019 */
    21 public class DeleteCommand implements Command, Visitor {
     20public class DeleteCommand implements Command {
    2221
     22        /**
     23         * The dataset this command operates on.
     24         */
     25        DataSet ds;
     26
     27        /**
     28         * Helper that adds the object.
     29         * @author imi
     30         */
     31        private final class AddVisitor implements Visitor {
     32                public void visit(Node n) {ds.nodes.add(n);}
     33                public void visit(LineSegment ls) {ds.lineSegments.add(ls);}
     34                public void visit(Track t) {ds.tracks.add(t);}
     35                public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");}
     36        }
     37
     38        /**
     39         * Helper that deletes the object. Does not respect back reference cache.
     40         * @author imi
     41         */
     42        private final class DeleteVisitor implements Visitor {
     43                public void visit(Node n) {ds.nodes.remove(n);}
     44                public void visit(LineSegment ls) {ds.lineSegments.remove(ls);}
     45                public void visit(Track t) {ds.tracks.remove(t);}
     46                public void visit(Key k) {throw new IllegalStateException("Keys are added by using ChangeKeyValueCommand");}
     47        }
     48       
     49       
     50       
    2351        /**
    2452         * The primitives that are going to deleted.
     
    2654        private final Collection<OsmPrimitive> data;
    2755       
    28         public DeleteCommand(Collection<OsmPrimitive> data) {
     56        public DeleteCommand(DataSet ds, Collection<OsmPrimitive> data) {
     57                this.ds = ds;
    2958                this.data = data;
    3059        }
    3160       
    3261        public void executeCommand() {
     62                Visitor v = new DeleteVisitor();
    3363                for (OsmPrimitive osm : data)
    34                         osm.visit(this);
     64                        osm.visit(v);
     65        }
     66
     67        public void undoCommand() {
     68                Visitor v = new AddVisitor();
     69                for (OsmPrimitive osm : data)
     70                        osm.visit(v);
    3571        }
    3672
     
    3975        }
    4076
    41         public void fillModifiedData(Collection<OsmPrimitive> modified,
    42                         Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
    43                 if (deleted != null)
    44                         deleted.addAll(data);
     77        public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
     78                deleted.addAll(data);
    4579        }
    46 
    47 
    48         public void visit(Node n) {
    49                 Main.main.ds.nodes.remove(n);
    50                 Main.main.ds.removeBackReference(n);
    51         }
    52 
    53         public void visit(LineSegment ls) {
    54                 Main.main.ds.pendingLineSegments.remove(ls);
    55                 LinkedList<Track> tracksToDelete = new LinkedList<Track>();
    56                 for (Track t : Main.main.ds.tracks) {
    57                         t.segments.remove(ls);
    58                         if (t.segments.isEmpty())
    59                                 tracksToDelete.add(t);
    60                 }
    61                 for (Track t : tracksToDelete) {
    62                         Main.main.ds.tracks.remove(t);
    63                         Main.main.ds.removeBackReference(t);
    64                 }
    65                 Main.main.ds.removeBackReference(ls);
    66         }
    67 
    68         public void visit(Track t) {
    69                 Main.main.ds.tracks.remove(t);
    70                 for (LineSegment ls : t.segments)
    71                         Main.main.ds.pendingLineSegments.add(ls);
    72                 Main.main.ds.removeBackReference(t);
    73         }
    74 
    75         public void visit(Key k) {
    76                 // TODO
    77         }
    78 
    7980}
  • src/org/openstreetmap/josm/command/MoveCommand.java

    r23 r30  
    22
    33import java.awt.Component;
     4import java.awt.geom.Point2D;
    45import java.util.Collection;
     6import java.util.Iterator;
     7import java.util.LinkedList;
     8import java.util.List;
    59
    610import javax.swing.JLabel;
     
    2125         * The objects that should be moved.
    2226         */
    23         private Collection<OsmPrimitive> objects;
     27        private List<OsmPrimitive> objects;
    2428        /**
    2529         * x difference movement. Coordinates are in northern/eastern
     
    3236
    3337        /**
     38         * x/y List of all old positions of the objects.
     39         */
     40        private List<Point2D.Double> oldPositions;
     41       
     42        /**
    3443         * Create a MoveCommand and assign the initial object set and movement vector.
    3544         */
    3645        public MoveCommand(Collection<OsmPrimitive> objects, double x, double y) {
    37                 this.objects = objects;
     46                this.objects = new LinkedList<OsmPrimitive>(objects);
    3847                this.x = x;
    3948                this.y = y;
    40         }
    41 
    42         /**
    43          * Move the objects additional to the current movement.
    44          */
    45         public void move(double x, double y) {
    46                 this.x += x;
    47                 this.y += y;
    4849        }
    4950
     
    5859        }
    5960
     61        public void undoCommand() {
     62                AllNodesVisitor visitor = new AllNodesVisitor();
     63                for (OsmPrimitive osm : objects)
     64                        osm.visit(visitor);
     65                Iterator<Point2D.Double> it = oldPositions.iterator();
     66                for (Node n : visitor.nodes) {
     67                        Point2D.Double p = it.next();
     68                        n.coor.x = p.x;
     69                        n.coor.y = p.y;
     70                }
     71        }
     72
    6073        public Component commandDescription() {
    6174                String xstr = Math.abs(x) + (x < 0 ? "W" : "E");
     
    6578
    6679        public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
    67                 if (modified != null)
    68                         for (OsmPrimitive osm : objects)
    69                                 if (!modified.contains(osm))
    70                                         modified.add(osm);
     80                for (OsmPrimitive osm : objects)
     81                        if (!modified.contains(osm))
     82                                modified.add(osm);
    7183        }
    7284}
Note: See TracChangeset for help on using the changeset viewer.