KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is part of "SnipSnap Radeox Rendering Engine".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://radeox.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library 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 GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.render.macro.list;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import java.util.TreeMap JavaDoc;
35
36 /**
37  * Formats a list as AtoZ listing separated by the alphabetical characters.
38  *
39  * @author Matthias L. Jugel
40  * @version $Id: AtoZListFormatter.java 1606 2004-05-17 10:56:18Z leo $
41  */

42 public class AtoZListFormatter implements ListFormatter {
43   public String JavaDoc getName() {
44     return "atoz";
45   }
46
47   private String JavaDoc removeParents(String JavaDoc name) {
48     int index = name.lastIndexOf("/");
49     if (-1 == index) {
50       return name;
51     } else if (name.length() == index + 1) {
52       return name.substring(0, index);
53     } else {
54       return name.substring(index + 1);
55     }
56   }
57
58   /**
59    * Create an A to Z index
60    */

61   public void format(Writer JavaDoc writer, Linkable current, String JavaDoc listComment, Collection JavaDoc c, String JavaDoc emptyText, boolean showSize)
62     throws IOException JavaDoc {
63     if (c.size() > 0) {
64       Iterator JavaDoc it = c.iterator();
65       Map JavaDoc atozMap = new HashMap JavaDoc();
66       Map JavaDoc numberRestList = new TreeMap JavaDoc();
67       Map JavaDoc otherRestList = new TreeMap JavaDoc();
68       while (it.hasNext()) {
69         Object JavaDoc object = it.next();
70         String JavaDoc name, indexChar;
71         if (object instanceof Nameable) {
72           name = ((Nameable) object).getName();
73         } else {
74           name = object.toString();
75         }
76         String JavaDoc finalName = removeParents(name);
77         indexChar = finalName.substring(0, 1).toUpperCase();
78         if (object instanceof Linkable) {
79           name = ((Linkable) object).getLink();
80         }
81
82         if (indexChar.charAt(0) >= 'A' && indexChar.charAt(0) <= 'Z') {
83           if (!atozMap.containsKey(indexChar)) {
84             atozMap.put(indexChar, new TreeMap JavaDoc());
85           }
86           Map JavaDoc list = (Map JavaDoc) atozMap.get(indexChar);
87           list.put(finalName, name);
88         } else if (indexChar.charAt(0) >= '0' && indexChar.charAt(0) <= '9') {
89           numberRestList.put(finalName, name);
90         } else {
91           otherRestList.put(finalName, name);
92         }
93       }
94
95       writer.write("<table width=\"100%\" class=\"index-top\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
96       writer.write("<colgroup width='5.5%' span='18'/>");
97       for (int idxChar = 'A'; idxChar <= 'Z';) {
98         writer.write("<tr>");
99         for (int i = 0; i < 6 && idxChar + i <= 'Z'; i++) {
100           String JavaDoc ch = "" + (char) (idxChar + i);
101           writer.write("<th><b> &nbsp;<a HREF=\"");
102           writer.write(current.getLink());
103           writer.write("#idx" + ch + "\">");
104           writer.write(ch);
105           writer.write("</a></b></th>");
106           writer.write("<th>...</th><th>");
107           writer.write("" + (atozMap.get(ch) == null ? 0 : ((Map JavaDoc) atozMap.get(ch)).size()));
108           writer.write("&nbsp; </th>");
109         }
110         idxChar += 6;
111         if (idxChar >= 'Z') {
112           writer.write("<th><b> &nbsp;<a HREF=\"");
113           writer.write(current.getLink());
114           writer.write("#idx0-9\">0-9</a></b></th>");
115           writer.write("<th>...</th><th>");
116           writer.write("" + numberRestList.size());
117           writer.write("&nbsp; </th>");
118           writer.write("<th><b> &nbsp;<a HREF=\"");
119           writer.write(current.getLink());
120           writer.write("#idxAT\">@</a></b></th>");
121           writer.write("<th>...</th><th>");
122           writer.write("" + otherRestList.size());
123           writer.write("&nbsp; </th>");
124           writer.write("<th></th><th></th><th></th><th></th>");
125           writer.write("<th></th><th></th><th></th><th></th>");
126         }
127         writer.write("</tr>");
128
129       }
130       writer.write("</table>");
131
132       writer.write("<div class=\"list-title\">");
133       writer.write(listComment);
134       if (showSize) {
135         writer.write(" (");
136         writer.write("" + c.size());
137         writer.write(")");
138       }
139       writer.write("</div>");
140       writer.write("<table width=\"100%\" class=\"index\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
141       for (int ch = 'A'; ch <= 'Z'; ch += 2) {
142         String JavaDoc left = "" + (char) ch;
143         String JavaDoc right = "" + (char) (ch + 1);
144
145         insertCharHeader(writer, left, right);
146         addRows(writer, (Map JavaDoc) atozMap.get(left), (Map JavaDoc) atozMap.get(right));
147       }
148       insertCharHeader(writer, "0-9", "@");
149       addRows(writer, numberRestList, otherRestList);
150       writer.write("</table>");
151     } else {
152       writer.write(emptyText);
153     }
154   }
155
156   private void addRows(Writer JavaDoc writer, Map JavaDoc listLeft, Map JavaDoc listRight) throws IOException JavaDoc {
157     Iterator JavaDoc leftIt = listLeft != null ? listLeft.values().iterator() : new EmptyIterator();
158     Iterator JavaDoc rightIt = listRight != null ? listRight.values().iterator() : new EmptyIterator();
159
160     while (leftIt.hasNext() || rightIt.hasNext()) {
161       String JavaDoc leftName = (String JavaDoc) (leftIt != null && leftIt.hasNext() ? leftIt.next() : null);
162       String JavaDoc rightName = (String JavaDoc) (rightIt != null && rightIt.hasNext() ? rightIt.next() : null);
163       insertRow(writer, leftName, rightName, false);
164     }
165   }
166
167   private void insertCharHeader(Writer JavaDoc writer, String JavaDoc leftHeader, String JavaDoc rightHeader) throws IOException JavaDoc {
168     writer.write("<tr><th>");
169     writer.write("<b><a name=\"idx");
170     writer.write("@".equals(leftHeader) ? "AT" : leftHeader);
171     writer.write("\"></a>");
172     writer.write(leftHeader);
173     writer.write("</b></th><th> </th><th>");
174     writer.write("<b><a name=\"idx");
175     writer.write("@".equals(rightHeader) ? "AT" : rightHeader);
176     writer.write("\"></a>");
177     writer.write(rightHeader);
178     writer.write("</b></th></tr>");
179   }
180
181   private void insertRow(Writer JavaDoc writer, String JavaDoc left, String JavaDoc right, boolean odd) throws IOException JavaDoc {
182     writer.write("<tr><td>");
183     if (left != null) {
184       writer.write(left);
185     }
186     writer.write("</td><td> </td><td>");
187     if (right != null) {
188       writer.write(right);
189     }
190     writer.write("</td></tr>");
191   }
192
193   private class EmptyIterator implements Iterator JavaDoc {
194     public boolean hasNext() {
195       return false;
196     }
197
198     public Object JavaDoc next() {
199       return null;
200     }
201
202     public void remove() {
203     }
204   }
205 }
206
Popular Tags