1 25 package org.snipsnap.render.macro.list; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.util.Collection ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 33 34 import java.util.TreeMap ; 35 36 42 public class AtoZListFormatter implements ListFormatter { 43 public String getName() { 44 return "atoz"; 45 } 46 47 private String removeParents(String 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 61 public void format(Writer writer, Linkable current, String listComment, Collection c, String emptyText, boolean showSize) 62 throws IOException { 63 if (c.size() > 0) { 64 Iterator it = c.iterator(); 65 Map atozMap = new HashMap (); 66 Map numberRestList = new TreeMap (); 67 Map otherRestList = new TreeMap (); 68 while (it.hasNext()) { 69 Object object = it.next(); 70 String name, indexChar; 71 if (object instanceof Nameable) { 72 name = ((Nameable) object).getName(); 73 } else { 74 name = object.toString(); 75 } 76 String 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 ()); 85 } 86 Map list = (Map ) 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 ch = "" + (char) (idxChar + i); 101 writer.write("<th><b> <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 ) atozMap.get(ch)).size())); 108 writer.write(" </th>"); 109 } 110 idxChar += 6; 111 if (idxChar >= 'Z') { 112 writer.write("<th><b> <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(" </th>"); 118 writer.write("<th><b> <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(" </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 left = "" + (char) ch; 143 String right = "" + (char) (ch + 1); 144 145 insertCharHeader(writer, left, right); 146 addRows(writer, (Map ) atozMap.get(left), (Map ) 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 writer, Map listLeft, Map listRight) throws IOException { 157 Iterator leftIt = listLeft != null ? listLeft.values().iterator() : new EmptyIterator(); 158 Iterator rightIt = listRight != null ? listRight.values().iterator() : new EmptyIterator(); 159 160 while (leftIt.hasNext() || rightIt.hasNext()) { 161 String leftName = (String ) (leftIt != null && leftIt.hasNext() ? leftIt.next() : null); 162 String rightName = (String ) (rightIt != null && rightIt.hasNext() ? rightIt.next() : null); 163 insertRow(writer, leftName, rightName, false); 164 } 165 } 166 167 private void insertCharHeader(Writer writer, String leftHeader, String rightHeader) throws IOException { 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 writer, String left, String right, boolean odd) throws IOException { 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 { 194 public boolean hasNext() { 195 return false; 196 } 197 198 public Object next() { 199 return null; 200 } 201 202 public void remove() { 203 } 204 } 205 } 206 | Popular Tags |