Changeset 11240 in josm


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
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java

    r10755 r11240  
    4343import org.openstreetmap.josm.gui.Notification;
    4444import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
     45import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4546import org.openstreetmap.josm.tools.Geometry;
    4647import org.openstreetmap.josm.tools.Pair;
     
    541542     * @throws UserCancelException if user cancels the operation
    542543     */
    543     private JoinAreasResult joinAreas(List<Multipolygon> areas) throws UserCancelException {
     544    public JoinAreasResult joinAreas(List<Multipolygon> areas) throws UserCancelException {
    544545
    545546        JoinAreasResult result = new JoinAreasResult();
     
    12961297     * @return list of polygons, or null if too complex relation encountered.
    12971298     */
    1298     private static List<Multipolygon> collectMultipolygons(Collection<Way> selectedWays) {
     1299    public static List<Multipolygon> collectMultipolygons(Collection<Way> selectedWays) {
    12991300
    13001301        List<Multipolygon> result = new ArrayList<>();
     
    14041405    private RelationRole addOwnMultipolygonRelation(Collection<Way> inner) {
    14051406        if (inner.isEmpty()) return null;
     1407        OsmDataLayer layer = Main.getLayerManager().getEditLayer();
    14061408        // Create new multipolygon relation and add all inner ways to it
    14071409        Relation newRel = new Relation();
     
    14101412            newRel.addMember(new RelationMember("inner", w));
    14111413        }
    1412         cmds.add(new AddCommand(newRel));
     1414        cmds.add(layer != null ? new AddCommand(layer, newRel) :
     1415            new AddCommand(inner.iterator().next().getDataSet(), newRel));
    14131416        addedRelations.add(newRel);
    14141417
     
    14261429        List<RelationRole> result = new ArrayList<>();
    14271430
    1428         for (Relation r : Main.getLayerManager().getEditDataSet().getRelations()) {
     1431        for (Relation r : osm.getDataSet().getRelations()) {
    14291432            if (r.isDeleted()) {
    14301433                continue;
     
    14801483        }
    14811484
     1485        OsmDataLayer layer = Main.getLayerManager().getEditLayer();
    14821486        Relation newRel;
    14831487        switch (multiouters.size()) {
     
    15081512            }
    15091513            newRel.addMember(new RelationMember("outer", outer));
    1510             cmds.add(new AddCommand(newRel));
     1514            cmds.add(layer != null ? new AddCommand(layer, newRel) : new AddCommand(outer.getDataSet(), newRel));
    15111515        }
    15121516    }
  • trunk/src/org/openstreetmap/josm/actions/PurgeAction.java

    r10619 r11240  
    9393            return;
    9494
    95         Collection<OsmPrimitive> sel = getLayerManager().getEditDataSet().getAllSelected();
     95        doPurge(getLayerManager().getEditDataSet().getAllSelected(), true);
     96    }
     97
     98    /**
     99     * Performs purge on selected OSM primitives.
     100     * @param sel selected OSM primitives
     101     * @param confirm asks user confirmation through a popup dialog
     102     * @since 11240
     103     */
     104    public void doPurge(Collection<OsmPrimitive> sel, boolean confirm) {
    96105        layer = Main.getLayerManager().getEditLayer();
    97106
     
    205214        boolean clearUndoRedo = false;
    206215
    207         if (!GraphicsEnvironment.isHeadless()) {
     216        if (!GraphicsEnvironment.isHeadless() && confirm) {
    208217            final boolean answer = ConditionalOptionPaneUtil.showConfirmationDialog(
    209218                    "purge", Main.parent, buildPanel(modified), tr("Confirm Purging"),
     
    216225        }
    217226
    218         Main.main.undoRedo.add(new PurgeCommand(Main.getLayerManager().getEditLayer(), toPurgeChecked, makeIncomplete));
     227        Main.main.undoRedo.add(layer != null ? new PurgeCommand(layer, toPurgeChecked, makeIncomplete) :
     228            new PurgeCommand(toPurgeChecked.iterator().next().getDataSet(), toPurgeChecked, makeIncomplete));
    219229
    220230        if (clearUndoRedo) {
  • trunk/src/org/openstreetmap/josm/actions/SelectByInternalPointAction.java

    r10448 r11240  
    3030
    3131    /**
    32      * Returns the surrounding polygons/multipolygons
    33      * ordered by their area size (from small to large)
     32     * Returns the surrounding polygons/multipolygons ordered by their area size (from small to large)
    3433     * which contain the internal point.
    3534     *
     
    3837     */
    3938    public static Collection<OsmPrimitive> getSurroundingObjects(EastNorth internalPoint) {
    40         final DataSet ds = Main.getLayerManager().getEditDataSet();
     39        return getSurroundingObjects(Main.getLayerManager().getEditDataSet(), internalPoint);
     40    }
     41
     42    /**
     43     * Returns the surrounding polygons/multipolygons ordered by their area size (from small to large)
     44     * which contain the internal point.
     45     *
     46     * @param ds the data set
     47     * @param internalPoint the internal point.
     48     * @return the surrounding polygons/multipolygons
     49     * @since 11240
     50     */
     51    public static Collection<OsmPrimitive> getSurroundingObjects(DataSet ds, EastNorth internalPoint) {
    4152        if (ds == null) {
    4253            return Collections.emptySet();
  • trunk/src/org/openstreetmap/josm/actions/SplitWayAction.java

    r10775 r11240  
    539539        final Way changedWay = new Way(way);
    540540        changedWay.setNodes(wayToKeep.getNodes());
    541         commandList.add(new ChangeCommand(way, changedWay));
     541        commandList.add(layer != null ? new ChangeCommand(layer, way, changedWay) : new ChangeCommand(way.getDataSet(), way, changedWay));
    542542        if (!newSelection.contains(way)) {
    543543            newSelection.add(way);
     
    548548        newSelection.addAll(newWays);
    549549        for (Way wayToAdd : newWays) {
    550             commandList.add(new AddCommand(layer, wayToAdd));
     550            commandList.add(layer != null ? new AddCommand(layer, wayToAdd) : new AddCommand(way.getDataSet(), wayToAdd));
    551551        }
    552552
     
    678678
    679679            if (c != null) {
    680                 commandList.add(new ChangeCommand(layer, r, c));
     680                commandList.add(layer != null ? new ChangeCommand(layer, r, c) : new ChangeCommand(r.getDataSet(), r, c));
    681681            }
    682682        }
  • 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()) {
  • trunk/src/org/openstreetmap/josm/data/UndoRedoHandler.java

    r10467 r11240  
    107107            return;
    108108        DataSet ds = Main.getLayerManager().getEditDataSet();
    109         Collection<? extends OsmPrimitive> oldSelection = ds.getSelected();
    110         ds.beginUpdate();
     109        Collection<? extends OsmPrimitive> oldSelection = null;
     110        if (ds != null) {
     111            oldSelection = ds.getSelected();
     112            ds.beginUpdate();
     113        }
    111114        try {
    112115            for (int i = 1; i <= num; ++i) {
     
    120123            }
    121124        } finally {
    122             ds.endUpdate();
    123         }
    124         fireCommandsChanged();
    125         fireIfSelectionChanged(ds, oldSelection);
     125            if (ds != null) {
     126                ds.endUpdate();
     127            }
     128        }
     129        fireCommandsChanged();
     130        if (ds != null) {
     131            fireIfSelectionChanged(ds, oldSelection);
     132        }
    126133    }
    127134
Note: See TracChangeset for help on using the changeset viewer.