public abstract class AjaxButton extends org.apache.wicket.ajax.markup.html.form.AjaxButton implements IJQueryWidget { private static final long serialVersionUID = 1L; private static final String METHOD = "button"; /** * Constructor * @param id the markup id */ public AjaxButton(String id) { super(id); } /** * Constructor * @param id the markup id * @param form the {@link Form} */ public AjaxButton(String id, Form<?> form) { super(id, form); } /** * Constructor * @param id the markup id * @param model the {@link IModel} */ public AjaxButton(String id, IModel<String> model) { super(id, model); } /** * Constructor * @param id the markup id * @param model the {@link IModel} * @param form the {@link Form} */ public AjaxButton(String id, IModel<String> model, Form<?> form) { super(id, model, form); } /** * Gets the icon being displayed in the button * @return null by default */ protected String getIcon() { return null; } // Events // @Override protected void onInitialize() { super.onInitialize(); this.add(JQueryWidget.newWidgetBehavior(this)); //cannot be in ctor as the markupId may be set manually afterward } @Override public void onConfigure(JQueryBehavior behavior) { if (this.getIcon() != null) { behavior.setOption("icons", String.format("{ primary: '%s' }", this.getIcon())); } } @Override public void onBeforeRender(JQueryBehavior behavior) { } @Override protected void onError(AjaxRequestTarget target, Form<?> form) { } // IJQueryWidget // @Override public JQueryBehavior newWidgetBehavior(String selector) { return new JQueryBehavior(selector, METHOD); } } |
public interface IJQueryWidget { /** * Called when the {@link IJQueryWidget} is about to be rendered. * * @param behavior the {@link JQueryBehavior} */ void onBeforeRender(JQueryBehavior behavior); /** * Called immediately after the onConfigure method in a behavior. Since this is before the rendering * cycle has begun, the behavior can modify the configuration of the component (i.e. {@link Options}) * * @param behavior the {@link JQueryBehavior} */ void onConfigure(JQueryBehavior behavior); /** * Gets a new {@link JQueryBehavior} that should be bound to the {@link IJQueryWidget} object. * @param selector the html selector (ie: '#myId') * @return the {@link JQueryBehavior} */ JQueryBehavior newWidgetBehavior(String selector); public static class JQueryWidget { /** * Helper method that returns the component's html selector (ie: '#myId'). * @param component the {@link Component} * @return the html selector */ public static String getSelector(Component component) { return "#" + component.setOutputMarkupId(true).getMarkupId(); } /** * Factory method that gets a new {@link JQueryBehavior} by invoking {@link IJQueryWidget#newWidgetBehavior(String)} on the supplied widget. * @param <T> the type * @param widget the {@link IJQueryWidget} object * @return the {@link JQueryBehavior} */ public static <T extends Component & IJQueryWidget> JQueryBehavior newWidgetBehavior(T widget) { return widget.newWidgetBehavior(JQueryWidget.getSelector(widget)); } /** * Factory method that gets a new {@link JQueryBehavior} by invoking {@link IJQueryWidget#newWidgetBehavior(String)} on the supplied widget. * @param widget object implementing the {@link IJQueryWidget} interface. * @param component the {@link Component} the selector will be calculated from. * @return the {@link JQueryBehavior} */ public static JQueryBehavior newWidgetBehavior(IJQueryWidget widget, Component component) { return widget.newWidgetBehavior(JQueryWidget.getSelector(component)); } /** * Factory method that gets a new {@link JQueryBehavior} by invoking {@link IJQueryWidget#newWidgetBehavior(String)} on the supplied widget. * @param widget object implementing the {@link IJQueryWidget} interface. * @param selector the html selector (ie: '#myId') * @return the {@link JQueryBehavior} */ public static JQueryBehavior newWidgetBehavior(IJQueryWidget widget, String selector) { return widget.newWidgetBehavior(selector); } } } |