2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.wicket.behavior;
18 import org.apache.wicket.Component;
19 import org.apache.wicket.Page;
20 import org.apache.wicket.behavior.HeaderContributor;
21 import org.apache.wicket.markup.html.IHeaderResponse;
22 import org.apache.wicket.markup.html.JavascriptPackageResource;
23 import org.wamblee.wicket.jquery.JQueryBehavior;
24 import org.wamblee.wicket.jquery.JQueryUtils;
27 * Preselection behavior that preselects unaltered text in forms. When applied
28 * to a certain component, the behavior is valid for all children of the
29 * component. Therefore, it is possible to attach this behavior to any component,
30 * well as to a form or even individual fields in a form. It is not possible however to apply this
31 * behavior to a page. The behavior applies
32 * to text input fields and textareas. Upon focus, the text in the field is
33 * selected if it it still identical to the default value.
35 * @author Erik Brakkee
38 public class PreselectionBehavior extends CompositeBehavior {
40 static final String PRESELECT_SCRIPT = "wamblee-preselect.js";
42 private static HeaderContributor CACHE = JavascriptPackageResource
43 .getHeaderContribution(PreselectionBehavior.class, PRESELECT_SCRIPT);
45 private Component component;
47 public PreselectionBehavior() {
48 super(new JQueryBehavior(), new NamespaceBehavior(), CACHE);
52 public void bind(Component aComponent) {
53 if (component != null) {
54 throw new IllegalStateException(
55 "this kind of handler cannot be attached to " +
56 "multiple components; it is already attached to component " +
57 component + ", but component " + aComponent +
58 " wants to be attached too");
60 if ( aComponent instanceof Page) {
61 throw new IllegalStateException("This behavior cannot be applied to a page: " + aComponent);
63 component = aComponent;
64 super.bind(aComponent);
65 aComponent.setOutputMarkupId(true);
69 public void renderHead(IHeaderResponse aResponse) {
70 super.renderHead(aResponse);
71 String jsString = JQueryUtils.createReadyFunction("preselectSetup", component);
72 aResponse.renderJavascript(jsString, null);