1 29 30 package nextapp.echo2.webcontainer.syncpeer; 31 32 import nextapp.echo2.app.Alignment; 33 import nextapp.echo2.app.Border; 34 import nextapp.echo2.app.CheckBox; 35 import nextapp.echo2.app.Color; 36 import nextapp.echo2.app.Component; 37 import nextapp.echo2.app.Extent; 38 import nextapp.echo2.app.FillImage; 39 import nextapp.echo2.app.Font; 40 import nextapp.echo2.app.ImageReference; 41 import nextapp.echo2.app.Insets; 42 import nextapp.echo2.app.RadioButton; 43 import nextapp.echo2.app.ResourceImageReference; 44 import nextapp.echo2.app.button.AbstractButton; 45 import nextapp.echo2.app.button.ButtonGroup; 46 import nextapp.echo2.app.button.ToggleButton; 47 import nextapp.echo2.app.update.ServerComponentUpdate; 48 import nextapp.echo2.webcontainer.ActionProcessor; 49 import nextapp.echo2.webcontainer.ContainerInstance; 50 import nextapp.echo2.webcontainer.DomUpdateSupport; 51 import nextapp.echo2.webcontainer.PropertyUpdateProcessor; 52 import nextapp.echo2.webcontainer.RenderContext; 53 import nextapp.echo2.webcontainer.ComponentSynchronizePeer; 54 import nextapp.echo2.webcontainer.image.ImageRenderSupport; 55 import nextapp.echo2.webcontainer.image.ImageTools; 56 import nextapp.echo2.webcontainer.propertyrender.AlignmentRender; 57 import nextapp.echo2.webcontainer.propertyrender.BorderRender; 58 import nextapp.echo2.webcontainer.propertyrender.ColorRender; 59 import nextapp.echo2.webcontainer.propertyrender.ExtentRender; 60 import nextapp.echo2.webcontainer.propertyrender.FillImageRender; 61 import nextapp.echo2.webcontainer.propertyrender.FontRender; 62 import nextapp.echo2.webcontainer.propertyrender.ImageReferenceRender; 63 import nextapp.echo2.webcontainer.propertyrender.InsetsRender; 64 import nextapp.echo2.webcontainer.propertyrender.LayoutDirectionRender; 65 import nextapp.echo2.webrender.ServerMessage; 66 import nextapp.echo2.webrender.Service; 67 import nextapp.echo2.webrender.WebRenderServlet; 68 import nextapp.echo2.webrender.output.CssStyle; 69 import nextapp.echo2.webrender.servermessage.DomUpdate; 70 import nextapp.echo2.webrender.service.JavaScriptService; 71 72 import org.w3c.dom.Document ; 73 import org.w3c.dom.DocumentFragment ; 74 import org.w3c.dom.Element ; 75 import org.w3c.dom.Node ; 76 import org.w3c.dom.Text ; 77 78 85 public class ButtonPeer 86 implements ActionProcessor, DomUpdateSupport, ImageRenderSupport, PropertyUpdateProcessor, ComponentSynchronizePeer { 87 88 private static final Alignment DEFAULT_TEXT_POSITION = new Alignment(Alignment.TRAILING, Alignment.DEFAULT); 89 private static final Alignment DEFAULT_STATE_POSITION = new Alignment(Alignment.LEADING, Alignment.DEFAULT); 90 private static final Extent DEFAULT_ICON_TEXT_MARGIN = new Extent(5); 91 private static final ImageReference DEFAULT_CHECKBOX_ICON 92 = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/CheckBoxOff.gif"); 93 private static final ImageReference DEFAULT_SELECTED_CHECKBOX_ICON 94 = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/CheckBoxOn.gif"); 95 private static final ImageReference DEFAULT_RADIOBUTTON_ICON 96 = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/RadioButtonOff.gif"); 97 private static final ImageReference DEFAULT_SELECTED_RADIOBUTTON_ICON 98 = new ResourceImageReference("/nextapp/echo2/webcontainer/resource/image/RadioButtonOn.gif"); 99 100 private static final String [] BUTTON_INIT_KEYS = new String []{"default-style", "rollover-style", "pressed-style"}; 101 102 private static final String IMAGE_ID_BACKGROUND = "background"; 103 private static final String IMAGE_ID_ICON = "icon"; 104 private static final String IMAGE_ID_ROLLOVER_BACKGROUND = "rolloverBackground"; 105 private static final String IMAGE_ID_ROLLOVER_ICON = "rolloverIcon"; 106 private static final String IMAGE_ID_ROLLOVER_STATE_ICON = "rolloverStateIcon"; 107 private static final String IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON = "rolloverSelectedStateIcon"; 108 private static final String IMAGE_ID_PRESSED_BACKGROUND = "pressedBackground"; 109 private static final String IMAGE_ID_PRESSED_ICON = "pressedIcon"; 110 private static final String IMAGE_ID_PRESSED_STATE_ICON = "pressedStateIcon"; 111 private static final String IMAGE_ID_PRESSED_SELECTED_STATE_ICON = "pressedSelectedStateIcon"; 112 private static final String IMAGE_ID_STATE_ICON = "stateIcon"; 113 private static final String IMAGE_ID_SELECTED_STATE_ICON = "selectedStateIcon"; 114 115 private static final String CONTAINER_TABLE_CSS_TEXT_DEFAULT = "border:0px none;border-collapse:collapse;"; 116 private static final String CONTAINER_TABLE_CSS_TEXT_LEFT = "border:0px none;border-collapse:collapse; margin: 0 auto 0 0"; 117 private static final String CONTAINER_TABLE_CSS_TEXT_CENTER = "border:0px none;border-collapse:collapse; margin: 0 auto;"; 118 private static final String CONTAINER_TABLE_CSS_TEXT_RIGHT = "border:0px none;border-collapse:collapse; margin: 0 0 0 auto;"; 119 120 123 private static final Service BUTTON_SERVICE = JavaScriptService.forResource("Echo.Button", 124 "/nextapp/echo2/webcontainer/resource/js/Button.js"); 125 126 static { 127 WebRenderServlet.getServiceRegistry().add(BUTTON_SERVICE); 128 } 129 130 137 private static String getContainerTableCssText(AbstractButton button) { 138 Alignment alignment = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT); 139 if (alignment != null) { 140 int horizontal = AlignmentRender.getRenderedHorizontal(alignment, button); 141 switch (horizontal) { 142 case Alignment.LEFT: 143 return CONTAINER_TABLE_CSS_TEXT_LEFT; 144 case Alignment.CENTER: 145 return CONTAINER_TABLE_CSS_TEXT_CENTER; 146 case Alignment.RIGHT: 147 return CONTAINER_TABLE_CSS_TEXT_RIGHT; 148 } 149 } 150 return CONTAINER_TABLE_CSS_TEXT_DEFAULT; 151 } 152 153 156 public String getContainerId(Component child) { 157 throw new UnsupportedOperationException ("Component does not support children."); 158 } 159 160 170 private Alignment combineAlignment(Alignment primary, Alignment secondary) { 171 if (primary == null) { 172 return secondary; 173 } else if (secondary == null) { 174 return primary; 175 } 176 int horizontal = primary.getHorizontal(); 177 int vertical = primary.getVertical(); 178 Alignment alignment = new Alignment(horizontal == Alignment.DEFAULT ? secondary.getHorizontal() : horizontal, 179 vertical == Alignment.DEFAULT ? secondary.getVertical() : vertical); 180 return alignment; 181 } 182 183 187 public ImageReference getImage(Component component, String imageId) { 188 if (IMAGE_ID_ICON.equals(imageId)) { 189 if (component.isRenderEnabled()) { 190 return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ICON); 191 } else { 192 ImageReference icon = (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_DISABLED_ICON); 193 if (icon == null) { 194 icon = (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ICON); 195 } 196 return icon; 197 } 198 } else if (IMAGE_ID_ROLLOVER_ICON.equals(imageId)) { 199 return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ICON); 200 } else if (IMAGE_ID_PRESSED_ICON.equals(imageId)) { 201 return (ImageReference) component.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ICON); 202 } else if (IMAGE_ID_STATE_ICON.equals(imageId)) { 203 return getStateIcon((ToggleButton) component); 204 } else if (IMAGE_ID_SELECTED_STATE_ICON.equals(imageId)) { 205 return getSelectedStateIcon((ToggleButton) component); 206 } else if (IMAGE_ID_BACKGROUND.equals(imageId)) { 207 FillImage backgroundImage; 208 if (component.isRenderEnabled()) { 209 backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE); 210 } else { 211 backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND_IMAGE); 212 if (backgroundImage == null) { 213 backgroundImage = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE); 214 } 215 } 216 if (backgroundImage == null) { 217 return null; 218 } else { 219 return backgroundImage.getImage(); 220 } 221 } else if (IMAGE_ID_ROLLOVER_BACKGROUND.equals(imageId)) { 222 FillImage backgroundImage 223 = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND_IMAGE); 224 if (backgroundImage == null) { 225 return null; 226 } else { 227 return backgroundImage.getImage(); 228 } 229 } else if (IMAGE_ID_PRESSED_BACKGROUND.equals(imageId)) { 230 FillImage backgroundImage 231 = (FillImage) component.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND_IMAGE); 232 if (backgroundImage == null) { 233 return null; 234 } else { 235 return backgroundImage.getImage(); 236 } 237 } else if (IMAGE_ID_ROLLOVER_STATE_ICON.equals(imageId)) { 238 ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_STATE_ICON); 239 return icon == null ? getStateIcon((ToggleButton) component) : icon; 240 } else if (IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON.equals(imageId)) { 241 ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_SELECTED_STATE_ICON); 242 return icon == null ? getSelectedStateIcon((ToggleButton) component) : icon; 243 } else if (IMAGE_ID_PRESSED_STATE_ICON.equals(imageId)) { 244 ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_PRESSED_STATE_ICON); 245 return icon == null ? getStateIcon((ToggleButton) component) : icon; 246 } else if (IMAGE_ID_PRESSED_SELECTED_STATE_ICON.equals(imageId)) { 247 ImageReference icon = (ImageReference) component.getRenderProperty(ToggleButton.PROPERTY_PRESSED_SELECTED_STATE_ICON); 248 return icon == null ? getSelectedStateIcon((ToggleButton) component) : icon; 249 } else { 250 return null; 251 } 252 } 253 254 261 private ImageReference getSelectedStateIcon(ToggleButton toggleButton) { 262 ImageReference selectedStateIcon 263 = (ImageReference) toggleButton.getRenderProperty(ToggleButton.PROPERTY_SELECTED_STATE_ICON); 264 if (selectedStateIcon == null) { 265 if (toggleButton instanceof CheckBox) { 266 selectedStateIcon = DEFAULT_SELECTED_CHECKBOX_ICON; 267 } else if (toggleButton instanceof RadioButton) { 268 selectedStateIcon = DEFAULT_SELECTED_RADIOBUTTON_ICON; 269 } 270 } 271 return selectedStateIcon; 272 } 273 274 281 private ImageReference getStateIcon(ToggleButton toggleButton) { 282 ImageReference stateIcon = (ImageReference) toggleButton.getRenderProperty(ToggleButton.PROPERTY_STATE_ICON); 283 if (stateIcon == null) { 284 if (toggleButton instanceof CheckBox) { 285 stateIcon = DEFAULT_CHECKBOX_ICON; 286 } else if (toggleButton instanceof RadioButton) { 287 stateIcon = DEFAULT_RADIOBUTTON_ICON; 288 } 289 } 290 return stateIcon; 291 } 292 293 297 public void processAction(ContainerInstance ci, Component component, Element actionElement) { 298 ci.getUpdateManager().getClientUpdateManager().setComponentAction(component, AbstractButton.INPUT_CLICK, null); 299 } 300 301 305 public void processPropertyUpdate(ContainerInstance ci, Component component, Element propertyElement) { 306 307 String propertyName = propertyElement.getAttribute(PropertyUpdateProcessor.PROPERTY_NAME); 308 if (ToggleButton.SELECTED_CHANGED_PROPERTY.equals(propertyName)) { 309 Boolean propertyValue = new Boolean ("true".equals(propertyElement.getAttribute("value"))); 310 ci.getUpdateManager().getClientUpdateManager().setComponentProperty(component, 311 ToggleButton.SELECTED_CHANGED_PROPERTY, propertyValue); 312 } 313 } 314 315 319 public void renderAdd(RenderContext rc, ServerComponentUpdate update, String targetId, Component component) { 320 Element domAddElement = DomUpdate.renderElementAdd(rc.getServerMessage()); 321 DocumentFragment htmlFragment = rc.getServerMessage().getDocument().createDocumentFragment(); 322 renderHtml(rc, update, htmlFragment, component); 323 DomUpdate.renderElementAddContent(rc.getServerMessage(), domAddElement, targetId, htmlFragment); 324 } 325 326 335 private Element renderButtonContainer(RenderContext rc, Node parentNode, AbstractButton button) { 336 Element divElement = parentNode.getOwnerDocument().createElement("div"); 337 divElement.setAttribute("id", ContainerInstance.getElementId(button)); 338 339 if (button.isFocusTraversalParticipant()) { 340 divElement.setAttribute("tabindex", Integer.toString(button.getFocusTraversalIndex())); 341 } else { 342 divElement.setAttribute("tabindex", "-1"); 343 } 344 345 boolean renderEnabled = button.isRenderEnabled(); 346 347 String toolTipText = (String ) button.getRenderProperty(AbstractButton.PROPERTY_TOOL_TIP_TEXT); 348 if (renderEnabled && toolTipText != null) { 349 divElement.setAttribute("title", toolTipText); 350 } 351 divElement.setAttribute("style", "visibility:hidden;"); 352 353 parentNode.appendChild(divElement); 354 return divElement; 355 } 356 357 365 private void renderButtonContent(RenderContext rc, Element buttonContainerElement, AbstractButton button) { 366 Node contentNode; 367 Document document = rc.getServerMessage().getDocument(); 368 ToggleButton toggleButton = button instanceof ToggleButton ? (ToggleButton) button : null; 369 String elementId = ContainerInstance.getElementId(button); 370 371 String text = (String ) button.getRenderProperty(AbstractButton.PROPERTY_TEXT); 372 ImageReference icon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_ICON); 373 374 Text textNode = text == null ? null : rc.getServerMessage().getDocument().createTextNode( 376 (String ) button.getRenderProperty(AbstractButton.PROPERTY_TEXT)); 377 378 Element iconElement; 379 if (icon == null) { 380 iconElement = null; 381 } else { 382 iconElement = ImageReferenceRender.renderImageReferenceElement(rc, ButtonPeer.this, button, 383 IMAGE_ID_ICON); 384 iconElement.setAttribute("id", elementId + "_icon"); 385 } 386 387 Element stateIconElement; 388 if (toggleButton == null) { 389 stateIconElement = null; 390 } else { 391 stateIconElement = ImageReferenceRender.renderImageReferenceElement(rc, ButtonPeer.this, button, 392 toggleButton.isSelected() ? IMAGE_ID_SELECTED_STATE_ICON : IMAGE_ID_STATE_ICON); 393 stateIconElement.setAttribute("id", elementId + "_stateicon"); 394 } 395 396 int entityCount = (textNode == null ? 0 : 1) + (iconElement == null ? 0 : 1) + (stateIconElement == null ? 0 : 1); 397 398 Extent iconTextMargin; 399 Alignment textPosition; 400 Element tableElement; 401 402 switch (entityCount) { 403 case 1: 404 if (textNode != null) { 405 contentNode = textNode; 406 } else if (iconElement != null) { 407 contentNode = iconElement; 408 } else { contentNode = stateIconElement; 410 } 411 break; 412 case 2: 413 iconTextMargin = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_ICON_TEXT_MARGIN, 414 DEFAULT_ICON_TEXT_MARGIN); 415 TriCellTable tct; 416 textPosition = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_POSITION, 417 DEFAULT_TEXT_POSITION); 418 if (stateIconElement == null) { 419 int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, button); 421 tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin); 422 423 renderCellText(tct, textNode, button); 424 renderCellIcon(tct, iconElement, 1, button); 425 } else { 426 Extent stateMargin = (Extent) button.getRenderProperty(ToggleButton.PROPERTY_STATE_MARGIN, 428 DEFAULT_ICON_TEXT_MARGIN); 429 Alignment statePosition = (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_POSITION, 430 DEFAULT_STATE_POSITION); 431 int orientation = TriCellTableConfigurator.convertStatePositionToOrientation(statePosition, button); 432 tct = new TriCellTable(rc, document, elementId, orientation, stateMargin); 433 434 if (textNode == null) { 435 renderCellIcon(tct, iconElement, 0, button); 436 } else { 437 renderCellText(tct, textNode, button); 438 } 439 renderCellState(tct, stateIconElement, 1, button); 440 } 441 442 tct.addCellCssText("padding:0px;"); 443 tableElement = tct.getTableElement(); 444 tableElement.setAttribute("id", elementId + "_table"); 445 tableElement.setAttribute("style", getContainerTableCssText(button)); 446 contentNode = tableElement; 447 break; 448 case 3: 449 iconTextMargin = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_ICON_TEXT_MARGIN, 450 DEFAULT_ICON_TEXT_MARGIN); 451 textPosition = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_POSITION, 452 DEFAULT_TEXT_POSITION); 453 Extent stateMargin = (Extent) button.getRenderProperty(ToggleButton.PROPERTY_STATE_MARGIN, 454 DEFAULT_ICON_TEXT_MARGIN); 455 Alignment statePosition = (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_POSITION, 456 DEFAULT_STATE_POSITION); 457 int stateOrientation = TriCellTableConfigurator.convertStatePositionToOrientation(statePosition, button); 458 int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, button); 459 tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin, stateOrientation, stateMargin); 460 461 renderCellText(tct, textNode, button); 462 renderCellIcon(tct, iconElement, 1, button); 463 renderCellState(tct, stateIconElement, 2, button); 464 465 tct.addCellCssText("padding:0px;"); 466 tableElement = tct.getTableElement(); 467 tableElement.setAttribute("id", elementId + "_table"); 468 tableElement.setAttribute("style", getContainerTableCssText(button)); 469 contentNode = tableElement; 470 break; 471 default: 472 contentNode = null; 474 } 475 476 if (contentNode != null) { 477 buttonContainerElement.appendChild(contentNode); 478 } 479 } 480 481 490 private void renderCellIcon(TriCellTable tct, Element iconElement, int cellIndex, AbstractButton button) { 491 Element iconTdElement = tct.getTdElement(cellIndex); 492 Alignment alignment = (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT); 493 if (alignment != null) { 494 CssStyle style = new CssStyle(); 495 AlignmentRender.renderToStyle(style, alignment, button); 496 iconTdElement.setAttribute("style", style.renderInline()); 497 } 498 iconTdElement.appendChild(iconElement); 499 } 500 501 511 private void renderCellState(TriCellTable tct, Element stateIconElement, int cellIndex, AbstractButton button) { 512 Element stateTdElement = tct.getTdElement(cellIndex); 513 CssStyle stateTdCssStyle = new CssStyle(); 514 AlignmentRender.renderToStyle(stateTdCssStyle, 515 (Alignment) button.getRenderProperty(ToggleButton.PROPERTY_STATE_ALIGNMENT), button); 516 stateTdElement.setAttribute("style", stateTdCssStyle.renderInline()); 517 stateTdElement.appendChild(stateIconElement); 518 } 519 520 529 private void renderCellText(TriCellTable tct, Text textNode, AbstractButton button) { 530 Element textTdElement = tct.getTdElement(0); 531 CssStyle textTdCssStyle = new CssStyle(); 532 533 if (Boolean.FALSE.equals(button.getRenderProperty(AbstractButton.PROPERTY_LINE_WRAP))) { 534 textTdCssStyle.setAttribute("white-space", "nowrap"); 535 } 536 537 Alignment alignment = combineAlignment((Alignment) button.getRenderProperty(AbstractButton.PROPERTY_TEXT_ALIGNMENT), 538 (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT)); 539 AlignmentRender.renderToStyle(textTdCssStyle, alignment, button); 540 541 if (textTdCssStyle.hasAttributes()) { 542 textTdElement.setAttribute("style", textTdCssStyle.renderInline()); 543 } 544 545 textTdElement.appendChild(textNode); 546 } 547 548 551 private CssStyle renderDefaultStyle(RenderContext rc, AbstractButton button) { 552 CssStyle cssStyle = new CssStyle(); 553 LayoutDirectionRender.renderToStyle(cssStyle, button.getLayoutDirection(), button.getLocale()); 554 ExtentRender.renderToStyle(cssStyle, "width", (Extent) button.getRenderProperty(AbstractButton.PROPERTY_WIDTH)); 555 Extent height = (Extent) button.getRenderProperty(AbstractButton.PROPERTY_HEIGHT); 556 if (height != null) { 557 ExtentRender.renderToStyle(cssStyle, "height", height); 558 cssStyle.setAttribute("overflow", "hidden"); 559 } 560 if (Boolean.FALSE.equals(button.getRenderProperty(AbstractButton.PROPERTY_LINE_WRAP))) { 561 cssStyle.setAttribute("white-space", "nowrap"); 562 } 563 564 boolean renderEnabled = button.isRenderEnabled(); 565 566 Border border; 567 Color foreground, background; 568 Font font; 569 FillImage backgroundImage; 570 if (!renderEnabled) { 571 background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND); 573 backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BACKGROUND_IMAGE); 574 border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_BORDER); 575 font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_FONT); 576 foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_DISABLED_FOREGROUND); 577 578 if (background == null) { 580 background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND); 581 if (backgroundImage == null) { 582 backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE); 586 } 587 } 588 if (border == null) { 589 border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_BORDER); 590 } 591 if (font == null) { 592 font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_FONT); 593 } 594 if (foreground == null) { 595 foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_FOREGROUND); 596 } 597 } else { 598 border = (Border) button.getRenderProperty(AbstractButton.PROPERTY_BORDER); 599 foreground = (Color) button.getRenderProperty(AbstractButton.PROPERTY_FOREGROUND); 600 background = (Color) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND); 601 font = (Font) button.getRenderProperty(AbstractButton.PROPERTY_FONT); 602 backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE); 603 } 604 605 BorderRender.renderToStyle(cssStyle, border); 606 ColorRender.renderToStyle(cssStyle, foreground, background); 607 FontRender.renderToStyle(cssStyle, font); 608 FillImageRender.renderToStyle(cssStyle, rc, this, button, IMAGE_ID_BACKGROUND, backgroundImage, 609 FillImageRender.FLAG_DISABLE_FIXED_MODE); 610 InsetsRender.renderToStyle(cssStyle, "padding", (Insets) button.getRenderProperty(AbstractButton.PROPERTY_INSETS)); 611 612 AlignmentRender.renderToStyle(cssStyle, 613 (Alignment) button.getRenderProperty(AbstractButton.PROPERTY_ALIGNMENT), button); 614 return cssStyle; 615 } 616 617 621 public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) { 622 rc.getServerMessage().addLibrary(BUTTON_SERVICE.getId()); 623 renderDisposeDirective(rc, (AbstractButton) component); 624 } 625 626 634 private void renderDisposeDirective(RenderContext rc, AbstractButton button) { 635 ServerMessage serverMessage = rc.getServerMessage(); 636 Element itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_PREREMOVE, 637 "EchoButton.MessageProcessor", "dispose", new String [0], new String [0]); 638 Element itemElement = serverMessage.getDocument().createElement("item"); 639 itemElement.setAttribute("eid", ContainerInstance.getElementId(button)); 640 itemizedUpdateElement.appendChild(itemElement); 641 } 642 643 647 public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node parentNode, Component component) { 648 ServerMessage serverMessage = rc.getServerMessage(); 649 serverMessage.addLibrary(BUTTON_SERVICE.getId()); 650 AbstractButton button = (AbstractButton) component; 651 Element containerDivElement = renderButtonContainer(rc, parentNode, button); 652 renderInitDirective(rc, button); 653 renderButtonContent(rc, containerDivElement, button); 654 } 655 656 664 private void renderInitDirective(RenderContext rc, AbstractButton button) { 665 String elementId = ContainerInstance.getElementId(button); 666 ServerMessage serverMessage = rc.getServerMessage(); 667 FillImage backgroundImage = (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_BACKGROUND_IMAGE); 668 669 boolean rolloverEnabled = ((Boolean ) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ENABLED, 670 Boolean.FALSE)).booleanValue(); 671 boolean pressedEnabled = ((Boolean ) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ENABLED, 672 Boolean.FALSE)).booleanValue(); 673 674 String pressedStyle = ""; 675 String rolloverStyle = ""; 676 677 String defaultIconUri = null; 678 String rolloverIconUri = null; 679 String pressedIconUri = null; 680 681 if (rolloverEnabled || pressedEnabled) { 682 boolean hasIcon = button.getRenderProperty(AbstractButton.PROPERTY_ICON) != null; 683 if (hasIcon) { 684 defaultIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_ICON); 685 } 686 687 if (rolloverEnabled) { 688 CssStyle rolloverCssStyle = new CssStyle(); 689 BorderRender.renderToStyle(rolloverCssStyle, 690 (Border) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BORDER)); 691 ColorRender.renderToStyle(rolloverCssStyle, 692 (Color) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_FOREGROUND), 693 (Color) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND)); 694 FontRender.renderToStyle(rolloverCssStyle, 695 (Font) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_FONT)); 696 if (backgroundImage != null) { 697 FillImageRender.renderToStyle(rolloverCssStyle, rc, this, button, IMAGE_ID_ROLLOVER_BACKGROUND, 698 (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_BACKGROUND_IMAGE), 699 FillImageRender.FLAG_DISABLE_FIXED_MODE); 700 } 701 if (rolloverCssStyle.hasAttributes()) { 702 rolloverStyle = rolloverCssStyle.renderInline(); 703 } 704 if (hasIcon) { 705 ImageReference rolloverIcon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_ROLLOVER_ICON); 706 if (rolloverIcon != null) { 707 rolloverIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_ROLLOVER_ICON); 708 } 709 } 710 } 711 712 if (pressedEnabled) { 713 CssStyle pressedCssStyle = new CssStyle(); 714 BorderRender.renderToStyle(pressedCssStyle, 715 (Border) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BORDER)); 716 ColorRender.renderToStyle(pressedCssStyle, 717 (Color) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_FOREGROUND), 718 (Color) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND)); 719 FontRender.renderToStyle(pressedCssStyle, 720 (Font) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_FONT)); 721 if (backgroundImage != null) { 722 FillImageRender.renderToStyle(pressedCssStyle, rc, this, button, IMAGE_ID_PRESSED_BACKGROUND, 723 (FillImage) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_BACKGROUND_IMAGE), 724 FillImageRender.FLAG_DISABLE_FIXED_MODE); 725 } 726 if (pressedCssStyle.hasAttributes()) { 727 pressedStyle = pressedCssStyle.renderInline(); 728 } 729 if (hasIcon) { 730 ImageReference pressedIcon = (ImageReference) button.getRenderProperty(AbstractButton.PROPERTY_PRESSED_ICON); 731 if (pressedIcon != null) { 732 pressedIconUri = ImageTools.getUri(rc, this, button, IMAGE_ID_PRESSED_ICON); 733 } 734 } 735 } 736 } 737 738 CssStyle defaultCssStyle = renderDefaultStyle(rc, button); 739 String defaultStyle = defaultCssStyle.renderInline(); 740 741 Element itemizedUpdateElement = serverMessage.getItemizedDirective(ServerMessage.GROUP_ID_POSTUPDATE, 742 "EchoButton.MessageProcessor", "init", BUTTON_INIT_KEYS, new String []{defaultStyle, rolloverStyle, pressedStyle}); 743 Element itemElement = serverMessage.getDocument().createElement("item"); 744 itemElement.setAttribute("eid", elementId); 745 if (defaultIconUri != null) { 746 itemElement.setAttribute("default-icon", defaultIconUri); 747 } 748 if (rolloverIconUri != null) { 749 itemElement.setAttribute("rollover-icon", rolloverIconUri); 750 } 751 if (pressedIconUri != null) { 752 itemElement.setAttribute("pressed-icon", pressedIconUri); 753 } 754 if (!button.hasActionListeners()) { 755 itemElement.setAttribute("server-notify", "false"); 756 } 757 if (!button.isRenderEnabled()) { 758 itemElement.setAttribute("enabled", "false"); 759 } 760 761 if (button instanceof ToggleButton) { 762 ToggleButton toggleButton = (ToggleButton) button; 763 itemElement.setAttribute("toggle", "true"); 764 itemElement.setAttribute("selected", toggleButton.isSelected() ? "true" : "false"); 765 itemElement.setAttribute("state-icon", ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_STATE_ICON)); 766 itemElement.setAttribute("selected-state-icon", ImageTools.getUri(rc, this, toggleButton, 767 IMAGE_ID_SELECTED_STATE_ICON)); 768 769 if (rolloverEnabled && toggleButton.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_STATE_ICON) != null 770 && toggleButton.getRenderProperty(ToggleButton.PROPERTY_ROLLOVER_SELECTED_STATE_ICON) != null) { 771 itemElement.setAttribute("rollover-state-icon", 772 ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_ROLLOVER_STATE_ICON)); 773 itemElement.setAttribute("rollover-selected-state-icon", 774 ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_ROLLOVER_SELECTED_STATE_ICON)); 775 } 776 if (pressedEnabled && toggleButton.getRenderProperty(ToggleButton.PROPERTY_PRESSED_STATE_ICON) != null 777 && toggleButton.getRenderProperty(ToggleButton.PROPERTY_PRESSED_SELECTED_STATE_ICON) != null) { 778 itemElement.setAttribute("pressed-state-icon", 779 ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_PRESSED_STATE_ICON)); 780 itemElement.setAttribute("pressed-selected-state-icon", 781 ImageTools.getUri(rc, this, toggleButton, IMAGE_ID_PRESSED_SELECTED_STATE_ICON)); 782 } 783 784 if (button instanceof RadioButton) { 785 ButtonGroup buttonGroup = ((RadioButton) toggleButton).getGroup(); 786 if (buttonGroup != null) { 787 rc.getContainerInstance().getIdTable().register(buttonGroup); 788 itemElement.setAttribute("group", buttonGroup.getRenderId()); 789 } 790 } 791 } 792 793 itemizedUpdateElement.appendChild(itemElement); 794 } 795 796 800 public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String targetId) { 801 String parentId = ContainerInstance.getElementId(update.getParent()); 802 DomUpdate.renderElementRemove(rc.getServerMessage(), parentId); 803 renderAdd(rc, update, targetId, update.getParent()); 804 return false; 805 } 806 } 807 | Popular Tags |