source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java@ 6340

Last change on this file since 6340 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 12.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10import java.awt.event.ItemEvent;
11import java.awt.event.ItemListener;
12
13import javax.swing.BorderFactory;
14import javax.swing.JCheckBox;
15import javax.swing.JLabel;
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.data.Preferences;
19import org.openstreetmap.josm.data.oauth.OAuthParameters;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane;
21import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
22import org.openstreetmap.josm.gui.help.HelpUtil;
23import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
24import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
25import org.openstreetmap.josm.tools.CheckParameterUtil;
26import org.openstreetmap.josm.tools.ImageProvider;
27import org.openstreetmap.josm.gui.widgets.JosmTextField;
28
29/**
30 * Panel allowing the user to setup advanced OAuth parameters:
31 * <li>Consumer key</li>
32 * <li>Consumer secret</li>
33 * <li>Request token URL</li>
34 * <li>Access token URL</li>
35 * <li>Authorize URL</li>
36 *
37 * @see OAuthParameters
38 * @since 2746
39 */
40public class AdvancedOAuthPropertiesPanel extends VerticallyScrollablePanel {
41
42 private JCheckBox cbUseDefaults;
43 private JosmTextField tfConsumerKey;
44 private JosmTextField tfConsumerSecret;
45 private JosmTextField tfRequestTokenURL;
46 private JosmTextField tfAccessTokenURL;
47 private JosmTextField tfAuthoriseURL;
48 private UseDefaultItemListener ilUseDefault;
49 private String apiUrl;
50
51 protected void build() {
52 setLayout(new GridBagLayout());
53 setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
54 GridBagConstraints gc = new GridBagConstraints();
55
56 gc.anchor = GridBagConstraints.NORTHWEST;
57 gc.fill = GridBagConstraints.HORIZONTAL;
58 gc.weightx = 1.0;
59 gc.insets = new Insets(0,0, 3, 3);
60 gc.gridwidth = 2;
61 cbUseDefaults = new JCheckBox(tr("Use default settings"));
62 add(cbUseDefaults, gc);
63
64 // -- consumer key
65 gc.gridy = 1;
66 gc.weightx = 0.0;
67 gc.gridwidth = 1;
68 add(new JLabel(tr("Consumer Key:")), gc);
69
70 gc.gridx = 1;
71 gc.weightx = 1.0;
72 add(tfConsumerKey = new JosmTextField(), gc);
73 SelectAllOnFocusGainedDecorator.decorate(tfConsumerKey);
74
75 // -- consumer secret
76 gc.gridy = 2;
77 gc.gridx = 0;
78 gc.weightx = 0.0;
79 add(new JLabel(tr("Consumer Secret:")), gc);
80
81 gc.gridx = 1;
82 gc.weightx = 1.0;
83 add(tfConsumerSecret = new JosmTextField(), gc);
84 SelectAllOnFocusGainedDecorator.decorate(tfConsumerSecret);
85
86 // -- request token URL
87 gc.gridy = 3;
88 gc.gridx = 0;
89 gc.weightx = 0.0;
90 add(new JLabel(tr("Request Token URL:")), gc);
91
92 gc.gridx = 1;
93 gc.weightx = 1.0;
94 add(tfRequestTokenURL = new JosmTextField(), gc);
95 SelectAllOnFocusGainedDecorator.decorate(tfRequestTokenURL);
96
97 // -- access token URL
98 gc.gridy = 4;
99 gc.gridx = 0;
100 gc.weightx = 0.0;
101 add(new JLabel(tr("Access Token URL:")), gc);
102
103 gc.gridx = 1;
104 gc.weightx = 1.0;
105 add(tfAccessTokenURL = new JosmTextField(), gc);
106 SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenURL);
107
108
109 // -- authorise URL
110 gc.gridy = 5;
111 gc.gridx = 0;
112 gc.weightx = 0.0;
113 add(new JLabel(tr("Authorize URL:")), gc);
114
115 gc.gridx = 1;
116 gc.weightx = 1.0;
117 add(tfAuthoriseURL = new JosmTextField(), gc);
118 SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL);
119
120 cbUseDefaults.addItemListener(ilUseDefault = new UseDefaultItemListener());
121 }
122
123 protected boolean hasCustomSettings() {
124 OAuthParameters params = OAuthParameters.createDefault(apiUrl);
125 return
126 ! tfConsumerKey.getText().equals(params.getConsumerKey())
127 || ! tfConsumerSecret.getText().equals(params.getConsumerSecret())
128 || ! tfRequestTokenURL.getText().equals(params.getRequestTokenUrl())
129 || ! tfAccessTokenURL.getText().equals(params.getAccessTokenUrl())
130 || ! tfAuthoriseURL.getText().equals(params.getAuthoriseUrl());
131 }
132
133 protected boolean confirmOverwriteCustomSettings() {
134 ButtonSpec[] buttons = new ButtonSpec[] {
135 new ButtonSpec(
136 tr("Continue"),
137 ImageProvider.get("ok"),
138 tr("Click to reset the OAuth settings to default values"),
139 null /* no dedicated help topic */
140 ),
141 new ButtonSpec(
142 tr("Cancel"),
143 ImageProvider.get("cancel"),
144 tr("Click to abort resetting to the OAuth default values"),
145 null /* no dedicated help topic */
146 )
147 };
148 int ret = HelpAwareOptionPane.showOptionDialog(
149 AdvancedOAuthPropertiesPanel.this,
150 tr(
151 "<html>JOSM is about to reset the OAuth settings to default values.<br>"
152 + "The current custom settings are not saved.</html>"
153 ),
154 tr("Overwrite custom OAuth settings?"),
155 JOptionPane.WARNING_MESSAGE,
156 null, /* no dedicated icon */
157 buttons,
158 buttons[0],
159 HelpUtil.ht("/Dialog/OAuthAuthorisationWizard")
160 );
161
162 return ret == 0; // OK button clicked
163 }
164
165 protected void resetToDefaultSettings() {
166 cbUseDefaults.setSelected(true);
167 OAuthParameters params = OAuthParameters.createDefault(apiUrl);
168 tfConsumerKey.setText(params.getConsumerKey());
169 tfConsumerSecret.setText(params.getConsumerSecret());
170 tfRequestTokenURL.setText(params.getRequestTokenUrl());
171 tfAccessTokenURL.setText(params.getAccessTokenUrl());
172 tfAuthoriseURL.setText(params.getAuthoriseUrl());
173
174 setChildComponentsEnabled(false);
175 }
176
177 protected void setChildComponentsEnabled(boolean enabled){
178 for (Component c: getComponents()) {
179 if (c instanceof JosmTextField || c instanceof JLabel) {
180 c.setEnabled(enabled);
181 }
182 }
183 }
184
185 /**
186 * Replies the OAuth parameters currently edited in this properties panel.
187 *
188 * @return the OAuth parameters
189 */
190 public OAuthParameters getAdvancedParameters() {
191 if (cbUseDefaults.isSelected())
192 return OAuthParameters.createDefault(apiUrl);
193 OAuthParameters parameters = new OAuthParameters();
194 parameters.setConsumerKey(tfConsumerKey.getText());
195 parameters.setConsumerSecret(tfConsumerSecret.getText());
196 parameters.setRequestTokenUrl(tfRequestTokenURL.getText());
197 parameters.setAccessTokenUrl(tfAccessTokenURL.getText());
198 parameters.setAuthoriseUrl(tfAuthoriseURL.getText());
199 return parameters;
200 }
201
202 /**
203 * Sets the advanced parameters to be displayed
204 *
205 * @param parameters the advanced parameters. Must not be null.
206 * @throws IllegalArgumentException thrown if parameters is null.
207 */
208 public void setAdvancedParameters(OAuthParameters parameters) throws IllegalArgumentException{
209 CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
210 if (parameters.equals(OAuthParameters.createDefault(apiUrl))) {
211 cbUseDefaults.setSelected(true);
212 setChildComponentsEnabled(false);
213 } else {
214 cbUseDefaults.setSelected(false);
215 setChildComponentsEnabled(true);
216 tfConsumerKey.setText( parameters.getConsumerKey() == null ? "" : parameters.getConsumerKey());
217 tfConsumerSecret.setText( parameters.getConsumerSecret() == null ? "" : parameters.getConsumerSecret());
218 tfRequestTokenURL.setText(parameters.getRequestTokenUrl() == null ? "" : parameters.getRequestTokenUrl());
219 tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl());
220 tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl());
221 }
222 }
223
224 /**
225 * Constructs a new {@code AdvancedOAuthPropertiesPanel}.
226 */
227 public AdvancedOAuthPropertiesPanel() {
228 build();
229 }
230
231 /**
232 * Initializes the panel from the values in the preferences <code>preferences</code>.
233 *
234 * @param pref the preferences. Must not be null.
235 * @throws IllegalArgumentException thrown if pref is null
236 */
237 public void initFromPreferences(Preferences pref) throws IllegalArgumentException {
238 CheckParameterUtil.ensureParameterNotNull(pref, "pref");
239 setApiUrl(pref.get("osm-server-url"));
240 boolean useDefault = pref.getBoolean("oauth.settings.use-default", true);
241 ilUseDefault.setEnabled(false);
242 if (useDefault) {
243 resetToDefaultSettings();
244 } else {
245 cbUseDefaults.setSelected(false);
246 tfConsumerKey.setText(pref.get("oauth.settings.consumer-key", ""));
247 tfConsumerSecret.setText(pref.get("oauth.settings.consumer-secret", ""));
248 tfRequestTokenURL.setText(pref.get("oauth.settings.request-token-url", ""));
249 tfAccessTokenURL.setText(pref.get("oauth.settings.access-token-url", ""));
250 tfAuthoriseURL.setText(pref.get("oauth.settings.authorise-url", ""));
251 setChildComponentsEnabled(true);
252 }
253 ilUseDefault.setEnabled(true);
254 }
255
256 /**
257 * Remembers the current values in the preferences <code>pref</code>.
258 *
259 * @param pref the preferences. Must not be null.
260 * @throws IllegalArgumentException thrown if pref is null.
261 */
262 public void rememberPreferences(Preferences pref) throws IllegalArgumentException {
263 CheckParameterUtil.ensureParameterNotNull(pref, "pref");
264 pref.put("oauth.settings.use-default", cbUseDefaults.isSelected());
265 if (cbUseDefaults.isSelected()) {
266 pref.put("oauth.settings.consumer-key", null);
267 pref.put("oauth.settings.consumer-secret", null);
268 pref.put("oauth.settings.request-token-url", null);
269 pref.put("oauth.settings.access-token-url", null);
270 pref.put("oauth.settings.authorise-url", null);
271 } else {
272 pref.put("oauth.settings.consumer-key", tfConsumerKey.getText().trim());
273 pref.put("oauth.settings.consumer-secret", tfConsumerSecret.getText().trim());
274 pref.put("oauth.settings.request-token-url", tfRequestTokenURL.getText().trim());
275 pref.put("oauth.settings.access-token-url", tfAccessTokenURL.getText().trim());
276 pref.put("oauth.settings.authorise-url", tfAuthoriseURL.getText().trim());
277 }
278 }
279
280 class UseDefaultItemListener implements ItemListener {
281 private boolean enabled;
282
283 @Override
284 public void itemStateChanged(ItemEvent e) {
285 if (!enabled) return;
286 switch (e.getStateChange()) {
287 case ItemEvent.SELECTED:
288 if (hasCustomSettings()) {
289 if (!confirmOverwriteCustomSettings()) {
290 cbUseDefaults.setSelected(false);
291 return;
292 }
293 }
294 resetToDefaultSettings();
295 break;
296 case ItemEvent.DESELECTED:
297 setChildComponentsEnabled(true);
298 break;
299 }
300 }
301
302 public void setEnabled(boolean enabled) {
303 this.enabled = enabled;
304 }
305 }
306
307 /**
308 * Sets the URL of the OSM API for which this panel is currently displaying OAuth properties.
309 *
310 * @param apiUrl the api URL
311 * @since 5422
312 */
313 public void setApiUrl(String apiUrl) {
314 this.apiUrl = apiUrl;
315 if (cbUseDefaults.isSelected()) {
316 resetToDefaultSettings();
317 }
318 }
319}
Note: See TracBrowser for help on using the repository browser.