001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.changeset.query;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007
008import javax.swing.BorderFactory;
009import javax.swing.JOptionPane;
010
011import org.openstreetmap.josm.gui.HelpAwareOptionPane;
012import org.openstreetmap.josm.gui.help.HelpUtil;
013import org.openstreetmap.josm.gui.widgets.BoundingBoxSelectionPanel;
014import org.openstreetmap.josm.io.ChangesetQuery;
015
016/**
017 * This is the panel for selecting whether the query should be restricted to a specific bounding box.
018 * @since 11326 (extracted from AdvancedChangesetQueryPanel)
019 */
020public class BBoxRestrictionPanel extends BoundingBoxSelectionPanel implements RestrictionPanel {
021
022    /**
023     * Constructs a new {@code BBoxRestrictionPanel}.
024     */
025    public BBoxRestrictionPanel() {
026        setBorder(BorderFactory.createCompoundBorder(
027                BorderFactory.createEmptyBorder(3, 3, 3, 3),
028                BorderFactory.createCompoundBorder(
029                        BorderFactory.createLineBorder(Color.GRAY),
030                        BorderFactory.createEmptyBorder(5, 5, 5, 5)
031                )
032        ));
033    }
034
035    /**
036     * Determines if the changeset query bbox is valid.
037     * @return {@code true} if the changeset query bbox is defined.
038     */
039    @Override
040    public boolean isValidChangesetQuery() {
041        return getBoundingBox() != null;
042    }
043
044    /**
045     * Sets the query restrictions on <code>query</code> for bbox based restrictions.
046     * @param query query to fill
047     */
048    @Override
049    public void fillInQuery(ChangesetQuery query) {
050        if (!isValidChangesetQuery())
051            throw new IllegalStateException(tr("Cannot restrict the changeset query to a specific bounding box. The input is invalid."));
052        query.inBbox(getBoundingBox());
053    }
054
055    @Override
056    public void displayMessageIfInvalid() {
057        if (isValidChangesetQuery())
058            return;
059        HelpAwareOptionPane.showOptionDialog(
060                this,
061                tr(
062                        "<html>Please enter valid longitude/latitude values to restrict<br>" +
063                        "the changeset query to a specific bounding box.</html>"
064                ),
065                tr("Invalid bounding box"),
066                JOptionPane.ERROR_MESSAGE,
067                HelpUtil.ht("/Dialog/ChangesetQueryDialog#InvalidBoundingBox")
068        );
069    }
070}