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