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