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.BorderLayout; 007import java.awt.FlowLayout; 008import java.awt.GridBagConstraints; 009import java.awt.GridBagLayout; 010import java.awt.Insets; 011import java.awt.event.ActionEvent; 012import java.beans.PropertyChangeEvent; 013import java.beans.PropertyChangeListener; 014import java.util.concurrent.Executor; 015 016import javax.swing.AbstractAction; 017import javax.swing.BorderFactory; 018import javax.swing.JButton; 019import javax.swing.JCheckBox; 020import javax.swing.JLabel; 021import javax.swing.JPanel; 022import javax.swing.JTabbedPane; 023import javax.swing.event.DocumentEvent; 024import javax.swing.event.DocumentListener; 025import javax.swing.text.JTextComponent; 026 027import org.openstreetmap.josm.data.oauth.OAuth20Exception; 028import org.openstreetmap.josm.data.oauth.OAuth20Token; 029import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder; 030import org.openstreetmap.josm.data.oauth.OAuthVersion; 031import org.openstreetmap.josm.gui.widgets.DefaultTextComponentValidator; 032import org.openstreetmap.josm.gui.widgets.HtmlPanel; 033import org.openstreetmap.josm.gui.widgets.JosmTextField; 034import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 035import org.openstreetmap.josm.tools.ImageProvider; 036import org.openstreetmap.josm.tools.JosmRuntimeException; 037 038/** 039 * This is an UI which supports a JOSM user to get an OAuth Access Token in a fully manual process. 040 * 041 * @since 2746 042 */ 043public class ManualAuthorizationUI extends AbstractAuthorizationUI { 044 045 private final JosmTextField tfAccessTokenKey = new JosmTextField(); 046 private transient AccessTokenKeyValidator valAccessTokenKey; 047 private final JCheckBox cbSaveToPreferences = new JCheckBox(tr("Save Access Token in preferences")); 048 private final HtmlPanel pnlMessage = new HtmlPanel(); 049 private final transient Executor executor; 050 051 /** 052 * Constructs a new {@code ManualAuthorizationUI} for the given API URL. 053 * @param apiUrl The OSM API URL 054 * @param executor the executor used for running the HTTP requests for the authorization 055 * @param oAuthVersion The OAuthVersion to use for this UI 056 * @since 18991 057 */ 058 public ManualAuthorizationUI(String apiUrl, Executor executor, OAuthVersion oAuthVersion) { 059 super(null /* don't pass apiURL because setApiUrl is overridden and references a local field */, 060 oAuthVersion); 061 setApiUrl(apiUrl); 062 this.executor = executor; 063 build(); 064 } 065 066 protected JPanel buildAccessTokenPanel() { 067 JPanel pnl = new JPanel(new GridBagLayout()); 068 pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 069 GridBagConstraints gc = new GridBagConstraints(); 070 AccessTokenBuilder accessTokenBuilder = new AccessTokenBuilder(); 071 072 // the access token key input field 073 gc.anchor = GridBagConstraints.NORTHWEST; 074 gc.fill = GridBagConstraints.HORIZONTAL; 075 gc.weightx = 0.0; 076 gc.gridwidth = 2; 077 gc.insets = new Insets(0, 0, 5, 0); 078 pnlMessage.setText("<html><body>" 079 + tr("Please enter an OAuth Access Token which is authorized to access the OSM server " 080 + "''{0}''.", 081 getApiUrl()) + "</body></html>"); 082 pnl.add(pnlMessage, gc); 083 084 // the access token key input field 085 gc.gridy = 1; 086 gc.weightx = 0.0; 087 gc.gridwidth = 1; 088 gc.insets = new Insets(0, 0, 0, 3); 089 pnl.add(new JLabel(tr("Access Token Key:")), gc); 090 091 gc.gridx = 1; 092 gc.weightx = 1.0; 093 pnl.add(tfAccessTokenKey, gc); 094 SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenKey); 095 valAccessTokenKey = new AccessTokenKeyValidator(tfAccessTokenKey); 096 valAccessTokenKey.validate(); 097 tfAccessTokenKey.getDocument().addDocumentListener(accessTokenBuilder); 098 099 // the checkbox for saving to preferences 100 gc.gridy = 3; 101 gc.gridx = 0; 102 gc.gridwidth = 2; 103 gc.weightx = 1.0; 104 pnl.add(cbSaveToPreferences, gc); 105 cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences()); 106 107 // filler - grab remaining space 108 gc.gridy = 3; 109 gc.gridx = 0; 110 gc.gridwidth = 2; 111 gc.weightx = 1.0; 112 gc.weighty = 1.0; 113 gc.fill = GridBagConstraints.BOTH; 114 pnl.add(new JPanel(), gc); 115 return pnl; 116 } 117 118 protected JPanel buildTabbedPreferencesPanel() { 119 JPanel pnl = new JPanel(new BorderLayout()); 120 121 JTabbedPane tp = new JTabbedPane(); 122 tp.add(buildAccessTokenPanel()); 123 tp.add(getAdvancedPropertiesPanel()); 124 125 tp.setTitleAt(0, tr("Access Token")); 126 tp.setTitleAt(1, tr("Advanced OAuth parameters")); 127 128 tp.setToolTipTextAt(0, tr("Enter the OAuth Access Token")); 129 tp.setToolTipTextAt(1, tr("Enter advanced OAuth properties")); 130 131 pnl.add(tp, BorderLayout.CENTER); 132 return pnl; 133 } 134 135 protected JPanel buildActionsPanel() { 136 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT)); 137 TestAccessTokenAction actTestAccessToken = new TestAccessTokenAction(); 138 pnl.add(new JButton(actTestAccessToken)); 139 this.addPropertyChangeListener(actTestAccessToken); 140 return pnl; 141 } 142 143 @Override 144 public void setApiUrl(String apiUrl) { 145 super.setApiUrl(apiUrl); 146 if (pnlMessage != null) { 147 pnlMessage.setText(tr("<html><body>" 148 + "Please enter an OAuth Access Token which is authorized to access the OSM server " 149 + "''{0}''." 150 + "</body></html>", 151 getApiUrl() 152 )); 153 } 154 } 155 156 protected final void build() { 157 setLayout(new BorderLayout()); 158 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 159 add(buildTabbedPreferencesPanel(), BorderLayout.CENTER); 160 add(buildActionsPanel(), BorderLayout.SOUTH); 161 } 162 163 @Override 164 public boolean isSaveAccessTokenToPreferences() { 165 return cbSaveToPreferences.isSelected(); 166 } 167 168 private static class AccessTokenKeyValidator extends DefaultTextComponentValidator { 169 AccessTokenKeyValidator(JTextComponent tc) { 170 super(tc, tr("Please enter an Access Token Key"), 171 tr("The Access Token Key must not be empty. Please enter an Access Token Key")); 172 } 173 } 174 175 class AccessTokenBuilder implements DocumentListener { 176 177 public void build() { 178 if (!valAccessTokenKey.isValid()) { 179 setAccessToken(null); 180 } else { 181 try { 182 setAccessToken(new OAuth20Token(getOAuthParameters(), "{\"token_type\":\"bearer\", \"access_token\":\"" 183 + tfAccessTokenKey.getText().trim() + "\"}")); 184 } catch (OAuth20Exception e) { 185 throw new JosmRuntimeException(e); 186 } 187 } 188 } 189 190 @Override 191 public void changedUpdate(DocumentEvent e) { 192 build(); 193 } 194 195 @Override 196 public void insertUpdate(DocumentEvent e) { 197 build(); 198 } 199 200 @Override 201 public void removeUpdate(DocumentEvent e) { 202 build(); 203 } 204 } 205 206 /** 207 * Action for testing an Access Token 208 */ 209 class TestAccessTokenAction extends AbstractAction implements PropertyChangeListener { 210 TestAccessTokenAction() { 211 putValue(NAME, tr("Test Access Token")); 212 new ImageProvider("oauth", "oauth-small").getResource().attachImageIcon(this); 213 putValue(SHORT_DESCRIPTION, tr("Click to test the Access Token")); 214 updateEnabledState(); 215 } 216 217 @Override 218 public void actionPerformed(ActionEvent evt) { 219 TestAccessTokenTask task = new TestAccessTokenTask( 220 ManualAuthorizationUI.this, 221 getApiUrl(), 222 getAccessToken() 223 ); 224 executor.execute(task); 225 } 226 227 protected final void updateEnabledState() { 228 setEnabled(hasAccessToken()); 229 } 230 231 @Override 232 public void propertyChange(PropertyChangeEvent evt) { 233 if (!evt.getPropertyName().equals(AbstractAuthorizationUI.ACCESS_TOKEN_PROP)) 234 return; 235 updateEnabledState(); 236 } 237 } 238}