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

Last change on this file since 8496 was 8496, checked in by simon04, 9 years ago

fix #11505 - Download objects: load a range of OSM objects (modified patch by windu.2b)

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