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