package org.eparapher.rcp.dialog; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.IInputValidator; import org.eclipse.jface.resource.StringConverter; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eparapher.core.crypto.EPKeystoreManager; public class PINORPassphraseDialog extends Dialog { /** * Creates an PIN or passphrase dialog with OK and Cancel buttons. Note that the dialog * will have no visual representation (no widgets) until it is told to open. *
* Note that the open
method blocks for input dialogs.
*
null
to create a top-level
* shell
* @param dialogTitle
* the dialog title, or null
if none
* @param dialogMessage
* the dialog message, or null
if none
* @param initialValue
* the initial input value, or null
if none
* (equivalent to the empty string)
* @param validator
* an input validator, or null
if none
*/
public PINORPassphraseDialog() {
super(Display.getCurrent().getActiveShell());
pinORpassphrase = "";
if (EPKeystoreManager.isPKCS11Used()) {
title = "SmartCard Access";
message = "Enter your PIN :";
validator = new PINLengthValidator();
}else {
title = "Keystore Secret";
message = "Passphrase :";
validator = new PassphraseLengthValidator();
}
}
/**
* The title of the dialog.
*/
private String title;
/**
* The message to display, or null
if none.
*/
private String message;
/**
* The input value; the empty string by default.
*/
private String pinORpassphrase = "";//$NON-NLS-1$
/**
* The input validator, or null
if none.
*/
private IInputValidator validator;
/**
* Ok button widget.
*/
private Button okButton;
/**
* Input text widget.
*/
private Text text;
/**
* Error message label widget.
*/
private Text errorMessageText;
/**
* Error message string.
*/
private String errorMessage;
/*
* (non-Javadoc) Method declared on Dialog.
*/
protected void buttonPressed(int buttonId) {
if (buttonId == IDialogConstants.OK_ID) {
pinORpassphrase = text.getText();
} else {
pinORpassphrase = null;
}
super.buttonPressed(buttonId);
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
*/
protected void configureShell(Shell shell) {
super.configureShell(shell);
if (title != null) {
shell.setText(title);
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
*/
protected void createButtonsForButtonBar(Composite parent) {
// create OK and Cancel buttons by default
okButton = createButton(parent, IDialogConstants.OK_ID,
IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID,
IDialogConstants.CANCEL_LABEL, false);
//do this here because setting the text will set enablement on the ok
// button
text.setFocus();
if (pinORpassphrase != null) {
text.setText(pinORpassphrase);
text.selectAll();
}
}
/*
* (non-Javadoc) Method declared on Dialog.
*/
protected Control createDialogArea(Composite parent) {
// create composite
Composite composite = (Composite) super.createDialogArea(parent);
GridLayout l = new GridLayout(2, false);
l.marginBottom = 10;
l.marginTop = 10;
l.marginRight = 20;
l.marginLeft = 20;
l.horizontalSpacing = 5;
l.verticalSpacing = 5;
composite.setLayout( l );
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// create message
Label label = new Label(composite, SWT.WRAP);
label.setText(message);
label.setLayoutData(new GridData( GridData.HORIZONTAL_ALIGN_END ) );
//label.setFont(parent.getFont());
text = new Text(composite, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD );
resizeText();
text.setLayoutData(new GridData( GridData.FILL_HORIZONTAL) );
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
validateInput();
}
});
Label emptylabel = new Label(composite, SWT.WRAP);
errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
errorMessageText.setBackground(errorMessageText.getDisplay()
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
// Set the error message text
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
setErrorMessage(errorMessage);
applyDialogFont(composite);
return composite;
}
private void resizeText() {
//show 15 chars
int columns = 30;
if (EPKeystoreManager.isPKCS11Used())
columns = 8;
GC gc = new GC (text);
FontMetrics fm = gc.getFontMetrics ();
int width = columns * fm.getAverageCharWidth ();
int height = fm.getHeight ();
gc.dispose ();
text.setSize (text.computeSize (width, height));
}
/**
* Returns the error message label.
*
* @return the error message label
* @deprecated use setErrorMessage(String) instead
*/
protected Label getErrorMessageLabel() {
return null;
}
/**
* Returns the ok button.
*
* @return the ok button
*/
protected Button getOkButton() {
return okButton;
}
/**
* Returns the text area.
*
* @return the text area
*/
protected Text getText() {
return text;
}
/**
* Returns the validator.
*
* @return the validator
*/
protected IInputValidator getValidator() {
return validator;
}
/**
* Returns the string typed into this input dialog.
*
* @return the input string
*/
public String getValue() {
return pinORpassphrase;
}
/**
* Validates the input.
* * The default implementation of this framework method delegates the request * to the supplied input validator object; if it finds the input invalid, * the error message is displayed in the dialog's message line. This hook * method is called whenever the text changes in the input field. *
*/ protected void validateInput() { String errorMessage = null; if (validator != null) { errorMessage = validator.isValid(text.getText()); } // Bug 16256: important not to treat "" (blank error) the same as null // (no error) setErrorMessage(errorMessage); } /** * Sets or clears the error message. * If notnull
, the OK button is disabled.
*
* @param errorMessage
* the error message, or null
to clear
* @since 3.0
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
if (errorMessageText != null && !errorMessageText.isDisposed()) {
errorMessageText.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$
// Disable the error message text control if there is no error, or
// no error text (empty or whitespace only). Hide it also to avoid
// color change.
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281
boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
errorMessageText.setEnabled(hasError);
errorMessageText.setVisible(hasError);
errorMessageText.getParent().update();
// Access the ok button by id, in case clients have overridden button creation.
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
Control button = getButton(IDialogConstants.OK_ID);
if (button != null) {
button.setEnabled(errorMessage == null);
}
}
}
/**
* This class validates a String. It makes sure that the String is between 5 and 8
* characters
*/
class PassphraseLengthValidator implements IInputValidator {
/**
* Validates the String. Returns null for no error, or an error message
*
* @param newText the String to validate
* @return String
*/
public String isValid(String newText) {
int len = newText.length();
/*
// Determine if input is too short or too long
if (len < 4) return "Too short";
if (len > 12) return "Too long";
*/
// Input must be OK
return null;
}
}
/**
* This class validates a String. It makes sure that the String is
* between 5 and 8 characters
*/
class PINLengthValidator implements IInputValidator {
/**
* Validates the String. Returns null for no error, or an error
* message
*
* @param newText
* the String to validate
* @return String
*/
public String isValid(String newText) {
/*
int len = newText.length();
// Determine if input is too short or too long
if (len < 2) return "Too short";
if (len > 12) return "Too long";*/
// Input must be OK
return null;
}
}
}