[2801] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.oauth;
|
---|
| 3 |
|
---|
| 4 | import org.openstreetmap.josm.data.Preferences;
|
---|
| 5 | import org.openstreetmap.josm.data.oauth.OAuthParameters;
|
---|
| 6 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
| 7 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
---|
| 8 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 9 |
|
---|
[8308] | 10 | import com.sun.org.apache.xerces.internal.utils.Objects;
|
---|
| 11 |
|
---|
[2801] | 12 | /**
|
---|
| 13 | * This is the abstract base class for the three authorisation UIs.
|
---|
[3530] | 14 | *
|
---|
[5422] | 15 | * @since 2746
|
---|
[2801] | 16 | */
|
---|
[5422] | 17 | public abstract class AbstractAuthorizationUI extends VerticallyScrollablePanel {
|
---|
[2801] | 18 | /**
|
---|
| 19 | * The property name for the Access Token property
|
---|
| 20 | */
|
---|
[6883] | 21 | public static final String ACCESS_TOKEN_PROP = AbstractAuthorizationUI.class.getName() + ".accessToken";
|
---|
[2801] | 22 |
|
---|
| 23 | private String apiUrl;
|
---|
[5422] | 24 | private final AdvancedOAuthPropertiesPanel pnlAdvancedProperties;
|
---|
[8308] | 25 | private transient OAuthToken accessToken;
|
---|
[2801] | 26 |
|
---|
| 27 | protected void fireAccessTokenChanged(OAuthToken oldValue, OAuthToken newValue) {
|
---|
| 28 | firePropertyChange(ACCESS_TOKEN_PROP, oldValue, newValue);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[5422] | 31 | /**
|
---|
| 32 | * Constructs a new {@code AbstractAuthorizationUI} for the given API URL.
|
---|
| 33 | * @param apiUrl The OSM API URL
|
---|
| 34 | * @since 5422
|
---|
| 35 | */
|
---|
| 36 | public AbstractAuthorizationUI(String apiUrl) {
|
---|
[2801] | 37 | pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel();
|
---|
[5422] | 38 | setApiUrl(apiUrl);
|
---|
[2801] | 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Replies the URL of the OSM API for which this UI is currently trying to retrieve an OAuth
|
---|
| 43 | * Access Token
|
---|
[3530] | 44 | *
|
---|
[2801] | 45 | * @return the API URL
|
---|
| 46 | */
|
---|
| 47 | public String getApiUrl() {
|
---|
| 48 | return apiUrl;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Sets the URL of the OSM API for which this UI is currently trying to retrieve an OAuth
|
---|
| 53 | * Access Token
|
---|
[3530] | 54 | *
|
---|
[2801] | 55 | * @param apiUrl the api URL
|
---|
| 56 | */
|
---|
| 57 | public void setApiUrl(String apiUrl) {
|
---|
| 58 | this.apiUrl = apiUrl;
|
---|
[5422] | 59 | this.pnlAdvancedProperties.setApiUrl(apiUrl);
|
---|
[2801] | 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
[5266] | 63 | * Replies the panel for entering advanced OAuth parameters (see {@link OAuthParameters})
|
---|
[3530] | 64 | *
|
---|
[2801] | 65 | * @return the panel for entering advanced OAuth parameters
|
---|
| 66 | * @see #getOAuthParameters()
|
---|
| 67 | */
|
---|
| 68 | protected AdvancedOAuthPropertiesPanel getAdvancedPropertiesPanel() {
|
---|
| 69 | return pnlAdvancedProperties;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * Replies the current set of advanced OAuth parameters in this UI
|
---|
[3530] | 74 | *
|
---|
[2801] | 75 | * @return the current set of advanced OAuth parameters in this UI
|
---|
| 76 | */
|
---|
| 77 | public OAuthParameters getOAuthParameters() {
|
---|
| 78 | return pnlAdvancedProperties.getAdvancedParameters();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Replies the retrieved Access Token. null, if no Access Token was retrieved.
|
---|
[3530] | 83 | *
|
---|
[2801] | 84 | * @return the retrieved Access Token
|
---|
| 85 | */
|
---|
| 86 | public OAuthToken getAccessToken() {
|
---|
| 87 | return accessToken;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
[5266] | 91 | * Sets the current Access Token. This will fire a property change event for {@link #ACCESS_TOKEN_PROP}
|
---|
[2801] | 92 | * if the access token has changed
|
---|
[3530] | 93 | *
|
---|
[2801] | 94 | * @param accessToken the new access token. null, to clear the current access token
|
---|
| 95 | */
|
---|
| 96 | protected void setAccessToken(OAuthToken accessToken) {
|
---|
| 97 | OAuthToken oldValue = this.accessToken;
|
---|
| 98 | this.accessToken = accessToken;
|
---|
| 99 | if (oldValue == null ^ this.accessToken == null) {
|
---|
| 100 | fireAccessTokenChanged(oldValue, this.accessToken);
|
---|
| 101 | } else if (oldValue == null && this.accessToken == null) {
|
---|
| 102 | // no change - don't fire an event
|
---|
[8308] | 103 | } else if (!Objects.equals(oldValue, this.accessToken)) {
|
---|
[2801] | 104 | fireAccessTokenChanged(oldValue, this.accessToken);
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Replies true if this UI currently has an Access Token
|
---|
[3530] | 110 | *
|
---|
[2801] | 111 | * @return true if this UI currently has an Access Token
|
---|
| 112 | */
|
---|
| 113 | public boolean hasAccessToken() {
|
---|
| 114 | return accessToken != null;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
| 118 | * Replies whether the user has chosen to save the Access Token in the JOSM
|
---|
| 119 | * preferences or not.
|
---|
[3530] | 120 | *
|
---|
[2801] | 121 | * @return true if the user has chosen to save the Access Token
|
---|
| 122 | */
|
---|
| 123 | public abstract boolean isSaveAccessTokenToPreferences();
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Initializes the authorisation UI with preference values in <code>pref</code>.
|
---|
[3530] | 127 | *
|
---|
[2801] | 128 | * @param pref the preferences. Must not be null.
|
---|
[8291] | 129 | * @throws IllegalArgumentException if pref is null
|
---|
[2801] | 130 | */
|
---|
[8291] | 131 | public void initFromPreferences(Preferences pref) {
|
---|
[2801] | 132 | CheckParameterUtil.ensureParameterNotNull(pref, "pref");
|
---|
| 133 | pnlAdvancedProperties.initFromPreferences(pref);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|