001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collection;
007import java.util.Collections;
008import java.util.Objects;
009
010import javax.swing.JLabel;
011import javax.swing.text.JTextComponent;
012
013import org.openstreetmap.josm.data.osm.BBox;
014import org.openstreetmap.josm.data.osm.IPrimitive;
015import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
016import org.openstreetmap.josm.spi.preferences.Config;
017import org.openstreetmap.josm.tools.Utils;
018
019/**
020 * Input validators for {@link UploadDialog}
021 */
022abstract class UploadTextComponentValidator extends AbstractTextComponentValidator {
023    private final JLabel feedback;
024    protected boolean uploadRejected;
025
026    UploadTextComponentValidator(JTextComponent tc, JLabel feedback) {
027        super(tc);
028        this.feedback = feedback;
029        this.feedback.setOpaque(true);
030        validate();
031    }
032
033    @Override
034    protected void feedbackValid(String msg) {
035        msg = msg != null ? "<html>\u2714 " + msg : null;
036        super.feedbackValid(msg);
037        if (!Objects.equals(msg, feedback.getText())) {
038            feedback.setText(msg);
039            feedback.setForeground(VALID_COLOR);
040            feedback.setBackground(null);
041            feedback.setBorder(null);
042        }
043    }
044
045    @Override
046    protected void feedbackWarning(String msg) {
047        msg = msg != null ? "<html>" + msg : null;
048        super.feedbackWarning(msg);
049        if (!Objects.equals(msg, feedback.getText())) {
050            feedback.setText(msg);
051            feedback.setForeground(WARNING_FOREGROUND);
052            feedback.setBackground(WARNING_BACKGROUND);
053            feedback.setBorder(WARNING_BORDER);
054        }
055    }
056
057    @Override
058    public boolean isValid() {
059        throw new UnsupportedOperationException();
060    }
061
062    /**
063     * Determines if the input validator considers the violation bad enough to reject upload.
064     * @return {@code true} if the input validator considers the violation bad enough to reject upload
065     * @since 17238
066     */
067    public final boolean isUploadRejected() {
068        return uploadRejected;
069    }
070
071    /**
072     * Validator for the changeset {@code comment} tag
073     */
074    static class UploadCommentValidator extends UploadTextComponentValidator {
075
076        UploadCommentValidator(JTextComponent tc, JLabel feedback) {
077            super(tc, feedback);
078        }
079
080        @Override
081        public void validate() {
082            if (!Config.getPref().getBoolean("message.upload_comment_is_empty_or_very_short", true)) {
083                feedbackDisabled();
084                return;
085            }
086            final String uploadComment = getComponent().getText();
087            final String rejection = UploadDialog.UploadAction.validateUploadTag(uploadComment, "upload.comment",
088                    Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
089
090            // Reject the upload if tags are required and are not in the input. If the tags exist or are not
091            // required, then check the length of the input and warn if it's too short (a short msg is not a rejection)
092            uploadRejected = rejection != null;
093
094            if (uploadRejected) {
095                feedbackWarning(tr("Your upload comment is <i>rejected</i>.") + "<br />" + rejection);
096            } else if (UploadDialog.UploadAction.isUploadCommentTooShort(uploadComment)) {
097                feedbackWarning(tr("Your upload comment is <i>empty</i>, or <i>very short</i>.<br /><br />" +
098                        "This is technically allowed, but please consider that many users who are<br />" +
099                        "watching changes in their area depend on meaningful changeset comments<br />" +
100                        "to understand what is going on!<br /><br />" +
101                        "If you spend a minute now to explain your change, you will make life<br />" +
102                        "easier for many other mappers.").replace("<br />", " "));
103            } else {
104                feedbackValid(tr("Thank you for providing a changeset comment! " +
105                        "This gives other mappers a better understanding of your intent."));
106            }
107        }
108    }
109
110    /**
111     * Validator for the changeset {@code source} tag
112     */
113    static class UploadSourceValidator extends UploadTextComponentValidator {
114
115        UploadSourceValidator(JTextComponent tc, JLabel feedback) {
116            super(tc, feedback);
117        }
118
119        @Override
120        public void validate() {
121            if (!Config.getPref().getBoolean("message.upload_source_is_empty", true)) {
122                feedbackDisabled();
123                return;
124            }
125            final String uploadSource = getComponent().getText();
126            final String rejection = UploadDialog.UploadAction.validateUploadTag(
127                    uploadSource, "upload.source", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
128
129            // Reject the upload only if tags are required and are not in the input. If the tags exist or are not
130            // required, then check the length of the input and warn if it's too short (a short msg is not a rejection)
131            uploadRejected = rejection != null;
132
133            if (uploadRejected) {
134                feedbackWarning(tr("Your changeset source is <i>rejected</i>.") + "<br />" + rejection);
135            } else if (Utils.isStripEmpty(uploadSource)) {
136                feedbackWarning(tr("You did not specify a source for your changes.<br />" +
137                        "It is technically allowed, but this information helps<br />" +
138                        "other users to understand the origins of the data.<br /><br />" +
139                        "If you spend a minute now to explain your change, you will make life<br />" +
140                        "easier for many other mappers.").replace("<br />", " "));
141            } else {
142                feedbackValid(tr("Thank you for providing the data source!"));
143            }
144        }
145    }
146
147    /**
148     * Validator for the changeset area
149     */
150    static class UploadAreaValidator extends UploadTextComponentValidator {
151        private double area = Double.NaN;
152
153        UploadAreaValidator(JTextComponent tc, JLabel feedback) {
154            super(tc, feedback);
155        }
156
157        void computeArea(Collection<? extends IPrimitive> primitives) {
158            this.area = primitives.stream()
159                    .map(IPrimitive::getBBox)
160                    .reduce(new BBox(), (b1, b2) -> {
161                        b1.add(b2);
162                        return b1;
163                    }).area();
164            validate();
165        }
166
167        @Override
168        public void validate() {
169            if (Double.isFinite(area) && area <= Config.getPref().getDouble("upload.max-area", 3.)) {
170                feedbackValid(null);
171            } else {
172                feedbackWarning(tr("The bounding box of this changeset is very large – please consider splitting your changes!"));
173            }
174        }
175    }
176}