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