-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 1 addition & 10 deletions
11
src/org/openstreetmap/josm/plugins/kartverket/actions/CheckDirectionAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/org/openstreetmap/josm/plugins/kartverket/actions/ReplaceWayAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
|