I am in need for an ImageButton with AJAX fallback capability. I have created from AjaxFallbackButton a component AjaxFallbackImageButton that bases itself not on Button but on ImageButton. See below. So
far, this seems to work and maybe others want to use it, too.

Also: Have I overseen an easier way of accomplishing this? Wicket does not currently come with
an AjaxFallbackImageButton, and maybe there's a reason for that ...?

Regards,
Kaspar

--
import org.apache.wicket.Resource;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ImageButton;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.value.ValueMap;

/**
* An ajax submit image button that will degrade to a normal request if ajax is not available or
 * javascript is disabled.
 *
 * @author Jeremy Thomerson (jthomerson)
 * @author Alastair Maw
 */
public abstract class AjaxFallbackImageButton extends ImageButton
{
  private static final long serialVersionUID = 1L;

  private Form<?> mForm;

  /**
   * Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String).
   *
   * @param id
   * @param form
   */
  public AjaxFallbackImageButton(String id, Form<?> form)
  {
    super(id);
    initialize(form);
  }

  /**
* Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String, ResourceReference)}.
   *
   * @param id
   * @param resourceReference
   * @param form
   */
public AjaxFallbackImageButton(String id, ResourceReference resourceReference, Form<?> form)
  {
    super(id, resourceReference);
    initialize(form);
  }

  /**
* Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String, ResourceReference, ValueMap)}.
   *
   * @param id
   * @param resourceReference
   * @param resourceParameters
   * @param form
   */
public AjaxFallbackImageButton(String id, ResourceReference resourceReference, ValueMap resourceParameters,
      Form<?> form)
  {
    super(id, resourceReference);
    initialize(form);
  }

  /**
   * Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String, Resource).
   *
   * @param id
   * @param resource
   * @param form
   */
public AjaxFallbackImageButton(String id, Resource resource, Form<? > form)
  {
    super(id, resource);
    initialize(form);
  }

  /**
   * Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String, IModel)}.
   *
   * @param id
   * @param model
   * @param form
   */
public AjaxFallbackImageButton(String id, IModel<String> model, Form<?> form)
  {
    super(id, model);
    initialize(form);
  }

  /**
   * Construct, see [EMAIL PROTECTED] ImageButton#ImageButton(String, String)}.
   *
   * @param id
   * @param resourceReference
   * @param resourceParameters
   * @param form
   */
public AjaxFallbackImageButton(String id, String string, Form<?> form)
  {
    super(id, string);
    initialize(form);
  }

  private void initialize(Form<?> form)
  {
    mForm = form;

    add(new AjaxFormSubmitBehavior(form, "onclick")
    {
      private static final long serialVersionUID = 1L;

      @Override
      protected void onSubmit(AjaxRequestTarget target)
      {
AjaxFallbackImageButton.this.onSubmit(target, AjaxFallbackImageButton.this.getForm());
      }

      @Override
      protected void onError(AjaxRequestTarget target)
      {
AjaxFallbackImageButton.this.onError(target, AjaxFallbackImageButton.this.getForm());
      }

      @Override
      protected CharSequence getEventHandler()
      {
return new AppendingStringBuffer(super.getEventHandler()).append("; return false;");
      }

      @Override
      protected IAjaxCallDecorator getAjaxCallDecorator()
      {
        return AjaxFallbackImageButton.this.getAjaxCallDecorator();
      }
    });
  }

  /**
   * Listener method invoked on form submit with errors
   *
   * @param target
   * @param form
   *
   * TODO 1.3: Make abstract to be consistent with onsubmit()
   */
  protected void onError(AjaxRequestTarget target, Form<?> form)
  {
    // created to override
  }

  /**
* @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onSubmit()
   */
  @Override
  public final void onSubmit()
  {
if (!(getRequestCycle().getRequestTarget() instanceof AjaxRequestTarget))
    {
      onSubmit(null, getForm());
    }
  }

  /**
   *
   * @see org.apache.wicket.markup.html.form.Button#getForm()
   */
  @Override
  public Form<?> getForm()
  {
    return mForm == null ? super.getForm() : mForm;
  }

  /**
* Callback for the onClick event. If ajax failed and this event was generated via a normal
   * submission, the target argument will be null
   *
   * @param target
* ajax target if this linked was invoked using ajax, null otherwise
   * @param form
   */
protected abstract void onSubmit(final AjaxRequestTarget target, final Form<?> form);

  /**
   *
   * @return call decorator to use or null if none
   */
  protected IAjaxCallDecorator getAjaxCallDecorator()
  {
    return null;
  }

  /**
* Helper methods that both checks whether the link is enabled and whether the action ENABLE is
   * allowed.
   *
   * @return whether the link should be rendered as enabled
   */
  protected final boolean isButtonEnabled()
  {
    return isEnabled() && isEnableAllowed();
  }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to