KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > list > VerticalListFormatter


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Vertical list formatter. If the collections contains snips then
40  * the formatter renders comments snips different.
41  *
42  * @author Matthias L. Jugel
43  * @version $Id: VerticalListFormatter.java 1677 2004-06-24 12:24:16Z leo $
44  */

45 public class VerticalListFormatter implements ListFormatter {
46   public String JavaDoc getName() {
47     return "vertical";
48   }
49
50   /**
51    * Display a simple vertical list.
52    *
53    * @param writer Writer to write the list output to
54    * @param listComment String to display before the list
55    * @param c Collection of Linkables, Snips or Nameables to display
56    * @param emptyText Text to display if collection is empty
57    * @param showSize If showSize is true then the size of the collection is displayed
58    */

59   public void format(Writer JavaDoc writer, Linkable current, String JavaDoc listComment, Collection JavaDoc c, String JavaDoc emptyText, boolean showSize)
60           throws IOException JavaDoc {
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 JavaDoc nameIterator = c.iterator();
72       while (nameIterator.hasNext()) {
73         Object JavaDoc 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 JavaDoc object, Writer JavaDoc writer) throws IOException JavaDoc {
95     Snip snip = (Snip) object;
96     String JavaDoc name = snip.getName();
97     String JavaDoc 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       //SnipLink.appendLink(writer, name, realName);
108
writer.write(" (");
109       SnipLink.appendLink(writer, snip.getCUser());
110       writer.write(")");
111       // @TODO replace with Type Snip check
112
} else if (UserManagerFactory.getInstance().exists(name)) {
113       SnipLink.appendImage(writer, "Icon-Person", "");
114       writer.write(" ");
115       SnipLink.appendLink(writer, ((Nameable) object).getName());
116       //SnipLink.appendLinkWithRoot(writer, SnipLink.getCommentsRoot(), SnipLink.encode(realName) + "#" + name, realName);
117
} else {
118       SnipLink.appendImage(writer, "Icon-Snip", "");
119       writer.write(" ");
120       SnipLink.appendLink(writer, name, realName);
121     }
122   }
123 }
124
Popular Tags