1 29 30 package nextapp.echo2.webcontainer.syncpeer; 31 32 import nextapp.echo2.app.Extent; 33 import nextapp.echo2.webcontainer.RenderContext; 34 import nextapp.echo2.webcontainer.propertyrender.ExtentRender; 35 import nextapp.echo2.webrender.Service; 36 import nextapp.echo2.webrender.WebRenderServlet; 37 import nextapp.echo2.webrender.service.StaticBinaryService; 38 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 42 52 class TriCellTable { 53 54 private static final Service TRANSPARENT_SPACER_IMAGE_SERVICE = StaticBinaryService.forResource( 55 "Echo.TriCellTable.TransparentImage", "image/gif", "/nextapp/echo2/webcontainer/resource/image/Transparent.gif"); 56 57 static { 58 WebRenderServlet.getServiceRegistry().add(TRANSPARENT_SPACER_IMAGE_SERVICE); 59 } 60 61 static final int INVERTED = 1; 62 static final int VERTICAL = 2; 63 64 public static final int LEADING_TRAILING = 0; 65 public static final int TRAILING_LEADING = INVERTED; 66 public static final int TOP_BOTTOM = VERTICAL; 67 public static final int BOTTOM_TOP = INVERTED | VERTICAL; 68 69 private Element [] tdElements; 70 private Element [] marginTdElements = null; 71 private Element tableElement; 72 private Element tbodyElement; 73 private Document document; 74 private RenderContext rc; 75 76 85 private TriCellTable(RenderContext rc, Document document, String id) { 86 super(); 87 this.rc = rc; 88 this.document = document; 89 tableElement = document.createElement("table"); 90 tbodyElement = document.createElement("tbody"); 91 tbodyElement.setAttribute("id", id + "_tbody"); 92 tableElement.appendChild(tbodyElement); 93 } 94 95 111 TriCellTable(RenderContext rc, Document document, String id, int orientation0_1, Extent margin0_1) { 112 this(rc, document, id); 113 114 marginTdElements = new Element [1]; 115 tdElements = new Element [2]; 116 tdElements[0] = document.createElement("td"); 117 tdElements[0].setAttribute("id", id + "_td_0"); 118 tdElements[1] = document.createElement("td"); 119 tdElements[1].setAttribute("id", id + "_td_1"); 120 121 if (margin0_1 != null && margin0_1.getValue() > 0) { 122 marginTdElements[0] = document.createElement("td"); 123 marginTdElements[0].setAttribute("id", id + "_tdmargin_0_1"); 124 int size = ExtentRender.toPixels(margin0_1, 1); 125 if ((orientation0_1 & VERTICAL) == 0) { 126 marginTdElements[0].setAttribute("style", "width:" + size + "px;"); 127 addSpacer(marginTdElements[0], size, false); 128 } else { 129 marginTdElements[0].setAttribute("style", "height:" + size + "px;"); 130 addSpacer(marginTdElements[0], size, true); 131 } 132 } 133 134 if ((orientation0_1 & VERTICAL) == 0) { 135 Element trElement = document.createElement("tr"); 137 trElement.setAttribute("id", id + "_tr_0_1"); 138 if ((orientation0_1 & INVERTED) == 0) { 139 addColumn(trElement, tdElements[0]); 141 addColumn(trElement, marginTdElements[0]); 142 addColumn(trElement, tdElements[1]); 143 } else { 144 addColumn(trElement, tdElements[1]); 146 addColumn(trElement, marginTdElements[0]); 147 addColumn(trElement, tdElements[0]); 148 } 149 tbodyElement.appendChild(trElement); 150 } else { 151 if ((orientation0_1 & INVERTED) == 0) { 153 addRow(tdElements[0], id + "_tr_0"); 155 addRow(marginTdElements[0], id + "_trmargin_0_1"); 156 addRow(tdElements[1], id + "_tr_1"); 157 } else { 158 addRow(tdElements[1], id + "_tr_1"); 160 addRow(marginTdElements[0], id + "_trmargin_0_1"); 161 addRow(tdElements[0], id + "_tr_0"); 162 } 163 } 164 } 165 166 193 TriCellTable(RenderContext rc, Document document, String id, int orientation0_1, Extent margin0_1, int orientation01_2, 194 Extent margin01_2) { 195 this(rc, document, id); 196 197 Element trElement; 198 199 marginTdElements = new Element [2]; 200 tdElements = new Element [3]; 201 tdElements[0] = document.createElement("td"); 202 tdElements[0].setAttribute("id", id + "_td_0"); 203 tdElements[1] = document.createElement("td"); 204 tdElements[1].setAttribute("id", id + "_td_1"); 205 tdElements[2] = document.createElement("td"); 206 tdElements[2].setAttribute("id", id + "_td_2"); 207 208 if (margin0_1 != null || margin01_2 != null) { 210 if (margin0_1 != null && margin0_1.getValue() > 0) { 211 marginTdElements[0] = document.createElement("td"); 212 marginTdElements[0].setAttribute("id", id + "_tdmargin_0_1"); 213 214 int size = ExtentRender.toPixels(margin0_1, 1); 215 if ((orientation0_1 & VERTICAL) == 0) { 216 marginTdElements[0].setAttribute("style", "width:" + size + "px;"); 217 addSpacer(marginTdElements[0], size, false); 218 } else { 219 marginTdElements[0].setAttribute("style", "height:" + size + "px;"); 220 addSpacer(marginTdElements[0], size, true); 221 } 222 } 223 if (margin01_2 != null && margin01_2.getValue() > 0) { 224 marginTdElements[1] = document.createElement("td"); 225 marginTdElements[1].setAttribute("id", id + "_tdmargin_01_2"); 226 227 int size = ExtentRender.toPixels(margin01_2, 1); 228 if ((orientation01_2 & VERTICAL) == 0) { 229 marginTdElements[1].setAttribute("style", "width:" + size + "px;"); 230 addSpacer(marginTdElements[1], size, false); 231 } else { 232 marginTdElements[1].setAttribute("style", "height:" + size + "px;"); 233 addSpacer(marginTdElements[1], size, true); 234 } 235 } 236 } 237 238 if ((orientation0_1 & VERTICAL) == 0) { 239 if ((orientation01_2 & VERTICAL) == 0) { 241 trElement = document.createElement("tr"); 243 trElement.setAttribute("id", id + "_tr_0"); 244 if ((orientation01_2 & INVERTED) != 0) { 245 addColumn(trElement, tdElements[2]); 247 addColumn(trElement, marginTdElements[1]); 248 } 249 250 if ((orientation0_1 & INVERTED) == 0) { 252 addColumn(trElement, tdElements[0]); 254 addColumn(trElement, marginTdElements[0]); 255 addColumn(trElement, tdElements[1]); 256 } else { 257 addColumn(trElement, tdElements[1]); 259 addColumn(trElement, marginTdElements[0]); 260 addColumn(trElement, tdElements[0]); 261 } 262 263 if ((orientation01_2 & INVERTED) == 0) { 264 addColumn(trElement, marginTdElements[1]); 265 addColumn(trElement, tdElements[2]); 266 } 267 268 tbodyElement.appendChild(trElement); 269 } else { 270 272 int columns = (margin0_1 != null && margin0_1.getValue() > 0) ? 3 : 2; 274 tdElements[2].setAttribute("colspan", Integer.toString(columns)); 275 if (marginTdElements[1] != null) { 276 marginTdElements[1].setAttribute("colspan", Integer.toString(columns)); 277 } 278 279 if ((orientation01_2 & INVERTED) != 0) { 280 addRow(tdElements[2], id + "_tr_2"); 282 addRow(marginTdElements[1], id + "_trmargin_01_2"); 283 } 284 285 trElement = document.createElement("tr"); 287 trElement.setAttribute("id", "tr_" + id); 288 if ((orientation0_1 & INVERTED) == 0) { 289 addColumn(trElement, tdElements[0]); 291 addColumn(trElement, marginTdElements[0]); 292 addColumn(trElement, tdElements[1]); 293 } else { 294 addColumn(trElement, tdElements[1]); 296 addColumn(trElement, marginTdElements[0]); 297 addColumn(trElement, tdElements[0]); 298 } 299 tbodyElement.appendChild(trElement); 300 301 if ((orientation01_2 & INVERTED) == 0) { 302 addRow(marginTdElements[1], id + "_trmargin_01_2"); 304 addRow(tdElements[2], id + "_tr_2"); 305 } 306 } 307 } else { 308 if ((orientation01_2 & VERTICAL) == 0) { 310 312 int rows = (margin0_1 != null && margin0_1.getValue() > 0) ? 3 : 2; 314 tdElements[2].setAttribute("rowspan", Integer.toString(rows)); 315 if (marginTdElements[1] != null) { 316 marginTdElements[1].setAttribute("rowspan", Integer.toString(rows)); 317 } 318 319 trElement = document.createElement("tr"); 320 trElement.setAttribute("id", id + "_tr_0"); 321 if ((orientation01_2 & INVERTED) != 0) { 322 addColumn(trElement, tdElements[2]); 323 addColumn(trElement, marginTdElements[1]); 324 if ((orientation0_1 & INVERTED) == 0) { 325 addColumn(trElement, tdElements[0]); 326 } else { 327 addColumn(trElement, tdElements[1]); 328 } 329 } else { 330 if ((orientation0_1 & INVERTED) == 0) { 331 addColumn(trElement, tdElements[0]); 332 } else { 333 addColumn(trElement, tdElements[1]); 334 } 335 addColumn(trElement, marginTdElements[1]); 336 addColumn(trElement, tdElements[2]); 337 } 338 tbodyElement.appendChild(trElement); 339 addRow(marginTdElements[0], id + "_trmargin_0_1"); 340 if ((orientation0_1 & INVERTED) == 0) { 341 addRow(tdElements[1], id + "_tr_1"); 342 } else { 343 addRow(tdElements[0], id + "_tr_0"); 344 } 345 } else { 346 if ((orientation01_2 & INVERTED) != 0) { 348 addRow(tdElements[2], id + "_tr_2"); 350 addRow(marginTdElements[1], id + "_trmargin_01_2"); 351 } 352 353 if ((orientation0_1 & INVERTED) == 0) { 355 addRow(tdElements[0], id + "_tr_0"); 357 addRow(marginTdElements[0], id + "_trmargin_0_1"); 358 addRow(tdElements[1], id + "_tr_1"); 359 } else { 360 addRow(tdElements[1], id + "_tr_1"); 362 addRow(marginTdElements[0], id + "_trmargin_1_0"); 363 addRow(tdElements[0], id + "_tr_0"); 364 } 365 366 if ((orientation01_2 & INVERTED) == 0) { 367 addRow(marginTdElements[1], id + "_trmargin_01_2"); 369 addRow(tdElements[2], id + "_tr_2"); 370 } 371 } 372 } 373 } 374 375 382 private void addColumn(Element tr, Element td) { 383 if (td != null) { 384 tr.appendChild(td); 385 } 386 } 387 388 394 void addCellCssText(String cssText) { 395 for (int i = 0; i < tdElements.length; ++i) { 396 if (tdElements[i].hasAttribute("style")) { 397 tdElements[i].setAttribute("style", tdElements[i].getAttribute("style") + cssText); 398 } else { 399 tdElements[i].setAttribute("style", cssText); 400 } 401 } 402 if (marginTdElements != null) { 403 for (int i = 0; i < marginTdElements.length; ++i) { 404 if (marginTdElements[i] != null) { 405 if (marginTdElements[i].hasAttribute("style")) { 406 marginTdElements[i].setAttribute("style", marginTdElements[i].getAttribute("style") + cssText); 407 } else { 408 marginTdElements[i].setAttribute("style", cssText); 409 } 410 } 411 } 412 } 413 } 414 415 422 private void addRow(Element tdElement, String trElementId) { 423 if (tdElement != null) { 424 Element trElement = document.createElement("tr"); 425 trElement.setAttribute("id", trElementId); 426 trElement.appendChild(tdElement); 427 tbodyElement.appendChild(trElement); 428 } 429 } 430 431 444 private void addSpacer(Element parentElement, int size, boolean vertical) { 445 Element imgElement = document.createElement("img"); 446 imgElement.setAttribute("src", rc.getContainerInstance().getServiceUri(TRANSPARENT_SPACER_IMAGE_SERVICE)); 447 imgElement.setAttribute("alt", ""); 448 imgElement.setAttribute("width", vertical ? "1" : Integer.toString(size)); 449 imgElement.setAttribute("height", vertical ? Integer.toString(size) : "1"); 450 parentElement.appendChild(imgElement); 451 } 452 453 458 Element getTableElement() { 459 return tableElement; 460 } 461 462 470 Element getTdElement(int index) { 471 return tdElements[index]; 472 } 473 474 483 Element getMarginTdElement(int index) { 484 return marginTdElements[index]; 485 } 486 } 487 | Popular Tags |