001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.oauth;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagConstraints;
007import java.awt.GridBagLayout;
008import java.awt.Insets;
009
010import javax.swing.BorderFactory;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013import javax.swing.text.JTextComponent;
014
015import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
016import org.openstreetmap.josm.gui.widgets.JosmPasswordField;
017import org.openstreetmap.josm.gui.widgets.JosmTextField;
018import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
019
020public class FullyAutomaticPropertiesPanel extends JPanel {
021
022    private JosmTextField tfUserName;
023    private JosmPasswordField tfPassword;
024
025    protected final JPanel buildUserNamePasswordPanel() {
026        JPanel pnl = new JPanel(new GridBagLayout());
027        GridBagConstraints gc = new GridBagConstraints();
028
029        gc.anchor = GridBagConstraints.NORTHWEST;
030        gc.fill = GridBagConstraints.HORIZONTAL;
031        gc.weightx = 0.0;
032        gc.insets = new Insets(0, 0, 3, 3);
033        pnl.add(new JLabel(tr("Username: ")), gc);
034
035        gc.gridx = 1;
036        gc.weightx = 1.0;
037        pnl.add(tfUserName = new JosmTextField(), gc);
038        SelectAllOnFocusGainedDecorator.decorate(tfUserName);
039        UserNameValidator valUserName = new UserNameValidator(tfUserName);
040        valUserName.validate();
041
042        gc.anchor = GridBagConstraints.NORTHWEST;
043        gc.fill = GridBagConstraints.HORIZONTAL;
044        gc.gridy = 1;
045        gc.gridx = 0;
046        gc.weightx = 0.0;
047        pnl.add(new JLabel(tr("Password: ")), gc);
048
049        gc.gridx = 1;
050        gc.weightx = 1.0;
051        pnl.add(tfPassword = new JosmPasswordField(), gc);
052        SelectAllOnFocusGainedDecorator.decorate(tfPassword);
053
054        return pnl;
055    }
056
057    /**
058     * Constructs a new {@code FullyAutomaticPropertiesPanel}.
059     */
060    public FullyAutomaticPropertiesPanel() {
061        setLayout(new GridBagLayout());
062        GridBagConstraints gc = new GridBagConstraints();
063        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
064
065        gc.anchor = GridBagConstraints.NORTHWEST;
066        gc.fill = GridBagConstraints.HORIZONTAL;
067        gc.weightx = 1.0;
068        add(buildUserNamePasswordPanel(), gc);
069
070        gc.gridy = 1;
071        gc.weighty = 1.0;
072        gc.fill = GridBagConstraints.BOTH;
073        add(new JPanel(), gc);
074    }
075
076    private static class UserNameValidator extends AbstractTextComponentValidator {
077
078        UserNameValidator(JTextComponent tc) {
079            super(tc);
080        }
081
082        @Override
083        public boolean isValid() {
084            return !getComponent().getText().trim().isEmpty();
085        }
086
087        @Override
088        public void validate() {
089            if (isValid()) {
090                feedbackValid(tr("Please enter your OSM user name"));
091            } else {
092                feedbackInvalid(tr("The user name cannot be empty. Please enter your OSM user name"));
093            }
094        }
095    }
096}