0ea11828bf552fbd5deba77b687233be31319ade
[utils] / wicket / components / src / main / java / org / wamblee / wicket / behavior / PreselectionBehavior.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.wicket.behavior;
17
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;
25
26 /**
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.
34  * 
35  * @author Erik Brakkee
36  * 
37  */
38 public class PreselectionBehavior extends CompositeBehavior {
39
40     static final String PRESELECT_SCRIPT = "wamblee-preselect.js";
41
42     private static HeaderContributor CACHE = JavascriptPackageResource
43         .getHeaderContribution(PreselectionBehavior.class, PRESELECT_SCRIPT);
44
45     private Component component;
46
47     public PreselectionBehavior() {
48         super(new JQueryBehavior(), new NamespaceBehavior(), CACHE);
49     }
50
51     @Override
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");
59         }
60         if ( aComponent instanceof Page) { 
61             throw new IllegalStateException("This behavior cannot be applied to a page: " + aComponent);
62         }
63         component = aComponent;
64         super.bind(aComponent);
65         aComponent.setOutputMarkupId(true);
66     }
67
68     @Override
69     public void renderHead(IHeaderResponse aResponse) {
70         super.renderHead(aResponse);
71         String jsString = JQueryUtils.createReadyFunction("preselectSetup", component);
72         aResponse.renderJavascript(jsString, null);
73     }
74
75 }