Same styling of photo albums as with tapestry based application.
[photos] / src / main / java / org / wamblee / photos / wicket / BasePage.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.photos.wicket;
17
18 import javax.inject.Inject;
19 import javax.servlet.http.HttpServletRequest;
20
21 import org.apache.wicket.RedirectToUrlException;
22 import org.apache.wicket.markup.html.CSSPackageResource;
23 import org.apache.wicket.markup.html.WebPage;
24 import org.apache.wicket.markup.html.basic.Label;
25 import org.apache.wicket.markup.html.link.Link;
26 import org.apache.wicket.markup.html.panel.FeedbackPanel;
27 import org.apache.wicket.model.IModel;
28 import org.wamblee.wicket.behavior.TitleAttributeTooltipBehavior;
29 import org.wamblee.wicket.css.ResetCssBehavior;
30 import org.wamblee.wicket.page.ExpireBehavior;
31 import org.wamblee.wicket.page.WebApplicationBasePage;
32
33 public class BasePage extends WebApplicationBasePage {
34
35     @Inject
36     private HttpServletRequest request;
37
38     private boolean isExpired = false;
39
40     public BasePage() {
41         this(null);
42     }
43
44     public BasePage(IModel aModel) {
45         super(aModel);
46
47         if (request.getUserPrincipal() == null) {
48             redirectToLoginPage();
49         }
50
51         add(new ResetCssBehavior());
52         add(new TitleAttributeTooltipBehavior());
53         add(CSSPackageResource.getHeaderContribution(BasePage.class, "photos.css"));
54         disableCaching();
55
56         add(new FeedbackPanel("feedback"));
57         add(new Label("title", getTitle()));
58
59         addBehavior(new ExpireBehavior() {
60             @Override
61             protected boolean isExpired(WebPage aPage) {
62                 return isExpired;
63             }
64         });
65
66         add(new Link("logout") {
67             @Override
68             public void onClick() {
69                 getRequestCycle().getSession().invalidate();
70                 throw redirectToLoginPage();
71             }
72         });
73     }
74
75     private RedirectToUrlException redirectToLoginPage() {
76         return new RedirectToUrlException("login.jsp");
77     }
78
79     public void setExpired(boolean aExpired) {
80         isExpired = aExpired;
81     }
82
83     private String getTitle() {
84         String name = getClass().getSimpleName();
85         name = name.replaceAll("([A-Z]+)([A-Z][a-z])", "$1 $2");
86         name = name.replaceAll("[A-Z]+", " $0");
87         name = name.replaceAll("^ ", "");
88         name = name.replaceAll("  ", " ");
89         return name;
90     }
91 }