Author: fred
Date: 2007-09-15 17:53:01 +0000 (Sat, 15 Sep 2007)
New Revision: 15191

Added:
   trunk/plugins/Echo/src/plugins/echo/editor/
   trunk/plugins/Echo/src/plugins/echo/editor/BlocksPage.java
   trunk/plugins/Echo/src/plugins/echo/editor/CategoriesPage.java
   trunk/plugins/Echo/src/plugins/echo/editor/GeneratePage.java
   trunk/plugins/Echo/src/plugins/echo/editor/HTMLHelper.java
   trunk/plugins/Echo/src/plugins/echo/editor/InsertPage.java
   trunk/plugins/Echo/src/plugins/echo/editor/NodePage.java
   trunk/plugins/Echo/src/plugins/echo/editor/NodesPage.java
   trunk/plugins/Echo/src/plugins/echo/editor/Page.java
   trunk/plugins/Echo/src/plugins/echo/editor/StaticPage.java
Log:
Step 2 : Add the new pages

Added: trunk/plugins/Echo/src/plugins/echo/editor/BlocksPage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/BlocksPage.java                  
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/BlocksPage.java  2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,102 @@
+package plugins.echo.editor;
+
+import plugins.echo.block.Block;
+import plugins.echo.block.BlockManager;
+import plugins.echo.i18n.I18n;
+import freenet.support.api.HTTPRequest;
+
+import nu.xom.Element;
+import nu.xom.Attribute;
+
+import java.io.IOException;
+
+public class BlocksPage extends Page {
+
+       private BlockManager blockManager;
+       private String formPsw;
+
+       public BlocksPage(BlockManager blockManager, String formPassword) {
+
+               super("Blocks");
+               this.blockManager = blockManager;
+               this.formPsw = formPassword;
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {
+
+               clear();
+
+               if(request.isParameterSet("configure")){
+                       appendContent(HTMLHelper.element(null, "strong", 
"TODO"));
+                       return;
+               }
+               
+               if (request.isPartSet("submit")) {
+
+                       String[] blocksIds = blockManager.getIds();
+                       for(String id : blocksIds) {
+                               Block block = blockManager.getBlockById(id);
+                               
+                               String position = 
request.getPartAsString("position-" + id, 8);
+                               block.setPosition(position);
+                               
+                               String weight = 
request.getPartAsString("weight-" + id, 2);
+                               block.setWeight(Integer.parseInt(weight));
+                               
+                               try {
+                                       blockManager.write(block);
+                               } catch (IOException ioe) {
+                                       appendError(ioe);
+                               }               
+                       }
+               }
+
+               Element form = HTMLHelper.form("", formPsw);
+               Element table = new Element("table");
+               form.appendChild(table);
+               Element tHeader = new Element("tr");
+               table.appendChild(tHeader);
+               HTMLHelper.i18nElement(tHeader, "th", "echo.common.name");
+               HTMLHelper.i18nElement(tHeader, "th", "echo.common.position");
+               HTMLHelper.i18nElement(tHeader, "th", "echo.common.weight");
+               HTMLHelper.i18nElement(tHeader, "th", "echo.common.action");
+
+               Block[] blocks = blockManager.getBlocks();
+               boolean alternate = false;
+               for(Block b : blocks) {
+                       Element row = new Element("tr");
+                       if(alternate)
+                               row.addAttribute(new Attribute("class", 
"alternate"));
+                       
+                       HTMLHelper.element(row, "td", 
I18n.getString("echo.block." + b.getType().toString()));
+       
+                       Element positionSelect = new Element("select");
+                       positionSelect.addAttribute(new Attribute("name", 
"position-" + b.getId()));
+                       HTMLHelper.option(positionSelect, "left", 
"left".equals(b.getPosition()));
+                       HTMLHelper.option(positionSelect, "right", 
"right".equals(b.getPosition()));
+                       HTMLHelper.option(positionSelect, "top", 
"top".equals(b.getPosition()));
+                       HTMLHelper.option(positionSelect, "bottom", 
"bottom".equals(b.getPosition()));
+                       HTMLHelper.option(positionSelect, "disabled", 
"disabled".equals(b.getPosition()));
+                       HTMLHelper.element(row, "td", positionSelect);          
        
+
+                       Element weightSelect = new Element("select");
+                       weightSelect.addAttribute(new Attribute("name", 
"weight-" + b.getId()));
+                       for(int i=0; i <= 5; i++) 
+                               HTMLHelper.option(weightSelect, 
String.valueOf(i), b.getWeight() == i);
+                               
+                       HTMLHelper.element(row, "td", weightSelect);
+
+                       Element configureCell = HTMLHelper.element(row, "td", 
"");
+                       if(b.isConfigurable())
+                               HTMLHelper.link(configureCell, "?configure=" + 
b.getId(), "configure");
+
+                       table.appendChild(row);
+                       alternate = !alternate;
+               }
+               HTMLHelper.input(form, "submit", "submit");
+
+               appendContent(form);
+
+       }
+
+}
\ No newline at end of file

Added: trunk/plugins/Echo/src/plugins/echo/editor/CategoriesPage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/CategoriesPage.java              
                (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/CategoriesPage.java      
2007-09-15 17:53:01 UTC (rev 15191)
@@ -0,0 +1,107 @@
+package plugins.echo.editor;
+
+import plugins.echo.NodesManager;
+import freenet.support.api.HTTPRequest;
+
+import java.io.IOException;
+
+import nu.xom.Element;
+import nu.xom.Attribute;
+import nu.xom.ParsingException;
+
+public class CategoriesPage extends Page {
+
+       private static final int MAX_CATEGORY_NAME_LENGTH = 100;
+       
+       private NodesManager nodesManager;
+       private String formPsw;
+       
+       public CategoriesPage(NodesManager nodesManager, String formPsw) {
+
+               super("Categories");
+               this.nodesManager = nodesManager;
+               this.formPsw = formPsw;
+               
+               
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {
+               
+               clear();
+               
+               boolean rename= request.isParameterSet("rename");
+                               
+               if(request.isPartSet("submit")) {
+                       String name = request.getPartAsString("category-name", 
MAX_CATEGORY_NAME_LENGTH).trim();
+                       String catId = request.getPartAsString("category-id", 
plugins.echo.Node.CATEGORY_ID_LENGTH);
+                       
+                       if(! "".equals(name)) {
+                               if(! nodesManager.renameCategory(catId, name))
+                                       nodesManager.newCategory(name);
+                               try{
+                                       nodesManager.writeCategories();
+                               } catch (IOException ioe) {
+                                       appendError(ioe);
+                               }
+                               
+                               rename = false;
+                               
+                       } else {
+                               appendError("Fied \"name\" is empty");
+                       }
+               }
+               
+               if(request.isParameterSet("delete")) {
+                       try {
+                               String cat = request.getParam("delete");
+                               if(!nodesManager.deleteCategory(cat))
+                                       appendError("The category \"" + cat + 
"\" does not exist.");
+                       
+                               nodesManager.writeCategories();
+                       } catch (ParsingException pe) {
+                               appendError(pe);
+                       } catch (IOException ioe) {
+                               appendError(ioe);                       
+                       }
+               }
+               
+               if(nodesManager.countCategories() > 0 &&  ! rename) {
+               
+                       Element table = new Element("table");
+                       Element tHeader = new Element("tr");
+                       table.appendChild(tHeader);
+                       HTMLHelper.i18nElement(tHeader, "th", 
"echo.common.name");
+                       Element actionCell = HTMLHelper.i18nElement(tHeader, 
"th", "echo.common.action");
+                       actionCell.addAttribute(new Attribute("colspan","2"));
+                       
+                       String[] ids = nodesManager.getCategoriesIds();
+                       boolean alternate = false;
+                       for(String id : ids) {
+                               Element row = new Element("tr");
+                               if(alternate)
+                                       row.addAttribute(new Attribute("class", 
"alternate"));
+                               
+                               HTMLHelper.element(row, "td", 
nodesManager.getCategoryNameById(id));
+                               HTMLHelper.element(row, "td", 
HTMLHelper.i18nLink("?rename=" + id, "echo.common.rename"));
+                               HTMLHelper.element(row, "td", 
HTMLHelper.i18nLink("?delete=" + id, "echo.common.delete"));
+                               
+                               table.appendChild(row);
+                               alternate = !alternate;
+                       }
+                                               
+                       appendContent(table);
+               }
+               
+               
+               Element form = HTMLHelper.form((rename) ? "?rename=" + 
request.getParam("rename") :"", formPsw);
+               form.addAttribute(new Attribute("class", "inline"));
+               HTMLHelper.i18nLabel(form, "category-name", (rename) ? 
"echo.common.rename" : "echo.manage.newCategory");
+               Element nameInput = HTMLHelper.input(form, "text", 
"category-name");
+               if(rename)
+                       nameInput.addAttribute(new Attribute("value", 
nodesManager.getCategoryNameById(request.getParam("rename"))));
+               
+               HTMLHelper.input(form, "submit", "submit");
+                                               
+               appendContent(form);
+       }
+}
\ No newline at end of file

Added: trunk/plugins/Echo/src/plugins/echo/editor/GeneratePage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/GeneratePage.java                
                (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/GeneratePage.java        
2007-09-15 17:53:01 UTC (rev 15191)
@@ -0,0 +1,45 @@
+package plugins.echo.editor;
+
+import plugins.echo.Project;
+import plugins.echo.ProjectManager;
+import plugins.echo.SiteGenerator;
+
+import freenet.support.api.HTTPRequest;
+
+public class GeneratePage extends Page {
+
+       private ProjectManager projectManager;
+       private Project project;
+       private SiteGenerator generator;
+       
+       public GeneratePage(ProjectManager projectManager){
+       
+               super("Generate");
+               this.projectManager = projectManager;
+               
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {
+
+               clear();
+               project = projectManager.getCurrentProject();
+
+               
+               try {
+                       generator = new SiteGenerator(project);
+                       generator.generate();
+
+               } catch (Exception e) {
+                       appendError(e);
+               }       
+                                       
+               if(countErrors() == 0) {
+                       
+                       String path = generator.getOutDir().getAbsolutePath();
+                       
+                       appendContent("See the result : ");
+                       appendContent(HTMLHelper.link("file://" + path, path));
+               }
+       }
+
+}

Added: trunk/plugins/Echo/src/plugins/echo/editor/HTMLHelper.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/HTMLHelper.java                  
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/HTMLHelper.java  2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,137 @@
+package plugins.echo.editor;
+
+import plugins.echo.i18n.I18n;
+
+import nu.xom.Node;
+import nu.xom.Element;
+import nu.xom.Attribute;
+import nu.xom.Text;
+
+public class HTMLHelper {
+
+/**
+*
+*      Need to be rewrited !!!
+*
+*/
+
+       public static Element element(Element parent, String name, Node 
content) {
+               
+               Element e = new Element(name);
+
+               if(content != null)
+                       e.appendChild(content);
+               
+               if(parent != null)
+                       parent.appendChild(e);
+               
+               return e;
+       }
+
+       public static Element element(Element parent, String name, String 
content) {
+               
+               return element(parent, name, new Text(content));
+
+       }
+
+       public static Element i18nElement(Element parent, String name, String 
key) {
+               
+               return element(parent, name, I18n.getString(key));
+
+       }
+
+       public static Element link(Element parent, String href, String text) {
+
+               Element link = new Element("a");
+               link.addAttribute(new Attribute("href", href));
+               link.appendChild(text);
+
+               if(parent != null)
+                       parent.appendChild(link);
+
+               return link;
+
+       }
+
+       public static Element link(String href, String text) {
+       
+               return link(null, href, text);
+
+       }
+
+       public static Element i18nLink(Element parent, String href, String key) 
{
+
+               return link(parent, href, I18n.getString(key));
+
+       }
+
+       public static Element i18nLink(String href, String key) {
+
+               return link(null, href, I18n.getString(key));
+
+       }
+
+       public static Element input(Element parent, String type, String name) {
+
+               Element input = new Element("input");
+               input.addAttribute(new Attribute("type", type));
+               input.addAttribute(new Attribute("name", name));
+               input.addAttribute(new Attribute("id", name));
+
+               if(parent != null)
+                       parent.appendChild(input);
+
+               return input;
+       }
+
+       public static Element option(Element parent, String value) {
+               
+               Element option = element(parent, "option", value);
+               option.addAttribute(new Attribute("value",value));
+
+               return option;
+
+       }
+
+       public static Element option(Element parent, String value, boolean 
selected) {
+               
+               Element option = option(parent, value);
+               if(selected)
+                       option.addAttribute(new Attribute("selected", 
"selected"));
+
+               return option;
+
+       }
+
+       public static Element form(String action, String password) {
+
+               Element form = new Element("form");
+               form.addAttribute(new Attribute("method", "POST"));
+               form.addAttribute(new Attribute("action", action));
+               
+               Element pswInput = input(form, "hidden", "formPassword");
+               pswInput.addAttribute(new Attribute("value", password));
+
+               return form;
+
+       }
+
+       public static Element label(Element parent, String forInput, String 
text) {
+
+               Element label = new Element("label");
+               label.addAttribute(new Attribute("for", forInput));
+               label.appendChild(text);
+
+               if(parent != null)
+                       parent.appendChild(label);
+
+               return label;
+       }
+
+       public static Element i18nLabel(Element parent, String forInput, String 
key) {
+               
+               return label(parent, forInput, I18n.getString(key));
+               
+       }
+
+} 

Added: trunk/plugins/Echo/src/plugins/echo/editor/InsertPage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/InsertPage.java                  
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/InsertPage.java  2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,119 @@
+package plugins.echo.editor;
+
+import plugins.echo.Project;
+import plugins.echo.ProjectManager;
+import plugins.echo.SiteGenerator;
+import plugins.echo.SimpleDirectoryInserter;
+import freenet.keys.FreenetURI;
+import freenet.support.api.HTTPRequest;
+import freenet.node.fcp.FCPServer;
+
+import java.io.File;
+import java.net.MalformedURLException;
+
+import nu.xom.Element;
+import nu.xom.Attribute;
+
+public class InsertPage extends Page {
+
+       public static final int KEY_INPUT_SIZE = 70;
+       public static final int MAX_KEY_LENGTH = 1024*1024;
+
+       private ProjectManager projectManager;
+       private Project project;
+       private FCPServer fcpServer;
+       private String formPassword;
+       private FreenetURI insertURI;
+       private FreenetURI requestURI;
+ 
+       public InsertPage(ProjectManager projectManager, FCPServer server, 
String formPassword){
+       
+               super("Insert");
+               this.formPassword = formPassword;
+               this.projectManager = projectManager;
+               this.fcpServer = server;
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {
+
+               clear();
+               project = projectManager.getCurrentProject();
+
+               if (request.isPartSet("insert-key")) {
+                               
+//                             if(clientPutDir == null || 
clientPutDir.hasFinished()) {
+                                       insertURI = null;
+                                       requestURI = null;
+                                       
+                                       try {
+                                               insertURI = new 
FreenetURI(request.getPartAsString("insert-key", MAX_KEY_LENGTH));
+                                       } catch(MalformedURLException mue) {
+                                               appendError("Invalid insertion 
key : " + mue.getMessage());
+                                       }
+                                       
+                                       try {
+                                               requestURI = new 
FreenetURI(request.getPartAsString("request-key", MAX_KEY_LENGTH));
+                                       } catch(MalformedURLException mue) {
+                                               appendError("Invalid request 
key : " + mue.getMessage());
+                                       }
+                                       
+                                       if(insertURI != null && requestURI != 
null) {
+                                       
+                                               
+                                               if (! 
project.getInsertURI().equals(insertURI)){
+                                                       
project.setInsertURI(insertURI);
+                                                       
+                                               }
+                                               
+                                               if (! 
project.getRequestURI().equals(requestURI)) {
+                                                       
project.setRequestURI(requestURI);
+                                                       
+                                               }
+
+                                               try {
+
+                                                       SiteGenerator generator 
= new SiteGenerator(project);
+                                                       generator.generate();
+
+                                                       SimpleDirectoryInserter 
inserter = new SimpleDirectoryInserter(fcpServer);
+                                                       inserter.insert(new 
File(project.getProjectDir(), "out"), "index.html", insertURI);
+                                                       
+                                                       
appendContent(HTMLHelper.link("/queue/", "Go to the queue page."));
+                                                       
+                                               } catch (Exception e) {
+                                                       appendError(e);
+                                               }       
+                                               
+
+                                       } else 
+                                               appendContent(insertForm());
+//                             }
+               } else {
+                       insertURI = project.getInsertURI();
+                       requestURI = project.getRequestURI();
+                       appendContent(insertForm());
+               }
+       }
+
+       private Element insertForm() {
+
+               Element form = HTMLHelper.form("", formPassword);
+
+               HTMLHelper.label(form, "insert-key", "Insert key");
+               Element insertKeyInput = HTMLHelper.input(form, "text", 
"insert-key");
+               insertKeyInput.addAttribute(new Attribute("size", 
String.valueOf(KEY_INPUT_SIZE)));
+               if(insertURI != null)
+                       insertKeyInput.addAttribute(new Attribute("value", 
insertURI.toString()));
+
+               HTMLHelper.label(form, "request-key", "Request key");
+               Element requestKeyInput = HTMLHelper.input(form, "text", 
"request-key");
+               requestKeyInput.addAttribute(new Attribute("size", 
String.valueOf(KEY_INPUT_SIZE)));
+               if(requestURI != null)
+                       requestKeyInput.addAttribute(new Attribute("value", 
requestURI.toString()));
+
+               HTMLHelper.input(form, "submit", "submit");
+
+               return form;
+       }
+
+}

Added: trunk/plugins/Echo/src/plugins/echo/editor/NodePage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/NodePage.java                    
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/NodePage.java    2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,150 @@
+package plugins.echo.editor;
+
+import plugins.echo.Node;
+import plugins.echo.NodesManager;
+import freenet.support.api.HTTPRequest;
+import java.io.File;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+
+import nu.xom.Element;
+import nu.xom.Attribute;
+import nu.xom.ParsingException;
+
+public class NodePage extends Page {
+       
+       
+       private static final int MAX_TITLE_LENGTH = 200;
+       private static final int MAX_BODY_LENGTH = 100000;
+       
+       private NodesManager nodesManager;
+       private final String formPsw;
+       
+       
+       public NodePage(NodesManager nodesManager, String formPassword) {
+
+               super("Node");
+               this.nodesManager = nodesManager;
+               this.formPsw = formPassword;
+
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {
+
+               clear();
+               Node node = null;               
+               
+               String fileName = (new File(request.getPath())).getName();
+               
+               if("edit".equals(fileName)) {
+
+                       String nodeId = request.getParam("node");
+                       
+                       setTitle("Edit");
+                       try {
+
+                               node = nodesManager.getNodeById(nodeId);
+                               if(node == null) {
+                                       appendError("The node " + nodeId + 
"does not exist");
+                                       return;
+                               }
+
+                       } catch (IOException ioe) {
+                               appendError(ioe);
+                               return;
+                       } catch (ParsingException pe) {
+                               appendError("The node " + nodeId  + " is 
damaged : " + pe.getMessage());
+                               return;
+                       }
+                               
+               } else if(fileName.equals("newPost")) {
+                       setTitle("New Post");
+                       node = new Node(nodesManager.getFreeNodeId(), 
Node.NodeType.POST_NODE);
+                       
+               } else if(fileName.equals("newPage")) {
+                       setTitle("New Page");
+                       node = new Node(nodesManager.getFreeNodeId(), 
Node.NodeType.STATIC_PAGE_NODE);
+               }
+
+               if(request.isPartSet("edit-title") && 
request.isPartSet("body")) {
+                       String title = request.getPartAsString("edit-title", 
MAX_TITLE_LENGTH).trim();
+                       String body = request.getPartAsString("body", 
MAX_BODY_LENGTH).trim();
+                       
+                       if("".equals(title))
+                               appendError("Field \"title\" is empty");
+
+                       if("".equals(body)) 
+                               appendError("Field \"body\" is empty");
+
+                       node.setTitle(title);
+                       node.setBody(body);
+                       
+                       String[] cats = nodesManager.getCategoriesIds();
+                       for(String cat : cats)
+                               node.setCategory(cat, 
request.isPartSet("category-" + cat));
+                               
+                       if(countErrors() == 0) {
+                               
+                               try {
+
+                                       nodesManager.writeNode(node);
+                                       
+                               } catch (FileNotFoundException fnfe) {
+                                       appendError(fnfe);
+                               } catch (IOException ioe) {
+                                       appendError("Cannot write node " + 
node.getId() + " : " + ioe.getMessage());
+                               } catch (Exception e) {
+                                       appendError(e);
+                               }
+                               
+                               appendContent("Saved");
+                               return;
+                       }
+
+               }
+               
+               Element form = HTMLHelper.form("", formPsw);
+
+               HTMLHelper.i18nLabel(form, "edit-title", "echo.common.title");
+               Element titleInput = HTMLHelper.input(form, "text", 
"edit-title");
+               titleInput.addAttribute(new Attribute("size", "100"));
+               titleInput.addAttribute(new Attribute("value", 
node.getTitle()));
+
+               HTMLHelper.i18nLabel(form, "edit-body", "echo.write.nodeBody");
+
+               Element textarea = new Element("textarea");
+               textarea.addAttribute(new Attribute("id", "edit-body"));
+               textarea.addAttribute(new Attribute("name", "body"));
+               textarea.addAttribute(new Attribute("cols", "100"));
+               textarea.addAttribute(new Attribute("rows", "50"));
+               textarea.appendChild((node.getBody().equals("")) ? " " : 
node.getBody());
+               form.appendChild(textarea);
+
+               if(nodesManager.countCategories() != 0) {
+
+                       Element fieldset = new Element("fieldset");
+                       form.appendChild(fieldset);
+                       HTMLHelper.i18nElement(fieldset, "legend", 
"echo.common.categories");
+
+                       String[] ids = nodesManager.getCategoriesIds();
+                       for(String id : ids) {
+                               Element checkbox = HTMLHelper.input(fieldset, 
"checkbox", "category-" + id);
+                               checkbox.addAttribute(new Attribute("class", 
"inline"));
+
+                               if(node.isInCategory(id))
+                                       checkbox.addAttribute(new 
Attribute("checked", "checked"));
+
+                               HTMLHelper.label(fieldset, "category-" + id, 
nodesManager.getCategoryNameById(id));
+                       }
+
+               }
+
+               HTMLHelper.input(form, "submit", "submit");
+
+               appendContent(form);
+               
+       }
+
+
+
+}

Added: trunk/plugins/Echo/src/plugins/echo/editor/NodesPage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/NodesPage.java                   
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/NodesPage.java   2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,84 @@
+package plugins.echo.editor;
+
+import plugins.echo.Node;
+import plugins.echo.Nodes;
+import plugins.echo.NodesManager;
+import freenet.support.api.HTTPRequest;
+
+import nu.xom.Element;
+import nu.xom.Attribute;
+
+import java.io.IOException;
+
+public class NodesPage extends Page {
+
+       private NodesManager nodesManager;
+
+       public NodesPage(NodesManager nodesManager){
+       
+               super("My Nodes");
+               this.nodesManager = nodesManager;
+
+       }
+
+       public void handleHTTPRequest(HTTPRequest req) {
+
+               clear();
+               
+               if(req.isParameterSet("delete")) {
+                       String id = req.getParam("delete");
+                       if(nodesManager.nodeExists(id))
+                               try {
+                                       nodesManager.deleteNode(id);
+                               } catch(IOException ioe) {
+                                       appendError("Unable to delete the node 
" + id + " : " + ioe.getMessage());
+                               }
+                       else
+                               appendError("The node " + id + "does not 
exist");
+               }
+               
+               if(nodesManager.size() == 0) {
+                       appendContent("You don't have any node, ");
+                       appendContent(HTMLHelper.link("write","create a new 
one"));
+               } else {
+                       
+                       try {
+                               Nodes nodes = nodesManager.getNodes();
+                               Element table = new Element("table");
+                               Element tHeader = new Element("tr");
+                               table.appendChild(tHeader);
+                               HTMLHelper.element(tHeader, "th", "Id");
+                               HTMLHelper.i18nElement(tHeader, "th", 
"echo.common.date");
+                               HTMLHelper.i18nElement(tHeader, "th", 
"echo.common.title");
+                               HTMLHelper.i18nElement(tHeader, "th", 
"echo.common.nodeType");
+                               Element action = 
HTMLHelper.i18nElement(tHeader, "th", "echo.common.action");
+                               action.addAttribute(new Attribute("colspan", 
"2"));
+                               
+                               boolean altern = false;
+                               for(Node n : nodes) {
+       
+                                       Element row = new Element("tr");
+                                       if(altern)
+                                               row.addAttribute(new 
Attribute("class", "alternate"));
+
+                                       HTMLHelper.element(row, "td", 
n.getId());
+                                       HTMLHelper.element(row, "td", 
n.getCreationDate().toString());
+                                       HTMLHelper.element(row, "td", 
n.getTitle());
+                                       HTMLHelper.element(row, "td", "type");  
// TODO
+                                       HTMLHelper.element(row, "td", 
HTMLHelper.i18nLink("edit?node=" + n.getId(), "echo.common.edit"));
+                                       HTMLHelper.element(row, "td", 
HTMLHelper.i18nLink("?delete=" + n.getId(), "echo.common.delete"));
+
+                                       table.appendChild(row);
+                                       altern = !altern;
+       
+                               }
+
+                               appendContent(table);
+
+                       } catch (Exception e) {
+                               appendError(e);
+                       }
+               }
+
+       }
+}

Added: trunk/plugins/Echo/src/plugins/echo/editor/Page.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/Page.java                        
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/Page.java        2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,99 @@
+package plugins.echo.editor;
+
+import freenet.support.api.HTTPRequest;
+
+import java.util.List;
+import java.util.Vector;
+
+import nu.xom.Node;
+import nu.xom.Element;
+import nu.xom.Attribute;
+
+public abstract class Page {
+
+       private Element content;
+       private String title;
+       private List<String> errors;
+
+       protected Page() {
+
+               content = new Element("content");
+               errors = new Vector<String>();
+
+       }
+
+       protected Page(String title) {
+
+               this();
+               setTitle(title);
+
+       }
+
+       public abstract void handleHTTPRequest(HTTPRequest request);
+
+       protected void appendContent(Node n) {
+
+               content.appendChild(n);
+
+       }
+
+       protected void appendContent(String text) {
+
+               content.appendChild(text);
+
+       }
+
+       protected void appendError(String desc) {
+               
+               errors.add(desc);
+
+       }
+
+       protected void appendError(Throwable t){
+
+               errors.add(t.toString());
+
+       }
+
+       public int countErrors() {
+
+               return errors.size();
+
+       }
+
+       protected void setTitle(String title) {
+
+               this.title = title;
+
+       }
+
+       protected void clear() {
+
+               content.removeChildren();
+               errors.clear();
+
+       }
+
+       public Element toXML() {
+
+               content.detach();
+
+               Element page = new Element("page");
+               page.addAttribute(new Attribute("title", title));
+               page.appendChild(content);
+               
+               if(errors.size() != 0) {
+                       Element errorsElement = new Element("errors");
+                       for(String desc : errors) {
+                               Element error = new Element("error");
+                               error.appendChild(desc);
+                               errorsElement.appendChild(error);
+                       }
+
+                       page.appendChild(errorsElement);
+               }
+
+               return page;
+       }
+
+} 

Added: trunk/plugins/Echo/src/plugins/echo/editor/StaticPage.java
===================================================================
--- trunk/plugins/Echo/src/plugins/echo/editor/StaticPage.java                  
        (rev 0)
+++ trunk/plugins/Echo/src/plugins/echo/editor/StaticPage.java  2007-09-15 
17:53:01 UTC (rev 15191)
@@ -0,0 +1,39 @@
+package plugins.echo.editor;
+
+import plugins.echo.i18n.I18n;
+import freenet.support.api.HTTPRequest;
+
+import nu.xom.Builder;
+import nu.xom.Document;
+
+public class StaticPage extends Page {
+
+       protected StaticPage() {
+               
+               super();
+
+       }
+
+       public static StaticPage createFromContentFile(String title, String 
fileName) {
+
+               StaticPage page = new StaticPage();
+               page.setTitle(title);
+               
+               try {
+                       Builder parser = new Builder();
+                       Document doc = 
parser.build("/home/fred/prog/soc/trunk/plugins/Echo/src/xml/" + fileName);
+                       I18n.translateXML(doc);
+                       page.appendContent(doc.getRootElement().copy());
+
+               } catch (Exception e) {
+                       page.appendError(e);
+               }
+
+               return page;
+
+       }
+
+       public void handleHTTPRequest(HTTPRequest request) {    }
+
+}
+ 


Reply via email to