b1d271346ffe605579333ed8cf7f72bd84cceecc
[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 makes it easy to write jQuery behaviors:
28  * <ul>
29  * <li>Creating a ready function which will be invoked for the component</li>
30  * <li>Checking that the component is not a page</li>
31  * <li>Ensuring tha the markup id of the component is output</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      *            Function to be invoked from the ready handler. This function is invoked with a 
48      *            CSS selector that identifies the component.
49      * @param aBehaviors
50      *            Behaviors to add in addition to the basic JQuery stuff.
51      */
52     public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
53         super(getBehaviors(aBehaviors));
54         function = aFunction;
55     }
56
57     private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
58         IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
59         behaviors[0] = new JQueryHeaderContributor();
60         for (int i = 0; i < aBehaviors.length; i++) {
61             behaviors[i + 1] = aBehaviors[i];
62         }
63         return behaviors;
64     }
65
66     @Override
67     public void bind(Component aComponent) {
68         if (component != null) {
69             throw new IllegalStateException(
70                 "this kind of handler cannot be attached to " +
71                     "multiple components; it is already attached to component " +
72                     component + ", but component " + aComponent +
73                     " wants to be attached too");
74         }
75         if (aComponent instanceof Page) {
76             throw new IllegalStateException(
77                 "This behavior cannot be applied to a page: " + aComponent);
78         }
79         component = aComponent;
80         super.bind(aComponent);
81         aComponent.setOutputMarkupId(true);
82     }
83
84     @Override
85     public void renderHead(IHeaderResponse aResponse) {
86         super.renderHead(aResponse);
87         String jsString = createReadyFunction(function, component);
88         aResponse.renderJavascript(jsString, null);
89     }
90
91     /**
92      * Creates a jQuery ready handler that invokes a given javascript function
93      * with the id of a component.
94      * 
95      * @param aFunction
96      *            Javascript function to invoke.
97      * @param aComponent
98      *            Component to invoke the id for.
99      * @return
100      */
101     public static String createReadyFunction(String aFunction,
102         Component aComponent) {
103         if (!aComponent.getOutputMarkupId()) {
104             throw new IllegalStateException(
105                 "The component " +
106                     aComponent +
107                     " does not have its markup id set so this ready handler will not have any effect");
108         }
109         StringBuffer js = new StringBuffer();
110         js.append("jQuery(function(){");
111         js.append("org.wamblee." + aFunction + "(\"#" +
112             aComponent.getMarkupId() + "\");");
113         js.append("});");
114         String jsString = js.toString();
115         return jsString;
116     }
117 }