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 javax.swing.text.JTextComponent;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A text field designed to enter a single OSM changeset ID.
|
---|
12 | * @since 5765
|
---|
13 | */
|
---|
14 | public class ChangesetIdTextField extends AbstractIdTextField<ChangesetIdTextField.ChangesetIdValidator> {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Constructs a new {@link ChangesetIdTextField}
|
---|
18 | */
|
---|
19 | public ChangesetIdTextField() {
|
---|
20 | super(ChangesetIdValidator.class, 10);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Gets the entered changeset id.
|
---|
25 | * @return The entered changeset id
|
---|
26 | */
|
---|
27 | public final int getChangesetId() {
|
---|
28 | return validator.id;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Reads the changeset id.
|
---|
33 | * @return true if a valid changeset id has been successfully read, false otherwise
|
---|
34 | * @see ChangesetIdValidator#readChangesetId
|
---|
35 | */
|
---|
36 | @Override
|
---|
37 | public boolean readIds() {
|
---|
38 | return validator.readChangesetId();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Validator for a changeset ID entered in a {@link JTextComponent}.
|
---|
43 | */
|
---|
44 | public static class ChangesetIdValidator extends AbstractTextComponentValidator {
|
---|
45 |
|
---|
46 | private int id;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Constructs a new {@link ChangesetIdValidator}
|
---|
50 | * @param tc The text component to validate
|
---|
51 | */
|
---|
52 | public ChangesetIdValidator(JTextComponent tc) {
|
---|
53 | super(tc);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public boolean isValid() {
|
---|
58 | return readChangesetId();
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void validate() {
|
---|
63 | if (!isValid()) {
|
---|
64 | feedbackInvalid(tr("The current value is not a valid changeset ID. Please enter an integer value > 0"));
|
---|
65 | } else {
|
---|
66 | feedbackValid(tr("Please enter an integer value > 0"));
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Reads the changeset id.
|
---|
72 | * @return true if a valid changeset id has been successfully read, false otherwise
|
---|
73 | */
|
---|
74 | public boolean readChangesetId() {
|
---|
75 | String value = getComponent().getText();
|
---|
76 | if (value != null && !value.trim().isEmpty()) {
|
---|
77 | id = 0;
|
---|
78 | try {
|
---|
79 | int changesetId = Integer.parseInt(value.trim());
|
---|
80 | if (changesetId > 0) {
|
---|
81 | id = changesetId;
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | } catch (NumberFormatException e) {
|
---|
85 | // Ignored
|
---|
86 | Main.trace(e);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|