001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.oauth;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Component;
007import java.awt.GridBagConstraints;
008import java.awt.GridBagLayout;
009import java.awt.Insets;
010import java.awt.event.ItemEvent;
011import java.awt.event.ItemListener;
012
013import javax.swing.BorderFactory;
014import javax.swing.JCheckBox;
015import javax.swing.JLabel;
016import javax.swing.JOptionPane;
017
018import org.openstreetmap.josm.data.Preferences;
019import org.openstreetmap.josm.data.oauth.OAuthParameters;
020import org.openstreetmap.josm.gui.HelpAwareOptionPane;
021import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
022import org.openstreetmap.josm.gui.help.HelpUtil;
023import org.openstreetmap.josm.gui.widgets.JosmTextField;
024import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
025import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
026import org.openstreetmap.josm.tools.CheckParameterUtil;
027import org.openstreetmap.josm.tools.ImageProvider;
028
029/**
030 * Panel allowing the user to setup advanced OAuth parameters:
031 * <ul>
032 * <li>Consumer key</li>
033 * <li>Consumer secret</li>
034 * <li>Request token URL</li>
035 * <li>Access token URL</li>
036 * <li>Authorize URL</li>
037 * </ul>
038 *
039 * @see OAuthParameters
040 * @since 2746
041 */
042public class AdvancedOAuthPropertiesPanel extends VerticallyScrollablePanel {
043
044    private JCheckBox cbUseDefaults;
045    private JosmTextField tfConsumerKey;
046    private JosmTextField tfConsumerSecret;
047    private JosmTextField tfRequestTokenURL;
048    private JosmTextField tfAccessTokenURL;
049    private JosmTextField tfAuthoriseURL;
050    private transient UseDefaultItemListener ilUseDefault;
051    private String apiUrl;
052
053    protected final void build() {
054        setLayout(new GridBagLayout());
055        setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
056        GridBagConstraints gc = new GridBagConstraints();
057
058        gc.anchor = GridBagConstraints.NORTHWEST;
059        gc.fill = GridBagConstraints.HORIZONTAL;
060        gc.weightx = 1.0;
061        gc.insets = new Insets(0, 0, 3, 3);
062        gc.gridwidth = 2;
063        cbUseDefaults = new JCheckBox(tr("Use default settings"));
064        add(cbUseDefaults, gc);
065
066        // -- consumer key
067        gc.gridy = 1;
068        gc.weightx = 0.0;
069        gc.gridwidth = 1;
070        add(new JLabel(tr("Consumer Key:")), gc);
071
072        gc.gridx = 1;
073        gc.weightx = 1.0;
074        add(tfConsumerKey = new JosmTextField(), gc);
075        SelectAllOnFocusGainedDecorator.decorate(tfConsumerKey);
076
077        // -- consumer secret
078        gc.gridy = 2;
079        gc.gridx = 0;
080        gc.weightx = 0.0;
081        add(new JLabel(tr("Consumer Secret:")), gc);
082
083        gc.gridx = 1;
084        gc.weightx = 1.0;
085        add(tfConsumerSecret = new JosmTextField(), gc);
086        SelectAllOnFocusGainedDecorator.decorate(tfConsumerSecret);
087
088        // -- request token URL
089        gc.gridy = 3;
090        gc.gridx = 0;
091        gc.weightx = 0.0;
092        add(new JLabel(tr("Request Token URL:")), gc);
093
094        gc.gridx = 1;
095        gc.weightx = 1.0;
096        add(tfRequestTokenURL = new JosmTextField(), gc);
097        SelectAllOnFocusGainedDecorator.decorate(tfRequestTokenURL);
098
099        // -- 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;
107        add(tfAccessTokenURL = new JosmTextField(), gc);
108        SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenURL);
109
110
111        // -- authorise URL
112        gc.gridy = 5;
113        gc.gridx = 0;
114        gc.weightx = 0.0;
115        add(new JLabel(tr("Authorize URL:")), gc);
116
117        gc.gridx = 1;
118        gc.weightx = 1.0;
119        add(tfAuthoriseURL = new JosmTextField(), gc);
120        SelectAllOnFocusGainedDecorator.decorate(tfAuthoriseURL);
121
122        cbUseDefaults.addItemListener(ilUseDefault = new UseDefaultItemListener());
123    }
124
125    protected boolean hasCustomSettings() {
126        OAuthParameters params = OAuthParameters.createDefault(apiUrl);
127        return
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());
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(
151                this,
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);
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());
175
176        setChildComponentsEnabled(false);
177    }
178
179    protected void setChildComponentsEnabled(boolean enabled) {
180        for (Component c: getComponents()) {
181            if (c instanceof JosmTextField || c instanceof JLabel) {
182                c.setEnabled(enabled);
183            }
184        }
185    }
186
187    /**
188     * Replies the OAuth parameters currently edited in this properties panel.
189     *
190     * @return the OAuth parameters
191     */
192    public OAuthParameters getAdvancedParameters() {
193        if (cbUseDefaults.isSelected())
194            return OAuthParameters.createDefault(apiUrl);
195        return new OAuthParameters(
196            tfConsumerKey.getText(),
197            tfConsumerSecret.getText(),
198            tfRequestTokenURL.getText(),
199            tfAccessTokenURL.getText(),
200            tfAuthoriseURL.getText());
201    }
202
203    /**
204     * Sets the advanced parameters to be displayed
205     *
206     * @param parameters the advanced parameters. Must not be null.
207     * @throws IllegalArgumentException if parameters is null.
208     */
209    public void setAdvancedParameters(OAuthParameters parameters) {
210        CheckParameterUtil.ensureParameterNotNull(parameters, "parameters");
211        if (parameters.equals(OAuthParameters.createDefault(apiUrl))) {
212            cbUseDefaults.setSelected(true);
213            setChildComponentsEnabled(false);
214        } else {
215            cbUseDefaults.setSelected(false);
216            setChildComponentsEnabled(true);
217            tfConsumerKey.setText(parameters.getConsumerKey() == null ? "" : parameters.getConsumerKey());
218            tfConsumerSecret.setText(parameters.getConsumerSecret() == null ? "" : parameters.getConsumerSecret());
219            tfRequestTokenURL.setText(parameters.getRequestTokenUrl() == null ? "" : parameters.getRequestTokenUrl());
220            tfAccessTokenURL.setText(parameters.getAccessTokenUrl() == null ? "" : parameters.getAccessTokenUrl());
221            tfAuthoriseURL.setText(parameters.getAuthoriseUrl() == null ? "" : parameters.getAuthoriseUrl());
222        }
223    }
224
225    /**
226     * Constructs a new {@code AdvancedOAuthPropertiesPanel}.
227     */
228    public AdvancedOAuthPropertiesPanel() {
229        build();
230    }
231
232    /**
233     * Initializes the panel from the values in the preferences <code>preferences</code>.
234     *
235     * @param pref the preferences. Must not be null.
236     * @throws IllegalArgumentException if pref is null
237     */
238    public void initFromPreferences(Preferences pref) {
239        CheckParameterUtil.ensureParameterNotNull(pref, "pref");
240        setApiUrl(pref.get("osm-server.url"));
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);
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));
252            setChildComponentsEnabled(true);
253        }
254        ilUseDefault.setEnabled(true);
255    }
256
257    /**
258     * Remembers the current values in the preferences <code>pref</code>.
259     *
260     * @param pref the preferences. Must not be null.
261     * @throws IllegalArgumentException if pref is null.
262     */
263    public void rememberPreferences(Preferences pref) {
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());
277            pref.put("oauth.settings.authorise-url", tfAuthoriseURL.getText().trim());
278        }
279    }
280
281    class UseDefaultItemListener implements ItemListener {
282        private boolean enabled;
283
284        @Override
285        public void itemStateChanged(ItemEvent e) {
286            if (!enabled) return;
287            switch (e.getStateChange()) {
288            case ItemEvent.SELECTED:
289                if (hasCustomSettings() && !confirmOverwriteCustomSettings()) {
290                    cbUseDefaults.setSelected(false);
291                    return;
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    }
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    }
318}