Skip to content

Commit

Permalink
Added replace way action
Browse files Browse the repository at this point in the history
  • Loading branch information
tibnor committed Jun 7, 2015
1 parent 2d3f0f5 commit 0a949f6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 12 deletions.
7 changes: 5 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
<property name="commit.message" value="Initial"/>
<!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
<property name="plugin.main.version" value="7001"/>
<property name="plugin.version" value="1"/>
<property name="plugin.canloadatruntime" value="true"/>
<!-- Configure these properties (replace "..." accordingly).
See http://josm.openstreetmap.de/wiki/DevelopersGuide/DevelopingPlugins
-->
<property name="plugin.author" value="Torstein Ingebrigtsen Bø"/>
<property name="plugin.class" value="org.openstreetmap.josm.plugins.kartverket.KartverketPlugin"/>
<property name="plugin.description" value="Tools to help import Kartverket N50"/>
<property name="plugin.link" value="https://github.com/JOSM/kartverketimport"/>
<!--<<property name="plugin.icon" value="..."/>-->
<!--<property name="plugin.link" value="..."/>-->
<!--<property name="plugin.early" value="..."/>-->
<!--<property name="plugin.requires" value="..."/>-->
<property name="plugin.requires" value="utilsplugin2"/>
<!--<property name="plugin.stage" value="..."/>-->

<!-- ** include targets that all plugins have in common ** -->
<import file="../build-common.xml"/>
<fileset id="plugin.requires.jars" dir="${plugin.dist.dir}">
<include name="utilsplugin2.jar"/>
</fileset>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OsmPrimitive> 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<String, String> 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<String, String> 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<? extends OsmPrimitive> selection ) {
setEnabled(selection != null && selection.size() >= 2 );
}
}

0 comments on commit 0a949f6

Please sign in to comment.