1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.oauth;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.FlowLayout;
|
---|
8 | import java.awt.GridBagConstraints;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Insets;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.beans.PropertyChangeEvent;
|
---|
13 | import java.beans.PropertyChangeListener;
|
---|
14 |
|
---|
15 | import javax.swing.AbstractAction;
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JTabbedPane;
|
---|
21 | import javax.swing.JTextField;
|
---|
22 | import javax.swing.event.DocumentEvent;
|
---|
23 | import javax.swing.event.DocumentListener;
|
---|
24 | import javax.swing.text.JTextComponent;
|
---|
25 |
|
---|
26 | import org.openstreetmap.josm.Main;
|
---|
27 | import org.openstreetmap.josm.data.oauth.OAuthToken;
|
---|
28 | import org.openstreetmap.josm.gui.SideButton;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
32 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 |
|
---|
35 | public class ManualAuthorisationUI extends AbstractAuthorisationUI{
|
---|
36 |
|
---|
37 | private JTextField tfAccessTokenKey;
|
---|
38 | private AccessTokenKeyValidator valAccessTokenKey;
|
---|
39 | private JTextField tfAccessTokenSecret;
|
---|
40 | private AccessTokenSecretValidator valAccessTokenSecret;
|
---|
41 | private JCheckBox cbSaveToPreferences;
|
---|
42 | private HtmlPanel pnlMessage;
|
---|
43 |
|
---|
44 | protected JPanel buildAccessTokenPanel() {
|
---|
45 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
46 | pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
47 | GridBagConstraints gc = new GridBagConstraints();
|
---|
48 | AccessTokenBuilder accessTokenBuilder = new AccessTokenBuilder();
|
---|
49 |
|
---|
50 | // the access token key input field
|
---|
51 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
52 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
53 | gc.weightx = 0.0;
|
---|
54 | gc.gridwidth = 2;
|
---|
55 | gc.insets = new Insets(0,0,5,0);
|
---|
56 | pnlMessage= new HtmlPanel();
|
---|
57 | pnlMessage.setText(tr("<html><body>"
|
---|
58 | + "Please enter an OAuth Access Token which is authorised to access the OSM server "
|
---|
59 | + "''{0}''."
|
---|
60 | + "</body></html>",
|
---|
61 | getApiUrl()
|
---|
62 | ));
|
---|
63 | pnl.add(pnlMessage, gc);
|
---|
64 |
|
---|
65 | // the access token key input field
|
---|
66 | gc.gridy = 1;
|
---|
67 | gc.weightx = 0.0;
|
---|
68 | gc.gridwidth = 1;
|
---|
69 | gc.insets = new Insets(0,0,0,3);
|
---|
70 | pnl.add(new JLabel(tr("Access Token Key:")), gc);
|
---|
71 |
|
---|
72 | gc.gridx = 1;
|
---|
73 | gc.weightx = 1.0;
|
---|
74 | pnl.add(tfAccessTokenKey = new JTextField(), gc);
|
---|
75 | SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenKey);
|
---|
76 | valAccessTokenKey = new AccessTokenKeyValidator(tfAccessTokenKey);
|
---|
77 | valAccessTokenKey.validate();
|
---|
78 | tfAccessTokenKey.getDocument().addDocumentListener(accessTokenBuilder);
|
---|
79 |
|
---|
80 | // the access token key input field
|
---|
81 | gc.gridy = 2;
|
---|
82 | gc.gridx = 0;
|
---|
83 | gc.weightx = 0.0;
|
---|
84 | pnl.add(new JLabel(tr("Access Token Secret:")), gc);
|
---|
85 |
|
---|
86 | gc.gridx = 1;
|
---|
87 | gc.weightx = 1.0;
|
---|
88 | pnl.add(tfAccessTokenSecret = new JTextField(), gc);
|
---|
89 | SelectAllOnFocusGainedDecorator.decorate(tfAccessTokenSecret);
|
---|
90 | valAccessTokenSecret = new AccessTokenSecretValidator(tfAccessTokenSecret);
|
---|
91 | valAccessTokenSecret.validate();
|
---|
92 | tfAccessTokenSecret.getDocument().addDocumentListener(accessTokenBuilder);
|
---|
93 |
|
---|
94 | // the checkbox for saving to preferences
|
---|
95 | gc.gridy = 3;
|
---|
96 | gc.gridx = 0;
|
---|
97 | gc.gridwidth =2;
|
---|
98 | gc.weightx = 1.0;
|
---|
99 | pnl.add(cbSaveToPreferences = new JCheckBox(tr("Save Access Token to preferences")), gc);
|
---|
100 | cbSaveToPreferences.setSelected(OAuthAccessTokenHolder.getInstance().isSaveToPreferences());
|
---|
101 |
|
---|
102 | // filler - grab remaining space
|
---|
103 | gc.gridy = 3;
|
---|
104 | gc.gridx = 0;
|
---|
105 | gc.gridwidth =2;
|
---|
106 | gc.weightx = 1.0;
|
---|
107 | gc.weighty = 1.0;
|
---|
108 | gc.fill = GridBagConstraints.BOTH;
|
---|
109 | pnl.add(new JPanel(), gc);
|
---|
110 | return pnl;
|
---|
111 | }
|
---|
112 |
|
---|
113 | protected JPanel buildTabbedPreferencesPanel() {
|
---|
114 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
115 |
|
---|
116 | JTabbedPane tp = new JTabbedPane();
|
---|
117 | tp.add(buildAccessTokenPanel());
|
---|
118 | tp.add(getAdvancedPropertiesPanel());
|
---|
119 |
|
---|
120 | tp.setTitleAt(0, tr("Access Token"));
|
---|
121 | tp.setTitleAt(1, tr("Advanced OAuth parameters"));
|
---|
122 |
|
---|
123 | tp.setToolTipTextAt(0, tr("Enter the OAuth Access Token"));
|
---|
124 | tp.setToolTipTextAt(1, tr("Enter advanced OAuth properties"));
|
---|
125 |
|
---|
126 | pnl.add(tp, BorderLayout.CENTER);
|
---|
127 | return pnl;
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected JPanel buildActionsPanel() {
|
---|
131 | JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
132 | TestAccessTokenAction actTestAccessToken = new TestAccessTokenAction();
|
---|
133 | pnl.add(new SideButton(actTestAccessToken));
|
---|
134 | this.addPropertyChangeListener(actTestAccessToken);
|
---|
135 | return pnl;
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | public void setApiUrl(String apiUrl) {
|
---|
140 | super.setApiUrl(apiUrl);
|
---|
141 | pnlMessage.setText(tr("<html><body>"
|
---|
142 | + "Please enter an OAuth Access Token which is authorised to access the OSM server "
|
---|
143 | + "''{0}''."
|
---|
144 | + "</body></html>",
|
---|
145 | getApiUrl()
|
---|
146 | ));
|
---|
147 | }
|
---|
148 |
|
---|
149 | protected void build() {
|
---|
150 | setLayout(new BorderLayout());
|
---|
151 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
152 | add(buildTabbedPreferencesPanel(), BorderLayout.CENTER);
|
---|
153 | add(buildActionsPanel(), BorderLayout.SOUTH);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public ManualAuthorisationUI() {
|
---|
157 | build();
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public boolean isSaveAccessTokenToPreferences() {
|
---|
162 | return cbSaveToPreferences.isSelected();
|
---|
163 | }
|
---|
164 |
|
---|
165 | static private class AccessTokenKeyValidator extends AbstractTextComponentValidator {
|
---|
166 |
|
---|
167 | public AccessTokenKeyValidator(JTextComponent tc) throws IllegalArgumentException {
|
---|
168 | super(tc);
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public boolean isValid() {
|
---|
173 | return ! getComponent().getText().trim().equals("");
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public void validate() {
|
---|
178 | if (isValid()) {
|
---|
179 | feedbackValid(tr("Please enter an Access Token Key"));
|
---|
180 | } else {
|
---|
181 | feedbackInvalid(tr("The Access Token Key must not be empty. Please enter an Access Token Key"));
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | static private class AccessTokenSecretValidator extends AbstractTextComponentValidator {
|
---|
187 | public AccessTokenSecretValidator(JTextComponent tc) throws IllegalArgumentException {
|
---|
188 | super(tc);
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public boolean isValid() {
|
---|
193 | return ! getComponent().getText().trim().equals("");
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public void validate() {
|
---|
198 | if (isValid()) {
|
---|
199 | feedbackValid(tr("Please enter an Access Token Secret"));
|
---|
200 | } else {
|
---|
201 | feedbackInvalid(tr("The Access Token Secret must not be empty. Please enter an Access Token Secret"));
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | class AccessTokenBuilder implements DocumentListener {
|
---|
207 |
|
---|
208 | public void build() {
|
---|
209 | if (! valAccessTokenKey.isValid() || !valAccessTokenSecret.isValid()) {
|
---|
210 | setAccessToken(null);
|
---|
211 | } else {
|
---|
212 | setAccessToken(new OAuthToken(tfAccessTokenKey.getText().trim(), tfAccessTokenSecret.getText().trim()));
|
---|
213 | }
|
---|
214 | }
|
---|
215 | public void changedUpdate(DocumentEvent e) {
|
---|
216 | build();
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void insertUpdate(DocumentEvent e) {
|
---|
220 | build();
|
---|
221 | }
|
---|
222 |
|
---|
223 | public void removeUpdate(DocumentEvent e) {
|
---|
224 | build();
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Action for testing an Access Token
|
---|
230 | */
|
---|
231 | class TestAccessTokenAction extends AbstractAction implements PropertyChangeListener {
|
---|
232 | public TestAccessTokenAction() {
|
---|
233 | putValue(NAME, tr("Test Access Token"));
|
---|
234 | putValue(SMALL_ICON, ImageProvider.get("oauth", "oauth"));
|
---|
235 | putValue(SHORT_DESCRIPTION, tr("Click to test the Access Token"));
|
---|
236 | updateEnabledState();
|
---|
237 | }
|
---|
238 |
|
---|
239 | public void actionPerformed(ActionEvent evt) {
|
---|
240 | TestAccessTokenTask task = new TestAccessTokenTask(
|
---|
241 | ManualAuthorisationUI.this,
|
---|
242 | getApiUrl(),
|
---|
243 | getAdvancedPropertiesPanel().getAdvancedParameters(),
|
---|
244 | getAccessToken()
|
---|
245 | );
|
---|
246 | Main.worker.submit(task);
|
---|
247 | }
|
---|
248 |
|
---|
249 | protected void updateEnabledState() {
|
---|
250 | setEnabled(hasAccessToken());
|
---|
251 | }
|
---|
252 |
|
---|
253 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
254 | if (! evt.getPropertyName().equals(AbstractAuthorisationUI.ACCESS_TOKEN_PROP))
|
---|
255 | return;
|
---|
256 | updateEnabledState();
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|