(no commit message)
authorErik Brakkee <erik@brakkee.org>
Fri, 27 Aug 2010 22:05:21 +0000 (22:05 +0000)
committerErik Brakkee <erik@brakkee.org>
Fri, 27 Aug 2010 22:05:21 +0000 (22:05 +0000)
wicket/components/src/main/java/org/wamblee/wicket/behavior/CompositeBehavior.java [new file with mode: 0644]
wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java [new file with mode: 0644]
wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java [new file with mode: 0644]

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;
+    }
+}
diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/LocalizedCompositeBehavior.java
new file mode 100644 (file)
index 0000000..e43e48e
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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 org.apache.wicket.Component;
+import org.apache.wicket.behavior.IBehavior;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.model.StringResourceModel;
+
+
+/**
+ * Composite behavior that deals with (optional) localization of 
+ * the behavior. It does this by creating localized behaviors in the bind callback.
+ * This is required because the component is need in order to obtain the resources to use 
+ * for localization. 
+ */
+public abstract class LocalizedCompositeBehavior extends CompositeBehavior {
+       private boolean localize; 
+       
+       public LocalizedCompositeBehavior(IBehavior[] aBehaviors, boolean aLocalize) {
+               super(aBehaviors);
+               localize = aLocalize; 
+       }
+       
+       @Override
+       public void bind(Component aComponent) {
+               super.bind(aComponent);
+               for (IBehavior behavior: createLocalizedBehaviors(aComponent) ) { 
+                       behavior.bind(aComponent); 
+                       add(behavior);
+               }
+       }
+       
+       public boolean isLocalized() {
+               return localize;
+       }
+       
+       /**
+        * Utility method to obtain a localized message for a given component. 
+        * @param aComponent Component.
+        * @param aMessage Message or message key in case localization is used.
+        * @return Localized message.
+        */
+       protected String getLocalizedMessage(Component aComponent, String aMessage) { 
+               if ( !localize ) { 
+                       return aMessage;
+               }
+               return new StringResourceModel(aMessage, aComponent, new Model(aMessage)).toString();
+       }
+
+       /**
+        * Callback to create further localized components. 
+        * @param aLocalize True iff localization must be used.
+        * @return Array of behaviors. 
+        */
+       protected abstract IBehavior[] createLocalizedBehaviors(Component aComponent);
+}
diff --git a/wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java b/wicket/components/src/main/java/org/wamblee/wicket/behavior/package-info.java
new file mode 100644 (file)
index 0000000..8d05fd6
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * This package contains general support utilities for behaviors.
+ */
+package org.wamblee.wicket.behavior;
+