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 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    public FullyAutomaticPropertiesPanel() {
059        setLayout(new GridBagLayout());
060        GridBagConstraints gc = new GridBagConstraints();
061        setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
062
063        gc.anchor = GridBagConstraints.NORTHWEST;
064        gc.fill = GridBagConstraints.HORIZONTAL;
065        gc.weightx = 1.0;
066        add(buildUserNamePasswordPanel(), gc);
067
068        gc.gridy = 1;
069        gc.weighty = 1.0;
070        gc.fill = GridBagConstraints.BOTH;
071        add(new JPanel(), gc);
072    }
073
074    static private class UserNameValidator extends AbstractTextComponentValidator {
075
076        public UserNameValidator(JTextComponent tc) {
077            super(tc);
078        }
079
080        @Override
081        public boolean isValid() {
082            return getComponent().getText().trim().length() > 0;
083        }
084
085        @Override
086        public void validate() {
087            if (isValid()) {
088                feedbackValid(tr("Please enter your OSM user name"));
089            } else {
090                feedbackInvalid(tr("The user name cannot be empty. Please enter your OSM user name"));
091            }
092        }
093    }
094}