source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/OsmIdTextField.java@ 13969

Last change on this file since 13969 was 13969, checked in by Don-vip, 6 years ago

fix #16417 - Downloading primitive with id "0" causes IllegalArgumentException

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.List;
8import java.util.StringTokenizer;
9
10import javax.swing.text.JTextComponent;
11
12import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
13import org.openstreetmap.josm.data.osm.PrimitiveId;
14import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
15import org.openstreetmap.josm.tools.Logging;
16
17/**
18 * A text field designed to enter one or several OSM primitive IDs.
19 * @author Matthias Julius
20 */
21public class OsmIdTextField extends AbstractIdTextField<OsmIdTextField.OsmIdValidator> {
22
23 /**
24 * Constructs a new {@link OsmIdTextField}
25 */
26 public OsmIdTextField() {
27 super(OsmIdValidator.class);
28 }
29
30 /**
31 * Sets the type of primitive object
32 * @param type The type of primitive object (
33 * {@link OsmPrimitiveType#NODE NODE},
34 * {@link OsmPrimitiveType#WAY WAY},
35 * {@link OsmPrimitiveType#RELATION RELATION})
36 */
37 public void setType(OsmPrimitiveType type) {
38 validator.type = type;
39 }
40
41 /**
42 * Get entered ID list - supports "1,2,3" "1 2 ,3" or even "1 2 3 v2 6 v8"
43 * @return list of id's
44 */
45 public final List<PrimitiveId> getIds() {
46 return new ArrayList<>(validator.ids);
47 }
48
49 /**
50 * Reads the OSM primitive id(s)
51 * @return true if valid OSM objects IDs have been read, false otherwise
52 * @see OsmIdValidator#readOsmIds
53 */
54 @Override
55 public boolean readIds() {
56 return validator.readOsmIds();
57 }
58
59 /**
60 * Validator for an OSM primitive ID entered in a {@link JTextComponent}.
61 */
62 public static class OsmIdValidator extends AbstractTextComponentValidator {
63
64 private final List<PrimitiveId> ids = new ArrayList<>();
65 private OsmPrimitiveType type;
66
67 /**
68 * Constructs a new {@link OsmIdValidator}
69 * @param tc The text component to validate
70 */
71 public OsmIdValidator(JTextComponent tc) {
72 super(tc, false);
73 }
74
75 @Override
76 public boolean isValid() {
77 return readOsmIds();
78 }
79
80 @Override
81 public void validate() {
82 if (!isValid()) {
83 feedbackInvalid(tr("The current value is not a valid OSM ID. Please enter an integer value > 0"));
84 } else {
85 feedbackValid(tr("Please enter an integer value > 0"));
86 }
87 }
88
89 /**
90 * Reads the OSM primitive id(s)
91 * @return true if valid OSM objects IDs have been read, false otherwise
92 */
93 public boolean readOsmIds() {
94 String value = getComponent().getText();
95 char c;
96 if (value == null || value.trim().isEmpty()) {
97 return false;
98 }
99 ids.clear();
100 StringTokenizer st = new StringTokenizer(value, ",.+/ \t\n");
101 String s;
102 while (st.hasMoreTokens()) {
103 s = st.nextToken();
104 // convert tokens to int skipping v-words (version v2 etc)
105 c = s.charAt(0);
106 if (c != 'v') {
107 try {
108 ids.addAll(SimplePrimitiveId.multipleFromString(s));
109 } catch (IllegalArgumentException ex) {
110 try {
111 Logging.trace(ex);
112 long id = Long.parseLong(s);
113 if (id <= 0) {
114 return false;
115 } else if (type == OsmPrimitiveType.NODE) {
116 ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
117 } else if (type == OsmPrimitiveType.WAY || type == OsmPrimitiveType.CLOSEDWAY) {
118 ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.WAY));
119 } else if (type == OsmPrimitiveType.RELATION || type == OsmPrimitiveType.MULTIPOLYGON) {
120 ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.RELATION));
121 } else {
122 return false;
123 }
124 } catch (IllegalArgumentException ex2) {
125 Logging.trace(ex2);
126 return false;
127 }
128 }
129 }
130 }
131 return !ids.isEmpty();
132 }
133 }
134}
Note: See TracBrowser for help on using the repository browser.