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