View Javadoc

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       * Creates an PIN or passphrase dialog with OK and Cancel buttons. Note that the dialog
28       * will have no visual representation (no widgets) until it is told to open.
29       * <p>
30       * Note that the <code>open</code> method blocks for input dialogs.
31       * </p>
32       * 
33       * @param parentShell
34       *            the parent shell, or <code>null</code> to create a top-level
35       *            shell
36       * @param dialogTitle
37       *            the dialog title, or <code>null</code> if none
38       * @param dialogMessage
39       *            the dialog message, or <code>null</code> if none
40       * @param initialValue
41       *            the initial input value, or <code>null</code> if none
42       *            (equivalent to the empty string)
43       * @param validator
44       *            an input validator, or <code>null</code> if none
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       * The title of the dialog.
63       */
64      private String title;
65  
66      /**
67       * The message to display, or <code>null</code> if none.
68       */
69      private String message;
70  
71      /**
72       * The input value; the empty string by default.
73       */
74      private String pinORpassphrase = "";//$NON-NLS-1$
75  
76      /**
77       * The input validator, or <code>null</code> if none.
78       */
79      private IInputValidator validator;
80  
81      /**
82       * Ok button widget.
83       */
84      private Button okButton;
85  
86      /**
87       * Input text widget.
88       */
89      private Text text;
90  
91      /**
92       * Error message label widget.
93       */
94      private Text errorMessageText;
95      
96      /**
97       * Error message string.
98       */
99      private String errorMessage;
100 
101 
102     /*
103      * (non-Javadoc) Method declared on Dialog.
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      * (non-Javadoc)
116      * 
117      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
118      */
119     protected void configureShell(Shell shell) {
120         super.configureShell(shell);
121         if (title != null) {
122 			shell.setText(title);
123 		}
124     }
125 
126     /*
127      * (non-Javadoc)
128      * 
129      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
130      */
131     protected void createButtonsForButtonBar(Composite parent) {
132         // create OK and Cancel buttons by default
133         okButton = createButton(parent, IDialogConstants.OK_ID,
134                 IDialogConstants.OK_LABEL, true);
135         createButton(parent, IDialogConstants.CANCEL_ID,
136                 IDialogConstants.CANCEL_LABEL, false);
137         //do this here because setting the text will set enablement on the ok
138         // button
139         text.setFocus();
140         if (pinORpassphrase != null) {
141             text.setText(pinORpassphrase);
142             text.selectAll();
143         }
144     }
145 
146     /*
147      * (non-Javadoc) Method declared on Dialog.
148      */
149     protected Control createDialogArea(Composite parent) {
150         // create composite
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         // create message
163         Label label = new Label(composite, SWT.WRAP);
164         label.setText(message);
165         label.setLayoutData(new GridData( GridData.HORIZONTAL_ALIGN_END ) );
166         //label.setFont(parent.getFont());
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         // Set the error message text
182         // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
183         setErrorMessage(errorMessage);
184 
185         applyDialogFont(composite);
186         return composite;
187     }
188 
189     private void resizeText() {
190     	//show 15 chars
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      * Returns the error message label.
204      * 
205      * @return the error message label
206      * @deprecated use setErrorMessage(String) instead
207      */
208     protected Label getErrorMessageLabel() {
209         return null;
210     }
211 
212     /**
213      * Returns the ok button.
214      * 
215      * @return the ok button
216      */
217     protected Button getOkButton() {
218         return okButton;
219     }
220 
221     /**
222      * Returns the text area.
223      * 
224      * @return the text area
225      */
226     protected Text getText() {
227         return text;
228     }
229 
230     /**
231      * Returns the validator.
232      * 
233      * @return the validator
234      */
235     protected IInputValidator getValidator() {
236         return validator;
237     }
238 
239     /**
240      * Returns the string typed into this input dialog.
241      * 
242      * @return the input string
243      */
244     public String getValue() {
245         return pinORpassphrase;
246     }
247 
248     /**
249      * Validates the input.
250      * <p>
251      * The default implementation of this framework method delegates the request
252      * to the supplied input validator object; if it finds the input invalid,
253      * the error message is displayed in the dialog's message line. This hook
254      * method is called whenever the text changes in the input field.
255      * </p>
256      */
257     protected void validateInput() {
258         String errorMessage = null;
259         if (validator != null) {
260             errorMessage = validator.isValid(text.getText());
261         }
262         // Bug 16256: important not to treat "" (blank error) the same as null
263         // (no error)
264         setErrorMessage(errorMessage);
265     }
266 
267     /**
268      * Sets or clears the error message.
269      * If not <code>null</code>, the OK button is disabled.
270      * 
271      * @param errorMessage
272      *            the error message, or <code>null</code> to clear
273      * @since 3.0
274      */
275     public void setErrorMessage(String errorMessage) {
276     	this.errorMessage = errorMessage;
277     	if (errorMessageText != null && !errorMessageText.isDisposed()) {
278     		errorMessageText.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$
279     		// Disable the error message text control if there is no error, or
280     		// no error text (empty or whitespace only).  Hide it also to avoid
281     		// color change.
282     		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281
283     		boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
284     		errorMessageText.setEnabled(hasError);
285     		errorMessageText.setVisible(hasError);
286     		errorMessageText.getParent().update();
287     		// Access the ok button by id, in case clients have overridden button creation.
288     		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
289     		Control button = getButton(IDialogConstants.OK_ID);
290     		if (button != null) {
291     			button.setEnabled(errorMessage == null);
292     		}
293     	}
294     }
295 	/**
296 	 * This class validates a String. It makes sure that the String is between 5 and 8
297 	 * characters
298 	 */
299 	class PassphraseLengthValidator implements IInputValidator {
300 	  /**
301 	   * Validates the String. Returns null for no error, or an error message
302 	   * 
303 	   * @param newText the String to validate
304 	   * @return String
305 	   */
306 	  public String isValid(String newText) {
307 	    int len = newText.length();
308 /*
309 	    // Determine if input is too short or too long
310 	    if (len < 4) return "Too short";
311 	    if (len > 12) return "Too long";
312 */
313 	    // Input must be OK
314 	    return null;
315 	  }
316 	}
317 	/**
318 	 * This class validates a String. It makes sure that the String is
319 	 * between 5 and 8 characters
320 	 */
321 	class PINLengthValidator implements IInputValidator {
322 		  /**
323 			 * Validates the String. Returns null for no error, or an error
324 			 * message
325 			 * 
326 			 * @param newText
327 			 *            the String to validate
328 			 * @return String
329 			 */
330 		  public String isValid(String newText) {
331 			  /*
332 		    int len = newText.length();
333 			    // Determine if input is too short or too long
334 		    if (len < 2) return "Too short";
335 		    if (len > 12) return "Too long";*/
336 			    // Input must be OK
337 		    return null;
338 		  }
339 	}
340 }