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