KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.radeox.util.i18n.ResourceManager;
28
29 import java.io.IOException JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.text.CollationKey JavaDoc;
32 import java.text.Collator JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.TreeMap JavaDoc;
37
38 /**
39  * Formats a list as AtoZ listing separated by the alphabetical characters.
40  *
41  * @author Matthias L. Jugel
42  * @version $Id: CollationListFormatter.java 1674 2004-06-24 12:00:11Z leo $
43  */

44 public class CollationListFormatter implements ListFormatter {
45   public String JavaDoc getName() {
46     return "collator";
47   }
48
49   private String JavaDoc removeParents(String JavaDoc name) {
50     int index = name.lastIndexOf("/");
51     if (-1 == index) {
52       return name;
53     } else if (name.length() == index + 1) {
54       return name.substring(0, index);
55     } else {
56       return name.substring(index + 1);
57     }
58   }
59
60   /**
61    * Create a listing of entries sorted using a collator initialized in the
62    * locale provided.
63    */

64   public void format(Writer JavaDoc writer, Linkable current, String JavaDoc listComment, Collection JavaDoc c, String JavaDoc emptyText, boolean showSize)
65           throws IOException JavaDoc {
66     Collator JavaDoc collator = Collator.getInstance(ResourceManager.getLocale("radeox_messages"));
67
68     if (c.size() > 0) {
69       Iterator JavaDoc it = c.iterator();
70       Map JavaDoc atozMap = new TreeMap JavaDoc();
71       while (it.hasNext()) {
72         Object JavaDoc object = it.next();
73         String JavaDoc name, indexChar;
74
75         if (object instanceof Nameable) {
76           name = ((Nameable) object).getName();
77         } else {
78           name = object.toString();
79         }
80
81 // StringBuffer nameBuffer = new StringBuffer();
82
// StringBufferWriter stringBufferWriter = new StringBufferWriter();
83
// VerticalListFormatter.formatSnipName(object, stringBufferWriter);
84
// String finalName = nameBuffer.toString();
85

86         String JavaDoc finalName = removeParents(name);
87         indexChar = finalName.substring(0, 1).toUpperCase();
88         if (object instanceof Linkable) {
89           name = ((Linkable) object).getLink();
90         }
91
92         CollationKey JavaDoc cKey = collator.getCollationKey(indexChar);
93
94         if (!atozMap.containsKey(cKey)) {
95           atozMap.put(cKey, new TreeMap JavaDoc());
96         }
97         Map JavaDoc list = (Map JavaDoc) atozMap.get(cKey);
98         list.put(finalName, name);
99       }
100
101       writer.write("<table width=\"100%\" class=\"index-top\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
102       writer.write("<colgroup width='5.5%' span='18'/>");
103       Iterator JavaDoc collatorIt = atozMap.keySet().iterator();
104       while (collatorIt.hasNext()) {
105         writer.write("<tr>");
106         for (int i = 0; i < 6 && collatorIt.hasNext(); i++) {
107           CollationKey JavaDoc cKey = (CollationKey JavaDoc) collatorIt.next();
108           String JavaDoc ch = cKey.getSourceString();
109           writer.write("<th><b> &nbsp;<a HREF=\"");
110           writer.write(current.getLink());
111           writer.write("#idx" + Integer.toHexString(ch.charAt(0)) + "\">");
112           writer.write(ch);
113           writer.write("</a></b></th>");
114           writer.write("<th>...</th><th>");
115           writer.write("" + (atozMap.get(cKey) == null ? 0 : ((Map JavaDoc) atozMap.get(cKey)).size()));
116           writer.write("&nbsp; </th>");
117         }
118         writer.write("</tr>");
119       }
120       writer.write("</table>");
121
122       writer.write("<div class=\"list-title\">");
123       writer.write(listComment);
124       if (showSize) {
125         writer.write(" (");
126         writer.write("" + c.size());
127         writer.write(")");
128       }
129       writer.write("</div>");
130       writer.write("<table width=\"100%\" class=\"index\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
131       collatorIt = atozMap.keySet().iterator();
132       while (collatorIt.hasNext()) {
133         CollationKey JavaDoc leftKey = (CollationKey JavaDoc) collatorIt.next();
134         CollationKey JavaDoc rightKey = collatorIt.hasNext() ? (CollationKey JavaDoc) collatorIt.next() : null;
135
136         insertCharHeader(writer,
137                          leftKey.getSourceString(),
138                          rightKey != null ? rightKey.getSourceString() : null);
139         addRows(writer,
140                 (Map JavaDoc) atozMap.get(leftKey),
141                 rightKey != null ? (Map JavaDoc) atozMap.get(rightKey) : null);
142       }
143       writer.write("</table>");
144     } else {
145       writer.write(emptyText);
146     }
147   }
148
149   private void addRows(Writer JavaDoc writer, Map JavaDoc listLeft, Map JavaDoc listRight) throws IOException JavaDoc {
150     Iterator JavaDoc leftIt = listLeft != null ? listLeft.values().iterator() : new EmptyIterator();
151     Iterator JavaDoc rightIt = listRight != null ? listRight.values().iterator() : new EmptyIterator();
152
153     while (leftIt.hasNext() || rightIt.hasNext()) {
154       String JavaDoc leftName = (String JavaDoc) (leftIt != null && leftIt.hasNext() ? leftIt.next() : null);
155       String JavaDoc rightName = (String JavaDoc) (rightIt != null && rightIt.hasNext() ? rightIt.next() : null);
156       insertRow(writer, leftName, rightName, false);
157     }
158   }
159
160   private void insertCharHeader(Writer JavaDoc writer, String JavaDoc leftHeader, String JavaDoc rightHeader) throws IOException JavaDoc {
161     writer.write("<tr><th>");
162     writer.write("<b><a name=\"idx");
163     writer.write(Integer.toHexString(leftHeader.charAt(0)));
164     writer.write("\"></a>");
165     writer.write(leftHeader);
166     writer.write("</b></th><th> </th><th>");
167     if (null != rightHeader) {
168       writer.write("<b><a name=\"idx");
169       writer.write(Integer.toHexString(rightHeader.charAt(0)));
170       writer.write("\"></a>");
171       writer.write(rightHeader);
172       writer.write("</b>");
173     }
174     writer.write("</th></tr>");
175   }
176
177   private void insertRow(Writer JavaDoc writer, String JavaDoc left, String JavaDoc right, boolean odd) throws IOException JavaDoc {
178     writer.write("<tr><td>");
179     if (left != null) {
180       writer.write(left);
181     }
182     writer.write("</td><td> </td><td>");
183     if (right != null) {
184       writer.write(right);
185     }
186     writer.write("</td></tr>");
187   }
188
189   private class EmptyIterator implements Iterator JavaDoc {
190     public boolean hasNext() {
191       return false;
192     }
193
194     public Object JavaDoc next() {
195       return null;
196     }
197
198     public void remove() {
199     }
200   }
201 }
202
Popular Tags