1 29 30 package nextapp.echo2.webcontainer.syncpeer; 31 32 import nextapp.echo2.app.Alignment; 33 import nextapp.echo2.app.Color; 34 import nextapp.echo2.app.Component; 35 import nextapp.echo2.app.Extent; 36 import nextapp.echo2.app.Font; 37 import nextapp.echo2.app.ImageReference; 38 import nextapp.echo2.app.Label; 39 import nextapp.echo2.app.update.ServerComponentUpdate; 40 import nextapp.echo2.webcontainer.ContainerInstance; 41 import nextapp.echo2.webcontainer.DomUpdateSupport; 42 import nextapp.echo2.webcontainer.RenderContext; 43 import nextapp.echo2.webcontainer.ComponentSynchronizePeer; 44 import nextapp.echo2.webcontainer.image.ImageRenderSupport; 45 import nextapp.echo2.webcontainer.propertyrender.AlignmentRender; 46 import nextapp.echo2.webcontainer.propertyrender.ColorRender; 47 import nextapp.echo2.webcontainer.propertyrender.FontRender; 48 import nextapp.echo2.webcontainer.propertyrender.ImageReferenceRender; 49 import nextapp.echo2.webcontainer.propertyrender.LayoutDirectionRender; 50 import nextapp.echo2.webrender.output.CssStyle; 51 import nextapp.echo2.webrender.servermessage.DomUpdate; 52 import nextapp.echo2.webrender.util.DomUtil; 53 54 import org.w3c.dom.Document ; 55 import org.w3c.dom.DocumentFragment ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.Node ; 58 59 65 public class LabelPeer 66 implements DomUpdateSupport, ImageRenderSupport, ComponentSynchronizePeer { 67 68 private static final Alignment DEFAULT_TEXT_POSITION = new Alignment(Alignment.TRAILING, Alignment.DEFAULT); 69 private static final Extent DEFAULT_ICON_TEXT_MARGIN = new Extent(3); 70 private static final String IMAGE_ID_ICON = "icon"; 71 72 76 public void renderAdd(RenderContext rc, ServerComponentUpdate update, 77 String targetId, Component component) { 78 Element domAddElement = DomUpdate.renderElementAdd(rc.getServerMessage()); 79 DocumentFragment htmlFragment = rc.getServerMessage().getDocument().createDocumentFragment(); 80 renderHtml(rc, update, htmlFragment, component); 81 DomUpdate.renderElementAddContent(rc.getServerMessage(), domAddElement, targetId, htmlFragment); 82 } 83 84 87 public String getContainerId(Component child) { 88 throw new UnsupportedOperationException ("Component does not support children."); 89 } 90 91 94 public ImageReference getImage(Component component, String imageId) { 95 if (IMAGE_ID_ICON.equals(imageId)) { 96 return (ImageReference) component.getRenderProperty(Label.PROPERTY_ICON); 97 } else { 98 return null; 99 } 100 } 101 102 106 public void renderDispose(RenderContext rc, ServerComponentUpdate update, Component component) { 107 } 109 110 114 public void renderHtml(RenderContext rc, ServerComponentUpdate update, Node parentNode, Component component) { 115 Label label = (Label) component; 116 ImageReference icon = (ImageReference) label.getRenderProperty(Label.PROPERTY_ICON); 117 String text = (String ) label.getRenderProperty(Label.PROPERTY_TEXT); 118 119 if (icon != null) { 120 if (text != null) { 121 renderIconTextLabel(rc, parentNode, label); 122 } else { 123 renderIconLabel(rc, parentNode, label); 124 } 125 } else if (text != null) { 126 renderTextLabel(rc, parentNode, label); 127 } 128 } 129 130 137 private void renderIconLabel(RenderContext rc, Node parentNode, Label label) { 138 Element imgElement = ImageReferenceRender.renderImageReferenceElement(rc, this, label, IMAGE_ID_ICON); 139 imgElement.setAttribute("id", ContainerInstance.getElementId(label)); 140 imgElement.setAttribute("style", "border:0px none;"); 141 142 String toolTipText = (String ) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT); 143 if (toolTipText != null) { 144 imgElement.setAttribute("title", toolTipText); 145 } 146 147 parentNode.appendChild(imgElement); 148 } 149 150 157 private void renderIconTextLabel(RenderContext rc, Node parentNode, Label label) { 158 162 Document document = rc.getServerMessage().getDocument(); 163 String text = (String ) label.getRenderProperty(Label.PROPERTY_TEXT); 164 Alignment textPosition = (Alignment) label.getRenderProperty(Label.PROPERTY_TEXT_POSITION, DEFAULT_TEXT_POSITION); 165 Extent iconTextMargin = (Extent) label.getRenderProperty(Label.PROPERTY_ICON_TEXT_MARGIN, 166 DEFAULT_ICON_TEXT_MARGIN); 167 String elementId = ContainerInstance.getElementId(label); 168 169 int orientation = TriCellTableConfigurator.convertIconTextPositionToOrientation(textPosition, label); 170 TriCellTable tct = new TriCellTable(rc, document, elementId, orientation, iconTextMargin); 171 tct.addCellCssText("padding:0px;"); 172 173 Element textTdElement = tct.getTdElement(0); 174 CssStyle textTdCssStyle = new CssStyle(); 175 textTdCssStyle.setAttribute("border", "0px none"); 176 if (Boolean.FALSE.equals(label.getRenderProperty(Label.PROPERTY_LINE_WRAP))) { 177 textTdCssStyle.setAttribute("white-space", "nowrap"); 178 } 179 AlignmentRender.renderToStyle(textTdCssStyle, (Alignment) label.getRenderProperty(Label.PROPERTY_TEXT_ALIGNMENT), label); 180 textTdElement.setAttribute("style", textTdElement.getAttribute("style") + textTdCssStyle.renderInline()); 181 DomUtil.setElementText(textTdElement, text); 182 183 Element imgElement = ImageReferenceRender.renderImageReferenceElement(rc, this, label, IMAGE_ID_ICON); 184 Element iconTdElement = tct.getTdElement(1); 185 iconTdElement.appendChild(imgElement); 186 187 Element tableElement = tct.getTableElement(); 188 tableElement.setAttribute("id", elementId); 189 190 String toolTipText = (String ) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT); 191 if (toolTipText != null) { 192 tableElement.setAttribute("title", toolTipText); 193 } 194 195 CssStyle cssStyle = new CssStyle(); 196 LayoutDirectionRender.renderToStyle(cssStyle, label.getLayoutDirection(), label.getLocale()); 197 ColorRender.renderToStyle(cssStyle, (Color) label.getRenderProperty(Label.PROPERTY_FOREGROUND), 198 (Color) label.getRenderProperty(Label.PROPERTY_BACKGROUND)); 199 FontRender.renderToStyle(cssStyle, (Font) label.getRenderProperty(Label.PROPERTY_FONT)); 200 cssStyle.setAttribute("border", "0px none"); 201 cssStyle.setAttribute("border-collapse", "collapse"); 202 tableElement.setAttribute("style", cssStyle.renderInline()); 203 204 parentNode.appendChild(tableElement); 205 } 206 207 214 private void renderTextLabel(RenderContext rc, Node parentNode, Label label) { 215 Document document = rc.getServerMessage().getDocument(); 216 217 Element spanElement = document.createElement("span"); 218 spanElement.setAttribute("id", ContainerInstance.getElementId(label)); 219 DomUtil.setElementText(spanElement, (String ) label.getRenderProperty(Label.PROPERTY_TEXT)); 220 221 CssStyle cssStyle = new CssStyle(); 222 if (Boolean.FALSE.equals(label.getRenderProperty(Label.PROPERTY_LINE_WRAP))) { 223 cssStyle.setAttribute("white-space", "nowrap"); 224 } 225 ColorRender.renderToStyle(cssStyle, (Color) label.getRenderProperty(Label.PROPERTY_FOREGROUND), 226 (Color) label.getRenderProperty(Label.PROPERTY_BACKGROUND)); 227 FontRender.renderToStyle(cssStyle, (Font) label.getRenderProperty(Label.PROPERTY_FONT)); 228 if (cssStyle.hasAttributes()) { 229 spanElement.setAttribute("style", cssStyle.renderInline()); 230 } 231 232 String toolTipText = (String ) label.getRenderProperty(Label.PROPERTY_TOOL_TIP_TEXT); 233 if (toolTipText != null) { 234 spanElement.setAttribute("title", toolTipText); 235 } 236 237 parentNode.appendChild(spanElement); 238 } 239 240 244 public boolean renderUpdate(RenderContext rc, ServerComponentUpdate update, String targetId) { 245 DomUpdate.renderElementRemove(rc.getServerMessage(), ContainerInstance.getElementId(update.getParent())); 246 renderAdd(rc, update, targetId, update.getParent()); 247 return false; 248 } 249 } 250 | Popular Tags |