1 25 package org.snipsnap.render.macro.list; 26 27 import org.snipsnap.container.Components; 28 import org.snipsnap.snip.Snip; 29 import org.snipsnap.snip.SnipLink; 30 import org.snipsnap.snip.SnipSpace; 31 import org.snipsnap.user.UserManagerFactory; 32 33 import java.io.IOException ; 34 import java.io.Writer ; 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 38 45 public class VerticalListFormatter implements ListFormatter { 46 public String getName() { 47 return "vertical"; 48 } 49 50 59 public void format(Writer writer, Linkable current, String listComment, Collection c, String emptyText, boolean showSize) 60 throws IOException { 61 writer.write("<div class=\"list\"><div class=\"list-title\">"); 62 writer.write(listComment); 63 if (showSize) { 64 writer.write(" ("); 65 writer.write("" + c.size()); 66 writer.write(")"); 67 } 68 writer.write("</div>"); 69 if (c.size() > 0) { 70 writer.write("<ul>"); 71 Iterator nameIterator = c.iterator(); 72 while (nameIterator.hasNext()) { 73 Object object = nameIterator.next(); 74 writer.write("<li>"); 75 if (object instanceof Snip) { 76 formatSnipName(object, writer); 77 } else if (object instanceof Linkable) { 78 writer.write(((Linkable) object).getLink()); 79 } else if (object instanceof Nameable) { 80 SnipLink.appendLink(writer, ((Nameable) object).getName()); 81 } else { 82 writer.write(object.toString()); 83 } 84 writer.write("</li>"); 85 } 86 writer.write("</ul>"); 87 } else { 88 writer.write(emptyText); 89 } 90 writer.write("</div>"); 91 return; 92 } 93 94 private void formatSnipName(Object object, Writer writer) throws IOException { 95 Snip snip = (Snip) object; 96 String name = snip.getName(); 97 String realName = snip.getTitle(); 98 if (name.startsWith("comment-")) { 99 int lastIndex = name.lastIndexOf("-"); 100 SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class); 101 Snip commentedSnip = space.load(name.substring(name.indexOf("-") + 1, lastIndex)); 102 realName = commentedSnip.getTitle(); 103 SnipLink.appendImage(writer, "Icon-Comment", ""); 104 writer.write(" "); 105 SnipLink.appendLinkWithRoot(writer, SnipLink.getCommentsRoot(), 106 SnipLink.encode(commentedSnip.getName()) + "#" + name, realName); 107 writer.write(" ("); 109 SnipLink.appendLink(writer, snip.getCUser()); 110 writer.write(")"); 111 } else if (UserManagerFactory.getInstance().exists(name)) { 113 SnipLink.appendImage(writer, "Icon-Person", ""); 114 writer.write(" "); 115 SnipLink.appendLink(writer, ((Nameable) object).getName()); 116 } else { 118 SnipLink.appendImage(writer, "Icon-Snip", ""); 119 writer.write(" "); 120 SnipLink.appendLink(writer, name, realName); 121 } 122 } 123 } 124 | Popular Tags |