Index: /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/DoubleSplitAction.java
===================================================================
--- /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/DoubleSplitAction.java	(revision 34216)
+++ /applications/editors/josm/plugins/pt_assistant/src/org/openstreetmap/josm/plugins/pt_assistant/actions/DoubleSplitAction.java	(revision 34217)
@@ -4,8 +4,13 @@
 import static org.openstreetmap.josm.tools.I18n.tr;
 
+import java.awt.Color;
 import java.awt.Cursor;
+import java.awt.Graphics2D;
 import java.awt.GridBagLayout;
+import java.awt.Point;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
 import java.awt.event.MouseEvent;
 import java.util.ArrayList;
@@ -15,5 +20,4 @@
 import java.util.List;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import javax.swing.DefaultComboBoxModel;
@@ -28,8 +32,15 @@
 import org.openstreetmap.josm.command.ChangeCommand;
 import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.SequenceCommand;
 //import org.openstreetmap.josm.command.Command;
 import org.openstreetmap.josm.command.SplitWayCommand;
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.coor.ILatLon;
+import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.RelationMember;
 import org.openstreetmap.josm.data.osm.TagMap;
 import org.openstreetmap.josm.data.osm.Way;
@@ -37,4 +48,8 @@
 import org.openstreetmap.josm.gui.ExtendedDialog;
 import org.openstreetmap.josm.gui.MainApplication;
+import org.openstreetmap.josm.gui.MapView;
+import org.openstreetmap.josm.gui.layer.AbstractMapViewPaintable;
+import org.openstreetmap.josm.plugins.pt_assistant.utils.RouteUtils;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
 import org.openstreetmap.josm.tools.GBC;
 import org.openstreetmap.josm.tools.ImageProvider;
@@ -46,5 +61,5 @@
  * @author Biswesh
  */
