(no commit message)
[utils] / wicket / components / src / main / java / org / wamblee / wicket / page / CompositePageBehavior.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.page;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.apache.wicket.markup.html.WebPage;
22 import org.apache.wicket.protocol.http.WebResponse;
23
24 /**
25  * Implements a composite page behavior which invokes all behaviors in the order
26  * they were added.
27  * 
28  * To use this in an arbitrary page, override <code>onBeforeRender()</code>,
29  * <code>setHeaders()</code> and <code>onAfterRender</code> in the page and
30  * delegate to the corresponding methods in this class.
31  * 
32  * @author Erik Brakkee
33  */
34 public class CompositePageBehavior implements PageBehavior {
35
36     private List<PageBehavior> behaviors;
37
38     /**
39      * Constructs the compositie.
40      */
41     public CompositePageBehavior() {
42         behaviors = new ArrayList<PageBehavior>();
43     }
44
45     /**
46      * Adds a behavior.
47      * 
48      * @param aBehavior
49      *            Behavior
50      */
51     public void add(PageBehavior aBehavior) {
52         behaviors.add(aBehavior);
53     }
54
55     @Override
56     public void onBeforeRender(WebPage aPage) {
57         for (PageBehavior behavior : behaviors) {
58             behavior.onBeforeRender(aPage);
59         }
60     }
61
62     @Override
63     public void setHeaders(WebPage aPage, WebResponse aResponse) {
64         for (PageBehavior behavior : behaviors) {
65             behavior.setHeaders(aPage, aResponse);
66         }
67     }
68
69     @Override
70     public void onAfterRender(WebPage aPage) {
71         for (PageBehavior behavior : behaviors) {
72             behavior.onAfterRender(aPage);
73         }
74     }
75
76     @Override
77     public void onDetach(WebPage aPage) {
78         for (PageBehavior behavior : behaviors) {
79             behavior.onDetach(aPage);
80         }
81     }
82
83 }