001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.proxy.dwr;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.directwebremoting.ScriptBuffer;
023: import org.directwebremoting.ScriptSession;
024: import org.directwebremoting.proxy.ScriptProxy;
025:
026: /**
027: * Util is a server-side proxy that allows Java programmers to call client
028: * side Javascript from Java.
029: * <p>
030: * Each Util object is associated with a list of ScriptSessions and the
031: * proxy code is creates will be dynamically forwarded to all those browsers.
032: * <p>
033: * Currently this class contains only the write-only DOM manipulation functions
034: * from Util. It is possible that we could add the read methods, however
035: * the complexity in the callback and the fact that you are probably not going
036: * to need it means that we'll leave it for another day. Specifically,
037: * <code>getValue</code>, <code>getValues</code> and <code>getText</code> have
038: * been left out as being read functions and <code>useLoadingMessage</code> etc
039: * have been left out as not being DOM related.
040: * @author Joe Walker [joe at getahead dot ltd dot uk]
041: * @author Jorge Martin Cuervo [darthkorr at gmail dot com]
042: */
043: public class Util extends ScriptProxy {
044: /**
045: * Http thread constructor, that affects no browsers.
046: * Calls to {@link Util#addScriptSession(ScriptSession)} or to
047: * {@link Util#addScriptSessions(Collection)} will be needed
048: */
049: public Util() {
050: }
051:
052: /**
053: * Http thread constructor that alters a single browser
054: * @param scriptSession The browser to alter
055: */
056: public Util(ScriptSession scriptSession) {
057: super (scriptSession);
058: }
059:
060: /**
061: * Http thread constructor that alters a number of browsers
062: * @param scriptSessions A collection of ScriptSessions that we should act on.
063: */
064: public Util(Collection<ScriptSession> scriptSessions) {
065: super (scriptSessions);
066: }
067:
068: /**
069: * Set the value an HTML element to the specified value.
070: * <p><a href="http://getahead.org/dwr/browser/util/setvalue">More</a>.
071: * @param elementId The HTML element to update (by id)
072: * @param value The text to insert into the HTML element
073: */
074: public void setValue(String elementId, Object value) {
075: setValue(elementId, value, false);
076: }
077:
078: /**
079: * Set the value an HTML element to the specified value.
080: * <p><a href="http://getahead.org/dwr/browser/util/setvalue">More</a>.
081: * @param elementId The HTML element to update (by id)
082: * @param value The text to insert into the HTML element
083: * @param escapeHtml Should we escape HTML characters?
084: */
085: public void setValue(String elementId, Object value,
086: boolean escapeHtml) {
087: addFunctionCall("dwr.util.setValue", elementId, value,
088: getEscapeOptions(escapeHtml));
089: }
090:
091: /**
092: * Given a map, call setValue() for all the entries in the map using the
093: * entry key as an element id.
094: * <p><a href="http://getahead.org/dwr/browser/util/setvalues">More</a>.
095: * @param values The map of elementIds to values to alter
096: * @param escapeHtml Should we escape HTML characters?
097: */
098: public void setValues(Map<?, ?> values, boolean escapeHtml) {
099: addFunctionCall("dwr.util.setValues", values,
100: getEscapeOptions(escapeHtml));
101: }
102:
103: /**
104: * Add options to a list from an array or map.
105: * <p><a href="http://getahead.org/dwr/browser/lists">More</a>.
106: * @param elementId The HTML element to update (by id)
107: * @param array An array of strings to use as both value and text of options
108: */
109: public void addOptions(String elementId, String[] array) {
110: addFunctionCall("dwr.util.addOptions", elementId, array);
111: }
112:
113: /**
114: * Add options to a list from an array or map.
115: * <p><a href="http://getahead.org/dwr/browser/lists">More</a>.
116: * @param elementId The HTML element to update (by id)
117: * @param array And array of objects from which to create options
118: * @param property The object property to use for the option value and text
119: */
120: public void addOptions(String elementId, Collection<?> array,
121: String property) {
122: addFunctionCall("dwr.util.addOptions", elementId, array,
123: property);
124: }
125:
126: /**
127: * Add options to a list from an array or map.
128: * <p><a href="http://getahead.org/dwr/browser/lists">More</a>.
129: * @param elementId The HTML element to update (by id)
130: * @param array And array of objects from which to create options
131: * @param valueProperty The object property to use for the option value
132: * @param textProperty The object property to use for the option text
133: */
134: public void addOptions(String elementId, Collection<?> array,
135: String valueProperty, String textProperty) {
136: addFunctionCall("dwr.util.addOptions", elementId, array,
137: valueProperty, textProperty);
138: }
139:
140: /**
141: * Remove all the options from a select list (specified by id)
142: * <p><a href="http://getahead.org/dwr/browser/lists">More</a>.
143: * @param elementId The HTML element to update (by id)
144: */
145: public void removeAllOptions(String elementId) {
146: addFunctionCall("dwr.util.removeAllOptions", elementId);
147: }
148:
149: /**
150: * Create rows inside a the table, tbody, thead or tfoot element (given by id).
151: * <p><a href="http://getahead.org/dwr/browser/tables">More</a>.
152: * @param elementId The HTML element to update (by id)
153: * @param data The cells to add to the table
154: * @param options See link above for documentation on the options
155: */
156: public void addRows(String elementId, String[][] data,
157: String options) {
158: if (data.length > 0) {
159: StringBuffer functions = new StringBuffer();
160: for (int i = 0; i < data[0].length; i++) {
161: functions.append("function(data) { return data[")
162: .append(i).append("]},");
163: }
164:
165: functions.deleteCharAt(functions.length() - 1);
166:
167: ScriptBuffer script = new ScriptBuffer();
168: script
169: .appendScript("dwr.util.addRows(")
170: .appendData(elementId)
171: .appendScript(",")
172: .appendData(data)
173: .appendScript(",")
174: .appendScript("[" + functions.toString() + "]")
175: .appendScript(options == null ? "" : ", " + options)
176: .appendScript(");");
177:
178: addScript(script);
179: }
180: }
181:
182: /**
183: * Create rows inside a the table, tbody, thead or tfoot element (given by id).
184: * <p><a href="http://getahead.org/dwr/browser/tables">More</a>.
185: * @param elementId The HTML element to update (by id)
186: * @param data The cells to add to the table
187: */
188: public void addRows(String elementId, String[][] data) {
189: addRows(elementId, data, null);
190: }
191:
192: /**
193: * Remove all the children of a given node.
194: * <p><a href="http://getahead.org/dwr/browser/tables">More</a>.
195: * @param elementId The HTML element to update (by id)
196: */
197: public void removeAllRows(String elementId) {
198: addFunctionCall("dwr.util.removeAllRows", elementId);
199: }
200:
201: /**
202: * Clone a given node.
203: * <p><a href="http://getahead.org/dwr/browser/clonenode">More</a>.
204: * @param elementId The HTML element to update (by id)
205: */
206: public void cloneNode(String elementId) {
207: addFunctionCall("dwr.util.cloneNode", elementId);
208: }
209:
210: /**
211: * Clone a given node.
212: * <p><a href="http://getahead.org/dwr/browser/clonenode">More</a>.
213: * @param elementId The HTML element to update (by id)
214: * @param idPrefix How do we prefix ids in the cloned version of the node tree
215: * @param idSuffix How do we suffix ids in the cloned version of the node tree
216: */
217: public void cloneNode(String elementId, String idPrefix,
218: String idSuffix) {
219: ScriptBuffer script = new ScriptBuffer();
220: script.appendScript("dwr.util.cloneNode(")
221: .appendData(elementId).appendScript(", { idPrefix:")
222: .appendData(idPrefix).appendScript(", idSuffix:")
223: .appendData(idSuffix).appendScript("});");
224: addScript(script);
225: }
226:
227: /**
228: * Sets a CSS style on an element
229: * @param elementId The HTML element to update (by id)
230: */
231: public void removeNode(String elementId) {
232: ScriptBuffer script = new ScriptBuffer();
233: script
234: .appendScript("dwr.util._temp = dwr.util.byId(")
235: .appendData(elementId)
236: .appendScript("); ")
237: .appendScript(
238: "if (dwr.util._temp) { dwr.util._temp.parentNode.removeChild(dwr.util._temp); dwr.util._temp = null; }");
239: addScript(script);
240: }
241:
242: /**
243: * $(ele).className = "X", that we can call from Java easily
244: * @param elementId The HTML element to update (by id)
245: * @param className The CSS class to set for the element
246: */
247: public void setClassName(String elementId, String className) {
248: addFunctionCall("dwr.util.setClassName", elementId, className);
249: }
250:
251: /**
252: * $(ele).className += "X", that we can call from Java easily.
253: * @param elementId The HTML element to update (by id)
254: * @param className The CSS class to add to the element
255: */
256: public void addClassName(String elementId, String className) {
257: addFunctionCall("dwr.util.addClassName", elementId, className);
258: }
259:
260: /**
261: * $(ele).className -= "X", that we can call from Java easily From code originally by Gavin Kistner
262: * @param elementId The HTML element to update (by id)
263: * @param className The CSS class to remove from the element
264: */
265: public void removeClassName(String elementId, String className) {
266: addFunctionCall("dwr.util.removeClassName", elementId,
267: className);
268: }
269:
270: /**
271: * $(ele).className |= "X", that we can call from Java easily.
272: * @param elementId The HTML element to update (by id)
273: * @param className The CSS class to toggle on/off
274: */
275: public void toggleClassName(String elementId, String className) {
276: addFunctionCall("dwr.util.toggleClassName", elementId,
277: className);
278: }
279:
280: /**
281: * Sets a CSS style on an element
282: * @param elementId The HTML element to update (by id)
283: * @param selector The CSS selector to update
284: * @param value The new value for the CSS class on the given element
285: */
286: public void setStyle(String elementId, String selector, String value) {
287: ScriptBuffer script = new ScriptBuffer();
288: script.appendScript("dwr.util.byId(").appendData(elementId)
289: .appendScript(").style.").appendScript(selector)
290: .appendScript("=").appendData(value).appendScript(";");
291: addScript(script);
292: }
293:
294: /**
295: * Internal utility to fetch an options object that contains a single
296: * setting for "escapeHtml"
297: * @param escapeHtml Do we want the client to escape HTML?
298: * @return An options object containing the setting.
299: */
300: private Map<String, Boolean> getEscapeOptions(boolean escapeHtml) {
301: Map<String, Boolean> options = new HashMap<String, Boolean>();
302: options.put("escapeHtml", escapeHtml);
303: return options;
304: }
305: }
|