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.Component;
007
008import javax.swing.JLabel;
009import javax.swing.JList;
010import javax.swing.ListCellRenderer;
011import javax.swing.UIManager;
012
013import org.openstreetmap.josm.gui.widgets.JosmComboBox;
014
015/**
016 * Combo box that lets the user choose one of the avaliable {@link AuthorizationProcedure}s.
017 */
018public class AuthorizationProcedureComboBox extends JosmComboBox<AuthorizationProcedure> {
019
020    /**
021     * Constructs a new {@code AuthorizationProcedureComboBox}.
022     */
023    public AuthorizationProcedureComboBox() {
024        super(AuthorizationProcedure.values());
025        setRenderer(new AuthorisationProcedureCellRenderer());
026        setSelectedItem(AuthorizationProcedure.FULLY_AUTOMATIC);
027    }
028
029    private static class AuthorisationProcedureCellRenderer extends JLabel implements ListCellRenderer<AuthorizationProcedure> {
030        AuthorisationProcedureCellRenderer() {
031            setOpaque(true);
032        }
033
034        protected void renderColors(boolean isSelected) {
035            if (isSelected) {
036                setForeground(UIManager.getColor("List.selectionForeground"));
037                setBackground(UIManager.getColor("List.selectionBackground"));
038            } else {
039                setForeground(UIManager.getColor("List.foreground"));
040                setBackground(UIManager.getColor("List.background"));
041            }
042        }
043
044        protected void renderText(AuthorizationProcedure value) {
045            switch(value) {
046            case FULLY_AUTOMATIC:
047                setText(tr("Fully automatic"));
048                break;
049            case SEMI_AUTOMATIC:
050                setText(tr("Semi-automatic"));
051                break;
052            case MANUALLY:
053                setText(tr("Manual"));
054                break;
055            }
056        }
057
058        protected void renderToolTipText(AuthorizationProcedure value) {
059            switch(value) {
060            case FULLY_AUTOMATIC:
061                setToolTipText(tr(
062                        "<html>Run a fully automatic procedure to get an access token from the OSM website.<br>"
063                        + "JOSM accesses the OSM website on behalf of the JOSM user and fully<br>"
064                        + "automatically authorizes the user and retrieves an Access Token.</html>"
065                ));
066                break;
067            case SEMI_AUTOMATIC:
068                setToolTipText(tr(
069                        "<html>Run a semi-automatic procedure to get an access token from the OSM website.<br>"
070                        + "JOSM submits the standards OAuth requests to get a Request Token and an<br>"
071                        + "Access Token. It dispatches the user to the OSM website in an external browser<br>"
072                        + "to authenticate itself and to accept the request token submitted by JOSM.</html>"
073                ));
074                break;
075            case MANUALLY:
076                setToolTipText(tr(
077                        "<html>Enter an Access Token manually if it was generated and retrieved outside<br>"
078                        + "of JOSM.</html>"
079                ));
080                break;
081            }
082        }
083
084        @Override
085        public Component getListCellRendererComponent(JList<? extends AuthorizationProcedure> list, AuthorizationProcedure procedure,
086                int idx, boolean isSelected, boolean hasFocus) {
087            renderColors(isSelected);
088            renderText(procedure);
089            renderToolTipText(procedure);
090            return this;
091        }
092    }
093}