1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.awtui;
13
14 import java.awt.Container;
15 import java.awt.GridBagConstraints;
16 import java.awt.GridBagLayout;
17 import java.awt.Insets;
18 import java.net.PasswordAuthentication;
19
20 import javax.swing.JLabel;
21 import javax.swing.JOptionPane;
22 import javax.swing.JPanel;
23 import javax.swing.JPasswordField;
24 import javax.swing.JTextField;
25
26 import org.eclipse.jgit.util.CachedAuthenticator;
27
28
29
30
31 public class AwtAuthenticator extends CachedAuthenticator {
32
33
34
35 public static void install() {
36 setDefault(new AwtAuthenticator());
37 }
38
39
40 @Override
41 protected PasswordAuthentication promptPasswordAuthentication() {
42 final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
43 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
44 new Insets(0, 0, 0, 0), 0, 0);
45 final Container panel = new JPanel();
46 panel.setLayout(new GridBagLayout());
47
48 final StringBuilder instruction = new StringBuilder();
49 instruction.append(UIText.get().enterUsernameAndPasswordFor);
50 instruction.append(" ");
51 if (getRequestorType() == RequestorType.PROXY) {
52 instruction.append(getRequestorType());
53 instruction.append(" ");
54 instruction.append(getRequestingHost());
55 if (getRequestingPort() > 0) {
56 instruction.append(":");
57 instruction.append(getRequestingPort());
58 }
59 } else {
60 instruction.append(getRequestingURL());
61 }
62
63 gbc.weightx = 1.0;
64 gbc.gridwidth = GridBagConstraints.REMAINDER;
65 gbc.gridx = 0;
66 panel.add(new JLabel(instruction.toString()), gbc);
67 gbc.gridy++;
68
69 gbc.gridwidth = GridBagConstraints.RELATIVE;
70
71
72
73 final JTextField username;
74 gbc.fill = GridBagConstraints.NONE;
75 gbc.gridx = 0;
76 gbc.weightx = 1;
77 panel.add(new JLabel(UIText.get().username), gbc);
78
79 gbc.gridx = 1;
80 gbc.fill = GridBagConstraints.HORIZONTAL;
81 gbc.weighty = 1;
82 username = new JTextField(20);
83 panel.add(username, gbc);
84 gbc.gridy++;
85
86
87
88 final JPasswordField password;
89 gbc.fill = GridBagConstraints.NONE;
90 gbc.gridx = 0;
91 gbc.weightx = 1;
92 panel.add(new JLabel(UIText.get().password), gbc);
93
94 gbc.gridx = 1;
95 gbc.fill = GridBagConstraints.HORIZONTAL;
96 gbc.weighty = 1;
97 password = new JPasswordField(20);
98 panel.add(password, gbc);
99 gbc.gridy++;
100
101 if (JOptionPane.showConfirmDialog(null, panel,
102 UIText.get().authenticationRequired, JOptionPane.OK_CANCEL_OPTION,
103 JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
104 return new PasswordAuthentication(username.getText(), password
105 .getPassword());
106 }
107
108 return null;
109 }
110 }