source: josm/trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorisationWizard.java@ 2801

Last change on this file since 2801 was 2801, checked in by stoecker, 14 years ago

fixed line endings of recent checkins

File size: 13.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.oauth;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.FlowLayout;
10import java.awt.Font;
11import java.awt.GridBagConstraints;
12import java.awt.GridBagLayout;
13import java.awt.Insets;
14import java.awt.event.ActionEvent;
15import java.awt.event.ComponentEvent;
16import java.awt.event.ComponentListener;
17import java.awt.event.ItemEvent;
18import java.awt.event.ItemListener;
19import java.awt.event.KeyEvent;
20import java.awt.event.WindowAdapter;
21import java.awt.event.WindowEvent;
22import java.beans.PropertyChangeEvent;
23import java.beans.PropertyChangeListener;
24import java.util.logging.Logger;
25
26import javax.swing.AbstractAction;
27import javax.swing.BorderFactory;
28import javax.swing.JComponent;
29import javax.swing.JDialog;
30import javax.swing.JLabel;
31import javax.swing.JOptionPane;
32import javax.swing.JPanel;
33import javax.swing.JScrollPane;
34import javax.swing.KeyStroke;
35import javax.swing.UIManager;
36
37import org.openstreetmap.josm.Main;
38import org.openstreetmap.josm.data.oauth.OAuthParameters;
39import org.openstreetmap.josm.data.oauth.OAuthToken;
40import org.openstreetmap.josm.gui.SideButton;
41import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
42import org.openstreetmap.josm.gui.help.HelpUtil;
43import org.openstreetmap.josm.gui.widgets.HtmlPanel;
44import org.openstreetmap.josm.tools.CheckParameterUtil;
45import org.openstreetmap.josm.tools.ImageProvider;
46import org.openstreetmap.josm.tools.WindowGeometry;
47
48/**
49 * This wizard walks the user to the necessary steps to retrieve an OAuth Access Token which
50 * allows JOSM to access the OSM API on the users behalf.
51 *
52 */
53public class OAuthAuthorisationWizard extends JDialog {
54 static private final Logger logger = Logger.getLogger(OAuthAuthorisationWizard.class.getName());
55
56 private HtmlPanel pnlMessage;
57 private boolean canceled;
58 private String apiUrl;
59
60 private AuthorisationProcedureComboBox cbAuthorisationProcedure;
61 private FullyAutomaticAuthorisationUI pnlFullyAutomaticAuthorisationUI;
62 private SemiAutomaticAuthorisationUI pnlSemiAutomaticAuthorisationUI;
63 private ManualAuthorisationUI pnlManualAuthorisationUI;
64 private JScrollPane spAuthorisationProcedureUI;
65
66 /**
67 * Builds the row with the action buttons
68 *
69 * @return
70 */
71 protected JPanel buildButtonRow(){
72 JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
73
74 AcceptAccessTokenAction actAcceptAccessToken = new AcceptAccessTokenAction();
75 pnlFullyAutomaticAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
76 pnlSemiAutomaticAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
77 pnlManualAuthorisationUI.addPropertyChangeListener(actAcceptAccessToken);
78
79 pnl.add(new SideButton(actAcceptAccessToken));
80 pnl.add(new SideButton(new CancelAction()));
81 pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/OAuthAuthorisationWizard"))));
82
83 return pnl;
84 }
85
86 /**
87 * Builds the panel with general information in the header
88 *
89 * @return
90 */
91 protected JPanel buildHeaderInfoPanel() {
92 JPanel pnl = new JPanel(new GridBagLayout());
93 pnl.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
94 GridBagConstraints gc = new GridBagConstraints();
95
96 // the oauth logo in the header
97 gc.anchor = GridBagConstraints.NORTHWEST;
98 gc.fill = GridBagConstraints.HORIZONTAL;
99 gc.weightx = 1.0;
100 gc.gridwidth = 2;
101 JLabel lbl = new JLabel();
102 lbl.setIcon(ImageProvider.get("oauth", "oauth-logo"));
103 lbl.setOpaque(true);
104 pnl.add(lbl, gc);
105
106 // OAuth in a nutshell ...
107 gc.gridy = 1;
108 gc.insets = new Insets(5,0,0,5);
109 pnlMessage = new HtmlPanel();
110 pnlMessage.setText("<html><body>"
111 + "With OAuth you grant JOSM the right to upload map data and GPS tracks "
112 + "on your behalf (<a href=\"urn:josm-oauth-info\">more info...</a>)."
113 + "</body></html>"
114 );
115 pnl.add(pnlMessage, gc);
116
117 // the authorisation procedure
118 gc.gridy = 2;
119 gc.gridwidth = 1;
120 gc.weightx = 0.0;
121 lbl = new JLabel(tr("Please select an authorisation procedure: "));
122 lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
123 pnl.add(lbl,gc);
124
125 gc.gridx = 1;
126 gc.gridwidth = 1;
127 gc.weightx = 1.0;
128 pnl.add(cbAuthorisationProcedure = new AuthorisationProcedureComboBox(),gc);
129 cbAuthorisationProcedure.addItemListener(new AuthorisationProcedureChangeListener());
130 return pnl;
131 }
132
133 /**
134 * Refreshes the view of the authorisation panel, depending on the authorisation procedure
135 * currently selected
136 */
137 protected void refreshAuthorisationProcedurePanel() {
138 AuthorisationProcedure procedure = (AuthorisationProcedure)cbAuthorisationProcedure.getSelectedItem();
139 switch(procedure) {
140 case FULLY_AUTOMATIC:
141 spAuthorisationProcedureUI.getViewport().setView(pnlFullyAutomaticAuthorisationUI);
142 pnlFullyAutomaticAuthorisationUI.revalidate();
143 break;
144 case SEMI_AUTOMATIC:
145 spAuthorisationProcedureUI.getViewport().setView(pnlSemiAutomaticAuthorisationUI);
146 pnlSemiAutomaticAuthorisationUI.revalidate();
147 break;
148 case MANUALLY:
149 spAuthorisationProcedureUI.getViewport().setView(pnlManualAuthorisationUI);
150 pnlManualAuthorisationUI.revalidate();
151 break;
152 }
153 validate();
154 repaint();
155 }
156
157 /**
158 * builds the UI
159 */
160 protected void build() {
161 getContentPane().setLayout(new BorderLayout());
162 getContentPane().add(buildHeaderInfoPanel(), BorderLayout.NORTH);
163
164 pnlFullyAutomaticAuthorisationUI = new FullyAutomaticAuthorisationUI();
165 pnlFullyAutomaticAuthorisationUI.setApiUrl(apiUrl);
166
167 pnlSemiAutomaticAuthorisationUI = new SemiAutomaticAuthorisationUI();
168 pnlSemiAutomaticAuthorisationUI.setApiUrl(apiUrl);
169
170 pnlManualAuthorisationUI = new ManualAuthorisationUI();
171 pnlManualAuthorisationUI.setApiUrl(apiUrl);
172
173 spAuthorisationProcedureUI = new JScrollPane(new JPanel());
174 spAuthorisationProcedureUI.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
175 spAuthorisationProcedureUI.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
176 spAuthorisationProcedureUI.getVerticalScrollBar().addComponentListener(
177 new ComponentListener() {
178 public void componentShown(ComponentEvent e) {
179 spAuthorisationProcedureUI.setBorder(UIManager.getBorder("ScrollPane.border"));
180 }
181
182 public void componentHidden(ComponentEvent e) {
183 spAuthorisationProcedureUI.setBorder(null);
184 }
185
186 public void componentResized(ComponentEvent e) {}
187 public void componentMoved(ComponentEvent e) {}
188 }
189 );
190 getContentPane().add(spAuthorisationProcedureUI, BorderLayout.CENTER);
191 getContentPane().add(buildButtonRow(), BorderLayout.SOUTH);
192
193 addWindowListener(new WindowEventHandler());
194 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
195 getRootPane().getActionMap().put("cancel", new CancelAction());
196
197 refreshAuthorisationProcedurePanel();
198
199 HelpUtil.setHelpContext(getRootPane(), HelpUtil.ht("/Dialog/OAuthAuthorisationWizard"));
200 }
201
202 /**
203 * Creates the wizard.
204 *
205 * @param apiUrl the API URL. Must not be null.
206 * @throws IllegalArgumentException thrown if apiUrl is null
207 */
208 public OAuthAuthorisationWizard(String apiUrl) throws IllegalArgumentException {
209 super(JOptionPane.getFrameForComponent(Main.parent),true /* modal */);
210 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
211 build();
212 setApiUrl(apiUrl);
213 }
214
215 /**
216 * Creates the wizard.
217 *
218 * @param parent the component relative to which the dialog is displayed
219 * @param apiUrl the API URL. Must not be null.
220 * @throws IllegalArgumentException thrown if apiUrl is null
221 */
222 public OAuthAuthorisationWizard(Component parent, String apiUrl) {
223 super(JOptionPane.getFrameForComponent(parent),true /* modal */);
224 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
225 build();
226 setApiUrl(apiUrl);
227 }
228
229 /**
230 * Sets the API URL for the API for which this wizard is generating
231 * an Access Token.
232 *
233 * @param apiUrl the API URL. Must not be null.
234 * @throws IllegalArgumentException thrown if apiUrl is null
235 */
236 public void setApiUrl(String apiUrl) throws IllegalArgumentException{
237 CheckParameterUtil.ensureParameterNotNull(apiUrl, "apiUrl");
238 this.apiUrl = apiUrl;
239 setTitle(tr("Get an Access Token for ''{0}''", apiUrl));
240 if (pnlFullyAutomaticAuthorisationUI != null) {
241 pnlFullyAutomaticAuthorisationUI.setApiUrl(apiUrl);
242 }
243 if (pnlSemiAutomaticAuthorisationUI != null) {
244 pnlSemiAutomaticAuthorisationUI.setApiUrl(apiUrl);
245 }
246 if (pnlManualAuthorisationUI != null) {
247 pnlManualAuthorisationUI.setApiUrl(apiUrl);
248 }
249
250 }
251
252 /**
253 * Replies true if the dialog was cancelled
254 *
255 * @return true if the dialog was cancelled
256 */
257 public boolean isCanceled() {
258 return canceled;
259 }
260
261 protected AbstractAuthorisationUI getCurrentAuthorisationUI() {
262 switch((AuthorisationProcedure)cbAuthorisationProcedure.getSelectedItem()) {
263 case FULLY_AUTOMATIC: return pnlFullyAutomaticAuthorisationUI;
264 case MANUALLY: return pnlManualAuthorisationUI;
265 case SEMI_AUTOMATIC: return pnlSemiAutomaticAuthorisationUI;
266 default: return null;
267 }
268 }
269
270 /**
271 * Replies the Access Token entered using the wizard
272 *
273 * @return the access token. May be null if the wizard was canceled.
274 */
275 public OAuthToken getAccessToken() {
276 return getCurrentAuthorisationUI().getAccessToken();
277 }
278
279 /**
280 * Replies the current OAuth parameters.
281 *
282 * @return the current OAuth parameters.
283 */
284 public OAuthParameters getOAuthParameters() {
285 return getCurrentAuthorisationUI().getOAuthParameters();
286 }
287
288 /**
289 * Replies true if the currently selected Access Token shall be saved to
290 * the preferences.
291 *
292 * @return true if the currently selected Access Token shall be saved to
293 * the preferences
294 */
295 public boolean isSaveAccessTokenToPreferences() {
296 return getCurrentAuthorisationUI().isSaveAccessTokenToPreferences();
297 }
298
299 /**
300 * Initializes the dialog with values from the preferences
301 *
302 */
303 public void initFromPreferences() {
304 pnlFullyAutomaticAuthorisationUI.initFromPreferences(Main.pref);
305 pnlSemiAutomaticAuthorisationUI.initFromPreferences(Main.pref);
306 pnlManualAuthorisationUI.initFromPreferences(Main.pref);
307 }
308
309 @Override
310 public void setVisible(boolean visible) {
311 if (visible) {
312 new WindowGeometry(
313 getClass().getName() + ".geometry",
314 WindowGeometry.centerInWindow(
315 Main.parent,
316 new Dimension(400,400)
317 )
318 ).apply(this);
319 initFromPreferences();
320 } else if (!visible && isShowing()){
321 new WindowGeometry(this).remember(getClass().getName() + ".geometry");
322 }
323 super.setVisible(visible);
324 }
325
326 protected void setCanceled(boolean canceled) {
327 this.canceled = canceled;
328 }
329
330 class AuthorisationProcedureChangeListener implements ItemListener {
331 public void itemStateChanged(ItemEvent arg0) {
332 refreshAuthorisationProcedurePanel();
333 }
334 }
335
336 class CancelAction extends AbstractAction {
337 public CancelAction() {
338 putValue(NAME, tr("Cancel"));
339 putValue(SMALL_ICON, ImageProvider.get("cancel"));
340 putValue(SHORT_DESCRIPTION, tr("Close the dialog and cancel authorisation"));
341 }
342
343 public void cancel() {
344 setCanceled(true);
345 setVisible(false);
346 }
347
348 public void actionPerformed(ActionEvent evt) {
349 cancel();
350 }
351 }
352
353 class AcceptAccessTokenAction extends AbstractAction implements PropertyChangeListener {
354 private OAuthToken token;
355
356 public AcceptAccessTokenAction() {
357 putValue(NAME, tr("Accept Access Token"));
358 putValue(SMALL_ICON, ImageProvider.get("ok"));
359 putValue(SHORT_DESCRIPTION, tr("Close the dialog and accept the Access Token"));
360 updateEnabledState(null);
361 }
362
363 public void actionPerformed(ActionEvent evt) {
364 setCanceled(false);
365 setVisible(false);
366 }
367
368 public void updateEnabledState(OAuthToken token) {
369 setEnabled(token != null);
370 }
371
372 public void propertyChange(PropertyChangeEvent evt) {
373 if (!evt.getPropertyName().equals(AbstractAuthorisationUI.ACCESS_TOKEN_PROP))
374 return;
375 token = (OAuthToken)evt.getNewValue();
376 updateEnabledState(token);
377 }
378 }
379
380 class WindowEventHandler extends WindowAdapter {
381 @Override
382 public void windowClosing(WindowEvent arg0) {
383 new CancelAction().cancel();
384 }
385 }
386}
Note: See TracBrowser for help on using the repository browser.