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 jsx3.html;
017:
018: import org.directwebremoting.ScriptBuffer;
019: import org.directwebremoting.proxy.ScriptProxy;
020: import org.directwebremoting.proxy.io.Context;
021:
022: /**
023: * Represents an HTML element. Provides an object oriented way of painting to screen.
024:
025: This class is available only when the Charting add-in is enabled.
026: * @author Joe Walker [joe at getahead dot org]
027: * @author DRAPGEN - Dwr Reverse Ajax Proxy GENerator
028: */
029: public class Tag extends jsx3.lang.Object {
030: /**
031: * All reverse ajax proxies need context to work from
032: * @param scriptProxy The place we are writing scripts to
033: * @param context The script that got us to where we are now
034: */
035: public Tag(Context context, String extension,
036: ScriptProxy scriptProxy) {
037: super (context, extension, scriptProxy);
038: }
039:
040: /**
041: * The instance initializer.
042: * @param strTagNS
043: * @param strTagName
044: */
045: public Tag(String strTagNS, String strTagName) {
046: super ((Context) null, (String) null, (ScriptProxy) null);
047: ScriptBuffer script = new ScriptBuffer();
048: script.appendCall("new Tag", strTagNS, strTagName);
049: setInitScript(script);
050: }
051:
052: /**
053: * Sdds a child to the list of this tag's children; may be vetoed by onAppendChild().
054: * @param child the child to add, must not already have a parent
055: */
056: public void appendChild(jsx3.html.Tag child) {
057: ScriptBuffer script = new ScriptBuffer();
058: script.appendCall(getContextPath() + "appendChild", child);
059: getScriptProxy().addScript(script);
060: }
061:
062: /**
063: * Removes a child from the list of this tag's children; may be vetoed by onRemoveChild().
064: * @param child the child to remove, must exist in the list of children
065: */
066: public void removeChild(jsx3.html.Tag child) {
067: ScriptBuffer script = new ScriptBuffer();
068: script.appendCall(getContextPath() + "removeChild", child);
069: getScriptProxy().addScript(script);
070: }
071:
072: /**
073: * Replaces a child of this tag.
074: * @param child the new child.
075: * @param oldChild the child to replace.
076: */
077: public void replaceChild(jsx3.html.Tag child, jsx3.html.Tag oldChild) {
078: ScriptBuffer script = new ScriptBuffer();
079: script.appendCall(getContextPath() + "replaceChild", child,
080: oldChild);
081: getScriptProxy().addScript(script);
082: }
083:
084: /**
085: * Removes all the children of this tag.
086: */
087: public void removeChildren() {
088: ScriptBuffer script = new ScriptBuffer();
089: script.appendCall(getContextPath() + "removeChildren");
090: getScriptProxy().addScript(script);
091: }
092:
093: /**
094: * Returns the parent tag.
095: * @return parent
096: */
097: @SuppressWarnings("unchecked")
098: public jsx3.html.Tag getParent() {
099: String extension = "getParent().";
100: try {
101: java.lang.reflect.Constructor<jsx3.html.Tag> ctor = jsx3.html.Tag.class
102: .getConstructor(Context.class, String.class,
103: ScriptProxy.class);
104: return ctor.newInstance(this , extension, getScriptProxy());
105: } catch (Exception ex) {
106: throw new IllegalArgumentException("Unsupported type: "
107: + jsx3.html.Tag.class.getName());
108: }
109: }
110:
111: /**
112: * Returns the parent tag.
113: * @param returnType The expected return type
114: * @return parent
115: */
116: @SuppressWarnings("unchecked")
117: public <T> T getParent(Class<T> returnType) {
118: String extension = "getParent().";
119: try {
120: java.lang.reflect.Constructor<T> ctor = returnType
121: .getConstructor(Context.class, String.class,
122: ScriptProxy.class);
123: return ctor.newInstance(this , extension, getScriptProxy());
124: } catch (Exception ex) {
125: throw new IllegalArgumentException(
126: "Unsupported return type: " + returnType.getName());
127: }
128: }
129:
130: /**
131: * Returns the children tags.
132: * @param callback children
133: */
134: @SuppressWarnings("unchecked")
135: public void getChildren(
136: org.directwebremoting.proxy.Callback<Object[]> callback) {
137: ScriptBuffer script = new ScriptBuffer();
138: String callbackPrefix = "";
139:
140: if (callback != null) {
141: callbackPrefix = "var reply = ";
142: }
143:
144: script.appendCall(callbackPrefix + getContextPath()
145: + "getChildren");
146:
147: if (callback != null) {
148: String key = org.directwebremoting.extend.CallbackHelper
149: .saveCallback(callback, Object[].class);
150: script
151: .appendCall("__System.activateCallback", key,
152: "reply");
153: }
154:
155: getScriptProxy().addScript(script);
156: }
157:
158: /**
159: * Returns the id field.
160: * @param callback id
161: */
162: @SuppressWarnings("unchecked")
163: public void getId(
164: org.directwebremoting.proxy.Callback<String> callback) {
165: ScriptBuffer script = new ScriptBuffer();
166: String callbackPrefix = "";
167:
168: if (callback != null) {
169: callbackPrefix = "var reply = ";
170: }
171:
172: script.appendCall(callbackPrefix + getContextPath() + "getId");
173:
174: if (callback != null) {
175: String key = org.directwebremoting.extend.CallbackHelper
176: .saveCallback(callback, String.class);
177: script
178: .appendCall("__System.activateCallback", key,
179: "reply");
180: }
181:
182: getScriptProxy().addScript(script);
183: }
184:
185: /**
186: * Sets the id field.
187: * @param id the new value for id
188: */
189: public void setId(String id) {
190: ScriptBuffer script = new ScriptBuffer();
191: script.appendCall(getContextPath() + "setId", id);
192: getScriptProxy().addScript(script);
193: }
194:
195: /**
196: * Returns the cssClass field.
197: * @param callback cssClass
198: */
199: @SuppressWarnings("unchecked")
200: public void getClassName(
201: org.directwebremoting.proxy.Callback<String> callback) {
202: ScriptBuffer script = new ScriptBuffer();
203: String callbackPrefix = "";
204:
205: if (callback != null) {
206: callbackPrefix = "var reply = ";
207: }
208:
209: script.appendCall(callbackPrefix + getContextPath()
210: + "getClassName");
211:
212: if (callback != null) {
213: String key = org.directwebremoting.extend.CallbackHelper
214: .saveCallback(callback, String.class);
215: script
216: .appendCall("__System.activateCallback", key,
217: "reply");
218: }
219:
220: getScriptProxy().addScript(script);
221: }
222:
223: /**
224: * Sets the cssClass field, the HTML 'class' attribute.
225: * @param cssClass the new value for cssClass
226: */
227: public void setClassName(String cssClass) {
228: ScriptBuffer script = new ScriptBuffer();
229: script.appendCall(getContextPath() + "setClassName", cssClass);
230: getScriptProxy().addScript(script);
231: }
232:
233: /**
234: * Sets the extraStyles field, this string is prepended as-is to the generated value for the style attribute of the tag.
235: * @param extraStyles the new value for extraStyles
236: */
237: public void setExtraStyles(String extraStyles) {
238: ScriptBuffer script = new ScriptBuffer();
239: script.appendCall(getContextPath() + "setExtraStyles",
240: extraStyles);
241: getScriptProxy().addScript(script);
242: }
243:
244: /**
245: * Releases all bi-directional references between this instance and its children.
246: */
247: public void release() {
248: ScriptBuffer script = new ScriptBuffer();
249: script.appendCall(getContextPath() + "release");
250: getScriptProxy().addScript(script);
251: }
252:
253: /**
254: * Called before appending a child.
255: * @param child
256: * @param callback <code>true</code> to allow the append, <code>false</code> to veto.
257: */
258: @SuppressWarnings("unchecked")
259: public void onAppendChild(jsx3.html.Tag child,
260: org.directwebremoting.proxy.Callback<Boolean> callback) {
261: ScriptBuffer script = new ScriptBuffer();
262: String callbackPrefix = "";
263:
264: if (callback != null) {
265: callbackPrefix = "var reply = ";
266: }
267:
268: script.appendCall(callbackPrefix + getContextPath()
269: + "onAppendChild", child);
270:
271: if (callback != null) {
272: String key = org.directwebremoting.extend.CallbackHelper
273: .saveCallback(callback, Boolean.class);
274: script
275: .appendCall("__System.activateCallback", key,
276: "reply");
277: }
278:
279: getScriptProxy().addScript(script);
280: }
281:
282: /**
283: * Called before removing a child.
284: * @param child
285: * @param callback <code>true</code> to allow the removal, <code>false</code> to veto.
286: */
287: @SuppressWarnings("unchecked")
288: public void onRemoveChild(jsx3.html.Tag child,
289: org.directwebremoting.proxy.Callback<Boolean> callback) {
290: ScriptBuffer script = new ScriptBuffer();
291: String callbackPrefix = "";
292:
293: if (callback != null) {
294: callbackPrefix = "var reply = ";
295: }
296:
297: script.appendCall(callbackPrefix + getContextPath()
298: + "onRemoveChild", child);
299:
300: if (callback != null) {
301: String key = org.directwebremoting.extend.CallbackHelper
302: .saveCallback(callback, Boolean.class);
303: script
304: .appendCall("__System.activateCallback", key,
305: "reply");
306: }
307:
308: getScriptProxy().addScript(script);
309: }
310:
311: /**
312: * Sets an attribute of this HTML element. This method may be called with a variable number of arguments, which are
313: interpreted as name/value pairs, i.e.: tag.setProperty(n1, p1, n2, p2);.
314: * @param strName the name of the attribute.
315: * @param strValue the value of the attribute. If <code>null</code>, the attribute is removed.
316: */
317: public void setProperty(String strName, String strValue) {
318: ScriptBuffer script = new ScriptBuffer();
319: script.appendCall(getContextPath() + "setProperty", strName,
320: strValue);
321: getScriptProxy().addScript(script);
322: }
323:
324: /**
325: * Returns an attribute of this HTML element.
326: * @param strName the name of the attribute.
327: * @param callback the value of the attribute.
328: */
329: @SuppressWarnings("unchecked")
330: public void getProperty(String strName,
331: org.directwebremoting.proxy.Callback<String> callback) {
332: ScriptBuffer script = new ScriptBuffer();
333: String callbackPrefix = "";
334:
335: if (callback != null) {
336: callbackPrefix = "var reply = ";
337: }
338:
339: script.appendCall(callbackPrefix + getContextPath()
340: + "getProperty", strName);
341:
342: if (callback != null) {
343: String key = org.directwebremoting.extend.CallbackHelper
344: .saveCallback(callback, String.class);
345: script
346: .appendCall("__System.activateCallback", key,
347: "reply");
348: }
349:
350: getScriptProxy().addScript(script);
351: }
352:
353: /**
354: * Removes any number of properties from this HTML element.
355: * @param strName the names of the attributes.
356: */
357: public void removeProperty(String strName) {
358: ScriptBuffer script = new ScriptBuffer();
359: script.appendCall(getContextPath() + "removeProperty", strName);
360: getScriptProxy().addScript(script);
361: }
362:
363: /**
364: * Sets a style of this HTML element. This method may be called with a variable number of arguments, which are
365: interpreted as name/value pairs, i.e.: tag.setStyle(n1, s1, n2, s2);.
366: * @param strName the name of the style.
367: * @param strValue the value of the style.
368: */
369: public void setStyle(String strName, String strValue) {
370: ScriptBuffer script = new ScriptBuffer();
371: script.appendCall(getContextPath() + "setStyle", strName,
372: strValue);
373: getScriptProxy().addScript(script);
374: }
375:
376: /**
377: * Returns a style of this HTML element.
378: * @param strName the name of the style.
379: * @param callback the value of the style.
380: */
381: @SuppressWarnings("unchecked")
382: public void getStyle(String strName,
383: org.directwebremoting.proxy.Callback<String> callback) {
384: ScriptBuffer script = new ScriptBuffer();
385: String callbackPrefix = "";
386:
387: if (callback != null) {
388: callbackPrefix = "var reply = ";
389: }
390:
391: script.appendCall(callbackPrefix + getContextPath()
392: + "getStyle", strName);
393:
394: if (callback != null) {
395: String key = org.directwebremoting.extend.CallbackHelper
396: .saveCallback(callback, String.class);
397: script
398: .appendCall("__System.activateCallback", key,
399: "reply");
400: }
401:
402: getScriptProxy().addScript(script);
403: }
404:
405: /**
406: * Removes any number of styles from this HTML element.
407: * @param strName the names of the styles.
408: */
409: public void removeStyle(String strName) {
410: ScriptBuffer script = new ScriptBuffer();
411: script.appendCall(getContextPath() + "removeStyle", strName);
412: getScriptProxy().addScript(script);
413: }
414:
415: /**
416: * Returns the name of this HTML element, such as "table" or "div".
417: * @param callback the tag name
418: */
419: @SuppressWarnings("unchecked")
420: public void getTagName(
421: org.directwebremoting.proxy.Callback<String> callback) {
422: ScriptBuffer script = new ScriptBuffer();
423: String callbackPrefix = "";
424:
425: if (callback != null) {
426: callbackPrefix = "var reply = ";
427: }
428:
429: script.appendCall(callbackPrefix + getContextPath()
430: + "getTagName");
431:
432: if (callback != null) {
433: String key = org.directwebremoting.extend.CallbackHelper
434: .saveCallback(callback, String.class);
435: script
436: .appendCall("__System.activateCallback", key,
437: "reply");
438: }
439:
440: getScriptProxy().addScript(script);
441: }
442:
443: /**
444: * Returns the namespace of this HTML element.
445: * @param callback the tag name
446: */
447: @SuppressWarnings("unchecked")
448: public void getTagNS(
449: org.directwebremoting.proxy.Callback<String> callback) {
450: ScriptBuffer script = new ScriptBuffer();
451: String callbackPrefix = "";
452:
453: if (callback != null) {
454: callbackPrefix = "var reply = ";
455: }
456:
457: script.appendCall(callbackPrefix + getContextPath()
458: + "getTagNS");
459:
460: if (callback != null) {
461: String key = org.directwebremoting.extend.CallbackHelper
462: .saveCallback(callback, String.class);
463: script
464: .appendCall("__System.activateCallback", key,
465: "reply");
466: }
467:
468: getScriptProxy().addScript(script);
469: }
470:
471: /**
472: * Serializes this HTML element to an HTML string using various overridable methods in this class.
473: This method is only available in the VML version of this class.
474: * @param callback this tag serialized to HTML.
475: */
476: @SuppressWarnings("unchecked")
477: public void paint(
478: org.directwebremoting.proxy.Callback<String> callback) {
479: ScriptBuffer script = new ScriptBuffer();
480: String callbackPrefix = "";
481:
482: if (callback != null) {
483: callbackPrefix = "var reply = ";
484: }
485:
486: script.appendCall(callbackPrefix + getContextPath() + "paint");
487:
488: if (callback != null) {
489: String key = org.directwebremoting.extend.CallbackHelper
490: .saveCallback(callback, String.class);
491: script
492: .appendCall("__System.activateCallback", key,
493: "reply");
494: }
495:
496: getScriptProxy().addScript(script);
497: }
498:
499: /**
500: * Prepares this HTML element for insertion into the live browser DOM and returns the underlying native HTML element.
501: This method is only available in the SVG version of this class.
502: * @param callback the native browser html element.
503: */
504: @SuppressWarnings("unchecked")
505: public void paintDom(
506: org.directwebremoting.proxy.Callback<String> callback) {
507: ScriptBuffer script = new ScriptBuffer();
508: String callbackPrefix = "";
509:
510: if (callback != null) {
511: callbackPrefix = "var reply = ";
512: }
513:
514: script.appendCall(callbackPrefix + getContextPath()
515: + "paintDom");
516:
517: if (callback != null) {
518: String key = org.directwebremoting.extend.CallbackHelper
519: .saveCallback(callback, String.class);
520: script
521: .appendCall("__System.activateCallback", key,
522: "reply");
523: }
524:
525: getScriptProxy().addScript(script);
526: }
527:
528: /**
529: * This method is called on each HTML tag before it is painted to screen. Methods in subclasses of this class that
530: override this method should begin with a call to jsxsuper().
531: */
532: public void paintUpdate() {
533: ScriptBuffer script = new ScriptBuffer();
534: script.appendCall(getContextPath() + "paintUpdate");
535: getScriptProxy().addScript(script);
536: }
537:
538: /**
539: * Returns the first child tag of type type.
540: * @param type the fully-qualified class name or the class constructor function.
541: */
542: @SuppressWarnings("unchecked")
543: public jsx3.html.Tag getFirstChildOfType(String type) {
544: String extension = "getFirstChildOfType(\"" + type + "\").";
545: try {
546: java.lang.reflect.Constructor<jsx3.html.Tag> ctor = jsx3.html.Tag.class
547: .getConstructor(Context.class, String.class,
548: ScriptProxy.class);
549: return ctor.newInstance(this , extension, getScriptProxy());
550: } catch (Exception ex) {
551: throw new IllegalArgumentException("Unsupported type: "
552: + jsx3.html.Tag.class.getName());
553: }
554: }
555:
556: /**
557: * Returns the first child tag of type type.
558: * @param type the fully-qualified class name or the class constructor function.
559: * @param returnType The expected return type
560: */
561: @SuppressWarnings("unchecked")
562: public <T> T getFirstChildOfType(String type, Class<T> returnType) {
563: String extension = "getFirstChildOfType(\"" + type + "\").";
564: try {
565: java.lang.reflect.Constructor<T> ctor = returnType
566: .getConstructor(Context.class, String.class,
567: ScriptProxy.class);
568: return ctor.newInstance(this , extension, getScriptProxy());
569: } catch (Exception ex) {
570: throw new IllegalArgumentException(
571: "Unsupported return type: " + returnType.getName());
572: }
573: }
574:
575: /**
576: * Returns the first child tag of type type.
577: * @param type the fully-qualified class name or the class constructor function.
578: */
579: @SuppressWarnings("unchecked")
580: public jsx3.html.Tag getFirstChildOfType(
581: org.directwebremoting.proxy.CodeBlock type) {
582: String extension = "getFirstChildOfType(\"" + type + "\").";
583: try {
584: java.lang.reflect.Constructor<jsx3.html.Tag> ctor = jsx3.html.Tag.class
585: .getConstructor(Context.class, String.class,
586: ScriptProxy.class);
587: return ctor.newInstance(this , extension, getScriptProxy());
588: } catch (Exception ex) {
589: throw new IllegalArgumentException("Unsupported type: "
590: + jsx3.html.Tag.class.getName());
591: }
592: }
593:
594: /**
595: * Returns the first child tag of type type.
596: * @param type the fully-qualified class name or the class constructor function.
597: * @param returnType The expected return type
598: */
599: @SuppressWarnings("unchecked")
600: public <T> T getFirstChildOfType(
601: org.directwebremoting.proxy.CodeBlock type,
602: Class<T> returnType) {
603: String extension = "getFirstChildOfType(\"" + type + "\").";
604: try {
605: java.lang.reflect.Constructor<T> ctor = returnType
606: .getConstructor(Context.class, String.class,
607: ScriptProxy.class);
608: return ctor.newInstance(this , extension, getScriptProxy());
609: } catch (Exception ex) {
610: throw new IllegalArgumentException(
611: "Unsupported return type: " + returnType.getName());
612: }
613: }
614:
615: }
|