001: /* Body.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue Dec 13 10:50:07 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zhtml;
020:
021: import java.util.Collection;
022: import java.io.StringWriter;
023:
024: import org.zkoss.zk.ui.Component;
025: import org.zkoss.zk.ui.Page;
026: import org.zkoss.zk.ui.Execution;
027: import org.zkoss.zk.ui.Executions;
028: import org.zkoss.zk.ui.sys.PageCtrl;
029: import org.zkoss.zk.fn.ZkFns;
030: import org.zkoss.zhtml.impl.AbstractTag;
031:
032: /**
033: * The BODY tag.
034: *
035: * @author tomyeh
036: */
037: public class Body extends AbstractTag {
038: public Body() {
039: super ("body");
040: }
041:
042: //-- super --//
043: public void setParent(Component parent) {
044: final Page old = getPage();
045: super .setParent(parent);
046: fixDefaultParent(getPage(), old);
047: }
048:
049: public void setPage(Page page) {
050: final Page old = getPage();
051: super .setPage(page);
052: fixDefaultParent(page, old);
053: }
054:
055: private void fixDefaultParent(Page page, Page old) {
056: if (page != old) {
057: if (old != null) {
058: final PageCtrl oldc = (PageCtrl) old;
059: if (oldc.getDefaultParent() == this )
060: oldc.setDefaultParent(null);
061: }
062: if (page != null)
063: ((PageCtrl) page).setDefaultParent(this );
064: }
065: }
066:
067: //--Component-//
068: public void redraw(java.io.Writer out) throws java.io.IOException {
069: final StringWriter bufout = new StringWriter();
070: super .redraw(bufout);
071: final StringBuffer buf = bufout.getBuffer();
072:
073: final String zktags = Head.outZKHtmlTags(), zkresp = outZKResponses();
074: if (zktags != null || zkresp != null) {
075: final int j = buf.lastIndexOf("</body>");
076: if (j >= 0) {
077: if (zkresp != null)
078: buf.insert(j, zkresp);
079: if (zktags != null)
080: buf.insert(j, zktags);
081: } else {
082: if (zktags != null)
083: buf.append(zktags);
084: if (zkresp != null)
085: buf.append(zkresp);
086: }
087: }
088:
089: out.write(buf.toString());
090: out.write('\n');
091: }
092:
093: private static String outZKResponses() {
094: final Execution exec = Executions.getCurrent();
095: final String ATTR_RESPONSES = "zk_argResponses";
096: final Collection responses = (Collection) exec
097: .getAttribute(ATTR_RESPONSES);
098: if (responses == null || responses.isEmpty())
099: return null;
100:
101: final StringBuffer sb = new StringBuffer(256).append(
102: "\n<script type=\"text/javascript\">\n").append(
103: ZkFns.outResponseJavaScripts(responses)).append(
104: "\n</script>\n");
105:
106: exec.removeAttribute(ATTR_RESPONSES); //turn off page.dsp's generation
107: return sb.toString();
108: }
109: }
|