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

Last change on this file since 2877 was 2877, checked in by mjulius, 14 years ago

authorise --> authorize in localized messages
fixed typo

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