changeset 15:db8940420ed8

InputDialog
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 02:00:41 +0200
parents cf7413989c65
children e0f0aaf75edd
files dwtx/jface/dialogs/IInputValidator.d dwtx/jface/dialogs/InputDialog.d
diffstat 2 files changed, 323 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/dialogs/IInputValidator.d	Mon Mar 31 02:00:41 2008 +0200
@@ -0,0 +1,34 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.dialogs.IInputValidator;
+
+import dwt.dwthelper.utils;
+
+/**
+ * The IInputValidator is the interface for simple validators.
+ * @see dwtx.jface.dialogs.InputDialog
+ */
+public interface IInputValidator {
+    /**
+     * Validates the given string.  Returns an error message to display
+     * if the new text is invalid.  Returns <code>null</code> if there
+     * is no error.  Note that the empty string is not treated the same
+     * as <code>null</code>; it indicates an error state but with no message
+     * to display.
+     *
+     * @param newText the text to check for validity
+     *
+     * @return an error message or <code>null</code> if no error
+     */
+    public String isValid(String newText);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/dialogs/InputDialog.d	Mon Mar 31 02:00:41 2008 +0200
@@ -0,0 +1,289 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.dialogs.InputDialog;
+
+import dwtx.jface.dialogs.IDialogConstants;
+import dwtx.jface.dialogs.Dialog;
+import dwtx.jface.dialogs.IInputValidator;
+
+import dwt.DWT;
+import dwt.events.ModifyEvent;
+import dwt.events.ModifyListener;
+import dwt.layout.GridData;
+import dwt.widgets.Button;
+import dwt.widgets.Composite;
+import dwt.widgets.Control;
+import dwt.widgets.Label;
+import dwt.widgets.Shell;
+import dwt.widgets.Text;
+import dwtx.jface.resource.StringConverter;
+
+import dwt.dwthelper.utils;
+
+/**
+ * A simple input dialog for soliciting an input string from the user.
+ * <p>
+ * This concrete dialog class can be instantiated as is, or further subclassed as
+ * required.
+ * </p>
+ */
+public class InputDialog : Dialog {
+    /**
+     * The title of the dialog.
+     */
+    private String title;
+
+    /**
+     * The message to display, or <code>null</code> if none.
+     */
+    private String message;
+
+    /**
+     * The input value; the empty string by default.
+     */
+    private String value = "";//$NON-NLS-1$
+
+    /**
+     * The input validator, or <code>null</code> 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;
+
+    /**
+     * Creates an input dialog with OK and Cancel buttons. Note that the dialog
+     * will have no visual representation (no widgets) until it is told to open.
+     * <p>
+     * Note that the <code>open</code> method blocks for input dialogs.
+     * </p>
+     *
+     * @param parentShell
+     *            the parent shell, or <code>null</code> to create a top-level
+     *            shell
+     * @param dialogTitle
+     *            the dialog title, or <code>null</code> if none
+     * @param dialogMessage
+     *            the dialog message, or <code>null</code> if none
+     * @param initialValue
+     *            the initial input value, or <code>null</code> if none
+     *            (equivalent to the empty string)
+     * @param validator
+     *            an input validator, or <code>null</code> if none
+     */
+    public this(Shell parentShell, String dialogTitle,
+            String dialogMessage, String initialValue, IInputValidator validator) {
+        super(parentShell);
+        this.title = dialogTitle;
+        message = dialogMessage;
+        if (initialValue is null) {
+            value = "";//$NON-NLS-1$
+        } else {
+            value = initialValue;
+        }
+        this.validator = validator;
+    }
+
+    /*
+     * (non-Javadoc) Method declared on Dialog.
+     */
+    protected void buttonPressed(int buttonId) {
+        if (buttonId is IDialogConstants.OK_ID) {
+            value = text.getText();
+        } else {
+            value = null;
+        }
+        super.buttonPressed(buttonId);
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.window.Window#configureShell(dwt.widgets.Shell)
+     */
+    protected void configureShell(Shell shell) {
+        super.configureShell(shell);
+        if (title !is null) {
+            shell.setText(title);
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see dwtx.jface.dialogs.Dialog#createButtonsForButtonBar(dwt.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 (value !is null) {
+            text.setText(value);
+            text.selectAll();
+        }
+    }
+
+    /*
+     * (non-Javadoc) Method declared on Dialog.
+     */
+    protected Control createDialogArea(Composite parent) {
+        // create composite
+        Composite composite = cast(Composite) super.createDialogArea(parent);
+        // create message
+        if (message !is null) {
+            Label label = new Label(composite, DWT.WRAP);
+            label.setText(message);
+            GridData data = new GridData(GridData.GRAB_HORIZONTAL
+                    | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
+                    | GridData.VERTICAL_ALIGN_CENTER);
+            data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
+            label.setLayoutData(data);
+            label.setFont(parent.getFont());
+        }
+        text = new Text(composite, DWT.SINGLE | DWT.BORDER);
+        text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
+                | GridData.HORIZONTAL_ALIGN_FILL));
+        text.addModifyListener(new class ModifyListener {
+            public void modifyText(ModifyEvent e) {
+                validateInput();
+            }
+        });
+        errorMessageText = new Text(composite, DWT.READ_ONLY | DWT.WRAP);
+        errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
+                | GridData.HORIZONTAL_ALIGN_FILL));
+        errorMessageText.setBackground(errorMessageText.getDisplay()
+                .getSystemColor(DWT.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;
+    }
+
+    /**
+     * 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 value;
+    }
+
+    /**
+     * Validates the input.
+     * <p>
+     * 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.
+     * </p>
+     */
+    protected void validateInput() {
+        String errorMessage = null;
+        if (validator !is 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 not <code>null</code>, the OK button is disabled.
+     *
+     * @param errorMessage
+     *            the error message, or <code>null</code> to clear
+     * @since 3.0
+     */
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+        if (errorMessageText !is null && !errorMessageText.isDisposed()) {
+            errorMessageText.setText(errorMessage is 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
+            bool hasError = errorMessage !is 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 !is null) {
+                button.setEnabled(errorMessage is null);
+            }
+        }
+    }
+}