From d62089662cf1b47a66fa1c28d24ab6509fcfb90b Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Thu, 31 Oct 2019 16:09:22 -0600 Subject: [PATCH] Add dialog for when someone tries to add too many objects Signed-off-by: Taylor Smock --- .../backend/MapWithAIMoveAction.java | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveAction.java index b6330da..121fd04 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIMoveAction.java @@ -8,12 +8,15 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; +import javax.swing.JOptionPane; + import org.openstreetmap.josm.actions.JosmAction; import org.openstreetmap.josm.data.UndoRedoHandler; import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.data.osm.Node; import org.openstreetmap.josm.data.osm.OsmPrimitive; import org.openstreetmap.josm.gui.MainApplication; +import org.openstreetmap.josm.gui.Notification; import org.openstreetmap.josm.gui.layer.Layer; import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; @@ -44,26 +47,43 @@ public class MapWithAIMoveAction extends JosmAction { .collect(Collectors.toList()); ds.clearSelection(nodes); nodes.stream().map(Node::getReferrers).forEach(ds::addSelected); + if (ds.getSelected().size() > maxAddition) { + createMaxAddedDialog(maxAddition, ds.getSelected().size()); + } final Collection selected = maxAddition > 0 ? ds.getSelected().stream().limit(maxAddition).collect(Collectors.toList()) - : ds.getSelected(); - for (final OsmDataLayer osmLayer : osmLayers) { - if (!osmLayer.isLocked() && osmLayer.isVisible() && osmLayer.isUploadable() - && osmLayer.getClass().equals(OsmDataLayer.class)) { - editLayer = osmLayer; - break; - } - } - if (editLayer != null) { - final MapWithAIAddCommand command = new MapWithAIAddCommand(mapWithAI, editLayer, selected); - UndoRedoHandler.getInstance().add(command); - if (MapWithAIPreferenceHelper.isSwitchLayers()) { - MainApplication.getLayerManager().setActiveLayer(editLayer); - } - } + : ds.getSelected(); + for (final OsmDataLayer osmLayer : osmLayers) { + if (!osmLayer.isLocked() && osmLayer.isVisible() && osmLayer.isUploadable() + && osmLayer.getClass().equals(OsmDataLayer.class)) { + editLayer = osmLayer; + break; + } + } + if (editLayer != null) { + final MapWithAIAddCommand command = new MapWithAIAddCommand(mapWithAI, editLayer, selected); + UndoRedoHandler.getInstance().add(command); + if (MapWithAIPreferenceHelper.isSwitchLayers()) { + MainApplication.getLayerManager().setActiveLayer(editLayer); + } + } } } + private void createMaxAddedDialog(int maxAddition, int triedToAdd) { + final Notification notification = new Notification(); + final StringBuilder message = new StringBuilder(); + message.append(MapWithAIPlugin.NAME); + message.append(": "); + message.append(tr("maximum additions per action are ")).append(maxAddition).append(", "); + message.append(tr("tried to add ")).append(triedToAdd).append("."); + notification.setContent(message.toString()); + notification.setDuration(Notification.TIME_LONG); + notification.setIcon(JOptionPane.INFORMATION_MESSAGE); + notification.show(); + + } + @Override protected void updateEnabledState() { setEnabled(checkIfActionEnabled());