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

Last change on this file since 9309 was 9220, checked in by Don-vip, 8 years ago

update unitils-core to 3.4.2, increase unit test coverage with EqualsVerifier, make OAuthParameters immutable

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