Add stylesheet.
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / ui / web / WebInterface.java
1 /*
2  * WoTNS - WebInterface.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.wotns.ui.web;
19
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;
26
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.util.web.StaticPage;
34 import net.pterodactylus.wotns.freenet.wot.Identity;
35 import net.pterodactylus.wotns.main.IdentityComparator;
36 import net.pterodactylus.wotns.main.WoTNSPlugin;
37 import net.pterodactylus.wotns.template.IdentityAccessor;
38 import net.pterodactylus.wotns.web.FreenetRequest;
39 import net.pterodactylus.wotns.web.PageToadlet;
40 import net.pterodactylus.wotns.web.PageToadletFactory;
41 import freenet.clients.http.ToadletContainer;
42
43 /**
44  * TODO
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class WebInterface {
49
50         private final WoTNSPlugin wotNSPlugin;
51
52         private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
53
54         /** The registered toadlets. */
55         private final List<PageToadlet> pageToadlets = new ArrayList<PageToadlet>();
56
57         public WebInterface(WoTNSPlugin woTNSPlugin) {
58                 this.wotNSPlugin = woTNSPlugin;
59
60                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
61                 templateContextFactory.addAccessor(Identity.class, new IdentityAccessor());
62                 templateContextFactory.addFilter("html", new HtmlFilter());
63                 CollectionSortFilter sortFilter = new CollectionSortFilter();
64                 sortFilter.addComparator(Identity.class, IdentityComparator.NAME);
65                 templateContextFactory.addFilter("sort", sortFilter);
66         }
67
68         //
69         // ACCESSORS
70         //
71
72         public WoTNSPlugin getWoTNSPlugin() {
73                 return wotNSPlugin;
74         }
75
76         public TemplateContextFactory getTemplateContextFactory() {
77                 return templateContextFactory;
78         }
79
80         //
81         // ACTIONS
82         //
83
84         public void start() {
85                 registerToadlets();
86         }
87
88         public void stop() {
89                 unregisterToadlets();
90         }
91
92         //
93         // PRIVATE METHODS
94         //
95
96         private void registerToadlets() {
97                 Template indexTemplate = TemplateParser.parse(createReader("/templates/index.html"));
98                 Template unknownTemplate = TemplateParser.parse(createReader("/templates/unknown.html"));
99                 Template manageTemplate = TemplateParser.parse(createReader("/templates/manage.html"));
100                 Template addTargetTemplate = TemplateParser.parse(createReader("/templates/addTarget.html"));
101
102                 PageToadletFactory pageToadletFactory = new PageToadletFactory(wotNSPlugin.getHighLevelSimpleClient(), "/tns/");
103                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ResolverPage(unknownTemplate, this, wotNSPlugin.getResolver())));
104                 pageToadlets.add(pageToadletFactory.createPageToadlet(new IndexPage(indexTemplate, this), "Index"));
105                 pageToadlets.add(pageToadletFactory.createPageToadlet(new ManagePage(manageTemplate, this)));
106                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EnableIdentityPage(new Template(), this)));
107                 pageToadlets.add(pageToadletFactory.createPageToadlet(new AddTargetPage(addTargetTemplate, this)));
108                 pageToadlets.add(pageToadletFactory.createPageToadlet(new EditTargetPage(new Template(), this)));
109                 pageToadlets.add(pageToadletFactory.createPageToadlet(new StaticPage<FreenetRequest>("css/", "/static/css/", "text/css")));
110
111                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
112                 toadletContainer.getPageMaker().addNavigationCategory("/tns/index.html", "Navigation.Menu.Name", "Navigation.Menu.Tooltip", wotNSPlugin);
113                 for (PageToadlet toadlet : pageToadlets) {
114                         String menuName = toadlet.getMenuName();
115                         if (menuName != null) {
116                                 toadletContainer.register(toadlet, "Navigation.Menu.Name", toadlet.path(), true, "Navigation.Menu.Item." + menuName + ".Name", "Navigation.Menu.Item." + menuName + ".Tooltip", false, toadlet);
117                         } else {
118                                 toadletContainer.register(toadlet, null, toadlet.path(), true, false);
119                         }
120                 }
121         }
122
123         /**
124          * Unregisters all toadlets.
125          */
126         private void unregisterToadlets() {
127                 ToadletContainer toadletContainer = wotNSPlugin.getToadletContainer();
128                 for (PageToadlet pageToadlet : pageToadlets) {
129                         toadletContainer.unregister(pageToadlet);
130                 }
131                 toadletContainer.getPageMaker().removeNavigationCategory("Navigation.Menu.Name");
132         }
133
134         /**
135          * Creates a {@link Reader} from the {@link InputStream} for the resource
136          * with the given name.
137          *
138          * @param resourceName
139          *            The name of the resource
140          * @return A {@link Reader} for the resource
141          */
142         private Reader createReader(String resourceName) {
143                 try {
144                         return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
145                 } catch (UnsupportedEncodingException uee1) {
146                         return null;
147                 }
148         }
149
150 }