Changeset 630 in josm


Ignore:
Timestamp:
2008-05-11T01:59:46+02:00 (16 years ago)
Author:
framm
Message:
  • make commands able to fail (patch by DH)
  • add Command.getOrig() (patch by DH)
  • add RemoveRelationMemberCommand (patch by DH)
Location:
trunk/src/org/openstreetmap/josm/command
Files:
1 added
9 edited

Legend:

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

    r627 r630  
    4444        }
    4545
    46         @Override public void executeCommand() {
     46        @Override public boolean executeCommand() {
    4747                osm.visit(new AddVisitor(ds));
     48                return true;
    4849        }
    4950
  • trunk/src/org/openstreetmap/josm/command/ChangeCommand.java

    r627 r630  
    2929    }
    3030
    31         @Override public void executeCommand() {
     31        @Override public boolean executeCommand() {
    3232            super.executeCommand();
    3333            osm.cloneFrom(newOsm);
    3434            osm.modified = true;
     35                return true;
    3536    }
    3637
  • trunk/src/org/openstreetmap/josm/command/ChangePropertyCommand.java

    r628 r630  
    5252        }
    5353       
    54         @Override public void executeCommand() {
     54        @Override public boolean executeCommand() {
    5555                super.executeCommand(); // save old
    5656                if (value == null) {
     
    6565                        }
    6666                }
     67                return true;
    6768        }
    6869
  • trunk/src/org/openstreetmap/josm/command/Command.java

    r627 r630  
    1010import javax.swing.tree.MutableTreeNode;
    1111
     12import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.data.osm.DataSet;
    1214import org.openstreetmap.josm.data.osm.Relation;
    1315import org.openstreetmap.josm.data.osm.Node;
     
    3133abstract public class Command {
    3234
    33         private static final class CloneVisitor implements Visitor {
    34                 public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
     35   private static final class CloneVisitor implements Visitor {
     36      public Map<OsmPrimitive, OsmPrimitive> orig = new HashMap<OsmPrimitive, OsmPrimitive>();
    3537
    36                 public void visit(Node n) {
    37                         orig.put(n, new Node(n));
    38                 }
    39                 public void visit(Way w) {
    40                         orig.put(w, new Way(w));
    41                 }
    42                 public void visit(Relation e) {
    43                         orig.put(e, new Relation(e));
    44                 }
    45         }
     38      public void visit(Node n) {
     39         orig.put(n, new Node(n));
     40      }
     41      public void visit(Way w) {
     42         orig.put(w, new Way(w));
     43      }
     44      public void visit(Relation e) {
     45         orig.put(e, new Relation(e));
     46      }
     47   }
    4648
    47         private CloneVisitor orig;
     49   private CloneVisitor orig;
    4850
    49         /**
    50          * Executes the command on the dataset. This implementation will remember all
    51          * primitives returned by fillModifiedData for restoring them on undo.
    52          */
    53         public void executeCommand() {
    54                 orig = new CloneVisitor();
    55                 Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
    56                 fillModifiedData(all, all, all);
    57                 for (OsmPrimitive osm : all)
    58                         osm.visit(orig);
    59         }
     51   protected DataSet ds;
    6052
    61         /**
    62          * Undoes the command.
    63          * It can be assumed, that all objects are in the same state they were before.
    64          * It can also be assumed that executeCommand was called exactly once before.
    65          *
    66          * This implementation undoes all objects stored by a former call to executeCommand.
    67          */
    68         public void undoCommand() {
    69                 for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
    70                         e.getKey().cloneFrom(e.getValue());
    71         }
     53   public Command() {
     54      this.ds = Main.main.editLayer().data;
     55   }
     56   /**
     57    * Executes the command on the dataset. This implementation will remember all
     58    * primitives returned by fillModifiedData for restoring them on undo.
     59    */
     60   public boolean did_execute = false;
     61   public boolean executeCommand() {
     62      did_execute = true;
     63      orig = new CloneVisitor();
     64      Collection<OsmPrimitive> all = new HashSet<OsmPrimitive>();
     65      fillModifiedData(all, all, all);
     66      for (OsmPrimitive osm : all)
     67         osm.visit(orig);
     68      return true;
     69   }
    7270
     71   /**
     72    * Undoes the command.
     73    * It can be assumed, that all objects are in the same state they were before.
     74    * It can also be assumed that executeCommand was called exactly once before.
     75    *
     76    * This implementation undoes all objects stored by a former call to executeCommand.
     77    */
     78   public void undoCommand() {
     79      for (Entry<OsmPrimitive, OsmPrimitive> e : orig.orig.entrySet())
     80         e.getKey().cloneFrom(e.getValue());
     81   }
    7382
    74         /**
    75         * Called, when a layer has been removed to have the command remove itself from
    76         * any buffer if it is not longer applicable to the dataset (e.g. it was part of
    77         * the removed layer)
    78         */
    79         public boolean invalidBecauselayerRemoved(Layer oldLayer) {
    80                 if (!(oldLayer instanceof OsmDataLayer))
    81                         return false;
    82                 HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>();
    83                 fillModifiedData(modified, modified, modified);
    84                 if (modified.isEmpty())
    85                         return false;
     83   /**
     84    * Called, when a layer has been removed to have the command remove itself from
     85    * any buffer if it is not longer applicable to the dataset (e.g. it was part of
     86    * the removed layer)
     87    */
     88   public boolean invalidBecauselayerRemoved(Layer oldLayer) {
     89      if (!(oldLayer instanceof OsmDataLayer))
     90         return false;
     91      HashSet<OsmPrimitive> modified = new HashSet<OsmPrimitive>();
     92      fillModifiedData(modified, modified, modified);
     93      if (modified.isEmpty())
     94         return false;
    8695
    87                 HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives());
    88                 for (OsmPrimitive osm : all)
    89                         if (all.contains(osm))
    90                                 return true;
     96      HashSet<OsmPrimitive> all = new HashSet<OsmPrimitive>(((OsmDataLayer)oldLayer).data.allPrimitives());
     97      for (OsmPrimitive osm : all)
     98         if (all.contains(osm))
     99                 return true;
    91100
    92                 return false;
    93         }
     101      return false;
     102   }
    94103
    95         /**
    96          * Fill in the changed data this command operates on.
    97          * Add to the lists, don't clear them.
    98          *
    99          * @param modified  The modified primitives
    100          * @param deleted   The deleted primitives
    101          * @param added         The added primitives
    102          */
    103         abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
    104                         Collection<OsmPrimitive> deleted,
    105                         Collection<OsmPrimitive> added);
     104    /**
     105     * Lets other commands access the original version
     106     * of the object. Usually for undoing.
     107     */
     108    public OsmPrimitive getOrig(OsmPrimitive osm) {
     109        OsmPrimitive o = orig.orig.get(osm);
     110        if (o != null)
     111             return o;
     112        Main.debug("unable to find osm with id: " + osm.id + " hashCode: " + osm.hashCode());
     113        for (OsmPrimitive t : orig.orig.keySet()) {
     114             OsmPrimitive to = orig.orig.get(t);
     115             Main.debug("now: " + t.id + " hashCode: " + t.hashCode());
     116             Main.debug("orig: " + to.id + " hashCode: " + to.hashCode());
     117        }
     118        return o;
     119    }
    106120
    107         abstract public MutableTreeNode description();
     121   /**
     122    * Fill in the changed data this command operates on.
     123    * Add to the lists, don't clear them.
     124    *
     125    * @param modified  The modified primitives
     126    * @param deleted   The deleted primitives
     127    * @param added      The added primitives
     128    */
     129   abstract public void fillModifiedData(Collection<OsmPrimitive> modified,
     130         Collection<OsmPrimitive> deleted,
     131         Collection<OsmPrimitive> added);
     132
     133   abstract public MutableTreeNode description();
    108134}
  • trunk/src/org/openstreetmap/josm/command/ConflictResolveCommand.java

    r627 r630  
    3636        }
    3737
    38         @Override public void executeCommand() {
     38        @Override public boolean executeCommand() {
    3939                super.executeCommand();
    4040
     
    5757                        conflictDialog.rebuildList();
    5858                }
     59                return true;
    5960        }
    6061
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r627 r630  
    66
    77import java.util.Collection;
     8import java.util.Collections;
    89
    910import javax.swing.JLabel;
     
    2627        private final Collection<? extends OsmPrimitive> data;
    2728
     29    /**
     30     * Constructor for a collection of data
     31     */
    2832        public DeleteCommand(Collection<? extends OsmPrimitive> data) {
    2933                this.data = data;
    3034        }
     35    /**
     36     * Constructor for a single data item. Use the collection
     37     * constructor to delete multiple objects.
     38     */
     39    public DeleteCommand(OsmPrimitive data) {
     40        this.data = Collections.singleton(data);
     41    }
    3142
    32         @Override public void executeCommand() {
     43        @Override public boolean executeCommand() {
    3344                super.executeCommand();
    3445                for (OsmPrimitive osm : data) {
    3546                        osm.delete(true);
    3647                }
     48                return true;
    3749        }
    3850       
  • trunk/src/org/openstreetmap/josm/command/MoveCommand.java

    r627 r630  
    9595        }
    9696       
    97         @Override public void executeCommand() {
     97        @Override public boolean executeCommand() {
    9898                for (Node n : objects) {
    9999                        n.eastNorth = new EastNorth(n.eastNorth.east()+x, n.eastNorth.north()+y);
     
    101101                        n.modified = true;
    102102                }
     103                return true;
    103104        }
    104105
  • trunk/src/org/openstreetmap/josm/command/RotateCommand.java

    r627 r630  
    113113        }
    114114       
    115         @Override public void executeCommand() {
     115        @Override public boolean executeCommand() {
    116116                rotateNodes(true);
     117                return true;
    117118        }
    118119
  • trunk/src/org/openstreetmap/josm/command/SequenceCommand.java

    r627 r630  
    1010import javax.swing.tree.MutableTreeNode;
    1111
     12import org.openstreetmap.josm.Main;
    1213import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1314
     
    2324         */
    2425        private Command[] sequence;
     26        private boolean sequence_complete;
    2527        private final String name;
     28        public boolean continueOnError = false;
    2629
    2730        /**
     
    4245        }
    4346       
    44         @Override public void executeCommand() {
    45                 for (Command c : sequence)
    46                         c.executeCommand();
     47        public int executed_commands = 0;
     48        @Override public boolean executeCommand() {
     49                for (int i=0; i < sequence.length; i++) {
     50                        Command c = sequence[i];
     51                        boolean result = c.executeCommand();
     52                        if (!result)
     53                                Main.debug("SequenceCommand, executing command[" + i + "] " +  c + " result: " + result);
     54                        if (!result && !continueOnError) {
     55                                this.undoCommands(i-1);
     56                                return false;
     57                        }
     58                }
     59                sequence_complete = true;
     60                return true;
     61        }
     62
     63        private void undoCommands(int start) {
     64                // We probably aborted this halfway though the
     65                // execution sequence because of a sub-command
     66                // error.  We already undid the sub-commands.
     67                if (!sequence_complete)
     68                        return;
     69                for (int i = start; i >= 0; --i)
     70                        sequence[i].undoCommand();
    4771        }
    4872
    4973        @Override public void undoCommand() {
    50                 for (int i = sequence.length-1; i >= 0; --i)
    51                         sequence[i].undoCommand();
     74                this.undoCommands(sequence.length-1);
    5275        }
    5376
Note: See TracChangeset for help on using the changeset viewer.