KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > webrender > servermessage > DomUpdate


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.webrender.servermessage;
31
32 import nextapp.echo2.webrender.ServerMessage;
33
34 import org.w3c.dom.DocumentFragment JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37
38 /**
39  * A utility class to add <code>EchoDomUpdate</code> message parts to the
40  * <code>ServerMessage</code>. <code>EchoDomUpdate</code> message parts
41  * are used to directly update the client DOM with HTML code generated on
42  * the server.
43  */

44 public class DomUpdate {
45     
46     private static final String JavaDoc MESSAGE_PART_NAME = "EchoDomUpdate.MessageProcessor";
47     private static final String JavaDoc XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
48
49     /**
50      * Creates a <code>attribute-update</code> operation to update an
51      * element attribute of the element identified by <code>targetId</code>
52      * in the client DOM.
53      *
54      * @param serverMessage the outgoing <code>ServerMessage</code>
55      * @param targetId the id of the element whose attribute is to be updated
56      * @param attributeName the name of the attribute to update
57      * @param attributeValue the new value of the attribute
58      */

59     public static void renderAttributeUpdate(ServerMessage serverMessage, String JavaDoc targetId, String JavaDoc attributeName,
60             String JavaDoc attributeValue) {
61         //BUGBUG. support nulls for deletion.
62
Element JavaDoc 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     /**
70      * Prepares a <code>dom-add</code> operation by immediately appending an
71      * empty <code>dom-add</code> element to the end of the
72      * <code>ServerMessage</code>'s 'update' group.
73      * Content is added to the <code>dom-add</code> element by invoking
74      * <code>renderElementAddContent()</code>.
75      *
76      * @param serverMessage the <code>ServerMessage</code>
77      * @return the created <code>dom-add</code> <code>Element</code>.
78      */

79     public static Element JavaDoc renderElementAdd(ServerMessage serverMessage) {
80         Element JavaDoc domAddElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
81                 MESSAGE_PART_NAME, "dom-add");
82         return domAddElement;
83     }
84
85     /**
86      * Creates a <code>dom-add</code> operation to append HTML content to the
87      * end of the element identified by <code>parentId</code>.
88      *
89      * @param serverMessage the outgoing <code>ServerMessage</code>
90      * @param parentId the id of the element the HTML code will be appended to
91      * @param htmlFragment the HTML fragment to add to the DOM
92      * @deprecated use of this method can result in DOM modifications
93      * being performed in improper order
94      * (instead use <code>renderElementAdd(ServerMessage)</code> followed by
95      * <code>renderElementAddContent()</code>)
96      */

97     public static void renderElementAdd(ServerMessage serverMessage, String JavaDoc parentId, DocumentFragment JavaDoc htmlFragment) {
98         renderElementAdd(serverMessage, parentId, null, htmlFragment);
99     }
100
101     /**
102      * Creates a <code>dom-add</code> operation to insert HTML content in the
103      * element identified by <code>parentId</code>.
104      *
105      * @param serverMessage the outgoing <code>ServerMessage</code>
106      * @param parentId the id of the element into which the HTML code will be
107      * inserted
108      * @param siblingId The id of the element which the content will be inserted
109      * <strong>before</strong> (this element must be an immediate child
110      * of the element specified by <code>parentId</code>)
111      * @param htmlFragment the HTML fragment to add to the DOM
112      * @deprecated use of this method can result in DOM modifications
113      * being performed in improper order
114      * (instead use <code>renderElementAdd(ServerMessage)</code> followed by
115      * <code>renderElementAddContent()</code>)
116      */

117     public static void renderElementAdd(ServerMessage serverMessage, String JavaDoc parentId, String JavaDoc siblingId,
118             DocumentFragment JavaDoc htmlFragment) {
119         setContentNamespace(htmlFragment);
120         Element JavaDoc domAddElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE,
121                 MESSAGE_PART_NAME, "dom-add");
122         Element JavaDoc 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     /**
132      * Adds content to be added to an existing <code>dom-add</code> operation.
133      * The content will be appended to the end of the DOM element identified by
134      * <code>parentId</code>
135      *
136      * @param serverMessage the <code>ServerMessage</code>
137      * @param domAddElement the <code>dom-add</code> element created by a
138      * previous invocation of <code>renderAdd(ServerMessage)</code>
139      * @param parentId the id of the element the HTML code will be appended to
140      * @param htmlFragment the HTML fragment to add to the DOM
141      */

142     public static void renderElementAddContent(ServerMessage serverMessage, Element JavaDoc domAddElement, String JavaDoc parentId,
143             DocumentFragment JavaDoc htmlFragment) {
144         renderElementAddContent(serverMessage, domAddElement, parentId, null, htmlFragment);
145     }
146     
147     /**
148      * Adds content to be added to an existing <code>dom-add</code> operation.
149      * The content will be inserted into the DOM element identified by
150      * <code>parentId</code> before the specified <code>siblingId</code>.
151      *
152      * @param serverMessage the <code>ServerMessage</code>
153      * @param domAddElement the <code>dom-add</code> element created by a
154      * previous invocation of <code>renderAdd(ServerMessage)</code>
155      * @param parentId the id of the element the HTML code will be appended to
156      * @param siblingId The id of the element which the content will be inserted
157      * <strong>before</strong> (this element must be an immediate child
158      * of the element specified by <code>parentId</code>)
159      * @param htmlFragment the HTML fragment to add to the DOM
160      */

161     public static void renderElementAddContent(ServerMessage serverMessage, Element JavaDoc domAddElement, String JavaDoc parentId,
162             String JavaDoc siblingId, DocumentFragment JavaDoc htmlFragment) {
163         setContentNamespace(htmlFragment);
164         Element JavaDoc 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     /**
174      * Creates a <code>dom-remove</code> operation to remove the HTML element
175      * identified by <code>targetId</code> from the client DOM.
176      *
177      * @param serverMessage the outgoing <code>ServerMessage</code>
178      * @param targetId the id of the element to remove
179      */

180     public static void renderElementRemove(ServerMessage serverMessage, String JavaDoc targetId) {
181         Element JavaDoc domRemoveElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_REMOVE,
182                 MESSAGE_PART_NAME, "dom-remove");
183         domRemoveElement.setAttribute("target-id", targetId);
184     }
185
186     /**
187      * Creates a <code>dom-remove</code> operation to remove all child elements
188      * of the element identified by <code>targetId</code> from the client DOM.
189      *
190      * @param serverMessage the outgoing <code>ServerMessage</code>
191      * @param targetId the id of the element whose children ware to be removed
192      */

193     public static void renderElementRemoveChildren(ServerMessage serverMessage, String JavaDoc targetId) {
194         Element JavaDoc domRemoveElement = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_REMOVE,
195                 MESSAGE_PART_NAME, "dom-remove-children");
196         domRemoveElement.setAttribute("target-id", targetId);
197     }
198     
199     /**
200      * Creates a <code>style-update</code> operation to update a CSS style
201      * attribute of the element identified by <code>targetId</code> in the
202      * client DOM.
203      *
204      * @param serverMessage the outgoing <code>ServerMessage</code>
205      * @param targetId the id of the element whose <strong>style</strong>
206      * attribute is to be updated
207      * @param attributeName the name of the <strong>style</strong> attribute
208      * @param attributeValue the new value of the <strong>style</strong>
209      * attribute
210      */

