Help on creating a confirmation dialog

Updated code based on above discussion:



package com.vaadin.util;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Used to confirm events.
 * @author Joonas Lehtinen, Tommi Laukkanen
 */
public final class ConfirmationDialog extends Window implements Button.ClickListener {
    /** Confirmation dialog width. */
    private static final int CONFIRMATION_DIALOG_HEIGHT = 200;
    /** Confirmation dialog height. */
    private static final int CONFIRMATION_DIALOG_WIDTH = 320;
    /** Constant for 100 percent value used in dialog main layout width. */
    private static final int ONE_HUNDRED_PERCENT = 100;
    /** Serial version UID.*/
    private static final long serialVersionUID = 1L;
    /** The confirmation callback. */
    private final ConfirmationDialogCallback callback;
    /** Yes button. */
    private final Button okButton;
    /** No button. */
    private final Button cancelButton;

    /**
     * Constructor for configuring confirmation dialog.
     * @param caption the dialog caption.
     * @param question the question.
     * @param okLabel the Ok button label.
     * @param cancelLabel the cancel button label.
     * @param callback the callback.
     */
    public ConfirmationDialog(final String caption, final String question,
            final String okLabel, final String cancelLabel, final ConfirmationDialogCallback callback) {
        super(caption);
        setWidth(CONFIRMATION_DIALOG_WIDTH, ConfirmationDialog.UNITS_PIXELS);
        setHeight(CONFIRMATION_DIALOG_HEIGHT, ConfirmationDialog.UNITS_PIXELS);
        okButton = new Button(okLabel, this);
        cancelButton = new Button(cancelLabel, this);
        setModal(true);

        this.callback = callback;

        if (question != null) {
            addComponent(new Label(question));
        }

        final HorizontalLayout buttonLayout = new HorizontalLayout();
        buttonLayout.setSpacing(true);
        buttonLayout.addComponent(okButton);
        buttonLayout.addComponent(cancelButton);
        addComponent(buttonLayout);
        ((VerticalLayout) getContent()).setHeight(ONE_HUNDRED_PERCENT,
              ConfirmationDialog.UNITS_PERCENTAGE);
        ((VerticalLayout) getContent()).setComponentAlignment(buttonLayout, Alignment.BOTTOM_CENTER);
    }

    /**
     * Event handler for button clicks.
     * @param event the click event.
     */
    public void buttonClick(final ClickEvent event) {
        if (getParent() != null) {
            ((Window) getParent()).removeWindow(this);
        }
        callback.response(event.getSource() == okButton);
    }

    /**
     * Interface for confirmation dialog callbacks.
     */
    public interface ConfirmationDialogCallback {
        /**
         * The user response.
         * @param ok True if user clicked ok.
         */
        void response(boolean ok);
    }

}

Thanks. This is great. It needs one last small change, the internal interface ConfirmationDialogCallback needs to extend Serializable:

Took me a little while to figure out how to successfully implement it, so for others here’s an example:

    private void cancelAfterConfirming(final Object target) {
        class CancelDialogAction implements ConfirmationDialogCallback {

            private static final long serialVersionUID = -7613857455533797019L;

            @Override
            public void response(boolean ok) {
                if (ok) {
                    resetRow(target);
                }
            }
        }
        final ConfirmationDialog cd = new ConfirmationDialog("Really cancel?", "Your changes will be lost",
                "Lose Changes", "Keep Editing", new CancelDialogAction());
        this.getWindow().addWindow(cd);
        cd.bringToFront();
    }

This is a method within a Table which is why this.getWindow() works.

Jonathan

Hi all,

Just an update to those using the
ConfirmDialog add-on
. There was an problem with the older Vaadin 6.3 series that is now fixed in the 1.0.5 version. This is also compatible with Vaadin 6.4.x.

Read more:

vaadin.com/directory#addon/confirmdialog

It’s a pity this add-on does not support multiple options (say, 3 instead of just 2).

Based on the work of Sami Ekblad I’ve done a more JOptionPane - like Implementation, supporting yes/no/(cancel)/(ok) of JOptionPane

It’s not as polished as the add on. It uses some Swing classes. Please dont flame me for that. Packages are a litte strange because I use the classes in my framework.

If there’s a lot of response I could do it as add on but I don’t want to do the effort without need.

Bruno
12229.zip (3.1 KB)

Based on the work of Sami Ekblad I’ve done a more JOptionPane - like Implementation, supporting yes/no/(cancel)/(ok) of JOptionPane

It’s not as polished as the add on. It uses some Swing classes. Please dont flame me for that. Packages are a litte strange because I use the classes in my framework.

If there’s a lot of response I could do it as add on but I don’t want to do the effort without need.

Bruno
12230.zip (3.1 KB)

Hi.

The ConfirmDialog looks really nice. Great work!

But how to use it in the following context


public void deleteEntity(Object entity) {

    // useConfirm Dialog to ask user if he really want's to delete

  // if yes -> removeItem(entity);
  // if no -> do nothing

}

When I use the confirmDialog, in the onClose I can check for yes or no, but I can not remove the entity, because its outside the scope.
Is there any possibility to pass an object into the dialog?

Thanks for any help,
Horst

layout can not be cast to ordered layout …