(no commit message)
[utils] / wicket / components / src / main / java / org / wamblee / wicket / jquery / AbstractJQueryBehavior.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.jquery;
17
18 import java.util.Arrays;
19
20 import org.apache.wicket.Component;
21 import org.apache.wicket.Page;
22 import org.apache.wicket.behavior.IBehavior;
23 import org.apache.wicket.markup.html.IHeaderResponse;
24 import org.wamblee.wicket.behavior.CompositeBehavior;
25
26 /**
27  * Abstract JQuery hehavior class that performs some useful basic behaviors for
28  * the behavior such as:
29  * <ul>
30  * <li>Creating a ready function which will be invoked for the component</li>
31  * <li>Checking that the component is not a page</li>
32  * <li>Creating a call to an intialization function from the ready handler using the component id </li>
33  * </ul>
34  * 
35  * @author Erik Brakkee
36  * 
37  */
38 public class AbstractJQueryBehavior extends CompositeBehavior {
39
40     private Component component;
41     private String function;
42
43     /**
44      * Constructs the behavior.
45      * 
46      * @param aFunction
47      *            Ready function to be invoked.
48      * @param aBehaviors
49      *            Behaviors to add in addition to the basic JQuery stuff.
50      */
51     public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
52         super(getBehaviors(aBehaviors));
53         function = aFunction;
54     }
55
56     private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
57         IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
58         behaviors[0] = new JQueryHeaderContributor();
59         for (int i = 0; i < aBehaviors.length; i++) {
60             behaviors[i + 1] = aBehaviors[i];
61         }
62         return behaviors;
63     }
64
65     @Override
66     public void bind(Component aComponent) {
67         if (component != null) {
68             throw new IllegalStateException(
69                 "this kind of handler cannot be attached to " +
70                     "multiple components; it is already attached to component " +
71                     component + ", but component " + aComponent +
72                     " wants to be attached too");
73         }
74         if (aComponent instanceof Page) {
75             throw new IllegalStateException(
76                 "This behavior cannot be applied to a page: " + aComponent);
77         }
78         component = aComponent;
79         super.bind(aComponent);
80         aComponent.setOutputMarkupId(true);
81     }
82
83     @Override
84     public void renderHead(IHeaderResponse aResponse) {
85         super.renderHead(aResponse);
86         String jsString = createReadyFunction(function, component);
87         aResponse.renderJavascript(jsString, null);
88     }
89
90     /**
91      * Creates a jQuery ready handler that invokes a given javascript function
92      * with the id of a component.
93      * 
94      * @param aFunction
95      *            Javascript function to invoke.
96      * @param aComponent
97      *            Component to invoke the id for.
98      * @return
99      */
100     public static String createReadyFunction(String aFunction,
101         Component aComponent) {
102         if (!aComponent.getOutputMarkupId()) {
103             throw new IllegalStateException(
104                 "The component " +
105                     aComponent +
106                     " does not have its markup id set so this ready handler will not have any effect");
107         }
108         StringBuffer js = new StringBuffer();
109         js.append("jQuery(function(){");
110         js.append("org.wamblee." + aFunction + "(\"#" +
111             aComponent.getMarkupId() + "\");");
112         js.append("});");
113         String jsString = js.toString();
114         return jsString;
115     }
116 }