From 0a949f60b7fcb4d4116a2c598706d525684898f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20I=2E=20B=C3=B8?= Date: Sun, 7 Jun 2015 19:46:43 +0200 Subject: [PATCH] Added replace way action --- build.xml | 7 +- .../plugins/kartverket/KartverketPlugin.java | 3 + .../actions/CheckDirectionAction.java | 11 +- .../kartverket/actions/ReplaceWayAction.java | 121 ++++++++++++++++++ 4 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 src/org/openstreetmap/josm/plugins/kartverket/actions/ReplaceWayAction.java diff --git a/build.xml b/build.xml index 84df2cb..686c3fd 100644 --- a/build.xml +++ b/build.xml @@ -17,7 +17,6 @@ - - + + + + diff --git a/src/org/openstreetmap/josm/plugins/kartverket/KartverketPlugin.java b/src/org/openstreetmap/josm/plugins/kartverket/KartverketPlugin.java index 91307fc..4f58081 100644 --- a/src/org/openstreetmap/josm/plugins/kartverket/KartverketPlugin.java +++ b/src/org/openstreetmap/josm/plugins/kartverket/KartverketPlugin.java @@ -8,9 +8,11 @@ import org.openstreetmap.josm.plugins.Plugin; import org.openstreetmap.josm.plugins.PluginInformation; import org.openstreetmap.josm.plugins.kartverket.actions.CheckDirectionAction; +import org.openstreetmap.josm.plugins.kartverket.actions.ReplaceWayAction; public class KartverketPlugin extends Plugin { JMenuItem CheckDirection; + JMenuItem mergeWays; /** * Will be invoked by JOSM to bootstrap the plugin * @@ -20,6 +22,7 @@ public KartverketPlugin(PluginInformation info) { super(info); JMenu toolsMenu = Main.main.menu.moreToolsMenu; CheckDirection = MainMenu.add(toolsMenu, new CheckDirectionAction()); + mergeWays = MainMenu.add(toolsMenu, new ReplaceWayAction()); } @Override diff --git a/src/org/openstreetmap/josm/plugins/kartverket/actions/CheckDirectionAction.java b/src/org/openstreetmap/josm/plugins/kartverket/actions/CheckDirectionAction.java index 059ad60..7a4fade 100644 --- a/src/org/openstreetmap/josm/plugins/kartverket/actions/CheckDirectionAction.java +++ b/src/org/openstreetmap/josm/plugins/kartverket/actions/CheckDirectionAction.java @@ -1,28 +1,19 @@ package org.openstreetmap.josm.plugins.kartverket.actions; -import static org.openstreetmap.josm.gui.help.HelpUtil.ht; import static org.openstreetmap.josm.tools.I18n.tr; -import java.awt.Point; -import java.awt.Rectangle; + import java.awt.event.ActionEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; -import javax.swing.JOptionPane; -import javax.swing.SwingUtilities; - import org.openstreetmap.josm.Main; import org.openstreetmap.josm.actions.JosmAction; -import org.openstreetmap.josm.data.Bounds; import org.openstreetmap.josm.data.osm.Node; import org.openstreetmap.josm.data.osm.Way; import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; -import org.openstreetmap.josm.gui.ExtendedDialog; import org.openstreetmap.josm.gui.Notification; import org.openstreetmap.josm.plugins.kartverket.CheckDirectionDialog; import org.openstreetmap.josm.plugins.kartverket.CheckNextWayI; diff --git a/src/org/openstreetmap/josm/plugins/kartverket/actions/ReplaceWayAction.java b/src/org/openstreetmap/josm/plugins/kartverket/actions/ReplaceWayAction.java new file mode 100644 index 0000000..e142f03 --- /dev/null +++ b/src/org/openstreetmap/josm/plugins/kartverket/actions/ReplaceWayAction.java @@ -0,0 +1,121 @@ +package org.openstreetmap.josm.plugins.kartverket.actions; + +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.swing.JOptionPane; + +import org.openstreetmap.josm.Main; +import org.openstreetmap.josm.actions.JosmAction; +import org.openstreetmap.josm.data.osm.OsmPrimitive; +import org.openstreetmap.josm.data.osm.Way; +import org.openstreetmap.josm.gui.Notification; +import org.openstreetmap.josm.plugins.utilsplugin2.replacegeometry.ReplaceGeometryCommand; +import org.openstreetmap.josm.plugins.utilsplugin2.replacegeometry.ReplaceGeometryException; +import org.openstreetmap.josm.plugins.utilsplugin2.replacegeometry.ReplaceGeometryUtils; + +import static org.openstreetmap.josm.tools.I18n.tr; + +import org.openstreetmap.josm.tools.Shortcut; + +/** + * Replaces already existing object (id>0) with a new object (id<0). + * + * @author Zverik + */ +public class ReplaceWayAction extends JosmAction { + private static final String TITLE = tr("Replace way"); + + public ReplaceWayAction() { + super(TITLE, null, tr("Replace way of selected way with a new way"), + Shortcut.registerShortcut("tools:replacecoastline", tr("Tool: {0}", tr("Replace Geometry")), KeyEvent.VK_S, Shortcut.CTRL_SHIFT) + , true); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (getCurrentDataSet() == null) { + return; + } + + // There must be two ways selected: one with id > 0 and one new. + List selection = new ArrayList<>(getCurrentDataSet().getSelected()); + if (selection.size() != 2 && selection.get(0) instanceof Way && selection.get(1) instanceof Way) { + new Notification( + tr("This tool replaces coastline of one way with another, and so requires exactly two coatline ways to be selected.") + ).setIcon(JOptionPane.WARNING_MESSAGE).show(); + return; + } + + Way firstObject = (Way) selection.get(0); + Way secondObject = (Way) selection.get(1); + Map keys; + if (firstObject.getId() < 0) { + keys = firstObject.getKeys(); + } else { + keys = secondObject.getKeys(); + } + String source = null; + String sourceDate = null; + if (keys.containsKey("source")){ + source = keys.get("source"); + } + if (keys.containsKey("source:date")) { + sourceDate = keys.get("source:date"); + } + setSourceAndFixme(firstObject,source,sourceDate); + setSourceAndFixme(secondObject,source,sourceDate); + + try { + firstObject.getKeys(); + + ReplaceGeometryCommand replaceCommand = + ReplaceGeometryUtils.buildReplaceWayWithNewCommand(Arrays.asList(firstObject, secondObject)); + + // action was canceled + if (replaceCommand == null) + return; + + Main.main.undoRedo.add(replaceCommand); + } catch (IllegalArgumentException ex) { + new Notification( + ex.getMessage() + ).setIcon(JOptionPane.WARNING_MESSAGE).show(); + } catch (ReplaceGeometryException ex) { + new Notification( + ex.getMessage() + ).setIcon(JOptionPane.WARNING_MESSAGE).show(); + } + } + + private void setSourceAndFixme(Way way, String source, String sourceDate) { + Map keys = way.getKeys(); + if (source != null) + keys.put("source", source); + if (sourceDate != null) + keys.put("source:date", sourceDate); + if (keys.containsKey("FIXME") && keys.get("FIXME") == "Merge"){ + keys.remove("FIXME"); + } + way.setKeys(keys); + } + + @Override + protected void updateEnabledState() { + if( getCurrentDataSet() == null ) { + setEnabled(false); + } else + updateEnabledState(getCurrentDataSet().getSelected()); + } + + @Override + protected void updateEnabledState( Collection selection ) { + setEnabled(selection != null && selection.size() >= 2 ); + } +} +