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