001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2006 Nathan Hamblen nathan@technically.us
004
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder.components;
020
021 import org.apache.wicket.markup.ComponentTag;
022 import org.apache.wicket.markup.html.WebPage;
023 import org.apache.wicket.markup.html.form.TextField;
024 import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
025 import org.apache.wicket.model.IModel;
026
027 /**
028 * TextField that can be told to focus itself on the next request. Works in conjunction with
029 * the onload handler.
030 */
031 public class FocusableTextField extends TextField {
032 private boolean wantsFocus = false;
033
034 /**
035 * @param id Wicket id
036 * @param model text field model
037 */
038 public FocusableTextField(String id, IModel model) {
039 super (id, model);
040 add(ScriptLink.headerContributor(FocusableTextField.class));
041 }
042
043 /**
044 * @param id Wicket id
045 */
046 public FocusableTextField(String id) {
047 this(id, (IModel) null);
048 }
049
050 /**
051 * Page parameter no longer necessary; use other constructor.
052 * @deprecated
053 */
054 public FocusableTextField(String id, IModel model, WebPage page) {
055 this(id, model);
056 }
057 /**
058 * Page parameter no longer necessary; use other constructor.
059 * @deprecated
060 */
061 public FocusableTextField(String id, WebPage page) {
062 this(id);
063 }
064
065 @Override
066 public void renderHead(HtmlHeaderContainer container) {
067 super.renderHead(container);
068 container.getHeaderResponse().renderOnLoadJavascript("initFocusableTextField();");
069 }
070
071 /**
072 * Request focus on next rendering.
073 */
074 public void requestFocus() {
075 wantsFocus = true;
076 }
077
078 /** Adds flagging id attribute if focus has been requested. */
079 @Override
080 protected void onComponentTag(ComponentTag tag) {
081 if (wantsFocus) {
082 tag.put("id", "focusMe");
083 wantsFocus = false;
084 }
085 super.onComponentTag(tag);
086 }
087 }