(no commit message)
[utils] / wicket / components / src / main / java / org / wamblee / wicket / behavior / CompositeBehavior.java
diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/CompositeBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/CompositeBehavior.java
new file mode 100644 (file)
index 0000000..efd40b2
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * 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.behavior;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.wicket.Application;
+import org.apache.wicket.Component;
+import org.apache.wicket.Response;
+import org.apache.wicket.behavior.IBehavior;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.IHeaderResponse;
+import org.apache.wicket.markup.html.internal.HeaderResponse;
+
+/**
+ * Composite behavior that allows combining a sequence of behaviors in one. This
+ * behavior return true by default for {@link #getStatelessHint(Component)}. It
+ * also returns false by default for {@link #isTemporary()}.
+ */
+public class CompositeBehavior implements IBehavior, IHeaderContributor {
+
+    private List<IBehavior> behaviors;
+
+    /**
+     * Constructs the behvavior
+     * 
+     * @param aBehaviors
+     *            Array of behaviors.
+     */
+    public CompositeBehavior(IBehavior... aBehaviors) {
+        behaviors = new ArrayList<IBehavior>(Arrays.asList(aBehaviors));
+    }
+
+    /**
+     * Adds a behavior
+     * 
+     * @param aBehavior
+     *            Behavior.
+     */
+    public void add(IBehavior aBehavior) {
+        behaviors.add(aBehavior);
+    }
+
+    @Override
+    public void bind(Component aComponent) {
+        for (IBehavior behavior : behaviors) {
+            behavior.bind(aComponent);
+        }
+    }
+
+    @Override
+    public void detach(Component aComponent) {
+        for (IBehavior behavior : behaviors) {
+            behavior.detach(aComponent);
+        }
+    }
+
+    @Override
+    public void exception(Component aComponent, RuntimeException aException) {
+        for (IBehavior behavior : behaviors) {
+            behavior.exception(aComponent, aException);
+        }
+    }
+
+    @Override
+    public void onComponentTag(Component aComponent, ComponentTag aTag) {
+        for (IBehavior behavior : behaviors) {
+            behavior.onComponentTag(aComponent, aTag);
+        }
+    }
+
+    @Override
+    public void renderHead(IHeaderResponse aResponse) {
+        for (IBehavior behavior : behaviors) {
+            if (behavior instanceof IHeaderContributor) {
+                ((IHeaderContributor) behavior).renderHead(aResponse);
+            }
+        }
+    }
+
+    @Override
+    public void beforeRender(Component aComponent) {
+        for (IBehavior behavior : behaviors) {
+            behavior.beforeRender(aComponent);
+        }
+    }
+
+    @Override
+    public void afterRender(Component aComponent) {
+        for (IBehavior behavior : behaviors) {
+            behavior.afterRender(aComponent);
+        }
+    }
+
+    @Override
+    public boolean getStatelessHint(Component aComponent) {
+        return false;
+    }
+
+    @Override
+    public boolean isEnabled(Component aComponent) {
+        for (IBehavior behavior : behaviors) {
+            if (!behavior.isEnabled(aComponent)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    @Override
+    public boolean isTemporary() {
+        return false;
+    }
+}