2 * WoTNS - WebInterface.java - Copyright © 2011 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.wotns.ui.web;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.io.UnsupportedEncodingException;
24 import java.util.ArrayList;
25 import java.util.List;
27 import net.pterodactylus.util.template.CollectionSortFilter;
28 import net.pterodactylus.util.template.HtmlFilter;
29 import net.pterodactylus.util.template.ReflectionAccessor;
30 import net.pterodactylus.util.template.Template;
31 import net.pterodactylus.util.template.TemplateContextFactory;
32 import net.pterodactylus.util.template.TemplateParser;
33 import net.pterodactylus.wotns.freenet.wot.Identity;
34 import net.pterodactylus.wotns.main.IdentityComparator;
35 import net.pterodactylus.wotns.main.WoTNSPlugin;
36 import net.pterodactylus.wotns.template.IdentityAccessor;
37 import net.pterodactylus.wotns.web.PageToadlet;
38 import net.pterodactylus.wotns.web.PageToadletFactory;
39 import freenet.clients.http.ToadletContainer;
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class WebInterface {
48 private final WoTNSPlugin wotNSPlugin;
50 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
52 /** The registered toadlets. */
53 private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
55 public WebInterface(WoTNSPlugin woTNSPlugin) {
56 this.wotNSPlugin = woTNSPlugin;
58 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
59 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
60 templateContextFactory.addFilter("html", new HtmlFilter());
61 CollectionSortFilter sortFilter = new CollectionSortFilter();
62 sortFilter.addComparator(Identity.class, IdentityComparator.NAME);
63 templateContextFactory.addFilter("sort", sortFilter);
70 public WoTNSPlugin getWoTNSPlugin() {
74 public TemplateContextFactory getTemplateContextFactory() {
75 return templateContextFactory;
94 private void registerToadlets() {
95 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
96 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
97 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
98 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
100 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
101 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
102 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
103 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
104 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
106 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
107 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
108 for (PageToadlet toadlet : pageToadlets) {
109 String menuName = toadlet.getMenuName();
110 if (menuName != null) {
111 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
113 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
119 * Unregisters all toadlets.
121 private void unregisterToadlets() {
122 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
123 for (PageToadlet pageToadlet : pageToadlets) {
124 toadletContainer.unregister(pageToadlet);
126 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
130 * Creates a {@link Reader} from the {@link InputStream} for the resource
131 * with the given name.
133 * @param resourceName
134 * The name of the resource
135 * @return A {@link Reader} for the resource
137 private Reader createReader(String resourceName) {
139 return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
140 } catch (UnsupportedEncodingException uee1) {