source: josm/trunk/src/org/openstreetmap/josm/gui/io/UploadTextComponentValidator.java@ 16697

Last change on this file since 16697 was 16697, checked in by simon04, 4 years ago

see #19381 - Upload dialog: avoid unnecessary UI updates when validating

File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.Collections;
8import java.util.Objects;
9
10import javax.swing.JLabel;
11import javax.swing.text.JTextComponent;
12
13import org.openstreetmap.josm.data.osm.BBox;
14import org.openstreetmap.josm.data.osm.IPrimitive;
15import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
16import org.openstreetmap.josm.spi.preferences.Config;
17import org.openstreetmap.josm.tools.Utils;
18
19/**
20 * Input validators for {@link UploadDialog}
21 */
22abstract class UploadTextComponentValidator extends AbstractTextComponentValidator {
23 private final JLabel feedback;
24
25 UploadTextComponentValidator(JTextComponent tc, JLabel feedback) {
26 super(tc);
27 this.feedback = feedback;
28 this.feedback.setOpaque(true);
29 validate();
30 }
31
32 @Override
33 protected void feedbackValid(String msg) {
34 msg = msg != null ? "<html>\u2714 " + msg : null;
35 super.feedbackValid(msg);
36 if (!Objects.equals(msg, feedback.getText())) {
37 feedback.setText(msg);
38 feedback.setForeground(VALID_COLOR);
39 feedback.setBackground(null);
40 feedback.setBorder(null);
41 }
42 }
43
44 @Override
45 protected void feedbackWarning(String msg) {
46 msg = msg != null ? "<html>" + msg : null;
47 super.feedbackWarning(msg);
48 if (!Objects.equals(msg, feedback.getText())) {
49 feedback.setText(msg);
50 feedback.setForeground(null);
51 feedback.setBackground(WARNING_BACKGROUND);
52 feedback.setBorder(WARNING_BORDER);
53 }
54 }
55
56 @Override
57 public boolean isValid() {
58 throw new UnsupportedOperationException();
59 }
60
61 /**
62 * Validator for the changeset {@code comment} tag
63 */
64 static class UploadCommentValidator extends UploadTextComponentValidator {
65
66 UploadCommentValidator(JTextComponent tc, JLabel feedback) {
67 super(tc, feedback);
68 }
69
70 @Override
71 public void validate() {
72 if (!Config.getPref().getBoolean("message.upload_comment_is_empty_or_very_short", true)) {
73 feedbackDisabled();
74 return;
75 }
76 String uploadComment = getComponent().getText();
77 if (UploadDialog.UploadAction.isUploadCommentTooShort(uploadComment)) {
78 feedbackWarning(tr("Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />" +
79 "This is technically allowed, but please consider that many users who are<br />" +
80 "watching changes in their area depend on meaningful changeset comments<br />" +
81 "to understand what is going on!<br /><br />" +
82 "If you spend a minute now to explain your change, you will make life<br />" +
83 "easier for many other mappers.").replace("<br />", " "));
84 } else {
85 String rejection = UploadDialog.UploadAction.validateUploadTag(uploadComment, "upload.comment",
86 Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
87 if (rejection != null) {
88 feedbackWarning(tr("Your upload comment is <i>rejected</i>.") + "<br />" + rejection);
89 } else {
90 feedbackValid(tr("Thank you for providing a changeset comment! " +
91 "This gives other mappers a better understanding of your intent."));
92 }
93 }
94 }
95 }
96
97 /**
98 * Validator for the changeset {@code source} tag
99 */
100 static class UploadSourceValidator extends UploadTextComponentValidator {
101
102 UploadSourceValidator(JTextComponent tc, JLabel feedback) {
103 super(tc, feedback);
104 }
105
106 @Override
107 public void validate() {
108 if (!Config.getPref().getBoolean("message.upload_source_is_empty", true)) {
109 feedbackDisabled();
110 return;
111 }
112 String uploadSource = getComponent().getText();
113 if (Utils.isStripEmpty(uploadSource)) {
114 feedbackWarning(tr("You did not specify a source for your changes.<br />" +
115 "It is technically allowed, but this information helps<br />" +
116 "other users to understand the origins of the data.<br /><br />" +
117 "If you spend a minute now to explain your change, you will make life<br />" +
118 "easier for many other mappers.").replace("<br />", " "));
119 } else {
120 final String rejection = UploadDialog.UploadAction.validateUploadTag(
121 uploadSource, "upload.source", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
122 if (rejection != null) {
123 feedbackWarning(tr("Your changeset source is <i>rejected</i>.") + "<br />" + rejection);
124 } else {
125 feedbackValid(tr("Thank you for providing the data source!"));
126 }
127 }
128 }
129 }
130
131 /**
132 * Validator for the changeset area
133 */
134 static class UploadAreaValidator extends UploadTextComponentValidator {
135 private double area = Double.NaN;
136
137 UploadAreaValidator(JTextComponent tc, JLabel feedback) {
138 super(tc, feedback);
139 }
140
141 void computeArea(Collection<? extends IPrimitive> primitives) {
142 this.area = primitives.stream()
143 .map(IPrimitive::getBBox)
144 .reduce((b1, b2) -> {
145 b1.add(b2);
146 return b1;
147 }).map(BBox::area)
148 .orElse(Double.NaN);
149 validate();
150 }
151
152 @Override
153 public void validate() {
154 if (Double.isFinite(area) && area <= Config.getPref().getDouble("upload.max-area", 3.)) {
155 feedbackValid(null);
156 } else {
157 feedbackWarning(tr("The bounding box of this changeset is very large – please consider splitting your changes!"));
158 }
159 }
160 }
161}
Note: See TracBrowser for help on using the repository browser.