(no commit message)
[utils] / wicket / components / src / main / java / org / wamblee / wicket / behavior / CompositeBehavior.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
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
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
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.
15  */
16 package org.wamblee.wicket.behavior;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.apache.wicket.Application;
23 import org.apache.wicket.Component;
24 import org.apache.wicket.Response;
25 import org.apache.wicket.behavior.IBehavior;
26 import org.apache.wicket.markup.ComponentTag;
27 import org.apache.wicket.markup.html.IHeaderContributor;
28 import org.apache.wicket.markup.html.IHeaderResponse;
29 import org.apache.wicket.markup.html.internal.HeaderResponse;
30
31 /**
32  * Composite behavior that allows combining a sequence of behaviors in one. This
33  * behavior return true by default for {@link #getStatelessHint(Component)}. It
34  * also returns false by default for {@link #isTemporary()}.
35  */
36 public class CompositeBehavior implements IBehavior, IHeaderContributor {
37
38     private List<IBehavior> behaviors;
39
40     /**
41      * Constructs the behvavior
42      * 
43      * @param aBehaviors
44      *            Array of behaviors.
45      */
46     public CompositeBehavior(IBehavior... aBehaviors) {
47         behaviors = new ArrayList<IBehavior>(Arrays.asList(aBehaviors));
48     }
49
50     /**
51      * Adds a behavior
52      * 
53      * @param aBehavior
54      *            Behavior.
55      */
56     public void add(IBehavior aBehavior) {
57         behaviors.add(aBehavior);
58     }
59
60     @Override
61     public void bind(Component aComponent) {
62         for (IBehavior behavior : behaviors) {
63             behavior.bind(aComponent);
64         }
65     }
66
67     @Override
68     public void detach(Component aComponent) {
69         for (IBehavior behavior : behaviors) {
70             behavior.detach(aComponent);
71         }
72     }
73
74     @Override
75     public void exception(Component aComponent, RuntimeException aException) {
76         for (IBehavior behavior : behaviors) {
77             behavior.exception(aComponent, aException);
78         }
79     }
80
81     @Override
82     public void onComponentTag(Component aComponent, ComponentTag aTag) {
83         for (IBehavior behavior : behaviors) {
84             behavior.onComponentTag(aComponent, aTag);
85         }
86     }
87
88     @Override
89     public void renderHead(IHeaderResponse aResponse) {
90         for (IBehavior behavior : behaviors) {
91             if (behavior instanceof IHeaderContributor) {
92                 ((IHeaderContributor) behavior).renderHead(aResponse);
93             }
94         }
95     }
96
97     @Override
98     public void beforeRender(Component aComponent) {
99         for (IBehavior behavior : behaviors) {
100             behavior.beforeRender(aComponent);
101         }
102     }
103
104     @Override
105     public void afterRender(Component aComponent) {
106         for (IBehavior behavior : behaviors) {
107             behavior.afterRender(aComponent);
108         }
109     }
110
111     @Override
112     public boolean getStatelessHint(Component aComponent) {
113         return false;
114     }
115
116     @Override
117     public boolean isEnabled(Component aComponent) {
118         for (IBehavior behavior : behaviors) {
119             if (!behavior.isEnabled(aComponent)) {
120                 return false;
121             }
122         }
123         return true;
124     }
125
126     @Override
127     public boolean isTemporary() {
128         return false;
129     }
130 }