Ignore:
Timestamp:
2016-11-12T14:52:32+01:00 (3 years ago)
Author:
Don-vip
Message:

see #10387 - refactor various actions and commands so they can be used without data layer

Location:
trunk/src/org/openstreetmap/josm/command
Files:
7 edited

Legend:

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

    r11140 r11240  
    1111import javax.swing.Icon;
    1212
     13import org.openstreetmap.josm.data.osm.DataSet;
    1314import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1415import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     
    4748    public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
    4849        super(layer);
     50        this.osm = Objects.requireNonNull(osm, "osm");
     51    }
     52
     53    /**
     54     * Creates the command and specify the element to add in the context of the given data set.
     55     * @param data The data set. Must not be {@code null}
     56     * @param osm The primitive to add
     57     * @since 11240
     58     */
     59    public AddCommand(DataSet data, OsmPrimitive osm) {
     60        super(data);
    4961        this.osm = Objects.requireNonNull(osm, "osm");
    5062    }
  • trunk/src/org/openstreetmap/josm/command/AddPrimitivesCommand.java

    r10804 r11240  
    106106            // a subsequent command (e.g. MoveCommand) cannot be redone.
    107107            for (OsmPrimitive osm : createdPrimitives) {
    108                 getLayer().data.addPrimitive(osm);
     108                getAffectedDataSet().addPrimitive(osm);
    109109            }
    110110            primitivesToSelect = createdPrimitivesToSelect;
    111111        }
    112112
    113         getLayer().data.setSelected(primitivesToSelect);
     113        getAffectedDataSet().setSelected(primitivesToSelect);
    114114        return true;
    115115    }
  • trunk/src/org/openstreetmap/josm/command/ChangeCommand.java

    r10216 r11240  
    1010import javax.swing.Icon;
    1111
     12import org.openstreetmap.josm.data.osm.DataSet;
    1213import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1314import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     
    4748    public ChangeCommand(OsmDataLayer layer, OsmPrimitive osm, OsmPrimitive newOsm) {
    4849        super(layer);
     50        this.osm = osm;
     51        this.newOsm = newOsm;
     52        sanityChecks();
     53    }
     54
     55    /**
     56     * Constructs a new {@code ChangeCommand} in the context of a given data set.
     57     * @param data The data set
     58     * @param osm The existing primitive to modify
     59     * @param newOsm The new primitive
     60     * @since 11240
     61     */
     62    public ChangeCommand(DataSet data, OsmPrimitive osm, OsmPrimitive newOsm) {
     63        super(data);
    4964        this.osm = osm;
    5065        this.newOsm = newOsm;
  • trunk/src/org/openstreetmap/josm/command/Command.java

    r10970 r11240  
    137137    private final OsmDataLayer layer;
    138138
     139    /** the dataset which this command is applied to */
     140    private final DataSet data;
     141
    139142    /**
    140143     * Creates a new command in the context of the current edit layer, if any
     
    142145    public Command() {
    143146        this.layer = Main.getLayerManager().getEditLayer();
     147        this.data = layer != null ? layer.data : null;
    144148    }
    145149
     
    153157        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    154158        this.layer = layer;
     159        this.data = layer.data;
     160    }
     161
     162    /**
     163     * Creates a new command in the context of a specific data set, without data layer
     164     *
     165     * @param data the data set. Must not be null.
     166     * @throws IllegalArgumentException if data is null
     167     * @since 11240
     168     */
     169    public Command(DataSet data) {
     170        CheckParameterUtil.ensureParameterNotNull(data, "data");
     171        this.layer = null;
     172        this.data = data;
    155173    }
    156174
     
    226244     */
    227245    public DataSet getAffectedDataSet() {
    228         return layer == null ? null : layer.data;
     246        return data;
    229247    }
    230248
     
    331349        Command command = (Command) obj;
    332350        return Objects.equals(cloneMap, command.cloneMap) &&
    333                 Objects.equals(layer, command.layer);
     351               Objects.equals(layer, command.layer) &&
     352               Objects.equals(data, command.data);
    334353    }
    335354
  • trunk/src/org/openstreetmap/josm/command/DeleteCommand.java

    r10663 r11240  
    2929import org.openstreetmap.josm.actions.SplitWayAction;
    3030import org.openstreetmap.josm.actions.SplitWayAction.SplitWayResult;
     31import org.openstreetmap.josm.data.osm.DataSet;
    3132import org.openstreetmap.josm.data.osm.Node;
    3233import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    131132    public DeleteCommand(OsmDataLayer layer, Collection<? extends OsmPrimitive> data) {
    132133        super(layer);
     134        CheckParameterUtil.ensureParameterNotNull(data, "data");
     135        this.toDelete = data;
     136        checkConsistency();
     137    }
     138
     139    /**
     140     * Constructor for a collection of data to be deleted in the context of
     141     * a specific data set
     142     *
     143     * @param dataset the dataset context for deleting these primitives. Must not be null.
     144     * @param data the primitives to delete. Must neither be null nor empty.
     145     * @throws IllegalArgumentException if dataset is null
     146     * @throws IllegalArgumentException if data is null or empty
     147     * @since 11240
     148     */
     149    public DeleteCommand(DataSet dataset, Collection<? extends OsmPrimitive> data) {
     150        super(dataset);
    133151        CheckParameterUtil.ensureParameterNotNull(data, "data");
    134152        this.toDelete = data;
     
    448466        //
    449467        if (!primitivesToDelete.isEmpty()) {
    450             cmds.add(new DeleteCommand(layer, primitivesToDelete));
     468            cmds.add(layer != null ? new DeleteCommand(layer, primitivesToDelete) :
     469                new DeleteCommand(primitivesToDelete.iterator().next().getDataSet(), primitivesToDelete));
    451470        }
    452471
  • trunk/src/org/openstreetmap/josm/command/PurgeCommand.java

    r10378 r11240  
    4343    protected final ConflictCollection purgedConflicts = new ConflictCollection();
    4444
    45     protected final DataSet ds;
    46 
    4745    /**
     46     * Constructs a new {@code PurgeCommand} (handles conflicts).
    4847     * This command relies on a number of consistency conditions:
    4948     *  - makeIncomplete must be a subset of toPurge.
    50      *  - Each primitive, that is in toPurge but not in makeIncomplete, must
    51      *      have all its referrers in toPurge.
    52      *  - Each element of makeIncomplete must not be new and must have only
    53      *      referrers that are either a relation or included in toPurge.
     49     *  - Each primitive, that is in toPurge but not in makeIncomplete, must have all its referrers in toPurge.
     50     *  - Each element of makeIncomplete must not be new and must have only referrers that are either a relation or included in toPurge.
    5451     * @param layer OSM data layer
    5552     * @param toPurge primitives to purge
     
    5855    public PurgeCommand(OsmDataLayer layer, Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
    5956        super(layer);
    60         this.ds = layer.data;
     57        init(toPurge, makeIncomplete);
     58    }
     59
     60    /**
     61     * Constructs a new {@code PurgeCommand} (does not handle conflicts).
     62     * This command relies on a number of consistency conditions:
     63     *  - makeIncomplete must be a subset of toPurge.
     64     *  - Each primitive, that is in toPurge but not in makeIncomplete, must have all its referrers in toPurge.
     65     *  - Each element of makeIncomplete must not be new and must have only referrers that are either a relation or included in toPurge.
     66     * @param data OSM data set
     67     * @param toPurge primitives to purge
     68     * @param makeIncomplete primitives to make incomplete
     69     * @since 11240
     70     */
     71    public PurgeCommand(DataSet data, Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
     72        super(data);
     73        init(toPurge, makeIncomplete);
     74    }
     75
     76    private void init(Collection<OsmPrimitive> toPurge, Collection<OsmPrimitive> makeIncomplete) {
    6177        /**
    6278         * The topological sort is to avoid missing way nodes and missing
     
    8298    @Override
    8399    public boolean executeCommand() {
    84         ds.beginUpdate();
     100        getAffectedDataSet().beginUpdate();
    85101        try {
    86102            purgedConflicts.get().clear();
     
    105121                    osm.load(empty);
    106122                } else {
    107                     ds.removePrimitive(osm);
    108                     Conflict<?> conflict = getLayer().getConflicts().getConflictForMy(osm);
    109                     if (conflict != null) {
    110                         purgedConflicts.add(conflict);
    111                         getLayer().getConflicts().remove(conflict);
     123                    getAffectedDataSet().removePrimitive(osm);
     124                    if (getLayer() != null) {
     125                        Conflict<?> conflict = getLayer().getConflicts().getConflictForMy(osm);
     126                        if (conflict != null) {
     127                            purgedConflicts.add(conflict);
     128                            getLayer().getConflicts().remove(conflict);
     129                        }
    112130                    }
    113131                }
    114132            }
    115133        } finally {
    116             ds.endUpdate();
     134            getAffectedDataSet().endUpdate();
    117135        }
    118136        return true;
     
    121139    @Override
    122140    public void undoCommand() {
    123         if (ds == null)
     141        if (getAffectedDataSet() == null)
    124142            return;
    125143
     
    127145            PrimitiveData data = makeIncompleteDataByPrimId.get(osm);
    128146            if (data != null) {
    129                 if (ds.getPrimitiveById(osm) != osm)
     147                if (getAffectedDataSet().getPrimitiveById(osm) != osm)
    130148                    throw new AssertionError(
    131149                            String.format("Primitive %s has been made incomplete when purging, but it cannot be found on undo.", osm));
    132150                osm.load(data);
    133151            } else {
    134                 if (ds.getPrimitiveById(osm) != null)
     152                if (getAffectedDataSet().getPrimitiveById(osm) != null)
    135153                    throw new AssertionError(String.format("Primitive %s was removed when purging, but is still there on undo", osm));
    136                 ds.addPrimitive(osm);
     154                getAffectedDataSet().addPrimitive(osm);
    137155            }
    138156        }
     
    279297    @Override
    280298    public int hashCode() {
    281         return Objects.hash(super.hashCode(), toPurge, makeIncompleteData, makeIncompleteDataByPrimId, purgedConflicts, ds);
     299        return Objects.hash(super.hashCode(), toPurge, makeIncompleteData, makeIncompleteDataByPrimId, purgedConflicts, getAffectedDataSet());
    282300    }
    283301
     
    291309                Objects.equals(makeIncompleteData, that.makeIncompleteData) &&
    292310                Objects.equals(makeIncompleteDataByPrimId, that.makeIncompleteDataByPrimId) &&
    293                 Objects.equals(purgedConflicts, that.purgedConflicts) &&
    294                 Objects.equals(ds, that.ds);
     311                Objects.equals(purgedConflicts, that.purgedConflicts);
    295312    }
    296313}
  • trunk/src/org/openstreetmap/josm/command/conflict/DeletedStateConflictResolveCommand.java

    r9371 r11240  
    7474
    7575    private void deleteMy() {
    76         Set<OsmPrimitive> referrers = getLayer().data.unlinkReferencesToPrimitive(conflict.getMy());
     76        Set<OsmPrimitive> referrers = getAffectedDataSet().unlinkReferencesToPrimitive(conflict.getMy());
    7777        for (OsmPrimitive p : referrers) {
    7878            if (!p.isNew() && !p.isDeleted()) {
Note: See TracChangeset for help on using the changeset viewer.