001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.oauth; 003 004import java.util.Objects; 005 006import org.openstreetmap.josm.data.Preferences; 007import org.openstreetmap.josm.data.oauth.OAuthParameters; 008import org.openstreetmap.josm.data.oauth.OAuthToken; 009import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel; 010import org.openstreetmap.josm.tools.CheckParameterUtil; 011 012/** 013 * This is the abstract base class for the three authorisation UIs. 014 * 015 * @since 2746 016 */ 017public abstract class AbstractAuthorizationUI extends VerticallyScrollablePanel { 018 /** 019 * The property name for the Access Token property 020 */ 021 public static final String ACCESS_TOKEN_PROP = AbstractAuthorizationUI.class.getName() + ".accessToken"; 022 023 private String apiUrl; 024 private final AdvancedOAuthPropertiesPanel pnlAdvancedProperties; 025 private transient OAuthToken accessToken; 026 027 protected void fireAccessTokenChanged(OAuthToken oldValue, OAuthToken newValue) { 028 firePropertyChange(ACCESS_TOKEN_PROP, oldValue, newValue); 029 } 030 031 /** 032 * Constructs a new {@code AbstractAuthorizationUI} for the given API URL. 033 * @param apiUrl The OSM API URL 034 * @since 5422 035 */ 036 public AbstractAuthorizationUI(String apiUrl) { 037 pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel(); 038 setApiUrl(apiUrl); 039 } 040 041 /** 042 * Replies the URL of the OSM API for which this UI is currently trying to retrieve an OAuth 043 * Access Token 044 * 045 * @return the API URL 046 */ 047 public String getApiUrl() { 048 return apiUrl; 049 } 050 051 /** 052 * Sets the URL of the OSM API for which this UI is currently trying to retrieve an OAuth 053 * Access Token 054 * 055 * @param apiUrl the api URL 056 */ 057 public void setApiUrl(String apiUrl) { 058 this.apiUrl = apiUrl; 059 this.pnlAdvancedProperties.setApiUrl(apiUrl); 060 } 061 062 /** 063 * Replies the panel for entering advanced OAuth parameters (see {@link OAuthParameters}) 064 * 065 * @return the panel for entering advanced OAuth parameters 066 * @see #getOAuthParameters() 067 */ 068 protected AdvancedOAuthPropertiesPanel getAdvancedPropertiesPanel() { 069 return pnlAdvancedProperties; 070 } 071 072 /** 073 * Replies the current set of advanced OAuth parameters in this UI 074 * 075 * @return the current set of advanced OAuth parameters in this UI 076 */ 077 public OAuthParameters getOAuthParameters() { 078 return pnlAdvancedProperties.getAdvancedParameters(); 079 } 080 081 /** 082 * Replies the retrieved Access Token. null, if no Access Token was retrieved. 083 * 084 * @return the retrieved Access Token 085 */ 086 public OAuthToken getAccessToken() { 087 return accessToken; 088 } 089 090 /** 091 * Sets the current Access Token. This will fire a property change event for {@link #ACCESS_TOKEN_PROP} 092 * if the access token has changed 093 * 094 * @param accessToken the new access token. null, to clear the current access token 095 */ 096 protected void setAccessToken(OAuthToken accessToken) { 097 OAuthToken oldValue = this.accessToken; 098 this.accessToken = accessToken; 099 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 103 } else if (!Objects.equals(oldValue, this.accessToken)) { 104 fireAccessTokenChanged(oldValue, this.accessToken); 105 } 106 } 107 108 /** 109 * Replies true if this UI currently has an Access Token 110 * 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. 120 * 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>. 127 * 128 * @param pref the preferences. Must not be null. 129 * @throws IllegalArgumentException if pref is null 130 */ 131 public void initFromPreferences(Preferences pref) { 132 CheckParameterUtil.ensureParameterNotNull(pref, "pref"); 133 pnlAdvancedProperties.initFromPreferences(pref); 134 } 135}