001    package net.databinder.components;
002    
003    import org.apache.wicket.ResourceReference;
004    import org.apache.wicket.ajax.AjaxRequestTarget;
005    import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
006    import org.apache.wicket.markup.ComponentTag;
007    import org.apache.wicket.markup.html.IHeaderResponse;
008    
009    /*
010     * Databinder: a simple bridge from Wicket to Hibernate
011     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
012    
013     * This library is free software; you can redistribute it and/or
014     * modify it under the terms of the GNU Lesser General Public
015     * License as published by the Free Software Foundation; either
016     * version 2.1 of the License, or (at your option) any later version.
017     * 
018     * This library is distributed in the hope that it will be useful,
019     * but WITHOUT ANY WARRANTY; without even the implied warranty of
020     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
021     * Lesser General Public License for more details.
022     * 
023     * You should have received a copy of the GNU Lesser General Public
024     * License along with this library; if not, write to the Free Software
025     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
026     */
027    
028    /**
029     * Attaches itself to the onchange event for a TextField or TextArea, and enhances that
030     * event to fire not just when focus changes but also when keyboard input pauses. This
031     * is effected in JavaScript, with a timer that resets when the onkeyup event fires.
032     * @author Nathan Hamblen
033     *
034     */
035    public abstract class AjaxOnKeyPausedSubmitter extends AjaxFormSubmitBehavior {
036    
037            private static final ResourceReference JAVASCRIPT = new ResourceReference(
038                            AjaxOnKeyPausedSubmitter.class, "AjaxOnKeyPausedUpdater.js");
039    
040            /**
041             * Binds to onchange.
042             */
043            public AjaxOnKeyPausedSubmitter() {
044                    super("onchange");
045            }
046    
047            /**
048             * Adds needed JavaScript to header.
049             */
050            @Override
051            public void renderHead(IHeaderResponse response) {
052                    super.renderHead(response);
053                    response.renderJavascriptReference(JAVASCRIPT);
054            }
055    
056            /**
057             * Adds JavaScript listeners for onkeyup and onblur.
058             */
059            @Override
060            protected void onComponentTag(ComponentTag tag) {
061                    super.onComponentTag(tag);
062            tag.put("onkeyup", "AjaxOnKeyPausedTimerReset(this);");
063            tag.put("onblur", "AjaxOnKeyPausedTimerCancel();");
064            }
065    
066            /**
067             * Does nothing; override to respond to errors in the submitted form. (Submit on
068             * pause is probably not a good match for forms that need validation.)
069             */
070            @Override
071            protected void onError(AjaxRequestTarget target) {
072            }
073    }