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