1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.FlowLayout;
|
---|
9 | import java.awt.Font;
|
---|
10 | import java.awt.GridBagConstraints;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.Insets;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.FocusAdapter;
|
---|
15 | import java.awt.event.FocusEvent;
|
---|
16 | import java.awt.event.KeyEvent;
|
---|
17 | import java.awt.event.WindowAdapter;
|
---|
18 | import java.awt.event.WindowEvent;
|
---|
19 |
|
---|
20 | import javax.swing.AbstractAction;
|
---|
21 | import javax.swing.BorderFactory;
|
---|
22 | import javax.swing.JCheckBox;
|
---|
23 | import javax.swing.JComponent;
|
---|
24 | import javax.swing.JDialog;
|
---|
25 | import javax.swing.JLabel;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JPasswordField;
|
---|
28 | import javax.swing.JTextField;
|
---|
29 | import javax.swing.KeyStroke;
|
---|
30 |
|
---|
31 | import org.openstreetmap.josm.Main;
|
---|
32 | import org.openstreetmap.josm.gui.JMultilineLabel;
|
---|
33 | import org.openstreetmap.josm.gui.SideButton;
|
---|
34 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
35 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
36 | import org.openstreetmap.josm.io.OsmApi;
|
---|
37 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
38 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
39 |
|
---|
40 | public class CredentialDialog extends JDialog {
|
---|
41 |
|
---|
42 | static public CredentialDialog getOsmApiCredentialDialog(String username, String password) {
|
---|
43 | CredentialDialog dialog = new CredentialDialog();
|
---|
44 | dialog.prepareForOsmApiCredentials(username, password);
|
---|
45 | dialog.pack();
|
---|
46 | return dialog;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static public CredentialDialog getHttpProxyCredentialDialog(String username, String password) {
|
---|
50 | CredentialDialog dialog = new CredentialDialog();
|
---|
51 | dialog.prepareForProxyCredentials(username, password);
|
---|
52 | dialog.pack();
|
---|
53 | return dialog;
|
---|
54 | }
|
---|
55 |
|
---|
56 | private boolean canceled;
|
---|
57 | private CredentialPanel pnlCredentials;
|
---|
58 |
|
---|
59 | public boolean isCanceled() {
|
---|
60 | return canceled;
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected void setCanceled(boolean canceled) {
|
---|
64 | this.canceled = canceled;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void setVisible(boolean visible) {
|
---|
69 | if (visible) {
|
---|
70 | WindowGeometry.centerInWindow(Main.parent, new Dimension(350,300)).apply(this);
|
---|
71 | }
|
---|
72 | super.setVisible(visible);
|
---|
73 | }
|
---|
74 |
|
---|
75 | protected JPanel createButtonPanel() {
|
---|
76 | JPanel pnl = new JPanel(new FlowLayout());
|
---|
77 | pnl.add(new SideButton(new OKAction()));
|
---|
78 | pnl.add(new SideButton(new CancelAction()));
|
---|
79 | pnl.add(new SideButton(new ContextSensitiveHelpAction(HelpUtil.ht("/Dialog/PasswordDialog"))));
|
---|
80 | return pnl;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected void build() {
|
---|
84 | getContentPane().setLayout(new BorderLayout());
|
---|
85 | getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
|
---|
86 |
|
---|
87 | addWindowListener(new WindowEventHander());
|
---|
88 | getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape");
|
---|
89 | getRootPane().getActionMap().put("escape", new CancelAction());
|
---|
90 |
|
---|
91 | getRootPane().setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
92 | }
|
---|
93 |
|
---|
94 | public CredentialDialog() {
|
---|
95 | setModal(true);
|
---|
96 | try {
|
---|
97 | setAlwaysOnTop(true);
|
---|
98 | } catch(SecurityException e) {
|
---|
99 | System.out.println(tr("Warning: failed to put Credential Dialog always on top. Caught security exception."));
|
---|
100 | }
|
---|
101 | build();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void prepareForOsmApiCredentials(String username, String password) {
|
---|
105 | setTitle(tr("Enter credentials for OSM API"));
|
---|
106 | getContentPane().add(pnlCredentials = new OsmApiCredentialsPanel(), BorderLayout.CENTER);
|
---|
107 | pnlCredentials.init(username, password);
|
---|
108 | validate();
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void prepareForProxyCredentials(String username, String password) {
|
---|
112 | setTitle(tr("Enter credentials for HTTP proxy"));
|
---|
113 | getContentPane().add(pnlCredentials = new HttpProxyCredentialsPanel(), BorderLayout.CENTER);
|
---|
114 | pnlCredentials.init(username, password);
|
---|
115 | validate();
|
---|
116 | }
|
---|
117 |
|
---|
118 | public String getUsername() {
|
---|
119 | if (pnlCredentials== null) return null;
|
---|
120 | return pnlCredentials.getUserName();
|
---|
121 | }
|
---|
122 |
|
---|
123 | public char[] getPassword() {
|
---|
124 | if (pnlCredentials== null) return null;
|
---|
125 | return pnlCredentials.getPassword();
|
---|
126 | }
|
---|
127 |
|
---|
128 | public boolean isSaveCredentials() {
|
---|
129 | if (pnlCredentials== null) return false;
|
---|
130 | return pnlCredentials.isSaveCredentials();
|
---|
131 | }
|
---|
132 |
|
---|
133 | private static class CredentialPanel extends JPanel {
|
---|
134 | protected JTextField tfUserName;
|
---|
135 | protected JPasswordField tfPassword;
|
---|
136 | protected JCheckBox cbSaveCredentials;
|
---|
137 | protected JMultilineLabel lblHeading;
|
---|
138 | protected JMultilineLabel lblWarning;
|
---|
139 |
|
---|
140 | protected void build() {
|
---|
141 | tfUserName = new JTextField(20);
|
---|
142 | tfPassword = new JPasswordField(20);
|
---|
143 | tfUserName.addFocusListener(new SelectAllOnFocusHandler());
|
---|
144 | tfPassword.addFocusListener(new SelectAllOnFocusHandler());
|
---|
145 | cbSaveCredentials = new JCheckBox(tr("Save user and password (unencrypted)"));
|
---|
146 |
|
---|
147 | setLayout(new GridBagLayout());
|
---|
148 | GridBagConstraints gc = new GridBagConstraints();
|
---|
149 | gc.gridwidth = 2;
|
---|
150 | gc.gridheight = 1;
|
---|
151 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
152 | gc.weightx = 1.0;
|
---|
153 | gc.weighty = 0.0;
|
---|
154 | gc.insets = new Insets(0,0,10,0);
|
---|
155 | add(lblHeading = new JMultilineLabel(""), gc);
|
---|
156 |
|
---|
157 | gc.gridx = 0;
|
---|
158 | gc.gridy = 1;
|
---|
159 | gc.gridwidth = 1;
|
---|
160 | gc.gridheight = 1;
|
---|
161 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
162 | gc.weightx = 0.0;
|
---|
163 | gc.weighty = 0.0;
|
---|
164 | gc.insets = new Insets(0,0,10,10);
|
---|
165 | add(new JLabel(tr("Username")), gc);
|
---|
166 | gc.gridx = 1;
|
---|
167 | gc.gridy = 1;
|
---|
168 | gc.weightx = 1.0;
|
---|
169 | add(tfUserName, gc);
|
---|
170 | gc.gridx = 0;
|
---|
171 | gc.gridy = 2;
|
---|
172 | gc.weightx = 0.0;
|
---|
173 | add(new JLabel(tr("Password")), gc);
|
---|
174 |
|
---|
175 | gc.gridx = 1;
|
---|
176 | gc.gridy = 2;
|
---|
177 | gc.weightx = 0.0;
|
---|
178 | add(tfPassword, gc);
|
---|
179 |
|
---|
180 | gc.gridx = 0;
|
---|
181 | gc.gridy = 3;
|
---|
182 | gc.gridwidth = 2;
|
---|
183 | gc.gridheight = 1;
|
---|
184 | gc.fill = GridBagConstraints.BOTH;
|
---|
185 | gc.weightx = 1.0;
|
---|
186 | gc.weighty = 0.0;
|
---|
187 | lblWarning = new JMultilineLabel("");
|
---|
188 | lblWarning.setFont(lblWarning.getFont().deriveFont(Font.ITALIC));
|
---|
189 | add(lblWarning, gc);
|
---|
190 |
|
---|
191 | gc.gridx = 0;
|
---|
192 | gc.gridy = 4;
|
---|
193 | gc.weighty = 0.0;
|
---|
194 | add(cbSaveCredentials, gc);
|
---|
195 |
|
---|
196 | // consume the remaining space
|
---|
197 | gc.gridx = 0;
|
---|
198 | gc.gridy = 5;
|
---|
199 | gc.weighty = 1.0;
|
---|
200 | add(new JPanel(),gc);
|
---|
201 |
|
---|
202 | }
|
---|
203 |
|
---|
204 | public CredentialPanel() {
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void init(String username, String password) {
|
---|
208 | username = username == null ? "" : username;
|
---|
209 | password = password == null ? "" : password;
|
---|
210 | tfUserName.setText(username);
|
---|
211 | tfPassword.setText(password);
|
---|
212 | cbSaveCredentials.setSelected(!username.equals("") && ! password.equals(""));
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void startUserInput() {
|
---|
216 | tfUserName.requestFocusInWindow();
|
---|
217 | }
|
---|
218 |
|
---|
219 | public String getUserName() {
|
---|
220 | return tfUserName.getText();
|
---|
221 | }
|
---|
222 |
|
---|
223 | public char[] getPassword() {
|
---|
224 | return tfPassword.getPassword();
|
---|
225 | }
|
---|
226 |
|
---|
227 | public boolean isSaveCredentials() {
|
---|
228 | return cbSaveCredentials.isSelected();
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | private static class OsmApiCredentialsPanel extends CredentialPanel {
|
---|
233 |
|
---|
234 | @Override
|
---|
235 | protected void build() {
|
---|
236 | super.build();
|
---|
237 | tfUserName.setToolTipText(tr("Please enter the user name of your OSM account"));
|
---|
238 | tfPassword.setToolTipText(tr("Please enter the password name of your OSM account"));
|
---|
239 | lblHeading.setText(
|
---|
240 | "<html>" + tr("Authenticating at the OSM API ''{0}'' failed. Please enter a valid username and a valid password.",
|
---|
241 | OsmApi.getOsmApi().getBaseUrl()) + "</html>");
|
---|
242 | lblWarning.setText(tr("Warning: The password is transferred unencrypted."));
|
---|
243 | }
|
---|
244 |
|
---|
245 | public OsmApiCredentialsPanel() {
|
---|
246 | build();
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | private static class HttpProxyCredentialsPanel extends CredentialPanel {
|
---|
251 | @Override
|
---|
252 | protected void build() {
|
---|
253 | super.build();
|
---|
254 | tfUserName.setToolTipText(tr("Please enter the user name for authenticating at your proxy server"));
|
---|
255 | tfPassword.setToolTipText(tr("Please enter the password for authenticating at your proxy server"));
|
---|
256 | lblHeading.setText(
|
---|
257 | "<html>" + tr("Authenticating at the HTTP proxy ''{0}'' failed. Please enter a valid username and a valid password.",
|
---|
258 | System.getProperty("http.proxyHost") + ":" + System.getProperty("http.proxyPort")) + "</html>");
|
---|
259 | lblWarning.setText("<html>" + tr("Warning: depending on the authentication method the proxy server uses the password may be transferred unencrypted.") + "</html>");
|
---|
260 | }
|
---|
261 |
|
---|
262 | public HttpProxyCredentialsPanel() {
|
---|
263 | build();
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | static private class SelectAllOnFocusHandler extends FocusAdapter {
|
---|
268 | @Override
|
---|
269 | public void focusGained(FocusEvent e) {
|
---|
270 | if (e.getSource() instanceof JTextField) {
|
---|
271 | JTextField tf = (JTextField)e.getSource();
|
---|
272 | tf.selectAll();
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | class OKAction extends AbstractAction {
|
---|
278 | public OKAction() {
|
---|
279 | putValue(NAME, tr("Authenticate"));
|
---|
280 | putValue(SHORT_DESCRIPTION, tr("Authenticate with the supplied username and password"));
|
---|
281 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void actionPerformed(ActionEvent arg0) {
|
---|
285 | setCanceled(false);
|
---|
286 | setVisible(false);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | class CancelAction extends AbstractAction {
|
---|
291 | public CancelAction() {
|
---|
292 | putValue(NAME, tr("Cancel"));
|
---|
293 | putValue(SHORT_DESCRIPTION, tr("Cancel authentication"));
|
---|
294 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
295 | }
|
---|
296 |
|
---|
297 | public void cancel() {
|
---|
298 | setCanceled(true);
|
---|
299 | setVisible(false);
|
---|
300 | }
|
---|
301 |
|
---|
302 | public void actionPerformed(ActionEvent arg0) {
|
---|
303 | cancel();
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | class WindowEventHander extends WindowAdapter {
|
---|
308 |
|
---|
309 | @Override
|
---|
310 | public void windowActivated(WindowEvent e) {
|
---|
311 | if (pnlCredentials != null) {
|
---|
312 | pnlCredentials.startUserInput();
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | @Override
|
---|
317 | public void windowClosing(WindowEvent e) {
|
---|
318 | new CancelAction().cancel();
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|