From: David ‘Bombe’ Roden Date: Sat, 2 Apr 2011 09:08:07 +0000 (+0200) Subject: Allow pages to set titles per request. X-Git-Tag: 0.6^2~15 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=453ff369775ad37fb63a8dc632c8ebe4fb2c69aa Allow pages to set titles per request. --- diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index a04873d..a0f0a79 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -42,6 +42,9 @@ public class SoneTemplatePage extends TemplatePage { /** The Sone core. */ protected final WebInterface webInterface; + /** The page title l10n key. */ + private final String pageTitleKey; + /** Whether to require a login. */ private final boolean requireLogin; @@ -53,6 +56,21 @@ public class SoneTemplatePage extends TemplatePage { * The path of the page * @param template * The template to render + * @param webInterface + * The Sone web interface + */ + public SoneTemplatePage(String path, Template template, WebInterface webInterface) { + this(path, template, null, webInterface, false); + } + + /** + * Creates a new template page for Freetalk that does not require the user + * to be logged in. + * + * @param path + * The path of the page + * @param template + * The template to render * @param pageTitleKey * The l10n key of the page title * @param webInterface @@ -69,6 +87,22 @@ public class SoneTemplatePage extends TemplatePage { * The path of the page * @param template * The template to render + * @param webInterface + * The Sone web interface + * @param requireLogin + * Whether this page requires a login + */ + public SoneTemplatePage(String path, Template template, WebInterface webInterface, boolean requireLogin) { + this(path, template, null, webInterface, requireLogin); + } + + /** + * Creates a new template page for Freetalk. + * + * @param path + * The path of the page + * @param template + * The template to render * @param pageTitleKey * The l10n key of the page title * @param webInterface @@ -77,7 +111,8 @@ public class SoneTemplatePage extends TemplatePage { * Whether this page requires a login */ public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) { - super(path, webInterface.getTemplateContextFactory(), template, webInterface.getL10n(), pageTitleKey, "noPermission.html"); + super(path, webInterface.getTemplateContextFactory(), template, "noPermission.html"); + this.pageTitleKey = pageTitleKey; this.webInterface = webInterface; this.requireLogin = requireLogin; template.getInitialContext().set("webInterface", webInterface); @@ -163,6 +198,17 @@ public class SoneTemplatePage extends TemplatePage { * {@inheritDoc} */ @Override + protected String getPageTitle(Request request) { + if (pageTitleKey != null) { + return webInterface.getL10n().getString(pageTitleKey); + } + return ""; + } + + /** + * {@inheritDoc} + */ + @Override protected Collection getStyleSheets() { return Arrays.asList("css/sone.css"); } diff --git a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java index 6e0bebc..a6636e2 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java @@ -32,7 +32,6 @@ import freenet.clients.http.LinkEnabledCallback; import freenet.clients.http.PageMaker; import freenet.clients.http.PageNode; import freenet.clients.http.ToadletContext; -import freenet.l10n.BaseL10n; /** * Base class for all {@link Page}s that are rendered with {@link Template}s. @@ -53,12 +52,6 @@ public class TemplatePage implements Page, LinkEnabledCallback { /** The template to render. */ private final Template template; - /** The L10n handler. */ - private final BaseL10n l10n; - - /** The l10n key for the page title. */ - private final String pageTitleKey; - /** Where to redirect for invalid form passwords. */ private final String invalidFormPasswordRedirectTarget; @@ -71,20 +64,14 @@ public class TemplatePage implements Page, LinkEnabledCallback { * The template context factory * @param template * The template to render - * @param l10n - * The L10n handler - * @param pageTitleKey - * The l10n key of the title page * @param invalidFormPasswordRedirectTarget * The target to redirect to if a POST request does not contain * the correct form password */ - public TemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, BaseL10n l10n, String pageTitleKey, String invalidFormPasswordRedirectTarget) { + public TemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) { this.path = path; this.templateContextFactory = templateContextFactory; this.template = template; - this.l10n = l10n; - this.pageTitleKey = pageTitleKey; this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget; } @@ -97,6 +84,17 @@ public class TemplatePage implements Page, LinkEnabledCallback { } /** + * Returns the title of the page. + * + * @param request + * The request to serve + * @return The title of the page + */ + protected String getPageTitle(Request request) { + return null; + } + + /** * {@inheritDoc} */ @Override @@ -115,7 +113,7 @@ public class TemplatePage implements Page, LinkEnabledCallback { } } PageMaker pageMaker = toadletContext.getPageMaker(); - PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext); + PageNode pageNode = pageMaker.getPageNode(getPageTitle(request), toadletContext); for (String styleSheet : getStyleSheets()) { pageNode.addCustomStyleSheet(styleSheet); }