(no commit message)
authorErik Brakkee <erik@brakkee.org>
Sat, 28 Aug 2010 19:33:23 +0000 (19:33 +0000)
committerErik Brakkee <erik@brakkee.org>
Sat, 28 Aug 2010 19:33:23 +0000 (19:33 +0000)
wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java [new file with mode: 0644]
wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryHeaderContributor.java [moved from wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryBehavior.java with 57% similarity]
wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryUtils.java [deleted file]

diff --git a/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/jquery/AbstractJQueryBehavior.java
new file mode 100644 (file)
index 0000000..14692bf
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2005-2010 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.wicket.jquery;
+
+import java.util.Arrays;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.Page;
+import org.apache.wicket.behavior.IBehavior;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.wamblee.wicket.behavior.CompositeBehavior;
+
+/**
+ * Abstract JQuery hehavior class that performs some useful basic behaviors for
+ * the behavior such as:
+ * <ul>
+ * <li>Creating a ready function which will be invoked for the component</li>
+ * <li>Checking that the component is not a page</li>
+ * <li>Creating a call to an intialization function from the ready handler using the component id </li>
+ * </ul>
+ * 
+ * @author Erik Brakkee
+ * 
+ */
+public class AbstractJQueryBehavior extends CompositeBehavior {
+
+    private Component component;
+    private String function;
+
+    /**
+     * Constructs the behavior.
+     * 
+     * @param aFunction
+     *            Ready function to be invoked.
+     * @param aBehaviors
+     *            Behaviors to add in addition to the basic JQuery stuff.
+     */
+    public AbstractJQueryBehavior(String aFunction, IBehavior... aBehaviors) {
+        super(getBehaviors(aBehaviors));
+        function = aFunction;
+    }
+
+    private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
+        IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
+        behaviors[0] = new JQueryHeaderContributor();
+        for (int i = 0; i < aBehaviors.length; i++) {
+            behaviors[i + 1] = aBehaviors[i];
+        }
+        return behaviors;
+    }
+
+    @Override
+    public void bind(Component aComponent) {
+        if (component != null) {
+            throw new IllegalStateException(
+                "this kind of handler cannot be attached to " +
+                    "multiple components; it is already attached to component " +
+                    component + ", but component " + aComponent +
+                    " wants to be attached too");
+        }
+        if (aComponent instanceof Page) {
+            throw new IllegalStateException(
+                "This behavior cannot be applied to a page: " + aComponent);
+        }
+        component = aComponent;
+        super.bind(aComponent);
+        aComponent.setOutputMarkupId(true);
+    }
+
+    @Override
+    public void renderHead(IHeaderResponse aResponse) {
+        super.renderHead(aResponse);
+        String jsString = createReadyFunction(function, component);
+        aResponse.renderJavascript(jsString, null);
+    }
+
+    /**
+     * Creates a jQuery ready handler that invokes a given javascript function
+     * with the id of a component.
+     * 
+     * @param aFunction
+     *            Javascript function to invoke.
+     * @param aComponent
+     *            Component to invoke the id for.
+     * @return
+     */
+    public static String createReadyFunction(String aFunction,
+        Component aComponent) {
+        if (!aComponent.getOutputMarkupId()) {
+            throw new IllegalStateException(
+                "The component " +
+                    aComponent +
+                    " does not have its markup id set so this ready handler will not have any effect");
+        }
+        StringBuffer js = new StringBuffer();
+        js.append("jQuery(function(){");
+        js.append("org.wamblee." + aFunction + "(\"#" +
+            aComponent.getMarkupId() + "\");");
+        js.append("});");
+        String jsString = js.toString();
+        return jsString;
+    }
+}
similarity index 57%
rename from wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryBehavior.java
rename to wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryHeaderContributor.java
index 0efab6fabdfde8a926776877acf8b20887e071f1..fb4cdf489d0d5c6c9d54bb242cb253bcf38d1d20 100644 (file)
@@ -17,6 +17,8 @@ package org.wamblee.wicket.jquery;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.IHeaderResponse;
 import org.apache.wicket.markup.html.JavascriptPackageResource;
 
 /**
@@ -24,17 +26,21 @@ import org.apache.wicket.markup.html.JavascriptPackageResource;
  * 
  * @author Erik Brakkee
  */
-public class JQueryBehavior extends HeaderContributor {
+public class JQueryHeaderContributor extends HeaderContributor {
 
     static final String JQUERY_DEPLOYMENT = "jquery-1.4.2.min.js";
     static final String JQUERY_DEVELOPMENT = "jquery-1.4.2.js";
+    static final String JQUERY_NOCONFLICT = "jquery-noconflict.js";
 
-    private static HeaderContributor CACHE;
+    private static HeaderContributor JQUERY_CONTRIBUTOR;
+    private static HeaderContributor JQUERY_NOCONFLICT_CONTRIBUTOR =
+        JavascriptPackageResource.getHeaderContribution(
+            JQueryHeaderContributor.class, JQUERY_NOCONFLICT);
 
     /**
      * Constructs the behavior.
      */
-    public JQueryBehavior() {
+    public JQueryHeaderContributor() {
         super(getContributor());
     }
     
@@ -42,15 +48,21 @@ public class JQueryBehavior extends HeaderContributor {
      * Resets the cached value of the header contribution. Used typically for test only.
      */
     public static void clear() { 
-        CACHE = null; 
+        JQUERY_CONTRIBUTOR = null;
     }
 
-    private static HeaderContributor getContributor() {
-        if (CACHE == null) {
-            CACHE = JavascriptPackageResource.getHeaderContribution(
-                JQueryBehavior.class, getJQueryJavascript());
+    private static IHeaderContributor getContributor() {
+        if (JQUERY_CONTRIBUTOR == null) {
+            JQUERY_CONTRIBUTOR = JavascriptPackageResource.getHeaderContribution(
+                JQueryHeaderContributor.class, getJQueryJavascript());
         }
-        return CACHE;
+        return new IHeaderContributor() {        
+            @Override
+            public void renderHead(IHeaderResponse aResponse) {
+                JQUERY_CONTRIBUTOR.renderHead(aResponse);
+                JQUERY_NOCONFLICT_CONTRIBUTOR.renderHead(aResponse);
+            }
+        };
     }
 
     private static String getJQueryJavascript() {
diff --git a/wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryUtils.java b/wicket/components/src/main/java/org/wamblee/wicket/jquery/JQueryUtils.java
deleted file mode 100644 (file)
index f67c751..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 2005-2010 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.wicket.jquery;
-
-import org.apache.wicket.Component;
-
-/**
- * JQueryUtilities to be used for creating JQuery javascript functions from java.
- * 
- * @author Erik Brakkee
- *
- */
-public class JQueryUtils {
-    
-    /**
-     * Creates a jQuery ready handler that invokes a given javascript function with the id of 
-     * a component.  
-     * @param aFunction Javascript function to invoke.
-     * @param aComponent Component to invoke the id for. 
-     * @return
-     */
-    public static String createReadyFunction(String aFunction, Component aComponent) {
-        if ( !aComponent.getOutputMarkupId()) { 
-            throw new IllegalStateException("The component " + aComponent + 
-                " does not have its markup id set so this ready handler will not have any effect");
-        }
-        StringBuffer js = new StringBuffer(); 
-        js.append("$(function(){");
-        js.append("org.wamblee." + aFunction + "(\"#" + aComponent.getMarkupId() + "\");");
-        js.append("});");
-        String jsString = js.toString();
-        return jsString;
-    }
-}