source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/AbstractAuthorizationUI.java@ 12928

Last change on this file since 12928 was 12928, checked in by bastiK, 7 years ago

see #15229 - do not copy the entire preferences list, just to set a custom server API in OAuth wizard

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