1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.changeset.query;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import javax.swing.text.JTextComponent;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Validator for user ids entered in a {@link JTextComponent}.
|
---|
12 | * @since 11326 (extracted from AdvancedChangesetQueryPanel)
|
---|
13 | */
|
---|
14 | public class UidInputFieldValidator extends AbstractTextComponentValidator {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Constructs a new {@code TimeValidator} for the given text component.
|
---|
18 | * @param tc text component
|
---|
19 | */
|
---|
20 | public UidInputFieldValidator(JTextComponent tc) {
|
---|
21 | super(tc);
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Decorates the given text component.
|
---|
26 | * @param tc text component to decorate
|
---|
27 | * @return new uid validator attached to {@code tc}
|
---|
28 | */
|
---|
29 | public static UidInputFieldValidator decorate(JTextComponent tc) {
|
---|
30 | return new UidInputFieldValidator(tc);
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public boolean isValid() {
|
---|
35 | return getUid() > 0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void validate() {
|
---|
40 | String value = getComponent().getText();
|
---|
41 | if (value == null || value.trim().isEmpty()) {
|
---|
42 | feedbackInvalid("");
|
---|
43 | return;
|
---|
44 | }
|
---|
45 | try {
|
---|
46 | int uid = Integer.parseInt(value);
|
---|
47 | if (uid <= 0) {
|
---|
48 | feedbackInvalid(tr("The current value is not a valid user ID. Please enter an integer value > 0"));
|
---|
49 | return;
|
---|
50 | }
|
---|
51 | } catch (NumberFormatException e) {
|
---|
52 | feedbackInvalid(tr("The current value is not a valid user ID. Please enter an integer value > 0"));
|
---|
53 | return;
|
---|
54 | }
|
---|
55 | feedbackValid(tr("Please enter an integer value > 0"));
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Returns the user identifier.
|
---|
60 | * @return the user identifier
|
---|
61 | */
|
---|
62 | public int getUid() {
|
---|
63 | String value = getComponent().getText();
|
---|
64 | if (value == null || value.trim().isEmpty()) return 0;
|
---|
65 | try {
|
---|
66 | int uid = Integer.parseInt(value.trim());
|
---|
67 | if (uid > 0)
|
---|
68 | return uid;
|
---|
69 | return 0;
|
---|
70 | } catch (NumberFormatException e) {
|
---|
71 | return 0;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|