1 64 65 package com.jcorporate.expresso.services.html; 66 67 72 73 import java.io.PrintWriter ; 74 import java.util.Enumeration ; 75 import java.util.StringTokenizer ; 76 77 78 82 public class Table 83 extends HtmlElement { 84 private String thisClass = (this.getClass().getName() + "."); 85 private String alignment = "center"; 86 private int cellspacing = 0; 87 private int cellpadding = 0; 88 private int border = 0; 89 private String titleString = (""); 90 private String verticalAlignment = null; 91 private String caption = null; 92 private String width = null; 93 94 97 public Table() 98 throws HtmlException { 99 super(); 100 } 101 102 107 public Table(String newName) 108 throws HtmlException { 109 super(newName); 110 } 111 112 118 public synchronized void add(HtmlElement newElement) 119 throws HtmlException { 120 String myName = (thisClass + "add(HtmlElement)"); 121 122 if (newElement instanceof Row) { 123 super.add(newElement); 124 } else { 125 throw new HtmlException(myName + 126 ":Can't add anything except Rows " + 127 "to Tables. Attempted to add " + 128 newElement.getName() + " to " + getName()); 129 } 130 } 131 132 133 139 public synchronized void addHeading(String heading) 140 throws HtmlException { 141 setTitle(heading); 142 } 143 144 145 151 protected synchronized void display(PrintWriter out, int depth) 152 throws HtmlException { 153 154 165 out.println(); 166 this.padWithTabs(out, depth); 167 out.print("<table"); 168 169 if (cSSClass != null) { 170 out.print(" class=\"" + cSSClass + "\""); 171 } 172 if (cSSID != null) { 173 out.print(" id=\"" + cSSID + "\""); 174 } 175 176 out.print(" border=\"" + border + "\" cellspacing=\"" + cellspacing + 177 "\""); 178 179 if (alignment != null) { 180 out.print(" align=\"" + alignment + "\""); 181 } 182 if (width != null) { 183 out.print(" width=\"" + width + "\""); 184 } 185 if (verticalAlignment == null) { 186 out.println(">"); 187 } else { 188 out.println(" valign=\"" + verticalAlignment + "\">"); 189 } 190 if (caption != null) { 191 this.padWithTabs(out, depth); 192 out.println("<caption class=\"jc-caption\" align=\"top\">" + 193 caption + "</caption>"); 194 } 195 if (!titleString.equals("")) { 196 StringTokenizer stk = new StringTokenizer (titleString, "|"); 197 this.padWithTabs(out, depth); 198 out.println("<tr class=\"jc-tabletitlerow\">"); 199 200 while (stk.hasMoreTokens()) { 201 this.padWithTabs(out, depth); 202 out.print("<th class=\"jc-tabletitle\">"); 203 out.print(stk.nextToken()); 204 out.println("</th>"); 205 } 206 207 out.println("</tr>"); 208 } 209 210 211 HtmlElement oneElement = null; 212 213 for (Enumeration e = contents.elements(); e.hasMoreElements();) { 214 oneElement = (HtmlElement) e.nextElement(); 215 oneElement.display(out, depth + 1); 216 } 217 218 this.padWithTabs(out, depth); 219 out.println("</table>"); 220 setDisplayed(); 221 } 222 223 224 227 public synchronized void setAlignment(String newAlignment) 228 throws HtmlException { 229 String myName = (thisClass + "setAlignment(String)"); 230 231 if (newAlignment == null) { 232 alignment = null; 233 234 return; 235 } 236 if (newAlignment.equalsIgnoreCase("left")) { 237 alignment = newAlignment; 238 } else if (newAlignment.equalsIgnoreCase("right")) { 239 alignment = newAlignment; 240 } else if (newAlignment.equalsIgnoreCase("center")) { 241 alignment = newAlignment; 242 } else { 243 throw new HtmlException(myName + 244 ":Alignment must be left, right, or center for " + 245 getName()); 246 } 247 } 248 249 250 253 public synchronized void setBorder(int newBorder) 254 throws HtmlException { 255 border = newBorder; 256 } 257 258 259 264 public synchronized void setCaption(String newCaption) { 265 caption = newCaption; 266 } 267 268 271 public synchronized void setCellPadding(int newPadding) 272 throws HtmlException { 273 cellpadding = newPadding; 274 } 275 276 277 280 public synchronized void setCellSpacing(int newSpacing) 281 throws HtmlException { 282 cellspacing = newSpacing; 283 } 284 285 286 292 public synchronized void setTitle(String newTitleString) 293 throws HtmlException { 294 String myName = (thisClass + "setTitle(String)"); 295 296 if (newTitleString == null) { 297 throw new HtmlException(myName + ":Title string cannot be null"); 298 } 299 300 titleString = newTitleString; 301 } 302 303 304 310 public synchronized void setVerticalAlignment(String newAlignment) 311 throws HtmlException { 312 String myName = (thisClass + "setVerticalAlignment(String)"); 313 314 if (newAlignment.equalsIgnoreCase("top")) { 315 verticalAlignment = newAlignment; 316 } else if (newAlignment.equalsIgnoreCase("bottom")) { 317 verticalAlignment = newAlignment; 318 } else if (newAlignment.equalsIgnoreCase("center")) { 319 verticalAlignment = newAlignment; 320 } else { 321 throw new HtmlException(myName + 322 ":Alignment must be top, bottom," + 323 " or center for " + getName()); 324 } 325 } 326 327 328 331 public void setWidth(String newWidth) { 332 width = newWidth; 333 } 334 335 } 336 | Popular Tags |