7a90c76484bcd5a1d67a9f007dffb79baf9d6859
[utils] / wicket / components / src / main / java / org / wamblee / wicket / page / WebApplicationBasePage.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 javax.persistence.EntityManager;
19
20 import org.apache.wicket.IPageMap;
21 import org.apache.wicket.PageParameters;
22 import org.apache.wicket.markup.html.WebPage;
23 import org.apache.wicket.model.IModel;
24 import org.apache.wicket.protocol.http.WebResponse;
25
26 /**
27  * Base page for wicket applications that allows customization of various
28  * bahaviors of the page. This page has no associated markup.
29  * 
30  * The utility methods {@link #disableCaching()}, {@link #expireImmediately()},
31  * and {@link #flushEntityManagerAfterRender(EntityManager)} are provide for
32  * quicly adding behaviors. Alternatively, the method
33  * {@link #addBehavior(PageBehavior)} can be used.
34  * 
35  * @author Erik Brakkee
36  * 
37  */
38 public class WebApplicationBasePage extends WebPage {
39
40     private CompositePageBehavior behaviors;
41
42     protected WebApplicationBasePage() {
43         super();
44         
45         setEnabled(false);
46         init();
47     }
48
49     protected WebApplicationBasePage(final IModel<?> model) {
50         super(model);
51         init();
52     }
53
54     protected WebApplicationBasePage(final IPageMap pageMap) {
55         super(pageMap);
56         init();
57     }
58
59     protected WebApplicationBasePage(final IPageMap pageMap,
60         final IModel<?> model) {
61         super(pageMap, model);
62         init();
63     }
64
65     protected WebApplicationBasePage(final PageParameters parameters) {
66         super(parameters);
67         init();
68     }
69
70     protected WebApplicationBasePage(final IPageMap pageMap,
71         final PageParameters parameters) {
72         super(pageMap, parameters);
73         init();
74     }
75
76     private void init() {
77         behaviors = new CompositePageBehavior();
78     }
79
80     /**
81      * Disables caching. This implies expiry of the page from the page map.
82      */
83     protected void disableCaching() {
84         addBehavior(new DisableCachingBehavior());
85     }
86
87     /**
88      * Expires the page immediately. Refresh in the browser will lead to an
89      * expired page.
90      */
91     protected void expireImmediately() {
92         addBehavior(new ExpirePageImmediatelyBehavior());
93     }
94
95     /**
96      * Flushes the entitymanager immedately after rendering to make sure that
97      * errors are caught early.
98      * 
99      * @param aEntityManager
100      *            Contextual reference to an entity manager.
101      */
102     protected void flushEntityManagerAfterRender(EntityManager aEntityManager) {
103         addBehavior(new FlushEntityManagerBehavior(aEntityManager));
104     }
105
106     /**
107      * Adds a specific behavior to the page.
108      * 
109      * @param aBehavior
110      *            Behavior to add.
111      */
112     public void addBehavior(PageBehavior aBehavior) {
113         behaviors.add(aBehavior);
114     }
115
116     @Override
117     protected void onBeforeRender() {
118         behaviors.onBeforeRender(this);
119         super.onBeforeRender();
120     }
121
122     @Override
123     protected void setHeaders(WebResponse aResponse) {
124         super.setHeaders(aResponse);
125         behaviors.setHeaders(this, aResponse);
126     }
127
128     @Override
129     protected void onAfterRender() {
130         super.onAfterRender();
131         behaviors.onAfterRender(this);
132     }
133     
134     @Override
135     protected void onDetach() {
136         super.onDetach();
137         behaviors.onDetach(this);
138     }
139 }