1 25 package org.snipsnap.render.macro.list; 26 27 import org.radeox.util.i18n.ResourceManager; 28 29 import java.io.IOException ; 30 import java.io.Writer ; 31 import java.text.CollationKey ; 32 import java.text.Collator ; 33 import java.util.Collection ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.TreeMap ; 37 38 44 public class CollationListFormatter implements ListFormatter { 45 public String getName() { 46 return "collator"; 47 } 48 49 private String removeParents(String 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 64 public void format(Writer writer, Linkable current, String listComment, Collection c, String emptyText, boolean showSize) 65 throws IOException { 66 Collator collator = Collator.getInstance(ResourceManager.getLocale("radeox_messages")); 67 68 if (c.size() > 0) { 69 Iterator it = c.iterator(); 70 Map atozMap = new TreeMap (); 71 while (it.hasNext()) { 72 Object object = it.next(); 73 String name, indexChar; 74 75 if (object instanceof Nameable) { 76 name = ((Nameable) object).getName(); 77 } else { 78 name = object.toString(); 79 } 80 81 86 String finalName = removeParents(name); 87 indexChar = finalName.substring(0, 1).toUpperCase(); 88 if (object instanceof Linkable) { 89 name = ((Linkable) object).getLink(); 90 } 91 92 CollationKey cKey = collator.getCollationKey(indexChar); 93 94 if (!atozMap.containsKey(cKey)) { 95 atozMap.put(cKey, new TreeMap ()); 96 } 97 Map list = (Map ) 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 collatorIt = atozMap.keySet().iterator(); 104 while (collatorIt.hasNext()) { 105 writer.write("<tr>"); 106 for (int i = 0; i < 6 && collatorIt.hasNext(); i++) { 107 CollationKey cKey = (CollationKey ) collatorIt.next(); 108 String ch = cKey.getSourceString(); 109 writer.write("<th><b> <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 ) atozMap.get(cKey)).size())); 116 writer.write(" </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 leftKey = (CollationKey ) collatorIt.next(); 134 CollationKey rightKey = collatorIt.hasNext() ? (CollationKey ) collatorIt.next() : null; 135 136 insertCharHeader(writer, 137 leftKey.getSourceString(), 138 rightKey != null ? rightKey.getSourceString() : null); 139 addRows(writer, 140 (Map ) atozMap.get(leftKey), 141 rightKey != null ? (Map ) atozMap.get(rightKey) : null); 142 } 143 writer.write("</table>"); 144 } else { 145 writer.write(emptyText); 146 } 147 } 148 149 private void addRows(Writer writer, Map listLeft, Map listRight) throws IOException { 150 Iterator leftIt = listLeft != null ? listLeft.values().iterator() : new EmptyIterator(); 151 Iterator rightIt = listRight != null ? listRight.values().iterator() : new EmptyIterator(); 152 153 while (leftIt.hasNext() || rightIt.hasNext()) { 154 String leftName = (String ) (leftIt != null && leftIt.hasNext() ? leftIt.next() : null); 155 String rightName = (String ) (rightIt != null && rightIt.hasNext() ? rightIt.next() : null); 156 insertRow(writer, leftName, rightName, false); 157 } 158 } 159 160 private void insertCharHeader(Writer writer, String leftHeader, String rightHeader) throws IOException { 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 writer, String left, String right, boolean odd) throws IOException { 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 { 190 public boolean hasNext() { 191 return false; 192 } 193 194 public Object next() { 195 return null; 196 } 197 198 public void remove() { 199 } 200 } 201 } 202 | Popular Tags |