1 package org.eparapher.rcp.dialog;
2
3 import org.eclipse.jface.dialogs.Dialog;
4 import org.eclipse.jface.dialogs.IDialogConstants;
5 import org.eclipse.jface.dialogs.IInputValidator;
6 import org.eclipse.jface.resource.StringConverter;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.ModifyEvent;
9 import org.eclipse.swt.events.ModifyListener;
10 import org.eclipse.swt.graphics.FontMetrics;
11 import org.eclipse.swt.graphics.GC;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.swt.widgets.Text;
21 import org.eparapher.core.crypto.EPKeystoreManager;
22
23
24 public class PINORPassphraseDialog extends Dialog {
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public PINORPassphraseDialog() {
47 super(Display.getCurrent().getActiveShell());
48 pinORpassphrase = "";
49 if (EPKeystoreManager.isPKCS11Used()) {
50 title = "SmartCard Access";
51 message = "Enter your PIN :";
52 validator = new PINLengthValidator();
53 }else {
54 title = "Keystore Secret";
55 message = "Passphrase :";
56 validator = new PassphraseLengthValidator();
57 }
58
59 }
60
61
62
63
64 private String title;
65
66
67
68
69 private String message;
70
71
72
73
74 private String pinORpassphrase = "";
75
76
77
78
79 private IInputValidator validator;
80
81
82
83
84 private Button okButton;
85
86
87
88
89 private Text text;
90
91
92
93
94 private Text errorMessageText;
95
96
97
98
99 private String errorMessage;
100
101
102
103
104
105 protected void buttonPressed(int buttonId) {
106 if (buttonId == IDialogConstants.OK_ID) {
107 pinORpassphrase = text.getText();
108 } else {
109 pinORpassphrase = null;
110 }
111 super.buttonPressed(buttonId);
112 }
113
114
115
116
117
118
119 protected void configureShell(Shell shell) {
120 super.configureShell(shell);
121 if (title != null) {
122 shell.setText(title);
123 }
124 }
125
126
127
128
129
130
131 protected void createButtonsForButtonBar(Composite parent) {
132
133 okButton = createButton(parent, IDialogConstants.OK_ID,
134 IDialogConstants.OK_LABEL, true);
135 createButton(parent, IDialogConstants.CANCEL_ID,
136 IDialogConstants.CANCEL_LABEL, false);
137
138
139 text.setFocus();
140 if (pinORpassphrase != null) {
141 text.setText(pinORpassphrase);
142 text.selectAll();
143 }
144 }
145
146
147
148
149 protected Control createDialogArea(Composite parent) {
150
151 Composite composite = (Composite) super.createDialogArea(parent);
152 GridLayout l = new GridLayout(2, false);
153 l.marginBottom = 10;
154 l.marginTop = 10;
155 l.marginRight = 20;
156 l.marginLeft = 20;
157 l.horizontalSpacing = 5;
158 l.verticalSpacing = 5;
159 composite.setLayout( l );
160 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
161
162
163 Label label = new Label(composite, SWT.WRAP);
164 label.setText(message);
165 label.setLayoutData(new GridData( GridData.HORIZONTAL_ALIGN_END ) );
166
167
168 text = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD );
169 resizeText();
170 text.setLayoutData(new GridData( GridData.FILL_HORIZONTAL) );
171 text.addModifyListener(new ModifyListener() {
172 public void modifyText(ModifyEvent e) {
173 validateInput();
174 }
175 });
176 Label emptylabel = new Label(composite, SWT.WRAP);
177 errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
178 errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
179 errorMessageText.setBackground(errorMessageText.getDisplay()
180 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
181
182
183 setErrorMessage(errorMessage);
184
185 applyDialogFont(composite);
186 return composite;
187 }
188
189 private void resizeText() {
190
191 int columns = 30;
192 if (EPKeystoreManager.isPKCS11Used())
193 columns = 8;
194 GC gc = new GC (text);
195 FontMetrics fm = gc.getFontMetrics ();
196 int width = columns * fm.getAverageCharWidth ();
197 int height = fm.getHeight ();
198 gc.dispose ();
199 text.setSize (text.computeSize (width, height));
200 }
201
202
203
204
205
206
207
208 protected Label getErrorMessageLabel() {
209 return null;
210 }
211
212
213
214
215
216
217 protected Button getOkButton() {
218 return okButton;
219 }
220
221
222
223
224
225
226 protected Text getText() {
227 return text;
228 }
229
230
231
232
233
234
235 protected IInputValidator getValidator() {
236 return validator;
237 }
238
239
240
241
242
243
244 public String getValue() {
245 return pinORpassphrase;
246 }
247
248
249
250
251
252
253
254
255
256
257 protected void validateInput() {
258 String errorMessage = null;
259 if (validator != null) {
260 errorMessage = validator.isValid(text.getText());
261 }
262
263
264 setErrorMessage(errorMessage);
265 }
266
267
268
269
270
271
272
273
274
275 public void setErrorMessage(String errorMessage) {
276 this.errorMessage = errorMessage;
277 if (errorMessageText != null && !errorMessageText.isDisposed()) {
278 errorMessageText.setText(errorMessage == null ? " \n " : errorMessage);
279
280
281
282
283 boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
284 errorMessageText.setEnabled(hasError);
285 errorMessageText.setVisible(hasError);
286 errorMessageText.getParent().update();
287
288
289 Control button = getButton(IDialogConstants.OK_ID);
290 if (button != null) {
291 button.setEnabled(errorMessage == null);
292 }
293 }
294 }
295
296
297
298
299 class PassphraseLengthValidator implements IInputValidator {
300
301
302
303
304
305
306 public String isValid(String newText) {
307 int len = newText.length();
308
309
310
311
312
313
314 return null;
315 }
316 }
317
318
319
320
321 class PINLengthValidator implements IInputValidator {
322
323
324
325
326
327
328
329
330 public String isValid(String newText) {
331
332
333
334
335
336
337 return null;
338 }
339 }
340 }