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

Last change on this file since 8308 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import org.openstreetmap.josm.data.Preferences;
5import org.openstreetmap.josm.data.oauth.OAuthParameters;
6import org.openstreetmap.josm.data.oauth.OAuthToken;
7import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
8import org.openstreetmap.josm.tools.CheckParameterUtil;
9
10import com.sun.org.apache.xerces.internal.utils.Objects;
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;
25 private transient OAuthToken accessToken;
26
27 protected void fireAccessTokenChanged(OAuthToken oldValue, OAuthToken newValue) {
28 firePropertyChange(ACCESS_TOKEN_PROP, oldValue, newValue);
29 }
30
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) {
37 pnlAdvancedProperties = new AdvancedOAuthPropertiesPanel();
38 setApiUrl(apiUrl);
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
44 *
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
54 *
55 * @param apiUrl the api URL
56 */
57 public void setApiUrl(String apiUrl) {
58 this.apiUrl = apiUrl;
59 this.pnlAdvancedProperties.setApiUrl(apiUrl);
60 }
61
62 /**
63 * Replies the panel for entering advanced OAuth parameters (see {@link OAuthParameters})
64 *
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
74 *
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.
83 *
84 * @return the retrieved Access Token
85 */
86 public OAuthToken getAccessToken() {
87 return accessToken;
88 }
89
90 /**
91 * Sets the current Access Token. This will fire a property change event for {@link #ACCESS_TOKEN_PROP}
92 * if the access token has changed
93 *
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
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}
Note: See TracBrowser for help on using the repository browser.