Index: applications/editors/josm/plugins/wayselector/README
===================================================================
--- applications/editors/josm/plugins/00_plugin_dir_template/README	(revision 20480)
+++ applications/editors/josm/plugins/wayselector/README	(revision 20482)
@@ -1,4 +1,18 @@
-README 
+README
 ======
 
-Readme for your plugin 
+The way selector plugin allows easy selection of sequential ways that
+have been divided into segments.
+
+Created by Marko Mäkelä for selecting the mainland natural=coastline
+within an area without selecting any islands.  Thanks to Harry Wood
+for the way downloader plugin, which was a useful resource for learning.
+
+Usage: Select one or more ways and activate the plugin from the Tools
+menu or by the keyboard shortcut.  The selection of each selected way
+will be extended until the non-branching sequence of ways ends.  You
+may select one or more ways at the branches and activate the plugin
+again, until you have selected all the desired ways.
+
+When this plugin is activated, the selection may include other
+primitives than ways.  This plugin only operates on the selected ways.
Index: applications/editors/josm/plugins/wayselector/build.xml
===================================================================
--- applications/editors/josm/plugins/00_plugin_dir_template/build.xml	(revision 20480)
+++ applications/editors/josm/plugins/wayselector/build.xml	(revision 20482)
@@ -28,10 +28,10 @@
 **
 -->
-<project name="myPluginName" default="dist" basedir=".">
+<project name="wayselector" default="dist" basedir=".">
 
 	<!-- enter the SVN commit message -->
-	<property name="commit.message" value="Commit message" />
+	<property name="commit.message" value="Initial release of the Way Selector plugin" />
 	<!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
-	<property name="plugin.main.version" value="" />
+	<property name="plugin.main.version" value="3095" />
 
 
@@ -60,10 +60,10 @@
 	<!--
     **********************************************************
-    ** compile - complies the source tree
+    ** compile - compiles the source tree
     **********************************************************
     -->
 	<target name="compile" depends="init">
 		<echo message="compiling sources for  ${plugin.jar} ... "/>
-		<javac srcdir="src" classpath="${josm}" debug="true" destdir="${plugin.build.dir}">
+		<javac srcdir="src" classpath="${josm}" debug="true" destdir="${plugin.build.dir}" includeantruntime="false">
 			<compilerarg value="-Xlint:deprecation"/>
 			<compilerarg value="-Xlint:unchecked"/>
@@ -100,9 +100,9 @@
     -->
 			<manifest>
-				<attribute name="Author" value="..."/>
-				<attribute name="Plugin-Class" value="..."/>
+				<attribute name="Author" value="Marko Mäkelä"/>
+				<attribute name="Plugin-Class" value="org.openstreetmap.josm.plugins.wayselector.WaySelectorPlugin"/>
 				<attribute name="Plugin-Date" value="${version.entry.commit.date}"/>
-				<attribute name="Plugin-Description" value="..."/>
-				<attribute name="Plugin-Link" value="..."/>
+				<attribute name="Plugin-Description" value="Select a sequence of non-branching connected ways"/>
+				<attribute name="Plugin-Link" value="http://wiki.openstreetmap.org/wiki/JOSM/Plugins/WaySelectorPlugin"/>
 				<attribute name="Plugin-Mainversion" value="${plugin.main.version}"/>
 				<attribute name="Plugin-Version" value="${version.entry.commit.revision}"/>
@@ -157,8 +157,8 @@
 
 	<!--
-	************************** Publishing the plugin *********************************** 
+	************************** Publishing the plugin ***********************************
 	-->
 	<!--
-		** extracts the JOSM release for the JOSM version in ../core and saves it in the 
+		** extracts the JOSM release for the JOSM version in ../core and saves it in the
 		** property ${coreversion.info.entry.revision}
 		**
@@ -209,18 +209,18 @@
 
 	<!--
-		** commits the plugin.jar 
+		** commits the plugin.jar
 		-->
 	<target name="commit-dist">
 		<echo>
 	***** Properties of published ${plugin.jar} *****
-	Commit message    : '${commit.message}'					
+	Commit message    : '${commit.message}'
 	Plugin-Mainversion: ${plugin.main.version}
 	JOSM build version: ${coreversion.info.entry.revision}
 	Plugin-Version    : ${version.entry.commit.revision}
-	***** / Properties of published ${plugin.jar} *****					
-						
+	***** / Properties of published ${plugin.jar} *****
+
 	Now commiting ${plugin.jar} ...
 	</echo>
-		<exec append="true" output="svn.log" executable="svn" failifexecutionfails="false">
+		<exec append="true" output="svn.log" executable="svn" failifexecutionfails="true" failonerror="true">
 			<env key="LANG" value="C"/>
 			<arg value="-m '${commit.message}'"/>
