Skip to content

Commit

Permalink
see #13036 - see #15229 - see #15182 - make Commands depends only o…
Browse files Browse the repository at this point in the history
…n a `DataSet`, not a `Layer`. This removes a lot of GUI dependencies

git-svn-id: https://josm.openstreetmap.de/svn/trunk@12718 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
don-vip committed Sep 3, 2017
1 parent c0b295b commit f63b69b
Show file tree
Hide file tree
Showing 32 changed files with 459 additions and 179 deletions.
25 changes: 19 additions & 6 deletions src/org/openstreetmap/josm/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static boolean isDisplayingMapView() {
/**
* The commands undo/redo handler.
*/
public final UndoRedoHandler undoRedo = MainApplication.undoRedo;
public final UndoRedoHandler undoRedo = new UndoRedoHandler();

/**
* The main menu bar at top of screen.
Expand Down Expand Up @@ -629,13 +629,26 @@ public Collection<OsmPrimitive> getInProgressSelection() {
}

/**
* Gets the data set of the active edit layer.
* @return That data set, <code>null</code> if there is no edit layer.
* Gets the active edit data set.
* @return That data set, <code>null</code>.
* @since 12691
*/
public DataSet getEditDataSet() {
return null;
}
public abstract DataSet getEditDataSet();

/**
* Sets the active data set.
* @param ds New edit data set, or <code>null</code>
* @since 12718
*/
public abstract void setEditDataSet(DataSet ds);

/**
* Determines if the list of data sets managed by JOSM contains {@code ds}.
* @param ds the data set to look for
* @return {@code true} if the list of data sets managed by JOSM contains {@code ds}
* @since 12718
*/
public abstract boolean containsDataSet(DataSet ds);

/**
* Registers a {@code JosmAction} and its shortcut.
Expand Down
9 changes: 4 additions & 5 deletions src/org/openstreetmap/josm/actions/JoinAreasAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public JoinAreasResult joinAreas(List<Multipolygon> areas) throws UserCancelExce

// Delete the discarded inner ways
if (!discardedWays.isEmpty()) {
Command deleteCmd = DeleteCommand.delete(getLayerManager().getEditLayer(), discardedWays, true);
Command deleteCmd = DeleteCommand.delete(discardedWays, true);
if (deleteCmd != null) {
cmds.add(deleteCmd);
commitCommands(marktr("Delete Ways that are not part of an inner multipolygon"));
Expand Down Expand Up @@ -1017,7 +1017,7 @@ private List<Way> splitWayOnNodes(Way way, Set<Node> nodes) {
List<List<Node>> chunks = buildNodeChunks(way, nodes);

if (chunks.size() > 1) {
SplitWayResult split = SplitWayAction.splitWay(getLayerManager().getEditLayer(), way, chunks,
SplitWayResult split = SplitWayAction.splitWay(way, chunks,
Collections.<OsmPrimitive>emptyList(), SplitWayAction.Strategy.keepFirstChunk());

if (split != null) {
Expand Down Expand Up @@ -1466,7 +1466,7 @@ private RelationRole addOwnMultipolygonRelation(Collection<Way> inner) {
for (Way w : inner) {
newRel.addMember(new RelationMember("inner", w));
}
cmds.add(layer != null ? new AddCommand(layer, newRel) :
cmds.add(layer != null ? new AddCommand(layer.data, newRel) :
new AddCommand(inner.iterator().next().getDataSet(), newRel));
addedRelations.add(newRel);

Expand Down Expand Up @@ -1537,7 +1537,6 @@ private void fixRelations(List<RelationRole> rels, Way outer, RelationRole ownMu
cmds.add(new ChangeCommand(r.rel, newRel));
}

OsmDataLayer layer = getLayerManager().getEditLayer();
Relation newRel;
switch (multiouters.size()) {
case 0:
Expand Down Expand Up @@ -1566,7 +1565,7 @@ private void fixRelations(List<RelationRole> rels, Way outer, RelationRole ownMu
relationsToDelete.add(r.rel);
}
newRel.addMember(new RelationMember("outer", outer));
cmds.add(layer != null ? new AddCommand(layer, newRel) : new AddCommand(outer.getDataSet(), newRel));
cmds.add(new AddCommand(outer.getDataSet(), newRel));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/openstreetmap/josm/actions/PurgeAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void actionPerformed(ActionEvent e) {
public PurgeCommand getPurgeCommand(Collection<OsmPrimitive> sel) {
layer = getLayerManager().getEditLayer();
toPurgeAdditionally = new ArrayList<>();
PurgeCommand cmd = PurgeCommand.build(layer, sel, toPurgeAdditionally);
PurgeCommand cmd = PurgeCommand.build(sel, toPurgeAdditionally);
modified = cmd.getParticipatingPrimitives().stream().anyMatch(OsmPrimitive::isModified);
return cmd;
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/openstreetmap/josm/actions/RedoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import java.awt.event.KeyEvent;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MapFrame;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.tools.Shortcut;

/**
* Redoes the last command.
*
* @author imi
*/
public class RedoAction extends JosmAction implements OsmDataLayer.CommandQueueListener {
public class RedoAction extends JosmAction implements CommandQueueListener {

/**
* Construct the action with "Redo" as label.
Expand Down
87 changes: 76 additions & 11 deletions src/org/openstreetmap/josm/actions/SplitWayAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void actionPerformed(ActionEvent e) {
}
}
if (wayToKeep != null) {
final SplitWayResult result = doSplitWay(getLayerManager().getEditLayer(), selectedWay, wayToKeep, newWays, sel);
final SplitWayResult result = doSplitWay(selectedWay, wayToKeep, newWays, sel);
MainApplication.undoRedo.add(result.getCommand());
if (!result.getNewSelection().isEmpty()) {
getLayerManager().getEditDataSet().setSelected(result.getNewSelection());
Expand Down Expand Up @@ -293,8 +293,7 @@ protected void buttonAction(int buttonIndex, ActionEvent evt) {
super.buttonAction(buttonIndex, evt);
toggleSaveState(); // necessary since #showDialog() does not handle it due to the non-modal dialog
if (getValue() == 1) {
SplitWayResult result = doSplitWay(MainApplication.getLayerManager().getEditLayer(),
selectedWay, list.getSelectedValue(), newWays, selection);
SplitWayResult result = doSplitWay(selectedWay, list.getSelectedValue(), newWays, selection);
MainApplication.undoRedo.add(result.getCommand());
if (!result.getNewSelection().isEmpty()) {
MainApplication.getLayerManager().getEditDataSet().setSelected(result.getNewSelection());
Expand Down Expand Up @@ -497,10 +496,31 @@ protected static List<Way> createNewWaysFromChunks(Way way, Iterable<List<Node>>
* @param wayChunks the list of way chunks into the way is split. Must not be null.
* @param selection The list of currently selected primitives
* @return the result from the split operation
* @deprecated to be removed end of 2017. Use {@link #splitWay(Way, List, Collection)} instead
*/
@Deprecated
public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks,
Collection<? extends OsmPrimitive> selection) {
return splitWay(layer, way, wayChunks, selection, Strategy.keepLongestChunk());
return splitWay(way, wayChunks, selection);
}

/**
* Splits the way {@code way} into chunks of {@code wayChunks} and replies
* the result of this process in an instance of {@link SplitWayResult}.
*
* Note that changes are not applied to the data yet. You have to
* submit the command in {@link SplitWayResult#getCommand()} first,
* i.e. {@code Main.main.undoredo.add(result.getCommand())}.
*
* @param way the way to split. Must not be null.
* @param wayChunks the list of way chunks into the way is split. Must not be null.
* @param selection The list of currently selected primitives
* @return the result from the split operation
* @since 12718
*/
public static SplitWayResult splitWay(Way way, List<List<Node>> wayChunks,
Collection<? extends OsmPrimitive> selection) {
return splitWay(way, wayChunks, selection, Strategy.keepLongestChunk());
}

/**
Expand All @@ -520,9 +540,33 @@ public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Nod
* @param splitStrategy The strategy used to determine which way chunk should reuse the old id and its history
* @return the result from the split operation
* @since 8954
* @deprecated to be removed end of 2017. Use {@link #splitWay(Way, List, Collection, Strategy)} instead
*/
@Deprecated
public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Node>> wayChunks,
Collection<? extends OsmPrimitive> selection, Strategy splitStrategy) {
return splitWay(way, wayChunks, selection, splitStrategy);
}

/**
* Splits the way {@code way} into chunks of {@code wayChunks} and replies
* the result of this process in an instance of {@link SplitWayResult}.
* The {@link org.openstreetmap.josm.actions.SplitWayAction.Strategy} is used to determine which
* way chunk should reuse the old id and its history.
*
* Note that changes are not applied to the data yet. You have to
* submit the command in {@link SplitWayResult#getCommand()} first,
* i.e. {@code Main.main.undoredo.add(result.getCommand())}.
*
* @param way the way to split. Must not be null.
* @param wayChunks the list of way chunks into the way is split. Must not be null.
* @param selection The list of currently selected primitives
* @param splitStrategy The strategy used to determine which way chunk should reuse the old id and its history
* @return the result from the split operation
* @since 12718
*/
public static SplitWayResult splitWay(Way way, List<List<Node>> wayChunks,
Collection<? extends OsmPrimitive> selection, Strategy splitStrategy) {
// build a list of commands, and also a new selection list
final List<OsmPrimitive> newSelection = new ArrayList<>(selection.size() + wayChunks.size());
newSelection.addAll(selection);
Expand All @@ -533,11 +577,10 @@ public static SplitWayResult splitWay(OsmDataLayer layer, Way way, List<List<Nod
// Determine which part reuses the existing way
final Way wayToKeep = splitStrategy.determineWayToKeep(newWays);

return wayToKeep != null ? doSplitWay(layer, way, wayToKeep, newWays, newSelection) : null;
return wayToKeep != null ? doSplitWay(way, wayToKeep, newWays, newSelection) : null;
}

static SplitWayResult doSplitWay(OsmDataLayer layer, Way way, Way wayToKeep, List<Way> newWays,
List<OsmPrimitive> newSelection) {
static SplitWayResult doSplitWay(Way way, Way wayToKeep, List<Way> newWays, List<OsmPrimitive> newSelection) {

Collection<Command> commandList = new ArrayList<>(newWays.size());
Collection<String> nowarnroles = Main.pref.getCollection("way.split.roles.nowarn",
Expand All @@ -549,7 +592,7 @@ static SplitWayResult doSplitWay(OsmDataLayer layer, Way way, Way wayToKeep, Lis
// Change the original way
final Way changedWay = new Way(way);
changedWay.setNodes(wayToKeep.getNodes());
commandList.add(layer != null ? new ChangeCommand(layer, way, changedWay) : new ChangeCommand(way.getDataSet(), way, changedWay));
commandList.add(new ChangeCommand(way.getDataSet(), way, changedWay));
if (!isMapModeDraw && !newSelection.contains(way)) {
newSelection.add(way);
}
Expand All @@ -560,7 +603,7 @@ static SplitWayResult doSplitWay(OsmDataLayer layer, Way way, Way wayToKeep, Lis
newSelection.addAll(newWays);
}
for (Way wayToAdd : newWays) {
commandList.add(layer != null ? new AddCommand(layer, wayToAdd) : new AddCommand(way.getDataSet(), wayToAdd));
commandList.add(new AddCommand(way.getDataSet(), wayToAdd));
}

boolean warnmerole = false;
Expand Down Expand Up @@ -687,7 +730,7 @@ static SplitWayResult doSplitWay(OsmDataLayer layer, Way way, Way wayToKeep, Lis
}

if (c != null) {
commandList.add(layer != null ? new ChangeCommand(layer, r, c) : new ChangeCommand(r.getDataSet(), r, c));
commandList.add(new ChangeCommand(r.getDataSet(), r, c));
}
}
if (warnmerole) {
Expand Down Expand Up @@ -740,10 +783,32 @@ static OsmPrimitive findVia(Relation r, String type) {
* @param atNodes the list of nodes where the way is split. Must not be null.
* @param selection The list of currently selected primitives
* @return the result from the split operation
* @deprecated to be removed end of 2017. Use {@link #split(Way, List, Collection) instead}
*/
@Deprecated
public static SplitWayResult split(OsmDataLayer layer, Way way, List<Node> atNodes, Collection<? extends OsmPrimitive> selection) {
return split(way, atNodes, selection);
}

/**
* Splits the way {@code way} at the nodes in {@code atNodes} and replies
* the result of this process in an instance of {@link SplitWayResult}.
*
* Note that changes are not applied to the data yet. You have to
* submit the command in {@link SplitWayResult#getCommand()} first,
* i.e. {@code Main.main.undoredo.add(result.getCommand())}.
*
* Replies null if the way couldn't be split at the given nodes.
*
* @param way the way to split. Must not be null.
* @param atNodes the list of nodes where the way is split. Must not be null.
* @param selection The list of currently selected primitives
* @return the result from the split operation
* @since 12718
*/
public static SplitWayResult split(Way way, List<Node> atNodes, Collection<? extends OsmPrimitive> selection) {
List<List<Node>> chunks = buildSplitChunks(way, atNodes);
return chunks != null ? splitWay(layer, way, chunks, selection) : null;
return chunks != null ? splitWay(way, chunks, selection) : null;
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/org/openstreetmap/josm/actions/UndoAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import java.awt.event.KeyEvent;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.MapFrame;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.tools.Shortcut;

/**
* Undoes the last command.
*
* @author imi
*/
public class UndoAction extends JosmAction implements OsmDataLayer.CommandQueueListener {
public class UndoAction extends JosmAction implements CommandQueueListener {

/**
* Construct the action with "Undo" as label.
Expand Down
19 changes: 9 additions & 10 deletions src/org/openstreetmap/josm/actions/mapmode/DeleteAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ public void doActionPerformed(ActionEvent e) {

Command c;
if (ctrl) {
c = DeleteCommand.deleteWithReferences(editLayer, lm.getEditDataSet().getSelected());
c = DeleteCommand.deleteWithReferences(lm.getEditDataSet().getSelected());
} else {
c = DeleteCommand.delete(editLayer, lm.getEditDataSet().getSelected(), !alt /* also delete nodes in way */);
c = DeleteCommand.delete(lm.getEditDataSet().getSelected(), !alt /* also delete nodes in way */);
}
// if c is null, an error occurred or the user aborted. Don't do anything in that case.
if (c != null) {
Expand Down Expand Up @@ -351,7 +351,7 @@ public static void deleteRelations(OsmDataLayer layer, Collection<Relation> toDe
CheckParameterUtil.ensureParameterNotNull(layer, "layer");
CheckParameterUtil.ensureParameterNotNull(toDelete, "toDelete");

final Command cmd = DeleteCommand.delete(layer, toDelete);
final Command cmd = DeleteCommand.delete(toDelete);
if (cmd != null) {
// cmd can be null if the user cancels dialogs DialogCommand displays
MainApplication.undoRedo.add(cmd);
Expand Down Expand Up @@ -403,20 +403,19 @@ private DeleteParameters getDeleteParameters(MouseEvent e, int modifiers) {
*/
private Command buildDeleteCommands(MouseEvent e, int modifiers, boolean silent) {
DeleteParameters parameters = getDeleteParameters(e, modifiers);
OsmDataLayer editLayer = getLayerManager().getEditLayer();
switch (parameters.mode) {
case node:
return DeleteCommand.delete(editLayer, Collections.singleton(parameters.nearestNode), false, silent);
return DeleteCommand.delete(Collections.singleton(parameters.nearestNode), false, silent);
case node_with_references:
return DeleteCommand.deleteWithReferences(editLayer, Collections.singleton(parameters.nearestNode), silent);
return DeleteCommand.deleteWithReferences(Collections.singleton(parameters.nearestNode), silent);
case segment:
return DeleteCommand.deleteWaySegment(editLayer, parameters.nearestSegment);
return DeleteCommand.deleteWaySegment(parameters.nearestSegment);
case way:
return DeleteCommand.delete(editLayer, Collections.singleton(parameters.nearestSegment.way), false, silent);
return DeleteCommand.delete(Collections.singleton(parameters.nearestSegment.way), false, silent);
case way_with_nodes:
return DeleteCommand.delete(editLayer, Collections.singleton(parameters.nearestSegment.way), true, silent);
return DeleteCommand.delete(Collections.singleton(parameters.nearestSegment.way), true, silent);
case way_with_references:
return DeleteCommand.deleteWithReferences(editLayer, Collections.singleton(parameters.nearestSegment.way), true);
return DeleteCommand.deleteWithReferences(Collections.singleton(parameters.nearestSegment.way), true);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public void mouseReleased(MouseEvent e) {
nodes.remove(candidateNode);
newWay.setNodes(nodes);
if (nodes.size() < 2) {
final Command deleteCmd = DeleteCommand.delete(getLayerManager().getEditLayer(), Collections.singleton(targetWay), true);
final Command deleteCmd = DeleteCommand.delete(Collections.singleton(targetWay), true);
if (deleteCmd != null) {
MainApplication.undoRedo.add(deleteCmd);
}
Expand All @@ -499,7 +499,7 @@ public void mouseReleased(MouseEvent e) {
tr("Cannot delete node that has tags"),
tr("Error"), JOptionPane.ERROR_MESSAGE);
} else {
final Command deleteCmd = DeleteCommand.delete(getLayerManager().getEditLayer(), Collections.singleton(candidateNode), true);
final Command deleteCmd = DeleteCommand.delete(Collections.singleton(candidateNode), true);
if (deleteCmd != null) {
MainApplication.undoRedo.add(deleteCmd);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import javax.swing.plaf.basic.BasicArrowButton;

import org.openstreetmap.josm.actions.JosmAction;
import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
import org.openstreetmap.josm.data.osm.Relation;
import org.openstreetmap.josm.gui.MainApplication;
import org.openstreetmap.josm.gui.SideButton;
import org.openstreetmap.josm.gui.layer.Layer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer;
import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.Shortcut;

Expand Down
2 changes: 2 additions & 0 deletions src/org/openstreetmap/josm/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public AddCommand(OsmPrimitive osm) {
* Creates the command and specify the element to add in the context of the given data layer.
* @param layer The data layer. Must not be {@code null}
* @param osm The primitive to add
* @deprecated to be removed end of 2017. Use {@link #AddCommand(DataSet, OsmPrimitive)} instead
*/
@Deprecated
public AddCommand(OsmDataLayer layer, OsmPrimitive osm) {
super(layer);
this.osm = Objects.requireNonNull(osm, "osm");
Expand Down
Loading

0 comments on commit f63b69b

Please sign in to comment.