AjaxButton

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);
        }
    }
}

转载于:https://my.oschina.net/u/1047983/blog/146596

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值