Index: applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java
===================================================================
--- applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java	(revision 20482)
+++ applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java	(revision 20482)
@@ -0,0 +1,124 @@
+package org.openstreetmap.josm.plugins.wayselector;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.TreeSet;
+
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Way;
+
+/**
+ * Auxiliary class for the Way Selector plugin
+ *
+ * @author Marko Mäkelä
+ */
+public class WaySelection {
+    /** selected ways */
+    Collection<Way> ways;
+    /** outer endpoints of selected ways */
+    TreeSet<Node> outerNodes;
+    /** endpoints of selected ways */
+    TreeSet<Node> nodes;
+
+    /** Creates a way selection
+     @param[in] selection a selection of ways
+     */
+    public WaySelection(Collection<Way> ways) {
+        this.ways = ways;
+	outerNodes = null;
+	nodes = null;
+    }
+
+    /** Add a way endpoint to nodes, outerNodes
+     @param node a way endpoint */
+    private void addNodes(Node node) {
+        if (node == null);
+	else if (!nodes.add(node))
+	    outerNodes.remove(node);
+	else
+	    outerNodes.add(node);
+    }
+
+    /** Add the endpoints of the way to nodes, outerNodes
+     @param way a way whose endpoints are added */
+    private void addNodes(Way way) {
+        addNodes(way.firstNode());
+	addNodes(way.lastNode());
+    }
+
+    /** Find out if the selection can be extended
+     @return true if the selection can be extended */
+    public boolean canExtend() {
+        if (ways.isEmpty())
+            return false;
+
+        nodes = new TreeSet<Node>();
+        outerNodes = new TreeSet<Node>();
+
+        for (Way way : ways)
+	    addNodes(way);
+
+        return !outerNodes.isEmpty();
+    }
+
+    /**
+     Finds out if the current selection can be extended.
+     @param selection current selection (ways and others)
+     @param node perimeter node from which to extend the selection
+     @return a way by which to extend the selection, or null */
+    private Way findWay(Collection<OsmPrimitive> selection, Node node) {
+        TreeSet<Way> foundWays = new TreeSet<Way>();
+
+        for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
+						    Way.class))
+	    if (way.getNodesCount() >= 2 && !selection.contains(way) &&
+		way.isFirstLastNode(node))
+                foundWays.add(way);
+
+	return foundWays.size() == 1 ? foundWays.first() : null;
+    }
+
+    /**
+     Finds out if the current selection can be extended.
+
+     The members ways, outerNodes, nodes must have been
+     initialized; @see canExtend(). How to update these
+     members when extending the selection, @see extend().
+     @param selection current selection
+     @return a way by which to extend the selection, or null */
+    private Way findWay(Collection<OsmPrimitive> selection) {
+	for (Node node : outerNodes) {
+	    Way way = findWay(selection, node);
+	    if (way != null)
+	        return way;
+	}
+
+	return null;
+    }
+
+    /** Extend the current selection
+     @param data the data set in which to extend the selection */
+    void extend(DataSet data) {
+        Collection<OsmPrimitive> selection = data.getSelected();
+	boolean selectionChanged = false;
+        Way way;
+
+        if (!canExtend())
+	    return;
+
+	while ((way = findWay(selection)) != null) {
+	    if (!selection.add(way))
+	        break;
+
+	    selectionChanged = true;
+	    ways.add(way);
+	    addNodes(way);
+	}
+
+	if (selectionChanged)
+	    data.setSelected(selection, true);
+    }
+}
Index: applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelectorPlugin.java
===================================================================
--- applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelectorPlugin.java	(revision 20482)
+++ applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelectorPlugin.java	(revision 20482)
@@ -0,0 +1,67 @@
+package org.openstreetmap.josm.plugins.wayselector;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.TreeSet;
+
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.actions.JosmAction;
+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.data.osm.Way;
+import org.openstreetmap.josm.gui.MainMenu;
+import org.openstreetmap.josm.plugins.Plugin;
+import org.openstreetmap.josm.plugins.PluginInformation;
+import org.openstreetmap.josm.tools.Shortcut;
+
+/**
+ * Plugin class for the Way Selector plugin
+ *
+ * @author Marko Mäkelä
+ */
+public class WaySelectorPlugin extends Plugin {
+    /**
+     * Creates the plugin at JOSM startup
+     *
+     * @param info the plugin information describing the plugin.
+     */
+    public WaySelectorPlugin(PluginInformation info) {
+        super(info);
+
+        MainMenu.add(Main.main.menu.toolsMenu, new WaySelectAction());
+    }
+
+    private class WaySelectAction extends JosmAction {
+        /** Set up the action (text appearing on the menu, keyboard shortcut etc */
+        public WaySelectAction() {
+            super(tr("Way Select"),
+                  "way-select",
+                  tr("Select non-branching sequences of ways"),
+                  Shortcut.registerShortcut("wayselector:wayselect", tr("Way Select"), KeyEvent.VK_W, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT),
+                  true);
+        }
+
+        public void actionPerformed(ActionEvent ev) {
+	    DataSet ds = Main.main.getCurrentDataSet();
+	    WaySelection ws = new WaySelection(ds.getSelectedWays());
+	    ws.extend(ds);
+        }
+
+        /**
+         * Update the enabled state of the action when something in
+         * the JOSM state changes, i.e. when a layer is removed or
+         * added.
+         */
+        @Override
+        protected void updateEnabledState() {
+            setEnabled(Main.main.getCurrentDataSet() != null);
+        }
+    }
+}
