Revision history [back]

click to hide/show revision 1
initial version

asked 2011-12-18 08:42:22 +0800

pdavie gravatar image pdavie

Solved: Getting Confirm dialog messages

I'm sure many of you have had the same issue- using the confirm dialog. Up until now, I've generally designed my UI's not need a visual "Do you really want to do that?" popup.

There is no information (that I can find in the forums), well I came up with an elegant solution, other than embedding lots of boiler plate code everywhere, that is quite simple to implement and does NOT require changes to the ZK event thread.

The idea is simple: call the confirm dialog with an instance of an interface to call back. The code is below.

The interface:

package net.flowspace.zk;

public interface ConfirmResponse
   {
   public void onConfirmClick (String code, int button) ;
   }

As you can see, it's pretty simple. The reason for the 'code' part of the call, is that you can have multiple dialogs on one composer, so to tell them apart, each confirm query has a unique code, you test against in your implementing confirmResponse method.

The call to the confirm dialog looks like this:

Confirm Dialog Library Method:

/**
    * Because the ZK event queue is non-blocking by default we need to tell the receiver what confirm
    * question was answered, so for each confirm query a unique 'code' needs to be sent to the implementation
    * of ConfirmResponse.
    * 
    * @param code    - Unique code for the question being asked.
    * @param msg     - Text for the dialog box contents.
    * @param title   - Title for the confirm dialog popup window.
    * @param cnf     - Instance of ConfirmResponse to send results to.
    */
   public static void confirm(final String code, final String msg, final String title, final ConfirmResponse cnf)
      {

      try
         {
         Messagebox.show(msg, title, Messagebox.YES | Messagebox.NO, Messagebox.QUESTION, new EventListener()
            {
               public void onEvent(Event e)
                  {
                  if (null == cnf)
                     {
                     Dialogs.alert ("Internal Problem: ConfirmResponse cannot be null!") ;
                     return ;
                     }

                  if (Messagebox.ON_YES.equals(e.getName()))
                     {
                     cnf.onConfirmClick(code, Messagebox.YES) ;
                     }
                  else
                     if (Messagebox.ON_NO.equals(e.getName()))
                        {
                        cnf.onConfirmClick(code, Messagebox.NO) ;
                        }
                  }
            });

         }
      catch (InterruptedException e)
         {
         e.printStackTrace();
         }

      }

This way, if you put the code above in some library, mine is in a class called Dialogs, in your composer that implements ConfirmResponse you can simply call this:

Calling the static method:

...
   Dialogs.confirm ("DELTHING", "Are you sure you want to delete this thing?", "Delete", this) ;
   ...

Then in your composer you'd have a method like this:

Receiving the results of the call in your composer:

   @Override
   public void onConfirmClick(String code, int button)
      {

      if (code.equals("DELTHING") && button == Messagebox.YES)
         {
         // Delete your thing here.
         }

      }

I hope this is of some use to others. Have a great Christmas, or whatever you choose to celebrate. :-)

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More