Author: saces
Date: 2008-08-20 17:58:52 +0000 (Wed, 20 Aug 2008)
New Revision: 22053
Modified:
trunk/freenet/src/freenet/clients/http/PageMaker.java
trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
trunk/freenet/src/freenet/clients/http/ToadletContainer.java
Log:
make css themes enum
plugin look&feel integration
Modified: trunk/freenet/src/freenet/clients/http/PageMaker.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/PageMaker.java 2008-08-20
17:27:15 UTC (rev 22052)
+++ trunk/freenet/src/freenet/clients/http/PageMaker.java 2008-08-20
17:58:52 UTC (rev 22053)
@@ -2,18 +2,12 @@
import java.io.File;
import java.io.IOException;
-import java.net.JarURLConnection;
import java.net.URL;
-import java.net.URLConnection;
-import java.net.URLDecoder;
import java.util.ArrayList;
-import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
import freenet.l10n.L10n;
import freenet.node.NodeClientCore;
@@ -27,10 +21,48 @@
*/
public final class PageMaker {
- public static final String DEFAULT_THEME = "clean";
+ public enum THEME {
+ BOXED("boxed", "Boxed", ""),
+ CLEAN("clean", "Clean", "Mr. Proper"),
+ GRAYANDBLUE("grayandblue", "Gray And Blue", ""),
+ SKY("sky", "Sky", "");
+
+ public static final String[] possibleValues = {
+ BOXED.code,
+ CLEAN.code,
+ GRAYANDBLUE.code,
+ SKY.code
+ };
+
+ public final String code; // the internal name
+ public final String name; // the name in "human form"
+ public final String description; // description
+
+ private THEME(String code, String name, String description) {
+ this.code = code;
+ this.name = name;
+ this.description = description;
+ }
+
+ public static THEME themeFromName(String cssName) {
+ for(THEME t : THEME.values()) {
+ if(t.code.equalsIgnoreCase(cssName) ||
+ t.name.equalsIgnoreCase(cssName))
+ {
+ return t;
+ }
+ }
+ return getDefault();
+ }
+
+ public static THEME getDefault() {
+ return THEME.CLEAN;
+ }
+ }
+
public static final int MODE_SIMPLE = 1;
public static final int MODE_ADVANCED = 2;
- private String theme;
+ private THEME theme;
private File override;
private final List navigationLinkTexts = new ArrayList();
private final List navigationLinkTextsNonFull = new ArrayList();
@@ -40,18 +72,16 @@
private final Map/*<HTMLNode, HTMLNode>*/ headNodes = new HashMap();
private final Map /* <String, LinkEnabledCallback> */
navigationLinkCallbacks = new HashMap();
- /** Cache for themes read from the JAR file. */
- private List jarThemesCache = null;
private final FredPluginL10n plugin;
private final boolean pluginMode;
- public PageMaker(FredPluginL10n plug, String t) {
+ public PageMaker(FredPluginL10n plug, THEME t) {
setTheme(t);
plugin = plug;
pluginMode = true;
}
- public PageMaker(String t) {
+ public PageMaker(THEME t) {
setTheme(t);
plugin = null;
pluginMode = false;
@@ -61,15 +91,15 @@
this.override = f;
}
- public void setTheme(String theme) {
- if (theme == null) {
- this.theme = DEFAULT_THEME;
+ public void setTheme(THEME theme2) {
+ if (theme2 == null) {
+ this.theme = THEME.getDefault();
} else {
- URL themeurl =
getClass().getResource("staticfiles/themes/" + theme + "/theme.css");
+ URL themeurl =
getClass().getResource("staticfiles/themes/" + theme2.code + "/theme.css");
if (themeurl == null)
- this.theme = DEFAULT_THEME;
+ this.theme = THEME.getDefault();
else
- this.theme = theme;
+ this.theme = theme2;
}
}
@@ -111,12 +141,11 @@
headNode.addChild("meta", new String[] { "http-equiv",
"content" }, new String[] { "Content-Type", "text/html; charset=utf-8" });
headNode.addChild("title", title + " - Freenet");
if(override == null)
- headNode.addChild("link", new String[] { "rel", "href",
"type", "title" }, new String[] { "stylesheet", "/static/themes/" + theme +
"/theme.css", "text/css", theme });
+ headNode.addChild("link", new String[] { "rel", "href",
"type", "title" }, new String[] { "stylesheet", "/static/themes/" + theme.code
+ "/theme.css", "text/css", theme.code });
else
headNode.addChild(getOverrideContent());
- List themes = getThemes();
- for (Iterator themesIterator = themes.iterator();
themesIterator.hasNext();) {
- String themeName = (String) themesIterator.next();
+ for (THEME t: THEME.values()) {
+ String themeName = t.code;
headNode.addChild("link", new String[] { "rel", "href",
"type", "media", "title" }, new String[] { "alternate stylesheet",
"/static/themes/" + themeName + "/theme.css", "text/css", "screen", themeName
});
}
@@ -209,63 +238,6 @@
return infobox;
}
- /**
- * Returns an {@link ArrayList} containing the names of all available
- * themes. If Freenet was started from a JAR file the list is cached
- * (because the JAR file only changes between invocations), otherwise
the
- * filesystem is read on every page access.
- *
- * @return An {@link ArrayList} containing the names of all available
themes
- */
- public List<String> getThemes() {
- if (jarThemesCache != null) {
- return jarThemesCache;
- }
- List<String> themes = new ArrayList<String>();
- try {
- URL url = getClass().getResource("staticfiles/themes/");
- URLConnection urlConnection = url.openConnection();
- if (url.getProtocol().equals("file")) {
- File themesDirectory = new
File(URLDecoder.decode(url.getPath(), "ISO-8859-1").replaceAll("\\|", ":"));
- File[] themeDirectories =
themesDirectory.listFiles();
- for (int themeIndex = 0; (themeDirectories !=
null) && (themeIndex < themeDirectories.length); themeIndex++) {
- File themeDirectory =
themeDirectories[themeIndex];
- if (themeDirectory.isDirectory() &&
!themeDirectory.getName().startsWith(".")) {
-
themes.add(themeDirectory.getName());
- }
- }
- } else if (urlConnection instanceof JarURLConnection) {
- JarURLConnection jarUrlConnection =
(JarURLConnection) urlConnection;
- JarFile jarFile = jarUrlConnection.getJarFile();
- Enumeration entries = jarFile.entries();
- while (entries.hasMoreElements()) {
- JarEntry entry = (JarEntry)
entries.nextElement();
- String name = entry.getName();
- if
(name.startsWith("freenet/clients/http/staticfiles/themes/")) {
- name =
name.substring("freenet/clients/http/staticfiles/themes/".length());
- if (name.indexOf('/') != -1) {
- String themeName =
name.substring(0, name.indexOf('/'));
- if
(!themes.contains(themeName)) {
-
themes.add(themeName);
- }
- }
- }
- }
- jarThemesCache = themes;
- }
- } catch (IOException ioe1) {
- Logger.error(this, "error creating list of themes",
ioe1);
- } catch (NullPointerException npe) {
- Logger.error(this, "error creating list of themes (if
you're using the gnu-classpath, it's \"normal\")", npe);
- themes.add("clean");
- } finally {
- if (!themes.contains("clean")) {
- themes.add("clean");
- }
- }
- return themes;
- }
-
private HTMLNode getOverrideContent() {
HTMLNode result = new HTMLNode("style", "type", "text/css");
Modified: trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
2008-08-20 17:27:15 UTC (rev 22052)
+++ trunk/freenet/src/freenet/clients/http/SimpleToadletServer.java
2008-08-20 17:58:52 UTC (rev 22053)
@@ -23,6 +23,7 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
+import freenet.clients.http.PageMaker.THEME;
import freenet.clients.http.bookmark.BookmarkManager;
import freenet.config.EnumerableOptionCallback;
import freenet.config.InvalidConfigValueException;
@@ -63,7 +64,7 @@
BucketFactory bf;
NetworkInterface networkInterface;
private final LinkedList toadlets;
- private String cssName;
+ private THEME cssTheme;
private File cssOverride;
private Thread myThread;
private boolean advancedModeEnabled;
@@ -165,14 +166,16 @@
class FProxyCSSNameCallback extends StringCallback implements
EnumerableOptionCallback {
public String get() {
- return cssName;
+ return cssTheme.code;
}
public void set(String CSSName) throws
InvalidConfigValueException {
if((CSSName.indexOf(':') != -1) ||
(CSSName.indexOf('/') != -1))
throw new
InvalidConfigValueException(l10n("illegalCSSName"));
- cssName = CSSName;
- pageMaker.setTheme(cssName);
+ cssTheme = THEME.themeFromName(CSSName);
+ pageMaker.setTheme(cssTheme);
+ if (core.node.pluginManager != null)
+
core.node.pluginManager.setFProxyTheme(cssTheme);
}
public void setPossibleValues(String[] val) {
@@ -180,7 +183,7 @@
}
public String[] getPossibleValues() {
- return pageMaker.getThemes().toArray(new String[0]);
+ return THEME.possibleValues;
}
}
@@ -448,10 +451,11 @@
this.bf = bucketFactory;
port = fproxyConfig.getInt("port");
bindTo = fproxyConfig.getString("bindTo");
- cssName = fproxyConfig.getString("css");
+ String cssName = fproxyConfig.getString("css");
if((cssName.indexOf(':') != -1) || (cssName.indexOf('/') != -1))
throw new InvalidConfigValueException("CSS name must
not contain slashes or colons!");
- pageMaker = new PageMaker(cssName);
+ cssTheme = THEME.themeFromName(cssName);
+ pageMaker = new PageMaker(cssTheme);
if(!fproxyConfig.getOption("CSSOverride").isDefault()) {
cssOverride = new
File(fproxyConfig.getString("CSSOverride"));
@@ -625,12 +629,12 @@
}
- public String getCSSName() {
- return this.cssName;
+ public THEME getTheme() {
+ return this.cssTheme;
}
- public void setCSSName(String name) {
- this.cssName = name;
+ public void setCSSName(THEME theme) {
+ this.cssTheme = theme;
}
public synchronized boolean isAdvancedModeEnabled() {
Modified: trunk/freenet/src/freenet/clients/http/ToadletContainer.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ToadletContainer.java
2008-08-20 17:27:15 UTC (rev 22052)
+++ trunk/freenet/src/freenet/clients/http/ToadletContainer.java
2008-08-20 17:58:52 UTC (rev 22053)
@@ -7,6 +7,7 @@
import java.net.URI;
import java.net.URISyntaxException;
+import freenet.clients.http.PageMaker.THEME;
import freenet.support.HTMLNode;
import freenet.support.api.BucketFactory;
@@ -31,7 +32,7 @@
/**
* Get the name of the theme to be used by all the Toadlets
*/
- public String getCSSName();
+ public THEME getTheme();
/**
* Get the form password