1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.StringTokenizer;
|
---|
9 |
|
---|
10 | import javax.swing.text.JTextComponent;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
15 | import 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 | */
|
---|
21 | public 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 | continue;
|
---|
108 | } else {
|
---|
109 | try {
|
---|
110 | ids.addAll(SimplePrimitiveId.multipleFromString(s));
|
---|
111 | } catch (IllegalArgumentException ex) {
|
---|
112 | try {
|
---|
113 | Logging.trace(ex);
|
---|
114 | long id = Long.parseLong(s);
|
---|
115 | if (id <= 0) {
|
---|
116 | return false;
|
---|
117 | } else if (type == OsmPrimitiveType.NODE) {
|
---|
118 | ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
|
---|
119 | } else if (type == OsmPrimitiveType.WAY || type == OsmPrimitiveType.CLOSEDWAY) {
|
---|
120 | ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.WAY));
|
---|
121 | } else if (type == OsmPrimitiveType.RELATION || type == OsmPrimitiveType.MULTIPOLYGON) {
|
---|
122 | ids.add(new SimplePrimitiveId(id, OsmPrimitiveType.RELATION));
|
---|
123 | } else {
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | } catch (IllegalArgumentException ex2) {
|
---|
127 | Logging.trace(ex2);
|
---|
128 | return false;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|