Addition of tooltip behavior.
[utils] / wicket / components / src / main / java / org / wamblee / wicket / jquery / AbstractJQueryBehavior.java
index d49110dd3d14e929162d149d2e3ba950abea3b22..cdc72ab8b23629d283ef3bf81f21a8c44c3041fc 100644 (file)
@@ -29,22 +29,28 @@ import flexjson.JSONSerializer;
  * Abstract JQuery hehavior class that makes it easy to write jQuery behaviors:
  * <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>Ensuring tha the markup id of the component is output</li>
+ * <li>Checking whether or not the behavior may be attached to a page using
+ * {@link #isPageAllowed()}. By default, the behavior may be attached to
+ * pages. Behaviors that should not be allowed to be attached to pages should
+ * override this method.</li>
+ * <li>Ensuring that the markup id of the component is output</li>
  * <li>Creating a call to an intialization function from the ready handler using
  * the component id</li>
  * </ul>
  * 
- * The ready function will be invoked as part of a ready handler and will invoke 
- * a function with two arguments. The first is the id of the component and the second
- * is a configuration object. 
+ * The ready function will be invoked as part of a ready handler and will invoke
+ * a function with two arguments. The first is the selector of the component and
+ * the second is a configuration object. In case the behavior is attached to a
+ * component, a selector is used based on the unique markup id. When used on a
+ * page, the selector matches with the "body" of the page.
  * <p>
- * The second parameter is obtained through a call to {@link #getConfigurationJavascript()}.
+ * The second parameter is obtained through a call to
+ * {@link #getConfigurationJavascript()}.
  * 
  * 
  * @author Erik Brakkee
  * 
- * <ConfigType> 
+ *         <ConfigType>
  */
 public class AbstractJQueryBehavior<ConfigType> extends CompositeBehavior {
 
@@ -68,6 +74,15 @@ public class AbstractJQueryBehavior<ConfigType> extends CompositeBehavior {
         function = aFunction;
     }
 
+    /**
+     * Determines whether the behavior is allowed to be attached toa page.
+     * 
+     * @return True. 
+     */
+    protected boolean isPageAllowed() {
+        return true;
+    }
+
     private static IBehavior[] getBehaviors(IBehavior[] aBehaviors) {
         IBehavior[] behaviors = new IBehavior[aBehaviors.length + 1];
         behaviors[0] = new JQueryHeaderContributor();
@@ -86,13 +101,15 @@ public class AbstractJQueryBehavior<ConfigType> extends CompositeBehavior {
                     component + ", but component " + aComponent +
                     " wants to be attached too");
         }
-        if (aComponent instanceof Page) {
+        if (!isPageAllowed() && aComponent instanceof Page) {
             throw new IllegalStateException(
                 "This behavior cannot be applied to a page: " + aComponent);
         }
         component = aComponent;
         super.bind(aComponent);
-        aComponent.setOutputMarkupId(true);
+        if (!(aComponent instanceof Page)) {
+            aComponent.setOutputMarkupId(true);
+        }
     }
 
     @Override
@@ -115,7 +132,7 @@ public class AbstractJQueryBehavior<ConfigType> extends CompositeBehavior {
      * @return
      */
     String createReadyFunction() {
-        if (!component.getOutputMarkupId()) {
+        if (!(component instanceof Page) && !component.getOutputMarkupId()) {
             throw new IllegalStateException(
                 "The component " +
                     component +
@@ -124,7 +141,11 @@ public class AbstractJQueryBehavior<ConfigType> extends CompositeBehavior {
         StringBuffer js = new StringBuffer();
         js.append("jQuery(function(){");
         js.append(function + "(");
-        js.append("\"#" + component.getMarkupId() + "\"");
+        String selector = "body";
+        if (!(component instanceof Page)) {
+            selector = "#" + component.getMarkupId();
+        }
+        js.append("\"" + selector + "\"");
         String config = getConfigurationJavascript();
         if (config != null) {
             js.append(",");