1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jgit.awtui;
14
15 import java.awt.GridBagConstraints;
16 import java.awt.GridBagLayout;
17 import java.awt.Insets;
18
19 import javax.swing.JLabel;
20 import javax.swing.JOptionPane;
21 import javax.swing.JPanel;
22 import javax.swing.JPasswordField;
23 import javax.swing.JTextField;
24
25 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
26 import org.eclipse.jgit.transport.ChainingCredentialsProvider;
27 import org.eclipse.jgit.transport.CredentialItem;
28 import org.eclipse.jgit.transport.CredentialsProvider;
29 import org.eclipse.jgit.transport.NetRCCredentialsProvider;
30 import org.eclipse.jgit.transport.URIish;
31
32
33
34
35 public class AwtCredentialsProvider extends CredentialsProvider {
36
37
38
39 public static void install() {
40 final AwtCredentialsProvider c = new AwtCredentialsProvider();
41 CredentialsProvider cp = new ChainingCredentialsProvider(
42 new NetRCCredentialsProvider(), c);
43 CredentialsProvider.setDefault(cp);
44 }
45
46
47 @Override
48 public boolean isInteractive() {
49 return true;
50 }
51
52
53 @Override
54 public boolean supports(CredentialItem... items) {
55 for (CredentialItem i : items) {
56 if (i instanceof CredentialItem.StringType)
57 continue;
58
59 else if (i instanceof CredentialItem.CharArrayType)
60 continue;
61
62 else if (i instanceof CredentialItem.YesNoType)
63 continue;
64
65 else if (i instanceof CredentialItem.InformationalMessage)
66 continue;
67
68 else
69 return false;
70 }
71 return true;
72 }
73
74
75 @Override
76 public boolean get(URIish uri, CredentialItem... items)
77 throws UnsupportedCredentialItem {
78 switch (items.length) {
79 case 0:
80 return true;
81 case 1:
82 final CredentialItem item = items[0];
83
84 if (item instanceof CredentialItem.InformationalMessage) {
85 JOptionPane.showMessageDialog(null, item.getPromptText(),
86 UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
87 return true;
88
89 } else if (item instanceof CredentialItem.YesNoType) {
90 CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
91 int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
92 UIText.get().warning, JOptionPane.YES_NO_OPTION);
93 switch (r) {
94 case JOptionPane.YES_OPTION:
95 v.setValue(true);
96 return true;
97
98 case JOptionPane.NO_OPTION:
99 v.setValue(false);
100 return true;
101
102 case JOptionPane.CANCEL_OPTION:
103 case JOptionPane.CLOSED_OPTION:
104 default:
105 return false;
106 }
107
108 } else {
109 return interactive(uri, items);
110 }
111 default:
112 return interactive(uri, items);
113 }
114 }
115
116 private static boolean interactive(URIish uri, CredentialItem[] items) {
117 final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
118 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
119 new Insets(0, 0, 0, 0), 0, 0);
120 final JPanel panel = new JPanel();
121 panel.setLayout(new GridBagLayout());
122
123 final JTextField[] texts = new JTextField[items.length];
124 for (int i = 0; i < items.length; i++) {
125 CredentialItem item = items[i];
126
127 if (item instanceof CredentialItem.StringType
128 || item instanceof CredentialItem.CharArrayType) {
129 gbc.fill = GridBagConstraints.NONE;
130 gbc.gridwidth = GridBagConstraints.RELATIVE;
131 gbc.gridx = 0;
132 panel.add(new JLabel(item.getPromptText()), gbc);
133
134 gbc.fill = GridBagConstraints.HORIZONTAL;
135 gbc.gridwidth = GridBagConstraints.RELATIVE;
136 gbc.gridx = 1;
137 if (item.isValueSecure())
138 texts[i] = new JPasswordField(20);
139 else
140 texts[i] = new JTextField(20);
141 panel.add(texts[i], gbc);
142 gbc.gridy++;
143
144 } else if (item instanceof CredentialItem.InformationalMessage) {
145 gbc.fill = GridBagConstraints.NONE;
146 gbc.gridwidth = GridBagConstraints.REMAINDER;
147 gbc.gridx = 0;
148 panel.add(new JLabel(item.getPromptText()), gbc);
149 gbc.gridy++;
150
151 } else {
152 throw new UnsupportedCredentialItem(uri, item.getPromptText());
153 }
154 }
155
156 if (JOptionPane.showConfirmDialog(null, panel,
157 UIText.get().authenticationRequired,
158 JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
159 return false;
160
161 for (int i = 0; i < items.length; i++) {
162 CredentialItem item = items[i];
163 JTextField f = texts[i];
164
165 if (item instanceof CredentialItem.StringType) {
166 CredentialItem.StringType v = (CredentialItem.StringType) item;
167 if (f instanceof JPasswordField)
168 v.setValue(new String(((JPasswordField) f).getPassword()));
169 else
170 v.setValue(f.getText());
171
172 } else if (item instanceof CredentialItem.CharArrayType) {
173 CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
174 if (f instanceof JPasswordField)
175 v.setValueNoCopy(((JPasswordField) f).getPassword());
176 else
177 v.setValueNoCopy(f.getText().toCharArray());
178 }
179 }
180 return true;
181 }
182 }