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