-public class DoubleSplitAction extends MapMode {
+public class DoubleSplitAction extends MapMode implements KeyListener {
 
 	private static final String MAP_MODE_NAME = "Double Split";
@@ -52,7 +67,12 @@
 	private transient Set<OsmPrimitive> newHighlights = new HashSet<>();
 	private transient Set<OsmPrimitive> oldHighlights = new HashSet<>();
-	private int nodeCount = 0;
 	private List<Node> atNodes = new ArrayList<>();
-	private Way previousAffectedWay;
+	private final DoubleSplitLayer temporaryLayer = new DoubleSplitLayer();
+	ILatLon Pos1 = null;
+	ILatLon Pos2 = null;
+	Way SegWay1 = null;
+	Way SegWay2 = null;
+	Way affected;
+	Way previousAffectedWay;
 
 	private final Cursor cursorJoinNode;
@@ -64,13 +84,11 @@
 	public DoubleSplitAction() {
 		super(tr(MAP_MODE_NAME), "logo_double_split", tr(MAP_MODE_NAME), null, getCursor());
-
 		cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
 		cursorJoinWay = ImageProvider.getCursor("crosshair", "joinway");
+
 	}
 
 	private static Cursor getCursor() {
-		Cursor cursor = ImageProvider.getCursor("crosshair", "bus");
-		if (cursor == null)
-			cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
+		Cursor cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
 		return cursor;
 	}
@@ -81,54 +99,62 @@
 		MainApplication.getMap().mapView.addMouseListener(this);
 		MainApplication.getMap().mapView.addMouseMotionListener(this);
+		MainApplication.getMap().mapView.addKeyListener(this);
+		MainApplication.getMap().mapView.addTemporaryLayer(temporaryLayer);
 	}
 
 	@Override
 	public void exitMode() {
-		// if we have one node selected and we exit the mode then undo the node
-		if (nodeCount == 1) {
-			for (int i = 0; i < 2; i++) {
-				MainApplication.undoRedo.undo();
-			}
-			nodeCount = 0;
-			atNodes.clear();
-			previousAffectedWay = null;
-			updateHighlights();
-		}
+		reset();
 		super.exitMode();
 		MainApplication.getMap().mapView.removeMouseListener(this);
 		MainApplication.getMap().mapView.removeMouseMotionListener(this);
+		MainApplication.getMap().mapView.removeKeyListener(this);
+		MainApplication.getMap().mapView.removeTemporaryLayer(temporaryLayer);
+		resetLayer();
 	}
 
 	private void reset() {
-		nodeCount = 0;
-		atNodes.clear();
-		previousAffectedWay = null;
-		updateHighlights();
-	}
-
-	private boolean startEndPoints(){
-		if (atNodes.get(0).isConnectionNode() && atNodes.get(1).isConnectionNode()) {
+		try {
+			atNodes.clear();
+			updateHighlights();
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		System.out.println("reset");
+	}
+
+	private void resetLayer() {
+		Pos1 = null;
+		Pos2 = null;
+		SegWay1 = null;
+		SegWay2 = null;
+	}
+
+	private boolean startEndPoints(List<Command> commandList) {
+
+		try {
 			for (Way way : atNodes.get(0).getParentWays()) {
 				if (atNodes.get(1).getParentWays().contains(way)) {
-					List<TagMap> affectedKeysList = new ArrayList<>();
-					affectedKeysList.add(way.getKeys());
-					newHighlights.add(way);
-					dialogBox(Arrays.asList(way), affectedKeysList);
-					return true;
+					if (way.isFirstLastNode(atNodes.get(0)) && way.isFirstLastNode(atNodes.get(1))) {
+						List<TagMap> affectedKeysList = new ArrayList<>();
+						affectedKeysList.add(way.getKeys());
+						newHighlights.add(way);
+						dialogBox(3, null, way, way, commandList);
+						return true;
+					}
 				}
 			}
-
-			reset();
-			return true;
-		}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+
 		return false;
 	}
 
-	private boolean firstNodeIsConnectionNode(Node node, Way affected) {
+	private boolean firstNodeIsConnectionNode(Node node, Way affected, Way previousAffectedWay) {
 		if (node.isConnectionNode()) {
 			if (node.getParentWays().contains(affected))
 				previousAffectedWay = affected;
 			else {
-				reset();
 				return true;
 			}
@@ -137,10 +163,9 @@
 	}
 
-	private boolean secondNodeIsConnectionNode(Node node) {
+	private boolean secondNodeIsConnectionNode(Node node, Way previousAffectedWay) {
 		if (atNodes.get(1).isConnectionNode()) {
 			if (atNodes.get(1).getParentWays().contains(previousAffectedWay))
 				return false;
 			else {
-				reset();
 				return true;
 			}
@@ -149,7 +174,7 @@
 	}
 
-	private Node checkCommonNode(Way affected) {
-
-        // check if they have any common node
+	private Node checkCommonNode(Way affected, Way previousAffectedWay) {
+
+		// check if they have any common node
 		List<Node> presentNodeList = affected.getNodes();
 		for (Node previousNode : previousAffectedWay.getNodes()) {
@@ -158,56 +183,215 @@
 			}
 		}
-
 		return null;
 	}
 
-	private void removeFirstNode(Way affected) {
-
-		// select the first node
-		Node nodeToBeDeleted = atNodes.get(0);
-
-		if (nodeToBeDeleted != null) {
-			// remove first node from list
-			atNodes.remove(0);
-
-			// remove last 2 commands from command list
-			Command lastCommand = MainApplication.undoRedo.commands.removeLast();
-			Command secondLastCommand = MainApplication.undoRedo.commands.removeLast();
-
-			// now we can undo the previous node as the command for present node has been
-			// removed from list
-			for (int i = 0; i < 2; i++) {
-				MainApplication.undoRedo.undo();
-			}
-
-			// now again add back the last 2 commands, so overall we undo third last and
-		    // fourth last command
-			MainApplication.undoRedo.commands.add(secondLastCommand);
-			MainApplication.undoRedo.commands.add(lastCommand);
-
-			MainApplication.undoRedo.redo();
-		}
-
-		previousAffectedWay = affected;
-
-	}
-
-	private void addKeysOnBothWays(Node commonNode, Way affected) {
+	private void removeFirstNode() {
+
+		atNodes.get(0).setDeleted(true);
+		atNodes.get(1).setDeleted(true);
+		Pos1 = Pos2;
+		Pos2 = null;
+		SegWay1 = SegWay2;
+		SegWay2 = null;
+		reset();
+	}
+
+	private void action() {
+
+		List<Command> commandList = new ArrayList<>();
+
+		// check if the user has selected an existing node, or a new one
+		Node node1 = createNode(Pos1, commandList);
+		if (node1 == null) {
+			resetLayer();
+			reset();
+			return;
+		}
+		Node node2 = createNode(Pos2, commandList);
+		if (node2 == null) {
+			node1.setDeleted(true);
+			Pos2 = null;
+			SegWay2 = null;
+			reset();
+			return;
+		}
+
+		if (node1.equals(node2)) {
+			resetLayer();
+			return;
+		}
+
+		atNodes.add(node1);
+		atNodes.add(node2);
+
+		previousAffectedWay = SegWay1;
+		affected = SegWay2;
+
+		if (affected == null || previousAffectedWay == null) {
+			node1.setDeleted(true);
+			node2.setDeleted(true);
+			resetLayer();
+			return;
+		}
+
+		// if both the nodes are starting and ending points of the same way
+		// we don't split the way, just add new key-value to the way
+		boolean areStartEndPoints = startEndPoints(commandList);
+		if (areStartEndPoints) {
+			resetLayer();
+			return;
+		}
+
+		// if first node is a connection node
+		boolean isConnectionNode = firstNodeIsConnectionNode(atNodes.get(0), affected, previousAffectedWay);
+		if (isConnectionNode) {
+			resetLayer();
+			return;
+		}
+
+		// if second node is a connection node
+		isConnectionNode = secondNodeIsConnectionNode(atNodes.get(1), previousAffectedWay);
+		if (isConnectionNode) {
+			resetLayer();
+			return;
+		} else {
+			if (atNodes.get(1).isConnectionNode()) {
+				affected = previousAffectedWay;
+			}
+		}
+
+		// if both the nodes are not on same way and don't have any common node then
+		// make second node as first node
+		Node commonNode = null;
+		boolean twoWaysWithCommonNode = false;
+		if (previousAffectedWay != affected) {
+			commonNode = checkCommonNode(affected, previousAffectedWay);
+			if (commonNode == null) {
+				removeFirstNode();
+				return;
+			} else {
+				twoWaysWithCommonNode = true;
+			}
+		}
+
+		if (twoWaysWithCommonNode) {
+			dialogBox(1, commonNode, affected, previousAffectedWay, commandList);
+		} else {
+			dialogBox(2, commonNode, affected, previousAffectedWay, commandList);
+		}
+	}
+
+	private void dialogBox(int type, Node commonNode, Way affected, Way previousAffectedWay,
+			List<Command> commandList) {
+
+		final ExtendedDialog dialog = new SelectFromOptionDialog(type, commonNode, affected, previousAffectedWay,
+				commandList, atNodes);
+		dialog.toggleEnable("way.split.segment-selection-dialog");
+		if (!dialog.toggleCheckState()) {
+			dialog.setModal(false);
+			dialog.showDialog();
+			return; // splitting is performed in SegmentToKeepSelectionDialog.buttonAction()
+		}
+
+	}
+
+	private Node createNode(ILatLon Pos, List<Command> commandList) {
+		Boolean newNode = false;
+		Node newStopPos;
+
+		Point p = new Point();
+		p.setLocation(Pos.lat(), Pos.lon());
+
+		Node n = MainApplication.getMap().mapView.getNearestNode(p, OsmPrimitive::isUsable);
+		if (n == null) {
+			newNode = true;
+			newStopPos = new Node(MainApplication.getMap().mapView.getLatLon(Pos.lat(), Pos.lon()));
+		} else {
+			newStopPos = new Node(n);
+		}
+
+		if (newNode) {
+			commandList.add(new AddCommand(getLayerManager().getEditDataSet(), newStopPos));
+		} else {
+			commandList.add(new ChangeCommand(n, newStopPos));
+			MainApplication.getLayerManager().getEditLayer().data.setSelected(newStopPos);
+			newStopPos = n;
+		}
+
+		return newStopPos;
+	}
+
+	private void addKeys(Way affected, List<Command> commandList, JComboBox<String> keys, JComboBox<String> values) {
 		List<TagMap> affectedKeysList = new ArrayList<>();
+		Way selectedWay = null;
+
+		MainApplication.undoRedo.add(new SequenceCommand("Add Nodes", commandList));
+		commandList.clear();
+
+		addParentWay(atNodes.get(0));
+		addParentWay(atNodes.get(1));
+
+		SplitWayCommand result = SplitWayCommand.split(affected, atNodes, Collections.emptyList());
+		if (result == null)
+			return;
+
+		commandList.add(result);
+		MainApplication.undoRedo.add(new SequenceCommand("Split Way", commandList));
+
+		// Find the middle way after split
+		List<Way> affectedWayList = result.getNewWays();
+		affectedWayList.add(result.getOriginalWay());
+
+		for (Way way : affectedWayList) {
+			if (atNodes.contains(way.firstNode()) && atNodes.contains(way.lastNode())) {
+				selectedWay = way;
+				break;
+			}
+		}
+
+		if (selectedWay != null) {
+			affectedKeysList.add(affected.getKeys());
+			addTags(affectedKeysList, Arrays.asList(selectedWay), keys, values);
+		}
+	}
+
+	private void addKeysOnBothWays(Node commonNode, Way affected, Way previousAffectedWay, List<Command> commandList,
+			JComboBox<String> keys, JComboBox<String> values) {
+		List<TagMap> affectedKeysList = new ArrayList<>();
+
+		MainApplication.undoRedo.add(new SequenceCommand("Add Nodes", commandList));
+		commandList.clear();
+
+		// join newly created nodes to parent ways
+		addParentWay(atNodes.get(0));
+		addParentWay(atNodes.get(1));
 
 		List<Node> nodelist1 = Arrays.asList(atNodes.get(0), commonNode);
 		List<Node> nodelist2 = Arrays.asList(atNodes.get(1), commonNode);
 
+		// required to be added to newly split way
 		affectedKeysList.add(previousAffectedWay.getKeys());
 		affectedKeysList.add(affected.getKeys());
 
 		// split both the ways separately
-
 		SplitWayCommand result1 = SplitWayCommand.split(previousAffectedWay, nodelist1, Collections.emptyList());
-
 		SplitWayCommand result2 = SplitWayCommand.split(affected, nodelist2, Collections.emptyList());
 
-		MainApplication.undoRedo.add(result1);
-		MainApplication.undoRedo.add(result2);
+		if (result1 != null)
+			commandList.add(result1);
+		if (result2 != null)
+			commandList.add(result2);
+		MainApplication.undoRedo.add(new SequenceCommand("Split Way", commandList));
+
+		// add newly split way to relations
+		List<Relation> referrers1 = OsmPrimitive.getFilteredList(previousAffectedWay.getReferrers(), Relation.class);
+		referrers1.removeIf(r -> !RouteUtils.isPTRoute(r));
+
+		int Index1 = getIndex(previousAffectedWay, referrers1, previousAffectedWay);
+
+		List<Relation> referrers2 = OsmPrimitive.getFilteredList(affected.getReferrers(), Relation.class);
+		referrers2.removeIf(r -> !RouteUtils.isPTRoute(r));
+
+		int Index2 = getIndex(affected, referrers2, previousAffectedWay);
 
 		Way way1 = null, way2 = null;
@@ -217,4 +401,5 @@
 		boolean isOriginalWay = true; // we check both the original way and new ways
 		for (Way way : result1.getNewWays()) {
+			checkMembership(way, referrers1, Index1);
 			if (way.containsNode(commonNode) && way.containsNode(atNodes.get(0))) {
 				way1 = way;
@@ -223,4 +408,7 @@
 			}
 		}
+
+		checkMembership(result1.getOriginalWay(), referrers1, Index1);
+
 		if (isOriginalWay) {
 			Way way = result1.getOriginalWay();
@@ -234,4 +422,5 @@
 
 		for (Way way : result2.getNewWays()) {
+			checkMembership(way, referrers2, Index2);
 			if (way.containsNode(commonNode) && way.containsNode(atNodes.get(1))) {
 				way2 = way;
@@ -241,4 +430,6 @@
 		}
 
+		checkMembership(result2.getOriginalWay(), referrers2, Index2);
+
 		if (isOriginalWay) {
 			Way way = result2.getOriginalWay();
@@ -250,34 +441,92 @@
 		if (way1 != null && way2 != null) {
 			List<Way> selectedWays = Arrays.asList(way1, way2);
-			newHighlights.add(way1);
-			newHighlights.add(way2);
-			dialogBox(selectedWays, affectedKeysList);
-		}
-	}
-
-	private void addKeys(Way affected) {
+			addTags(affectedKeysList, selectedWays, keys, values);
+		}
+	}
+
+	private void addKeysWhenStartEndPoint(Way affected, List<Command> commandList, JComboBox<String> keys,
+			JComboBox<String> values) {
 		List<TagMap> affectedKeysList = new ArrayList<>();
-		Way selectedWay = null;
-
-		SplitWayCommand result = SplitWayCommand.split(affected, atNodes, Collections.emptyList());
-		if (result == null)
-			return;
-
-		MainApplication.undoRedo.add(result);
-
-		// Find the middle way after split
-		List<Way> affectedWayList = result.getNewWays();
-		for (Way way : affectedWayList) {
-			if (atNodes.contains(way.firstNode()) && atNodes.contains(way.lastNode())) {
-				selectedWay = way;
-				break;
-			}
-		}
+		Way selectedWay = affected;
+
+		MainApplication.undoRedo.add(new SequenceCommand("Add Nodes", commandList));
+		commandList.clear();
+
+		addParentWay(atNodes.get(0));
+		addParentWay(atNodes.get(1));
 
 		if (selectedWay != null) {
 			affectedKeysList.add(affected.getKeys());
-			newHighlights.add(selectedWay);
-			dialogBox(Arrays.asList(selectedWay), affectedKeysList);
-		}
+			addTags(affectedKeysList, Arrays.asList(selectedWay), keys, values);
+		}
+	}
+
+	private void addParentWay(Node node) {
+		if (node.getParentWays().size() == 0) {
+			MainApplication.getLayerManager().getEditLayer().data.setSelected(node);
+			// join the node to the way only if the node is new
+			JoinNodeWayAction joinNodeWayAction = JoinNodeWayAction.createMoveNodeOntoWayAction();
+			joinNodeWayAction.actionPerformed(null);
+		}
+	}
+
+	private void checkMembership(Way way, List<Relation> referrers, int Index) {
+		for (Relation r : referrers) {
+			boolean isMember = false;
+			for (RelationMember rm : r.getMembers()) {
+				if (rm.getType() == OsmPrimitiveType.WAY) {
+					if (rm.getWay().equals(way)) {
+						isMember = true;
+					}
+				}
+			}
+			if (!isMember) {
+				r.addMember(new RelationMember("", way));
+			}
+		}
+	}
+
+	private int getIndex(Way way, List<Relation> referrers, Way previousAffectedWay) {
+		int Index = -1;
+		for (Relation r : referrers) {
+			for (int i = 0; i < r.getMembers().size(); i++) {
+				if (r.getMembers().get(i).isWay() && r.getMembers().get(i).getWay().equals(previousAffectedWay)) {
+					Index = i;
+				}
+			}
+		}
+		return Index;
+	}
+
+	private void addTags(List<TagMap> affectedKeysList, List<Way> selectedWay, JComboBox<String> keys,
+			JComboBox<String> values) {
+		TagMap newKeys1 = affectedKeysList.get(0);
+		newKeys1.put(keys.getSelectedItem().toString(), values.getSelectedItem().toString());
+
+		if (keys.getSelectedItem() == "bridge") {
+			newKeys1.put("layer", "1");
+			selectedWay.get(0).setKeys(newKeys1);
+		} else if (keys.getSelectedItem() == "tunnel") {
+			newKeys1.put("layer", "-1");
+			selectedWay.get(0).setKeys(newKeys1);
+		} else {
+			selectedWay.get(0).setKeys(newKeys1);
+		}
+
+		if (affectedKeysList.size() == 2) {
+			TagMap newKeys2 = affectedKeysList.get(1);
+			newKeys2.put(keys.getSelectedItem().toString(), values.getSelectedItem().toString());
+
+			if (keys.getSelectedItem() == "bridge") {
+				newKeys2.put("layer", "1");
+				selectedWay.get(1).setKeys(newKeys2);
+			} else if (keys.getSelectedItem() == "tunnel") {
+				newKeys2.put("layer", "-1");
+				selectedWay.get(1).setKeys(newKeys2);
+			} else {
+				selectedWay.get(1).setKeys(newKeys2);
+			}
+		}
+		resetLayer();
 	}
 
@@ -313,116 +562,26 @@
 	public void mouseClicked(MouseEvent e) {
 
-		Boolean newNode = false;
-		Node newStopPos;
-
-		// check if the user has selected an existing node, or a new one
-		Node n = MainApplication.getMap().mapView.getNearestNode(e.getPoint(), OsmPrimitive::isUsable);
-		if (n == null) {
-			newNode = true;
-			newStopPos = new Node(MainApplication.getMap().mapView.getLatLon(e.getX(), e.getY()));
-		} else {
-			newStopPos = new Node(n);
-		}
-
-		if (newNode) {
-			MainApplication.undoRedo.add(new AddCommand(getLayerManager().getEditDataSet(), newStopPos));
-		} else {
-			MainApplication.undoRedo.add(new ChangeCommand(n, newStopPos));
-			newStopPos = n;
-		}
-
-		MainApplication.getLayerManager().getEditLayer().data.setSelected(newStopPos);
-
-		// join the node to the way only if the node is new
-		if (newNode) {
-			JoinNodeWayAction joinNodeWayAction = JoinNodeWayAction.createMoveNodeOntoWayAction();
-			joinNodeWayAction.actionPerformed(null);
-		}
-
-		if (newStopPos.getParentWays().isEmpty())
-			return;
-
-		Way affected = newStopPos.getParentWays().get(0);
-
-		if (affected == null)
-			return;
-
-		atNodes.add(newStopPos);
-
-		// to check the number of nodes that have been selected
-		// do not split if this is the first selected node
-		if (nodeCount == 0) {
-			previousAffectedWay = affected;
-			nodeCount++;
-			return;
-		}
-
-		// if both the nodes are starting and ending points of the same way
-		// we don't split the way, just add new key-value to the way
-		boolean areStartEndPoints = startEndPoints();
-		if (areStartEndPoints)
-			return;
-
-		// if first node is a connection node
-		boolean isConnectionNode = firstNodeIsConnectionNode(atNodes.get(0), affected);
-		if (isConnectionNode)
-			return;
-
-		// if second node is a connection node
-		isConnectionNode = secondNodeIsConnectionNode(atNodes.get(1));
-		if (isConnectionNode)
-			return;
-		else {
-			if (atNodes.get(1).isConnectionNode()) {
-				affected = previousAffectedWay;
-			}
-		}
-
-
-		// if both the nodes are not on same way and don't have any common node then
-		// make second node as first node
-		Node commonNode = null;
-		boolean twoWaysWithCommonNode = false;
-		if (previousAffectedWay != affected) {
-			commonNode = checkCommonNode(affected);
-			if (commonNode == null) {
-				removeFirstNode(affected);
-				return;
-			} else {
-				twoWaysWithCommonNode = true;
-			}
-		}
-
-
-		// ****need to add undoredo for previousAffectedWay, atNode, nodeCount
-
-		if (twoWaysWithCommonNode) {
-			addKeysOnBothWays(commonNode, affected);
-
-		} else {
-            addKeys(affected);
-		}
-
-		// reset values of all the variables after two nodes are selected and split
-		reset();
-
-	}
-
-	private void dialogBox(List<Way> selectedWay, List<TagMap> affectedKeysList) {
-
-		final ExtendedDialog dialog = new SelectFromOptionDialog(selectedWay, affectedKeysList);
-		dialog.toggleEnable("way.split.segment-selection-dialog");
-		if (!dialog.toggleCheckState()) {
-			dialog.setModal(false);
-			dialog.showDialog();
-			return; // splitting is performed in SegmentToKeepSelectionDialog.buttonAction()
-		}
-
+		if (Pos1 == null) {
+			SegWay1 = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isSelectable);
+			if (SegWay1 != null) {
+				Pos1 = new LatLon(e.getX(), e.getY());
+			}
+		} else if (Pos2 == null) {
+			if (Pos1.lat() != e.getX() || Pos1.lon() != e.getY()) {
+				SegWay2 = MainApplication.getMap().mapView.getNearestWay(e.getPoint(), OsmPrimitive::isSelectable);
+				if (SegWay2 != null)
+					Pos2 = new LatLon(e.getX(), e.getY());
+			}
+		}
+
+		if (Pos2 != null) {
+			reset();
+			action();
+		}
 	}
 
 	// turn off what has been highlighted on last mouse move and highlight what has
 	// to be highlighted now
-
-    private void updateHighlights() {
+	private void updateHighlights() {
 		if (oldHighlights.isEmpty() && newHighlights.isEmpty()) {
 			return;
@@ -444,18 +603,48 @@
 	}
 
+	@Override
+	public void keyTyped(KeyEvent e) {
+		System.out.println("keyTyped");
+	}
+
+	@Override
+	public void keyPressed(KeyEvent e) {
+		System.out.println("keyPressed");
+		boolean z = e.getKeyCode() == KeyEvent.VK_Z;
+		updateKeyModifiers(e);
+		System.out.println(ctrl);
+		if (z) {
+			if (Pos1 != null && Pos2 == null) {
+				Pos1 = null;
+				SegWay1 = null;
+				temporaryLayer.invalidate();
+			}
+		}
+	}
+
+	@Override
+	public void keyReleased(KeyEvent e) {
+		System.out.println("keyReleased");
+	}
+
 	// A dialogBox to query whether to select bus_bay, tunnel or bridge.
 
-	static class SelectFromOptionDialog extends ExtendedDialog {
-		static final AtomicInteger DISPLAY_COUNT = new AtomicInteger();
-		final transient List<Way> selectedWay;
+	private class SelectFromOptionDialog extends ExtendedDialog {
+		Way affected, previousAffectedWay;
 		private JComboBox<String> keys;
 		private JComboBox<String> values;
-		private List<TagMap> affectedKeysList;
-
-		SelectFromOptionDialog(List<Way> selectedWay, List<TagMap> affectedKeysList) {
+		private int type;
+		private List<Command> commandList;
+		private Node commonNode;
+
+		SelectFromOptionDialog(int type, Node commonNode, Way affected, Way previousAffectedWay,
+				List<Command> commandList, List<Node> atNode) {
 			super(Main.parent, tr("What do you want the segment to be?"), new String[] { tr("Ok"), tr("Cancel") },
 					true);
-			this.selectedWay = selectedWay;
-			this.affectedKeysList = affectedKeysList;
+			this.affected = affected;
+			this.previousAffectedWay = previousAffectedWay;
+			this.commandList = commandList;
+			this.type = type;
+			this.commonNode = commonNode;
 
 			setButtonIcons("ok", "cancel");
@@ -499,33 +688,35 @@
 
 			if (getValue() == 1) {
-				TagMap newKeys1 = this.affectedKeysList.get(0);
-				newKeys1.put(keys.getSelectedItem().toString(), values.getSelectedItem().toString());
-
-				if (keys.getSelectedItem() == "bridge") {
-					newKeys1.put("layer", "1");
-					this.selectedWay.get(0).setKeys(newKeys1);
-				} else if (keys.getSelectedItem() == "tunnel") {
-					newKeys1.put("layer", "-1");
-					this.selectedWay.get(0).setKeys(newKeys1);
-				} else {
-					this.selectedWay.get(0).setKeys(newKeys1);
+				if (this.type == 1) {
+					addKeysOnBothWays(this.commonNode, this.affected, this.previousAffectedWay, this.commandList, keys,
+							values);
+				} else if (this.type == 2) {
+					addKeys(this.affected, this.commandList, keys, values);
+				} else if (this.type == 3) {
+					addKeysWhenStartEndPoint(this.affected, this.commandList, keys, values);
 				}
 
-				if (this.affectedKeysList.size() == 2) {
-					TagMap newKeys2 = this.affectedKeysList.get(1);
-					newKeys2.put(keys.getSelectedItem().toString(), values.getSelectedItem().toString());
-
-					if (keys.getSelectedItem() == "bridge") {
-						newKeys2.put("layer", "1");
-						this.selectedWay.get(1).setKeys(newKeys2);
-					} else if (keys.getSelectedItem() == "tunnel") {
-						newKeys2.put("layer", "-1");
-						this.selectedWay.get(1).setKeys(newKeys2);
-					} else {
-						this.selectedWay.get(1).setKeys(newKeys2);
-					}
-				}
-			}
-		}
-	}
+			} else if (getValue() == 2) {
+				resetLayer();
+			}
+		}
+
+	}
+
+	private class DoubleSplitLayer extends AbstractMapViewPaintable {
+		@Override
+		public void paint(Graphics2D g, MapView mv, Bounds bbox) {
+			if (Pos1 != null) {
+				CheckParameterUtil.ensureParameterNotNull(mv, "mv");
+				g.setColor(Color.RED);
+				g.fillRect((int) Pos1.lat(), (int) Pos1.lon(), 6, 6);
+			}
+			if (Pos2 != null) {
+				CheckParameterUtil.ensureParameterNotNull(mv, "mv");
+				g.setColor(Color.RED);
+				g.fillRect((int) Pos2.lat(), (int) Pos2.lon(), 5, 5);
+			}
+		}
+	}
+
 }
