1 29 30 package nextapp.echo2.webcontainer.syncpeer; 31 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.DocumentFragment ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 38 import nextapp.echo2.app.Border; 39 import nextapp.echo2.app.Color; 40 import nextapp.echo2.app.Component; 41 import nextapp.echo2.app.Extent; 42 import nextapp.echo2.app.FillImage; 43 import nextapp.echo2.app.Font; 44 import nextapp.echo2.app.ImageReference; 45 import nextapp.echo2.app.Insets; 46 import nextapp.echo2.app.LayoutData; 47 import nextapp.echo2.app.Table; 48 import nextapp.echo2.app.layout.TableLayoutData; 49 import nextapp.echo2.app.list.ListSelectionModel; 50 import nextapp.echo2.app.table.TableColumnModel; 51 import nextapp.echo2.app.update.ServerComponentUpdate; 52 import nextapp.echo2.webcontainer.ActionProcessor; 53 import nextapp.echo2.webcontainer.ContainerInstance; 54 import nextapp.echo2.webcontainer.DomUpdateSupport; 55 import nextapp.echo2.webcontainer.PartialUpdateManager; 56 import nextapp.echo2.webcontainer.PropertyUpdateProcessor; 57 import nextapp.echo2.webcontainer.RenderContext; 58 import nextapp.echo2.webcontainer.ComponentSynchronizePeer; 59 import nextapp.echo2.webcontainer.SynchronizePeerFactory; 60 import nextapp.echo2.webcontainer.image.ImageRenderSupport; 61 import nextapp.echo2.webcontainer.propertyrender.BorderRender; 62 import nextapp.echo2.webcontainer.propertyrender.CellLayoutDataRender; 63 import nextapp.echo2.webcontainer.propertyrender.ColorRender; 64 import nextapp.echo2.webcontainer.propertyrender.ExtentRender; 65 import nextapp.echo2.webcontainer.propertyrender.FillImageRender; 66 import nextapp.echo2.webcontainer.propertyrender.FontRender; 67 import nextapp.echo2.webcontainer.propertyrender.InsetsRender; 68 import nextapp.echo2.webrender.ClientProperties; 69 import nextapp.echo2.webrender.ServerMessage; 70 import nextapp.echo2.webrender.Service; 71 import nextapp.echo2.webrender.WebRenderServlet; 72 import nextapp.echo2.webrender.output.CssStyle; 73 import nextapp.echo2.webrender.servermessage.DomUpdate; 74 import nextapp.echo2.webrender.service.JavaScriptService; 75 import nextapp.echo2.webrender.util.DomUtil; 76 77 83 public class TablePeer 84 implements ActionProcessor, ComponentSynchronizePeer, ImageRenderSupport, PropertyUpdateProcessor { 85 86 88 91 private static final String SIZING_DOTS = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . " 92 + ". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . "; 93 94 private static final String [] TABLE_INIT_KEYS = new String []{"rollover-style", "selection-style"}; 95 96 private static final String PROPERTY_SELECTION = "selection"; 97 98 private static final String IMAGE_ID_ROLLOVER_BACKGROUND = "rolloverBackground"; 99 private static final String IMAGE_ID_SELECTION_BACKGROUND = "selectionBackground"; 100 101 104 private static final Service TABLE_SERVICE = JavaScriptService.forResource("Echo.Table", 105 "/nextapp/echo2/webcontainer/resource/js/Table.js"); 106 107 static { 108 WebRenderServlet.getServiceRegistry().add(TABLE_SERVICE); 109 } 110 111 protected PartialUpdateManager propertyRenderRegistry; 112 113 116 public String getContainerId(Component child) { 117 return ContainerInstance.getElementId(child.getParent()) + "_cell_" + child.getRenderId(); 118 } 119 120 123 public ImageReference getImage(Component component, String imageId) { 124 if (IMAGE_ID_ROLLOVER_BACKGROUND.equals(imageId)) { 125 FillImage backgroundImage 126 = (FillImage) component.getRenderProperty(Table.PROPERTY_ROLLOVER_BACKGROUND_IMAGE); 127 if (backgroundImage == null) { 128 return null; 129 } else { 130 return backgroundImage.getImage(); 131 } 132 } else if (IMAGE_ID_SELECTION_BACKGROUND.equals(imageId)) { 133 FillImage backgroundImage 134 = (FillImage) component.getRenderProperty(Table.PROPERTY_SELECTION_BACKGROUND_IMAGE); 135 if (backgroundImage == null) { 136 return null; 137 } else { 138 return backgroundImage.getImage(); 139 } 140 } else { 141 return CellLayoutDataRender.getCellLayoutDataBackgroundImage(component, imageId); 143 } 144 } 145 146 155 private TableLayoutData getLayoutData(Component child) { 156 LayoutData layoutData = (LayoutData) child.getRenderProperty(Component.PROPERTY_LAYOUT_DATA); 157 if (layoutData == null) { 158 return null; 159 } else if (layoutData instanceof TableLayoutData) { 160 return (TableLayoutData) layoutData; 161 } else { 162 throw new RuntimeException ("Invalid LayoutData for Table Child: " + layoutData.getClass().getName()); 163 } 164 } 165 166 170 public void processAction(ContainerInstance ci, Component component, Element actionElement) { 171 ci.getUpdateManager().getClientUpdateManager().setComponentAction(component, Table.INPUT_ACTION, null); 172 } 173 174 178 public void processPropertyUpdate(ContainerInstance ci, Component component, Element propertyElement) { 179 String propertyName = propertyElement.getAttribute(PropertyUpdateProcessor.PROPERTY_NAME); 180 if (PROPERTY_SELECTION.equals(propertyName)) { 181 Element [] optionElements = DomUtil.getChildElementsByTagName(propertyElement, "row"); 182 int[] selectedIndices = new int[optionElements.length]; 183 for (int i = 0; i < optionElements.length; ++i) { 184 selectedIndices[i] = Integer.parseInt(optionElements[i].getAttribute("index")); 185 } 186 ci.getUpdateManager().getClientUpdateManager().setComponentProperty(component, 187 Table.SELECTION_CHANGED_PROPERTY, selectedIndices); 188 } 189 } 190 191 195 public void renderAdd(RenderContext rc, ServerComponentUpdate update, String targetId, Component component) { 196 Table table = (Table) component; 197 Border border = (Border) table.getRenderProperty(Table.PROPERTY_BORDER); 198 Insets tableInsets = (Insets) table.getRenderProperty(Table.PROPERTY_INSETS); 199 String defaultInsetsAttributeValue = tableInsets == null 200 ? "0px" : InsetsRender.renderCssAttributeValue(tableInsets); 201 CssStyle styleCss = new CssStyle(); 202 styleCss.setAttribute("padding", defaultInsetsAttributeValue); 203 BorderRender.renderToStyle(styleCss, border); 204 DomUpdate.renderStyleSheetAddRule(rc.getServerMessage(), "TD.c-" + component.getRenderId(), styleCss.renderInline()); 205 206 Element domAddTableElement = DomUpdate.renderElementAdd(rc.getServerMessage()); 207 DocumentFragment htmlFragment = rc.getServerMessage().getDocument().createDocumentFragment(); 208 renderHtml(rc, update, htmlFragment, component); 209 DomUpdate.renderElementAddContent(rc.getServerMessage(), domAddTableElement, targetId, htmlFragment); 210 } 211 212 220 private void renderAddChild(RenderContext rc, ServerComponentUpdate update, Element parentElement, Component child) { 221 if (!child.isVisible()) { 222 return; 224 } 225 ComponentSynchronizePeer syncPeer = SynchronizePeerFactory.getPeerForComponent(child.getClass()); 226 if (syncPeer instanceof DomUpdateSupport) { 227 ((DomUpdateSupport) syncPeer).renderHtml(rc, update, parentElement, child); 228 } else { 229 syncPeer.renderAdd(rc, update, getContainerId(child), child); 230 } 231 } 232 233 237 public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) { 238 rc.getServerMessage().addLibrary(TABLE_SERVICE.getId()); 239 renderDisposeDirective(rc, (Table) component); 240 } 241 242 250 private void renderDisposeDirective(RenderContext rc, Table table) { 251 DomUpdate.renderStyleSheetRemoveRule(rc.getServerMessage(), "TD.c-" + table.getRenderId()); 253 254 ServerMessage serverMessage = rc.getServerMessage(); 255 Element itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_PREREMOVE, 256 "EchoTable.MessageProcessor", "dispose", new String [0], new String [0]); 257 Element itemElement = serverMessage.getDocument().createElement("item"); 258 itemElement.setAttribute("eid", ContainerInstance.getElementId(table)); 259 itemizedUpdateElement.appendChild(itemElement); 260 } 261 262 266 public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node parentNode, Component component) { 267 ServerMessage serverMessage = rc.getServerMessage(); 268 serverMessage.addLibrary(TABLE_SERVICE.getId()); 269 Table table = (Table) component; 270 271 renderInitDirective(rc, table); 272 273 Border border = (Border) table.getRenderProperty(Table.PROPERTY_BORDER); 274 Extent borderSize = border == null ? null : border.getSize(); 275 276 String elementId = ContainerInstance.getElementId(table); 277 278 Document document = parentNode.getOwnerDocument(); 279 Element tableElement = document.createElement("table"); 280 tableElement.setAttribute("id", elementId); 281 282 CssStyle tableCssStyle = new CssStyle(); 283 tableCssStyle.setAttribute("border-collapse", "collapse"); 284 285 if (((Boolean ) table.getRenderProperty(Table.PROPERTY_SELECTION_ENABLED, Boolean.FALSE)).booleanValue()) { 286 tableCssStyle.setAttribute("cursor", "pointer"); 287 } 288 289 Insets tableInsets = (Insets) table.getRenderProperty(Table.PROPERTY_INSETS); 290 291 String defaultInsetsAttributeValue = tableInsets == null ? "0px" : InsetsRender.renderCssAttributeValue(tableInsets); 292 293 ColorRender.renderToStyle(tableCssStyle, component); 294 FontRender.renderToStyle(tableCssStyle, component); 295 BorderRender.renderToStyle(tableCssStyle, border); 296 if (borderSize != null) { 297 if (!rc.getContainerInstance().getClientProperties().getBoolean( 298 ClientProperties.QUIRK_CSS_BORDER_COLLAPSE_INSIDE)) { 299 tableCssStyle.setAttribute("margin", ExtentRender.renderCssAttributeValueHalf(borderSize)); 300 } 301 } 302 303 Extent width = (Extent) table.getRenderProperty(Table.PROPERTY_WIDTH); 304 boolean render100PercentWidthWorkaround = false; 305 if (rc.getContainerInstance().getClientProperties().getBoolean( 306 ClientProperties.QUIRK_IE_TABLE_PERCENT_WIDTH_SCROLLBAR_ERROR)) { 307 if (width != null && width.getUnits() == Extent.PERCENT && width.getValue() == 100) { 308 width = null; 309 render100PercentWidthWorkaround = true; 310 } 311 } 312 ExtentRender.renderToStyle(tableCssStyle, "width", width); 313 314 tableElement.setAttribute("style", tableCssStyle.renderInline()); 315 316 parentNode.appendChild(tableElement); 317 318 TableColumnModel columnModel = table.getColumnModel(); 319 int columnCount = columnModel.getColumnCount(); 320 321 boolean someColumnsHaveWidths = false; 322 for (int i = 0; i < columnCount; ++i) { 323 if (columnModel.getColumn(i).getWidth() != null) { 324 someColumnsHaveWidths = true; 325 } 326 } 327 if (someColumnsHaveWidths) { 328 Element colGroupElement = document.createElement("colgroup"); 329 tableElement.appendChild(colGroupElement); 330 331 for (int i = 0; i < columnCount; ++i) { 332 Element colElement = document.createElement("col"); 333 Extent columnWidth = columnModel.getColumn(i).getWidth(); 334 if (columnWidth != null) { 335 colElement.setAttribute("width", ExtentRender.renderCssAttributeValue(columnWidth)); 336 } 337 colGroupElement.appendChild(colElement); 338 } 339 } 340 341 Element tbodyElement = document.createElement("tbody"); 342 tbodyElement.setAttribute("id", elementId + "_tbody"); 343 tableElement.appendChild(tbodyElement); 344 345 Element firstTrElement = null; 346 347 if (table.isHeaderVisible()) { 348 firstTrElement = renderRow(rc, update, tbodyElement, table, Table.HEADER_ROW, defaultInsetsAttributeValue); 349 } 350 351 int rows = table.getModel().getRowCount(); 352 for (int rowIndex = 0; rowIndex < rows; ++rowIndex) { 353 if (firstTrElement == null && rowIndex == 0) { 354 firstTrElement = renderRow(rc, update, tbodyElement, table, rowIndex, defaultInsetsAttributeValue); 355 } else { 356 renderRow(rc, update, tbodyElement, table, rowIndex, defaultInsetsAttributeValue); 357 } 358 } 359 360 if (render100PercentWidthWorkaround && firstTrElement != null) { 361 NodeList childNodes = firstTrElement.getChildNodes(); 363 int length = childNodes.getLength(); 364 for (int i = 0; i < length; ++i) { 365 if (!"td".equals(childNodes.item(i).getNodeName())) { 366 continue; 367 } 368 Element tdElement = (Element ) childNodes.item(i); 369 Element sizingDivElement = document.createElement("div"); 370 sizingDivElement.setAttribute("style", "font-size:50px;height:0px;overflow:hidden;"); 371 sizingDivElement.appendChild(document.createTextNode(SIZING_DOTS)); 372 tdElement.appendChild(sizingDivElement); 373 } 374 } 375 } 376 377 385 private void renderInitDirective(RenderContext rc, Table table) { 386 String elementId = ContainerInstance.getElementId(table); 387 ServerMessage serverMessage = rc.getServerMessage(); 388 Document document = serverMessage.getDocument(); 389 390 boolean rolloverEnabled = ((Boolean ) table.getRenderProperty(Table.PROPERTY_ROLLOVER_ENABLED, 391 Boolean.FALSE)).booleanValue(); 392 boolean selectionEnabled = ((Boolean ) table.getRenderProperty(Table.PROPERTY_SELECTION_ENABLED, 393 Boolean.FALSE)).booleanValue(); 394 395 String rolloverStyle = ""; 396 if (rolloverEnabled) { 397 CssStyle rolloverCssStyle = new CssStyle(); 398 ColorRender.renderToStyle(rolloverCssStyle, 399 (Color) table.getRenderProperty(Table.PROPERTY_ROLLOVER_FOREGROUND), 400 (Color) table.getRenderProperty(Table.PROPERTY_ROLLOVER_BACKGROUND)); 401 FontRender.renderToStyle(rolloverCssStyle, 402 (Font) table.getRenderProperty(Table.PROPERTY_ROLLOVER_FONT)); 403 FillImageRender.renderToStyle(rolloverCssStyle, rc, this, table, IMAGE_ID_ROLLOVER_BACKGROUND, 404 (FillImage) table.getRenderProperty(Table.PROPERTY_ROLLOVER_BACKGROUND_IMAGE), 405 FillImageRender.FLAG_DISABLE_FIXED_MODE); 406 if (rolloverCssStyle.hasAttributes()) { 407 rolloverStyle = rolloverCssStyle.renderInline(); 408 } 409 } 410 411 String selectionStyle = ""; 412 if (selectionEnabled) { 413 CssStyle selectionCssStyle = new CssStyle(); 414 ColorRender.renderToStyle(selectionCssStyle, 415 (Color) table.getRenderProperty(Table.PROPERTY_SELECTION_FOREGROUND), 416 (Color) table.getRenderProperty(Table.PROPERTY_SELECTION_BACKGROUND)); 417 FontRender.renderToStyle(selectionCssStyle, 418 (Font) table.getRenderProperty(Table.PROPERTY_SELECTION_FONT)); 419 FillImageRender.renderToStyle(selectionCssStyle, rc, this, table, IMAGE_ID_SELECTION_BACKGROUND, 420 (FillImage) table.getRenderProperty(Table.PROPERTY_SELECTION_BACKGROUND_IMAGE), 421 FillImageRender.FLAG_DISABLE_FIXED_MODE); 422 if (selectionCssStyle.hasAttributes()) { 423 selectionStyle = selectionCssStyle.renderInline(); 424 } 425 } 426 427 Element itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, 428 "EchoTable.MessageProcessor", "init", TABLE_INIT_KEYS, 429 new String []{rolloverStyle, selectionStyle}); 430 Element itemElement = document.createElement("item"); 431 itemElement.setAttribute("eid", elementId); 432 if (table.isHeaderVisible()) { 433 itemElement.setAttribute("header-visible", "true"); 434 } 435 436 if (table.hasActionListeners()) { 437 itemElement.setAttribute("server-notify", "true"); 438 } 439 440 if (rolloverEnabled) { 441 itemElement.setAttribute("rollover-enabled", "true"); 442 } 443 444 if (selectionEnabled) { 445 itemElement.setAttribute("selection-enabled", "true"); 446 ListSelectionModel selectionModel = table.getSelectionModel(); 447 if (selectionModel.getSelectionMode() == ListSelectionModel.MULTIPLE_SELECTION) { 448 itemElement.setAttribute("selection-mode", "multiple"); 449 } 450 if (selectionModel.getMinSelectedIndex() != -1) { 451 Element selectionElement = document.createElement("selection"); 452 int minimumIndex = selectionModel.getMinSelectedIndex(); 453 int maximumIndex = selectionModel.getMaxSelectedIndex(); 454 if (maximumIndex > table.getModel().getRowCount() - 1) { 455 maximumIndex = table.getModel().getRowCount() - 1; 456 } 457 for (int i = minimumIndex; i <= maximumIndex; ++i) { 458 if (selectionModel.isSelectedIndex(i)) { 459 Element rowElement = document.createElement("row"); 460 rowElement.setAttribute("index", Integer.toString(i)); 461 selectionElement.appendChild(rowElement); 462 } 463 } 464 itemElement.appendChild(selectionElement); 465 } 466 } 467 468 if (!table.isRenderEnabled()) { 469 itemElement.setAttribute("enabled", "false"); 470 } 471 472 itemizedUpdateElement.appendChild(itemElement); 473 } 474 475 487 private Element renderRow(RenderContext rc, ServerComponentUpdate update, Element tbodyElement, Table table, int rowIndex, 488 String defaultInsetsAttributeValue) { 489 Document document = tbodyElement.getOwnerDocument(); 490 String elementId = ContainerInstance.getElementId(table); 491 492 Element trElement = document.createElement("tr"); 493 if (rowIndex == Table.HEADER_ROW) { 494 trElement.setAttribute("id", elementId + "_tr_header"); 495 } else { 496 trElement.setAttribute("id", elementId + "_tr_" + rowIndex); 497 } 498 tbodyElement.appendChild(trElement); 499 500 String className = "c-" + table.getRenderId(); 501 502 boolean inlineStyleRequired = rc.getContainerInstance().getClientProperties().getBoolean( 503 ClientProperties.NOT_SUPPORTED_CSS_MANIPULATION); 504 Border border = null; 505 if (inlineStyleRequired) { 506 border = (Border) table.getRenderProperty(Table.PROPERTY_BORDER); 507 } 508 509 int columns = table.getColumnModel().getColumnCount(); 510 for (int columnIndex = 0; columnIndex < columns; ++columnIndex) { 511 Component childComponent = table.getCellComponent(columnIndex, rowIndex); 512 Element tdElement = document.createElement("td"); 513 tdElement.setAttribute("id", elementId + "_cell_" + childComponent.getRenderId()); 514 515 CssStyle tdCssStyle = new CssStyle(); 516 517 if (inlineStyleRequired) { 518 BorderRender.renderToStyle(tdCssStyle, border); 519 CellLayoutDataRender.renderToElementAndStyle(tdElement, tdCssStyle, childComponent, getLayoutData(childComponent), 520 defaultInsetsAttributeValue); 521 } else { 522 tdElement.setAttribute("class", className); 523 CellLayoutDataRender.renderToElementAndStyle(tdElement, tdCssStyle, childComponent, getLayoutData(childComponent), 524 null); 525 } 526 527 CellLayoutDataRender.renderBackgroundImageToStyle(tdCssStyle, rc, this, table, childComponent); 528 if (tdCssStyle.hasAttributes()) { 529 tdElement.setAttribute("style", tdCssStyle.renderInline()); 530 } 531 532 trElement.appendChild(tdElement); 533 534 renderAddChild(rc, update, tdElement, childComponent); 535 } 536 537 return trElement; 538 } 539 540 544 public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String targetId) { 545 Table table = (Table) update.getParent(); 546 renderDisposeDirective(rc, table); 547 DomUpdate.renderElementRemove(rc.getServerMessage(), ContainerInstance.getElementId(table)); 548 renderAdd(rc, update, targetId, table); 549 return true; 550 } 551 } 552 | Popular Tags |