From bdbae0690eb4cb22e05d1d072624066e90bc9b84 Mon Sep 17 00:00:00 2001
From: Francescu Garoby <windu.2b@gmail.com>
Date: Sun, 31 May 2015 16:45:11 +0200
Subject: [PATCH] Load a range of OSM objects, using a hyphen as a separator
Using a hyphen as a separator, range of objects IDs can be loaded.
It is easier to write "w1-5" than "w1, w2, w3, w4, w5", and the result
is exactly the same.
---
.../josm/data/osm/SimplePrimitiveId.java | 19 ++++++++++++++++---
.../josm/gui/dialogs/OsmIdSelectionDialog.java | 7 +++++--
.../josm/gui/widgets/OsmIdTextField.java | 6 +++++-
.../josm/data/osm/SimplePrimitiveIdTest.groovy | 12 ++++++++++++
4 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java b/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
index 6bf4779..0e29563 100644
|
a
|
b
|
public class SimplePrimitiveId implements PrimitiveId, Serializable {
|
| 16 | 16 | |
| 17 | 17 | public static final Pattern ID_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)[ /]?)(\\d+)"); |
| 18 | 18 | |
| | 19 | public static final Pattern MULTIPLE_IDS_PATTERN = Pattern.compile("((n(ode)?|w(ay)?|r(el(ation)?)?)/?)(\\d+)(-(\\d+))?"); |
| | 20 | |
| 19 | 21 | public SimplePrimitiveId(long id, OsmPrimitiveType type) { |
| 20 | 22 | this.id = id; |
| 21 | 23 | this.type = type; |
| … |
… |
public class SimplePrimitiveId implements PrimitiveId, Serializable {
|
| 97 | 99 | */ |
| 98 | 100 | public static List<SimplePrimitiveId> fuzzyParse(String s) { |
| 99 | 101 | final List<SimplePrimitiveId> ids = new ArrayList<>(); |
| 100 | | final Matcher m = ID_PATTERN.matcher(s); |
| | 102 | final Matcher m = MULTIPLE_IDS_PATTERN.matcher(s); |
| 101 | 103 | while (m.find()) { |
| 102 | 104 | final char firstChar = s.charAt(m.start()); |
| 103 | | ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())), |
| 104 | | firstChar == 'n' |
| | 105 | if (null != m.group(m.groupCount())) { |
| | 106 | for(long r = Long.parseLong(m.group(m.groupCount())) - 2; r <= Long.parseLong(m.group(m.groupCount())); r ++) { |
| | 107 | ids.add(new SimplePrimitiveId(r, |
| | 108 | firstChar == 'n' |
| 105 | 109 | ? OsmPrimitiveType.NODE |
| 106 | 110 | : firstChar == 'w' |
| 107 | 111 | ? OsmPrimitiveType.WAY |
| 108 | 112 | : OsmPrimitiveType.RELATION)); |
| | 113 | } |
| | 114 | } else { |
| | 115 | ids.add(new SimplePrimitiveId(Long.parseLong(m.group(m.groupCount())) - 2, |
| | 116 | firstChar == 'n' |
| | 117 | ? OsmPrimitiveType.NODE |
| | 118 | : firstChar == 'w' |
| | 119 | ? OsmPrimitiveType.WAY |
| | 120 | : OsmPrimitiveType.RELATION)); |
| | 121 | } |
| 109 | 122 | } |
| 110 | 123 | return ids; |
| 111 | 124 | } |
diff --git a/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java b/src/org/openstreetmap/josm/gui/dialogs/OsmIdSelectionDialog.java
index 0c53f8e..8ca3249 100644
|
a
|
b
|
public class OsmIdSelectionDialog extends ExtendedDialog implements WindowListen
|
| 93 | 93 | HtmlPanel help = new HtmlPanel(/* I18n: {0} and {1} contains example strings not meant for translation. {2}=n, {3}=w, {4}=r. */ |
| 94 | 94 | tr("Object IDs can be separated by comma or space.<br/>" |
| 95 | 95 | + "Examples: {0}<br/>" |
| 96 | | + "In mixed mode, specify objects like this: {1}<br/>" |
| 97 | | + "({2} stands for <i>node</i>, {3} for <i>way</i>, and {4} for <i>relation</i>)", |
| | 96 | + "In mixed mode, specify objects like this: {1}<br/><br/>" |
| | 97 | + "Ranges of object IDs can also be catched, by a hyphen.<br/>" |
| | 98 | + "Examples: {2}<br/>" |
| | 99 | + "({3} stands for <i>node</i>, {4} for <i>way</i>, and {5} for <i>relation</i>)", |
| 98 | 100 | "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("1 2 5", "1,2,5")) + "</b>", |
| 99 | 101 | "<b>w123, n110, w12, r15</b>", |
| | 102 | "<b>" + Utils.joinAsHtmlUnorderedList(Arrays.asList("w1-5", "n1-7")) + "</b>", |
| 100 | 103 | "<b>n</b>", "<b>w</b>", "<b>r</b>" |
| 101 | 104 | )); |
| 102 | 105 | help.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED)); |
diff --git a/src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java b/src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java
index 1d11acb..9e7df4b 100644
|
a
|
b
|
public class OsmIdTextField extends AbstractIdTextField<OsmIdTextField.OsmIdVali
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | } catch (IllegalArgumentException ex2) { |
| 125 | | return false; |
| | 125 | try { |
| | 126 | ids.addAll(SimplePrimitiveId.fuzzyParse(s)); |
| | 127 | } catch (IllegalArgumentException ex3) { |
| | 128 | return false; |
| | 129 | } |
| 126 | 130 | } |
| 127 | 131 | } |
| 128 | 132 | } |
diff --git a/test/unit/org/openstreetmap/josm/data/osm/SimplePrimitiveIdTest.groovy b/test/unit/org/openstreetmap/josm/data/osm/SimplePrimitiveIdTest.groovy
index 75205e4..81f2df0 100644
|
a
|
b
|
class SimplePrimitiveIdTest extends GroovyTestCase {
|
| 33 | 33 | assert SimplePrimitiveId.fromString("way 123") == new SimplePrimitiveId(123, OsmPrimitiveType.WAY) |
| 34 | 34 | assert SimplePrimitiveId.fromString("relation 123") == new SimplePrimitiveId(123, OsmPrimitiveType.RELATION) |
| 35 | 35 | } |
| | 36 | |
| | 37 | void testMultipleIDs() { |
| | 38 | assert SimplePrimitiveId.fuzzyParse("node/123-125").toString() == "[node 123, node 124, node 125]" |
| | 39 | assert SimplePrimitiveId.fuzzyParse("n/123-125").toString() == "[node 123, node 124, node 125]" |
| | 40 | assert SimplePrimitiveId.fuzzyParse("node123-125").toString() == "[node 123, node 124, node 125]" |
| | 41 | assert SimplePrimitiveId.fuzzyParse("way/123-125").toString() == "[way 123, way 124, way 125]" |
| | 42 | assert SimplePrimitiveId.fuzzyParse("w/123-125").toString() == "[way 123, way 124, way 125]" |
| | 43 | assert SimplePrimitiveId.fuzzyParse("way123-125").toString() == "[way 123, way 124, way 125]" |
| | 44 | assert SimplePrimitiveId.fuzzyParse("relation/123-125").toString() == "[relation 123, relation 124, relation 125]" |
| | 45 | assert SimplePrimitiveId.fuzzyParse("r/123-125").toString() == "[relation 123, relation 124, relation 125]" |
| | 46 | assert SimplePrimitiveId.fuzzyParse("relation123-125").toString() == "[relation 123, relation 124, relation 125]" |
| | 47 | } |
| 36 | 48 | } |