Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: describe panel replacement approach

It's common to want the user to confirm his action if it would be hard to reverse, e.g. if he asked to delete something. The confirmation can be easily obtained via a javascript confirm dialog box. If you'd rather not use javascript, another approach is outlined below.

Using Javascript

Here's one way to do thisget confirmation, using Javascript:

Code Block
Link removeLink = new Link("removeLink") {
			@Override
			public void onClick() {
				// do something you want to confirm beforehand
			}
		};

removeLink.add( new SimpleAttributeModifier("onclick", "return confirm('are you sure?');"));

...

(The subclass could of course use SimpleAttributeModifier instead.)

Without Javascript

If you don't want to use javascript, you could bring up a separate web page with a confirmation form in it. But it's possible to display the confirmation request on the form page itself. The idea (due to Ed Eustace) is to use the wicket Component.replaceWith() method to replace some panel on the page with a confirmation panel.

First, here's the confirmation panel:

Code Block

public abstract class ConfirmDeletePanel extends Panel {
     public ConfirmDeletePanel(String id, String message) {
         super(id);
	 add(new Label("message", message));
	 add(new Link("confirm") {
	      @Override
	      public void onClick() {
	        onConfirm();
	      }
	    });
	 add(new Link("cancel") {
	      @Override
	      public void onClick() {
	        onCancel();
	      }
	    });
     }
     protected abstract void onCancel();
     protected abstract void onConfirm();
}

(The corresponding html should be pretty obvious.)

Now assume the link to be confirmed is inside a panel called MyPanel. The link has a onClick method that does the panel replacement:

Code Block

Link removeLink = new Link("removeLink") {
	@Override
	public void onClick() {
		MyPanel.this.replaceWith(new ConfirmDeletePanel(
				MyPanel.this.getId(), "are you sure") {

			@Override
			protected void onCancel() {
				this.replaceWith(MyPanel.this);
			}

			@Override
			protected void onConfirm() {
				// do something you want confirmed beforehand
				// .... then
				this.replaceWith(MyPanel.this);
			}
		});
	}

};