1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.oauth;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.FlowLayout;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Insets;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.beans.PropertyChangeEvent;
|
---|
13 | import java.beans.PropertyChangeListener;
|
---|
14 | import java.util.concurrent.Executor;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.BorderFactory;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JCheckBox;
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JTabbedPane;
|
---|
23 | import javax.swing.event.DocumentEvent;
|
---|
24 | import javax.swing.event.DocumentListener;
|
---|
25 | import javax.swing.text.JTextComponent;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
|
---|
28 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.DefaultTextComponentValidator;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
32 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This is an UI which supports a JOSM user to get an OAuth Access Token in a fully manual process.
|
---|
37 | *
|
---|
38 | * @since 2746
|
---|
39 | */
|
---|
40 | public class ManualAuthorizationUI extends AbstractAuthorizationUI {
|
---|
41 |
|
---|
42 | private final JosmTextField tfAccessTokenKey = new JosmTextField();
|
---|
43 | private transient AccessTokenKeyValidator valAccessTokenKey;
|
---|
44 | private final JosmTextField tfAccessTokenSecret = new JosmTextField();
|
---|
45 | private transient AccessTokenSecretValidator valAccessTokenSecret;
|
---|
46 | private final JCheckBox cbSaveToPreferences = new JCheckBox(tr("Save Access Token in preferences"));
|
---|
47 | private final HtmlPanel pnlMessage = new HtmlPanel();
|
---|
48 | private final transient Executor executor;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Constructs a new {@code ManualAuthorizationUI} for the given API URL.
|
---|
52 | * @param apiUrl The OSM API URL
|
---|
53 | * @param executor the executor used for running the HTTP requests for the authorization
|
---|
54 | * @since 5422
|
---|
55 | */
|
---|
56 | public ManualAuthorizationUI(String apiUrl, Executor executor) {
|
---|
57 | super(/* dont pass apiURL because setApiUrl is overriden and references a local field */);
|
---|
58 | setApiUrl(apiUrl);
|
---|
59 | this.executor = executor;
|
---|
60 | build();
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected JPanel buildAccessTokenPanel() {
|
---|
64 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
65 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
66 | GridBagConstraints gc = new GridBagConstraints();
|
---|
67 | AccessTokenBuilder accessTokenBuilder = new AccessTokenBuilder();
|
---|
68 |
|
---|
69 | // the access token key input field
|
---|
70 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
71 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
72 | gc.weightx = 0.0;
|
---|
73 | gc.gridwidth = 2;
|
---|
74 | gc.insets = new Insets(0, 0, 5, 0);
|
---|
75 | pnlMessage.setText("<html><body>"
|
---|
76 | + tr("Please enter an OAuth Access Token which is authorized to access the OSM server "
|
---|
77 | + "''{0}''.",
|
---|
78 | getApiUrl()) + "</body></html>");
|
---|
79 | pnl.add(pnlMessage, gc);
|
---|
80 |
|
---|
81 | // the access token key input field
|
---|
82 | gc.gridy = 1;
|
---|
83 | gc.weightx = 0.0;
|
---|
84 | gc.gridwidth = 1;
|
---|
85 | gc.insets = new Insets(0, 0, 0, 3);
|
---|
86 | pnl.add(new JLabel(tr("Access Token Key:")), gc);
|
---|
87 |
|
---|
88 | gc.gridx = 1;
|
---|
89 | gc.weightx = 1.0;
|
---|
90 | pnl.add(tfAccessTokenKey, gc);
|
---|
91 | SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenKey);
|
---|
92 | valAccessTokenKey = new AccessTokenKeyValidator(tfAccessTokenKey);
|
---|
93 | valAccessTokenKey.validate();
|
---|
94 | tfAccessTokenKey.getDocument().addDocumentListener(accessTokenBuilder);
|
---|
95 |
|
---|
96 | // the access token key input field
|
---|
97 | gc.gridy = 2;
|
---|
98 | gc.gridx = 0;
|
---|
99 | gc.weightx = 0.0;
|
---|
100 | pnl.add(new JLabel(tr("Access Token Secret:")), gc);
|
---|
101 |
|
---|
102 | gc.gridx = 1;
|
---|
103 | gc.weightx = 1.0;
|
---|
104 | pnl.add(tfAccessTokenSecret, gc);
|
---|
105 | SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenSecret);
|
---|
106 | valAccessTokenSecret = new AccessTokenSecretValidator(tfAccessTokenSecret);
|
---|
107 | valAccessTokenSecret.validate();
|
---|
108 | tfAccessTokenSecret.getDocument().addDocumentListener(accessTokenBuilder);
|
---|
109 |
|
---|
110 | // the checkbox for saving to preferences
|
---|
111 | gc.gridy = 3;
|
---|
112 | gc.gridx = 0;
|
---|
113 | gc.gridwidth = 2;
|
---|
114 | gc.weightx = 1.0;
|
---|
115 | pnl.add(cbSaveToPreferences, gc);
|
---|
116 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
117 |
|
---|
118 | // filler - grab remaining space
|
---|
119 | gc.gridy = 3;
|
---|
120 | gc.gridx = 0;
|
---|
121 | gc.gridwidth = 2;
|
---|
122 | gc.weightx = 1.0;
|
---|
123 | gc.weighty = 1.0;
|
---|
124 | gc.fill = GridBagConstraints.BOTH;
|
---|
125 | pnl.add(new JPanel(), gc);
|
---|
126 | return pnl;
|
---|
127 | }
|
---|
128 |
|
---|
129 | protected JPanel buildTabbedPreferencesPanel() {
|
---|
130 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
131 |
|
---|
132 | JTabbedPane tp = new JTabbedPane();
|
---|
133 | tp.add(buildAccessTokenPanel());
|
---|
134 | tp.add(getAdvancedPropertiesPanel());
|
---|
135 |
|
---|
136 | tp.setTitleAt(0, tr("Access Token"));
|
---|
137 | tp.setTitleAt(1, tr("Advanced OAuth parameters"));
|
---|
138 |
|
---|
139 | tp.setToolTipTextAt(0, tr("Enter the OAuth Access Token"));
|
---|
140 | tp.setToolTipTextAt(1, tr("Enter advanced OAuth properties"));
|
---|
141 |
|
---|
142 | pnl.add(tp, BorderLayout.CENTER);
|
---|
143 | return pnl;
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected JPanel buildActionsPanel() {
|
---|
147 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
148 | TestAccessTokenAction actTestAccessToken = new TestAccessTokenAction();
|
---|
149 | pnl.add(new JButton(actTestAccessToken));
|
---|
150 | this.addPropertyChangeListener(actTestAccessToken);
|
---|
151 | return pnl;
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override
|
---|
155 | public void setApiUrl(String apiUrl) {
|
---|
156 | super.setApiUrl(apiUrl);
|
---|
157 | if (pnlMessage != null) {
|
---|
158 | pnlMessage.setText(tr("<html><body>"
|
---|
159 | + "Please enter an OAuth Access Token which is authorized to access the OSM server "
|
---|
160 | + "''{0}''."
|
---|
161 | + "</body></html>",
|
---|
162 | getApiUrl()
|
---|
163 | ));
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | protected final void build() {
|
---|
168 | setLayout(new BorderLayout());
|
---|
169 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
170 | add(buildTabbedPreferencesPanel(), BorderLayout.CENTER);
|
---|
171 | add(buildActionsPanel(), BorderLayout.SOUTH);
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public boolean isSaveAccessTokenToPreferences() {
|
---|
176 | return cbSaveToPreferences.isSelected();
|
---|
177 | }
|
---|
178 |
|
---|
179 | private static class AccessTokenKeyValidator extends DefaultTextComponentValidator {
|
---|
180 | AccessTokenKeyValidator(JTextComponent tc) {
|
---|
181 | super(tc, tr("Please enter an Access Token Key"),
|
---|
182 | tr("The Access Token Key must not be empty. Please enter an Access Token Key"));
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | private static class AccessTokenSecretValidator extends DefaultTextComponentValidator {
|
---|
187 | AccessTokenSecretValidator(JTextComponent tc) {
|
---|
188 | super(tc, tr("Please enter an Access Token Secret"),
|
---|
189 | tr("The Access Token Secret must not be empty. Please enter an Access Token Secret"));
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | class AccessTokenBuilder implements DocumentListener {
|
---|
194 |
|
---|
195 | public void build() {
|
---|
196 | if (!valAccessTokenKey.isValid() || !valAccessTokenSecret.isValid()) {
|
---|
197 | setAccessToken(null);
|
---|
198 | } else {
|
---|
199 | setAccessToken(new OAuthToken(tfAccessTokenKey.getText().trim(), tfAccessTokenSecret.getText().trim()));
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | @Override
|
---|
204 | public void changedUpdate(DocumentEvent e) {
|
---|
205 | build();
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void insertUpdate(DocumentEvent e) {
|
---|
210 | build();
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public void removeUpdate(DocumentEvent e) {
|
---|
215 | build();
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Action for testing an Access Token
|
---|
221 | */
|
---|
222 | class TestAccessTokenAction extends AbstractAction implements PropertyChangeListener {
|
---|
223 | TestAccessTokenAction() {
|
---|
224 | putValue(NAME, tr("Test Access Token"));
|
---|
225 | new ImageProvider("oauth", "oauth-small").getResource().attachImageIcon(this);
|
---|
226 | putValue(SHORT_DESCRIPTION, tr("Click to test the Access Token"));
|
---|
227 | updateEnabledState();
|
---|
228 | }
|
---|
229 |
|
---|
230 | @Override
|
---|
231 | public void actionPerformed(ActionEvent evt) {
|
---|
232 | TestAccessTokenTask task = new TestAccessTokenTask(
|
---|
233 | ManualAuthorizationUI.this,
|
---|
234 | getApiUrl(),
|
---|
235 | getAdvancedPropertiesPanel().getAdvancedParameters(),
|
---|
236 | getAccessToken()
|
---|
237 | );
|
---|
238 | executor.execute(task);
|
---|
239 | }
|
---|
240 |
|
---|
241 | protected final void updateEnabledState() {
|
---|
242 | setEnabled(hasAccessToken());
|
---|
243 | }
|
---|
244 |
|
---|
245 | @Override
|
---|
246 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
247 | if (!evt.getPropertyName().equals(AbstractAuthorizationUI.ACCESS_TOKEN_PROP))
|
---|
248 | return;
|
---|
249 | updateEnabledState();
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|