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.JCheckBox;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013
014import org.openstreetmap.josm.data.oauth.OAuthToken;
015import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
016import org.openstreetmap.josm.gui.widgets.JosmTextField;
017
018/**
019 * Displays the key and the secret of an OAuth Access Token.
020 * @since 2746
021 */
022public class AccessTokenInfoPanel extends JPanel {
023
024    private final JosmTextField tfAccessTokenKey = new JosmTextField();
025    private final JosmTextField tfAccessTokenSecret = new JosmTextField();
026    private final JCheckBox cbSaveAccessTokenInPreferences = new JCheckBox(tr("Save Access Token in preferences"));
027
028    /**
029     * Constructs a new {@code AccessTokenInfoPanel}.
030     */
031    public AccessTokenInfoPanel() {
032        build();
033    }
034
035    protected final void build() {
036        setLayout(new GridBagLayout());
037        GridBagConstraints gc = new GridBagConstraints();
038
039        // the access token key
040        gc.anchor = GridBagConstraints.NORTHWEST;
041        gc.fill = GridBagConstraints.HORIZONTAL;
042        gc.weightx = 0.0;
043        gc.insets = new Insets(0, 0, 3, 3);
044        add(new JLabel(tr("Access Token Key:")), gc);
045
046        gc.gridx = 1;
047        gc.weightx = 1.0;
048        add(tfAccessTokenKey, gc);
049        tfAccessTokenKey.setEditable(false);
050
051        // the access token secret
052        gc.gridx = 0;
053        gc.gridy = 1;
054        gc.weightx = 0.0;
055        gc.insets = new Insets(0, 0, 3, 3);
056        add(new JLabel(tr("Access Token Secret:")), gc);
057
058        gc.gridx = 1;
059        gc.weightx = 1.0;
060        add(tfAccessTokenSecret, gc);
061        tfAccessTokenSecret.setEditable(false);
062
063        // the checkbox
064        gc.gridx = 0;
065        gc.gridy = 2;
066        gc.gridwidth = 2;
067        add(cbSaveAccessTokenInPreferences, gc);
068        cbSaveAccessTokenInPreferences.setToolTipText(tr(
069                "<html>Select to save the Access Token in the JOSM preferences.<br>"
070                + "Unselect to use the Access Token in this JOSM session only.</html>"
071        ));
072        cbSaveAccessTokenInPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
073
074        // filler - grab the remaining space
075        gc.gridx = 0;
076        gc.gridy = 3;
077        gc.weightx = 1.0;
078        gc.weighty = 1.0;
079        gc.fill = GridBagConstraints.BOTH;
080        gc.gridwidth = 2;
081        add(new JPanel(), gc);
082    }
083
084    /**
085     * Displays the key and secret in <code>token</code>.
086     *
087     * @param token the access  token. If null, the content in the info panel is cleared
088     */
089    public void setAccessToken(OAuthToken token) {
090        if (token == null) {
091            tfAccessTokenKey.setText("");
092            tfAccessTokenSecret.setText("");
093            return;
094        }
095        tfAccessTokenKey.setText(token.getKey());
096        tfAccessTokenSecret.setText(token.getSecret());
097    }
098
099    public void setSaveToPreferences(boolean saveToPreferences) {
100        cbSaveAccessTokenInPreferences.setSelected(saveToPreferences);
101    }
102
103    public boolean isSaveToPreferences() {
104        return cbSaveAccessTokenInPreferences.isSelected();
105    }
106}