1 29 30 package nextapp.echo2.webrender.servermessage; 31 32 import nextapp.echo2.webrender.ServerMessage; 33 34 import org.w3c.dom.DocumentFragment ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 38 44 public class DomUpdate { 45 46 private static final String MESSAGE_PART_NAME = "EchoDomUpdate.MessageProcessor"; 47 private static final String XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml"; 48 49 59 public static void renderAttributeUpdate(ServerMessage serverMessage, String targetId, String attributeName, 60 String attributeValue) { 61 Element element = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, 63 MESSAGE_PART_NAME, "attribute-update"); 64 element.setAttribute("target-id", targetId); 65 element.setAttribute("name", attributeName); 66 element.setAttribute("value", attributeValue); 67 } 68 69 79 public static Element renderElementAdd(ServerMessage serverMessage) { 80 Element domAddElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, 81 MESSAGE_PART_NAME, "dom-add"); 82 return domAddElement; 83 } 84 85 97 public static void renderElementAdd(ServerMessage serverMessage, String parentId, DocumentFragment htmlFragment) { 98 renderElementAdd(serverMessage, parentId, null, htmlFragment); 99 } 100 101 117 public static void renderElementAdd(ServerMessage serverMessage, String parentId, String siblingId, 118 DocumentFragment htmlFragment) { 119 setContentNamespace(htmlFragment); 120 Element domAddElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, 121 MESSAGE_PART_NAME, "dom-add"); 122 Element contentElement = domAddElement.getOwnerDocument().createElement("content"); 123 contentElement.setAttribute("parent-id", parentId); 124 if (siblingId != null) { 125 contentElement.setAttribute("sibling-id", siblingId); 126 } 127 domAddElement.appendChild(contentElement); 128 contentElement.appendChild(htmlFragment); 129 } 130 131 142 public static void renderElementAddContent(ServerMessage serverMessage, Element domAddElement, String parentId, 143 DocumentFragment htmlFragment) { 144 renderElementAddContent(serverMessage, domAddElement, parentId, null, htmlFragment); 145 } 146 147 161 public static void renderElementAddContent(ServerMessage serverMessage, Element domAddElement, String parentId, 162 String siblingId, DocumentFragment htmlFragment) { 163 setContentNamespace(htmlFragment); 164 Element contentElement = domAddElement.getOwnerDocument().createElement("content"); 165 contentElement.setAttribute("parent-id", parentId); 166 if (siblingId != null) { 167 contentElement.setAttribute("sibling-id", siblingId); 168 } 169 domAddElement.appendChild(contentElement); 170 contentElement.appendChild(htmlFragment); 171 } 172 173 180 public static void renderElementRemove(ServerMessage serverMessage, String targetId) { 181 Element domRemoveElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_REMOVE, 182 MESSAGE_PART_NAME, "dom-remove"); 183 domRemoveElement.setAttribute("target-id", targetId); 184 } 185 186 193 public static void renderElementRemoveChildren(ServerMessage serverMessage, String targetId) { 194 Element domRemoveElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_REMOVE, 195 MESSAGE_PART_NAME, "dom-remove-children"); 196 domRemoveElement.setAttribute("target-id", targetId); 197 } 198 199 211 public static void renderStyleUpdate(ServerMessage serverMessage, String targetId, String attributeName, 212 String attributeValue) { 213 Element element = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, MESSAGE_PART_NAME, "style-update"); 215 element.setAttribute("target-id", targetId); 216 element.setAttribute("name", attributeName); 217 element.setAttribute("value", attributeValue); 218 } 219 220 228 public static void renderStyleSheetAddRule(ServerMessage serverMessage, String selectorText, String style) { 229 Element element = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, MESSAGE_PART_NAME, 230 "stylesheet-add-rule"); 231 element.setAttribute("selector", selectorText); 232 element.setAttribute("style", style); 233 } 234 235 242 public static void renderStyleSheetRemoveRule(ServerMessage serverMessage, String selectorText) { 243 Element element = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, MESSAGE_PART_NAME, 244 "stylesheet-remove-rule"); 245 element.setAttribute("selector", selectorText); 246 } 247 253 private static void setContentNamespace(DocumentFragment htmlFragment) { 254 Node childNode = htmlFragment.getFirstChild(); 255 while (childNode != null) { 256 if (childNode.getNodeType() == Node.ELEMENT_NODE) { 257 ((Element ) childNode).setAttribute("xmlns", XHTML_NAMESPACE); 258 } 259 childNode = childNode.getNextSibling(); 260 } 261 } 262 } 263 | Popular Tags |