Letting end-user confirm destructive actions

February 21st, 2008 by Taimo Peelo

Aranea 1.1 standard component chain enriches Environment with a context called ConfirmationContext. This can be used for executing some code conditionally, depending on user actions. Context interface is simple and consists of following methods:

public interface ConfirmationContext {
  void confirm(Closure onConfirmClosure, String message);
  String getConfirmationMessage();
}

When confirmation is registered (with confirm() method), rendering mechanism will present end-user with the browser standard message box and ask for confirmation of requested action. Depending on users choice, action encapsulated in onConfirmClosure either will get executed or not.

Combined with TransitionHandler, confirmation could be asked whenever the user performs navigation that would make active flow unreachable and flow contains data that has not yet been saved.

getFlowCtx().setTransitionHandler(
  new CancelConfirmingTransitionHandler(
     new ShouldConfirmOnUnsavedData(),
     "Some data not saved yet. Continue anyway?"
  )
);

Here CancelConfirmingTransitionHandler registers the confirmation whenever FlowContext.cancel() is called from active flow and Predicate ShouldConfirmOnUnsavedData evaluates to true. Only after user confirms the navigation event will flow transition actually be performed.

ConfirmationContext and TransitionHandlers together are a reliable and convenient way of preventing end-users shooting themselves in the foot :) .

2 Responses to “Letting end-user confirm destructive actions”

  1. Anton Says:

    Could you show an example on ConfirmationContext usage?

  2. Taimo Peelo Says:

    One will define the closure that gets executed when end-user confirms the action, the message user is presented with and call ConfirmationContext.confirm():

    class DropDataBaseClosure implements Closure, Serializable {
      public void execute(Object obj) {
        // drop database;
      }
    }

    And one would register it:

    ConfirmationContext ctx =
      getEnvironment().requireEntry(ConfirmationContext.class);
    ctx.confirm(new DropDataBaseClosure(), "Sure you want to drop database?");

    You could also look at CancelConfirmingTransitionHandler.doTransition() for an example.

Leave a Reply