KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > proxy > dwr > Util


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.proxy.dwr;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.directwebremoting.ScriptBuffer;
22 import org.directwebremoting.ScriptSession;
23 import org.directwebremoting.proxy.ScriptProxy;
24
25 /**
26  * Util is a server-side proxy that allows Java programmers to call client
27  * side Javascript from Java.
28  * <p>
29  * Each Util object is associated with a list of ScriptSessions and the
30  * proxy code is creates will be dynamically forwarded to all those browsers.
31  * <p>
32  * Currently this class contains only the write-only DOM manipulation functions
33  * from Util. It is possible that we could add the read methods, however
34  * the complexity in the callback and the fact that you are probably not going
35  * to need it means that we'll leave it for another day. Specifically,
36  * <code>getValue</code>, <code>getValues</code> and <code>getText</code> have
37  * been left out as being read functions and <code>useLoadingMessage</code> etc
38  * have been left out as not being DOM related.
39  * @author Joe Walker [joe at getahead dot ltd dot uk]
40  * @author Jorge Martin Cuervo [darthkorr at gmail dot com]
41  */

42 public class Util extends ScriptProxy
43 {
44     /**
45      * Http thread constructor, that affects no browsers.
46      * Calls to {@link Util#addScriptSession(ScriptSession)} or to
47      * {@link Util#addScriptSessions(Collection)} will be needed
48      */

49     public Util()
50     {
51         super();
52     }
53
54     /**
55      * Http thread constructor that alters a single browser
56      * @param scriptSession The browser to alter
57      */

58     public Util(ScriptSession scriptSession)
59     {
60         super(scriptSession);
61     }
62
63     /**
64      * Http thread constructor that alters a number of browsers
65      * @param scriptSessions A collection of ScriptSessions that we should act on.
66      */

67     public Util(Collection JavaDoc scriptSessions)
68     {
69         super(scriptSessions);
70     }
71
72     /**
73      * Set the value an HTML element to the specified value.
74      * <p><a HREF="http://getahead.org/dwr/browser/util/setvalue">More</a>.
75      * @param elementId The HTML element to update (by id)
76      * @param value The text to insert into the HTML element
77      */

78     public void setValue(String JavaDoc elementId, String JavaDoc value)
79     {
80         setValue(elementId, value, false);
81     }
82
83     /**
84      * Set the value an HTML element to the specified value.
85      * <p><a HREF="http://getahead.org/dwr/browser/util/setvalue">More</a>.
86      * @param elementId The HTML element to update (by id)
87      * @param value The text to insert into the HTML element
88      * @param escapeHtml Should we escape HTML characters?
89      */

90     public void setValue(String JavaDoc elementId, String JavaDoc value, boolean escapeHtml)
91     {
92         String JavaDoc options = escapeHtml ? "{escapeHtml:true}" : "{escapeHtml:false}";
93         addFunctionCall("dwr.util.setValue", elementId, value, options);
94     }
95
96     /**
97      * Given a map, call setValue() for all the entries in the map using the
98      * entry key as an element id.
99      * <p><a HREF="http://getahead.org/dwr/browser/util/setvalues">More</a>.
100      * @param values The map of elementIds to values to alter
101      * @param escapeHtml Should we escape HTML characters?
102      */

103     public void setValues(Map JavaDoc values, boolean escapeHtml)
104     {
105         String JavaDoc options = escapeHtml ? "{escapeHtml:true}" : "{escapeHtml:false}";
106         addFunctionCall("dwr.util.setValue", values, options);
107     }
108
109     /**
110      * Add options to a list from an array or map.
111      * <p><a HREF="http://getahead.org/dwr/browser/lists">More</a>.
112      * @param elementId The HTML element to update (by id)
113      * @param array An array of strings to use as both value and text of options
114      */

115     public void addOptions(String JavaDoc elementId, String JavaDoc[] array)
116     {
117         addFunctionCall("dwr.util.addOptions", elementId, array);
118     }
119
120     /**
121      * Add options to a list from an array or map.
122      * <p><a HREF="http://getahead.org/dwr/browser/lists">More</a>.
123      * @param elementId The HTML element to update (by id)
124      * @param array And array of objects from which to create options
125      * @param property The object property to use for the option value and text
126      */

127     public void addOptions(String JavaDoc elementId, Collection JavaDoc array, String JavaDoc property)
128     {
129         addFunctionCall("dwr.util.addOptions", elementId, array, property);
130     }
131
132     /**
133      * Add options to a list from an array or map.
134      * <p><a HREF="http://getahead.org/dwr/browser/lists">More</a>.
135      * @param elementId The HTML element to update (by id)
136      * @param array And array of objects from which to create options
137      * @param valueProperty The object property to use for the option value
138      * @param textProperty The object property to use for the option text
139      */

140     public void addOptions(String JavaDoc elementId, Collection JavaDoc array, String JavaDoc valueProperty, String JavaDoc textProperty)
141     {
142         addFunctionCall("dwr.util.addOptions", elementId, array, valueProperty, textProperty);
143     }
144
145     /**
146      * Remove all the options from a select list (specified by id)
147      * <p><a HREF="http://getahead.org/dwr/browser/lists">More</a>.
148      * @param elementId The HTML element to update (by id)
149      */

150     public void removeAllOptions(String JavaDoc elementId)
151     {
152         addFunctionCall("dwr.util.removeAllOptions", elementId);
153     }
154
155     /**
156      * Create rows inside a the table, tbody, thead or tfoot element (given by id).
157      * <p><a HREF="http://getahead.org/dwr/browser/tables">More</a>.
158      * @param elementId The HTML element to update (by id)
159      * @param data The cells to add to the table
160      * @param options See link above for documentation on the options
161      */

162     public void addRows(String JavaDoc elementId, String JavaDoc[][] data, String JavaDoc options)
163     {
164         if (data.length > 0)
165         {
166             StringBuffer JavaDoc functions = new StringBuffer JavaDoc();
167             for (int i = 0; i < data[0].length; i++)
168             {
169                 functions.append("function(data) { return data[" + i + "]},");
170             }
171             functions.deleteCharAt(functions.length() - 1);
172
173             ScriptBuffer script = new ScriptBuffer();
174             script.appendScript("dwr.util.addRows(")
175                   .appendData(elementId)
176                   .appendScript(",")
177                   .appendData(data)
178                   .appendScript(",")
179                   .appendScript("[" + functions.toString() + "]")
180                   .appendScript(options == null ? "" : ", " + options)
181                   .appendScript(");");
182
183             addScript(script);
184         }
185     }
186
187     /**
188      * Create rows inside a the table, tbody, thead or tfoot element (given by id).
189      * <p><a HREF="http://getahead.org/dwr/browser/tables">More</a>.
190      * @param elementId The HTML element to update (by id)
191      * @param data The cells to add to the table
192      */

193     public void addRows(String JavaDoc elementId, String JavaDoc[][] data)
194     {
195         addRows(elementId, data, null);
196     }
197
198     /**
199      * Remove all the children of a given node.
200      * <p><a HREF="http://getahead.org/dwr/browser/tables">More</a>.
201      * @param elementId The HTML element to update (by id)
202      */

203     public void removeAllRows(String JavaDoc elementId)
204     {
205         addFunctionCall("dwr.util.removeAllRows", elementId);
206     }
207
208     /**
209      * Clone a given node.
210      * <p><a HREF="http://getahead.org/dwr/browser/TODO">More</a>.
211      * @param elementId The HTML element to update (by id)
212      */

213     public void cloneNode(String JavaDoc elementId)
214     {
215         addFunctionCall("dwr.util.cloneNode", elementId);
216     }
217
218     /**
219      * Clone a given node.
220      * <p><a HREF="http://getahead.org/dwr/browser/TODO">More</a>.
221      * @param elementId The HTML element to update (by id)
222      * @param idPrefix How do we prefix ids in the cloned version of the node tree
223      * @param idSuffix How do we suffix ids in the cloned version of the node tree
224      */

225     public void cloneNode(String JavaDoc elementId, String JavaDoc idPrefix, String JavaDoc idSuffix)
226     {
227         ScriptBuffer script = new ScriptBuffer();
228         script.appendScript("dwr.util.cloneNode(")
229               .appendData(elementId)
230               .appendScript(", { idPrefix:")
231               .appendData(idPrefix)
232               .appendScript(", idSuffix:")
233               .appendData(idSuffix)
234               .appendScript("});");
235         addScript(script);
236     }
237
238     /**
239      * Sets a CSS style on an element
240      * @param elementId The HTML element to update (by id)
241      * @param selector The CSS selector to update
242      * @param value The new value for the CSS class on the given element
243      */

244     public void removeNode(String JavaDoc elementId)
245     {
246         ScriptBuffer script = new ScriptBuffer();
247         script.appendScript("dwr.util._temp = dwr.util.byId(")
248               .appendData(elementId)
249               .appendScript("); ")
250               .appendScript("if (dwr.util._temp) { dwr.util._temp.parentNode.removeChild(dwr.util._temp); dwr.util._temp = null; }");
251         addScript(script);
252     }
253
254     /**
255      * $(ele).className = "X", that we can call from Java easily
256      * @param elementId The HTML element to update (by id)
257      * @param className The CSS class to set for the element
258      */

259     public void setClassName(String JavaDoc elementId, String JavaDoc className)
260     {
261         addFunctionCall("dwr.util.setClassName", elementId, className);
262     }
263
264     /**
265      * $(ele).className += "X", that we can call from Java easily.
266      * @param elementId The HTML element to update (by id)
267      * @param className The CSS class to add to the element
268      */

269     public void addClassName(String JavaDoc elementId, String JavaDoc className)
270     {
271         addFunctionCall("dwr.util.addClassName", elementId, className);
272     }
273
274     /**
275      * $(ele).className -= "X", that we can call from Java easily From code originally by Gavin Kistner
276      * @param elementId The HTML element to update (by id)
277      * @param className The CSS class to remove from the element
278      */

279     public void removeClassName(String JavaDoc elementId, String JavaDoc className)
280     {
281         addFunctionCall("dwr.util.removeClassName", elementId, className);
282     }
283
284     /**
285      * $(ele).className |= "X", that we can call from Java easily.
286      * @param elementId The HTML element to update (by id)
287      * @param className The CSS class to toggle on/off
288      */

289     public void toggleClassName(String JavaDoc elementId, String JavaDoc className)
290     {
291         addFunctionCall("dwr.util.toggleClassName", elementId, className);
292     }
293
294     /**
295      * Sets a CSS style on an element
296      * @param elementId The HTML element to update (by id)
297      * @param selector The CSS selector to update
298      * @param value The new value for the CSS class on the given element
299      */

300     public void setStyle(String JavaDoc elementId, String JavaDoc selector, String JavaDoc value)
301     {
302         ScriptBuffer script = new ScriptBuffer();
303         script.appendScript("dwr.util.byId(")
304               .appendData(elementId)
305               .appendScript(").style.")
306               .appendScript(selector)
307               .appendScript("=")
308               .appendData(value)
309               .appendScript(";");
310         addScript(script);
311     }
312 }
313
Popular Tags