211     public static void renderStyleUpdate(ServerMessage serverMessage, String JavaDoc targetId, String JavaDoc attributeName,
212             String JavaDoc attributeValue) {
213         //BUGBUG. support nulls for deletion.
214
Element JavaDoc 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     /**
221      * Creates a <code>stylesheet-add-rule</code> directive to add a rule
222      * to a stylesheet
223      *
224      * @param serverMessage the relevant <code>ServerMessage</code>
225      * @param selectorText the selector of the rule to add
226      * @param style the CSS text for the style
227      */

228     public static void renderStyleSheetAddRule(ServerMessage serverMessage, String JavaDoc selectorText, String JavaDoc style) {
229         Element JavaDoc 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     /**
236      * Creates a <code>stylesheet-remove-rule</code> directive to remove a rule
237      * from a stylesheet
238      *
239      * @param serverMessage the relevant <code>ServerMessage</code>
240      * @param selectorText the selector of the rule to remove
241      */

242     public static void renderStyleSheetRemoveRule(ServerMessage serverMessage, String JavaDoc selectorText) {
243         Element JavaDoc element = serverMessage.appendPartDirective(ServerMessage.GROUP_ID_UPDATE, MESSAGE_PART_NAME,
244                 "stylesheet-remove-rule");
245         element.setAttribute("selector", selectorText);
246     }
247     /**
248      * Configures all child elements of a "content" element to use the XHTML
249      * namespace.
250      *
251      * @param htmlFragment an HTML document fragment
252      */

253     private static void setContentNamespace(DocumentFragment JavaDoc htmlFragment) {
254         Node JavaDoc childNode = htmlFragment.getFirstChild();
255         while (childNode != null) {
256             if (childNode.getNodeType() == Node.ELEMENT_NODE) {
257                 ((Element JavaDoc) childNode).setAttribute("xmlns", XHTML_NAMESPACE);
258             }
259             childNode = childNode.getNextSibling();
260         }
261     }
262 }
263
Popular Tags