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.jquery;
18 import java.util.Arrays;
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;
27 * Abstract JQuery hehavior class that performs some useful basic behaviors for
28 * the behavior such as:
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>
35 * @author Erik Brakkee
38 public class AbstractJQueryBehavior extends CompositeBehavior {
40 private Component component;
41 private String function;
44 * Constructs the behavior.
47 * Ready function to be invoked.
49 * Behaviors to add in addition to the basic JQuery stuff.
51 public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
52 super(getBehaviors(aBehaviors));
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];
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");
74 if (aComponent instanceof Page) {
75 throw new IllegalStateException(
76 "This behavior cannot be applied to a page: " + aComponent);
78 component = aComponent;
79 super.bind(aComponent);
80 aComponent.setOutputMarkupId(true);
84 public void renderHead(IHeaderResponse aResponse) {
85 super.renderHead(aResponse);
86 String jsString = createReadyFunction(function, component);
87 aResponse.renderJavascript(jsString, null);
91 * Creates a jQuery ready handler that invokes a given javascript function
92 * with the id of a component.
95 * Javascript function to invoke.
97 * Component to invoke the id for.
100 public static String createReadyFunction(String aFunction,
101 Component aComponent) {
102 if (!aComponent.getOutputMarkupId()) {
103 throw new IllegalStateException(
106 " does not have its markup id set so this ready handler will not have any effect");
108 StringBuffer js = new StringBuffer();
109 js.append("jQuery(function(){");
110 js.append("org.wamblee." + aFunction + "(\"#" +
111 aComponent.getMarkupId() + "\");");
113 String jsString = js.toString();