001: /* BaseZScript.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Aug 8, 2007 5:48:27 PM 2007, Created by Dennis.Chen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 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.jsf.zul.impl;
020:
021: import java.io.IOException;
022: import java.io.StringWriter;
023: import javax.faces.context.FacesContext;
024: import javax.faces.context.ResponseWriter;
025:
026: import org.zkoss.util.logging.Log;
027: import org.zkoss.zk.ui.Component;
028: import org.zkoss.zk.ui.metainfo.ZScript;
029:
030: /**
031: * The Base implementation of ZScript.
032: * This component should be declared nested under {@link org.zkoss.jsf.zul.Page}.
033: * @author Dennis.Chen
034: */
035: public class BaseZScript extends AbstractComponent {
036:
037: private boolean _deferred;
038: private String _lang = null;
039:
040: private RootComponent _rootcomp;
041: private BranchComponent _parentcomp;
042:
043: StringWriter fakeSw = null;
044:
045: ResponseWriter fakeOw = null;
046:
047: public void loadZULTree(org.zkoss.zk.ui.Page page,
048: StringWriter writer) throws IOException {
049: if (!isRendered() || !isEffective())
050: return; //nothing to do
051:
052: String content = getBodyContent();
053: setBodyContent(null);//clear it,
054: if (content == null)
055: return;
056: final ZScript zscript = ZScript.parseContent(content);
057:
058: if (zscript.getLanguage() == null)
059: zscript.setLanguage(_lang != null ? _lang : _rootcomp
060: .getZScriptLanguage());
061:
062: Component _parentzulcomp = null;
063: if (_parentcomp != null) {
064: _parentzulcomp = _parentcomp.getZULComponent();
065: }
066:
067: _rootcomp.processZScript(_parentzulcomp, zscript);
068: }
069:
070: /**
071: * Override method,
072: * We Construct ZUL JSF Component tree here.
073: * This method is called by JSF implementation, deriving class rarely need to invoke this method.
074: */
075: public void encodeBegin(FacesContext context) throws IOException {
076: fakeSw = new StringWriter();
077:
078: fakeOw = context.getResponseWriter();
079:
080: context.setResponseWriter(fakeOw.cloneWithWriter(fakeSw));
081:
082: if (!isRendered() || !isEffective())
083: return; //nothing to do
084: super .encodeBegin(context);
085: final AbstractComponent ac = (AbstractComponent) findAncestorWithClass(
086: this , AbstractComponent.class);
087: if (ac instanceof RootComponent) { //root component tag
088: _rootcomp = (RootComponent) ac;
089: } else if (ac instanceof BranchComponent) {
090: _parentcomp = (BranchComponent) ac;
091: _rootcomp = _parentcomp.getRootComponent();
092: } else {
093: throw new IllegalStateException(
094: "Must be nested inside the page component: " + this );
095: }
096:
097: //keep component tree structure for zul jsf component
098: ComponentInfo cinfo = getComponentInfo();
099: if (cinfo != null) {
100: if (_parentcomp != null) {
101: cinfo.addChildInfo(_parentcomp, this );
102: } else if (_rootcomp != null) {
103: cinfo.addChildInfo(_rootcomp, this );
104: }
105: }
106: }
107:
108: /**
109: * Override Method
110: */
111: public void encodeEnd(FacesContext context) throws IOException {
112: context.setResponseWriter(fakeOw);
113: fakeSw.close();
114:
115: fakeSw = null;
116: super .encodeEnd(context);
117: }
118:
119: /**
120: * return ComponentInfo of RootComponent
121: */
122: protected ComponentInfo getComponentInfo() {
123: if (_rootcomp != null) {
124: return _rootcomp.getComponentInfo();
125: }
126: return null;
127: }
128:
129: /**
130: * Returns whether to defer the execution of this zscript.
131: * <p>Default: false.
132: */
133: public boolean isDeferred() {
134: return _deferred;
135: }
136:
137: /**
138: * Sets whether to defer the execution of this zscript.
139: * @param deferred whether to defer the execution.
140: */
141: public void setDeferred(boolean deferred) {
142: this ._deferred = deferred;
143: }
144:
145: /** Returns the name of the scripting language in this ZScript tag.
146: *
147: * <p>Default: null (use what is defined in {@link org.zkoss.jsf.zul.tag.PageTag}).
148: * @return the name of the scripting language in this ZScript tag.
149: */
150: public String getLanguage() {
151: return _lang;
152: }
153:
154: /**
155: * Sets the name of the scripting language in this ZScript tag.
156: *
157: * <p>Default: Java.
158: *
159: * @param lang the name of the scripting language, such as
160: * Java, Ruby and Groovy.
161: */
162: public void setLanguage(String lang) {
163: this ._lang = lang;
164: }
165:
166: public Object saveState(FacesContext context) {
167: Object values[] = new Object[3];
168: values[0] = super .saveState(context);
169: values[1] = _lang;
170: values[2] = _deferred ? Boolean.TRUE : Boolean.FALSE;
171: return (values);
172: }
173:
174: public void restoreState(FacesContext context, Object state) {
175:
176: Object values[] = (Object[]) state;
177: super .restoreState(context, values[0]);
178: _lang = ((String) values[1]);
179: _deferred = ((Boolean) values[2]).booleanValue();
180: }
181:
182: }
|