001: /* BaseAttribute.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:
024: import javax.faces.context.FacesContext;
025: import javax.faces.context.ResponseWriter;
026:
027: /**
028: * The Base implementation of Attribute.
029: * This component should be declared nested under {@link org.zkoss.jsf.zul.Page}.
030: * @author Dennis.Chen
031: */
032: public class BaseAttribute extends AbstractComponent {
033:
034: private String _name = null;
035:
036: //private RootComponent _rootcomp;
037: private BranchComponent _parentcomp;
038: StringWriter fakeSw = null;
039:
040: ResponseWriter fakeOw = null;
041:
042: /**
043: * Override Method, write a body content to parent's attribute
044: *
045: * @see BranchComponent#addZULDynamicAttribute(String, Object)
046: */
047: public void encodeEnd(FacesContext context) throws IOException {
048: context.setResponseWriter(fakeOw);
049: fakeSw.close();
050: String content = fakeSw.toString();
051:
052: fakeSw = null;
053: if (!isRendered() || !isEffective())
054: return; //nothing to do
055: if (_name != null) {
056: _parentcomp.addZULDynamicAttribute(_name, getBodyContent());
057: }
058: setBodyContent(null);//clear
059: }
060:
061: /**
062: * Override method,
063: * We Construct ZUL JSF Component tree here.
064: * This method is called by JSF implementation, deriving class rarely need to invoke this method.
065: */
066: public void encodeBegin(FacesContext context) throws IOException {
067: fakeSw = new StringWriter();
068:
069: fakeOw = context.getResponseWriter();
070:
071: context.setResponseWriter(fakeOw.cloneWithWriter(fakeSw));
072: if (!isRendered() || !isEffective())
073: return; //nothing to do
074: super .encodeBegin(context);
075: final AbstractComponent ac = (AbstractComponent) findAncestorWithClass(
076: this , AbstractComponent.class);
077: if (ac instanceof RootComponent) { //root component
078: //do nothing
079: } else if (ac instanceof BranchComponent) {
080: _parentcomp = (BranchComponent) ac;
081: } else {
082: throw new IllegalStateException(
083: "Must be nested inside the page component: " + this );
084: }
085: }
086:
087: /**
088: * @return name of attribute.
089: */
090: public String getName() {
091: return _name;
092: }
093:
094: /**
095: * set name of attribute.
096: * @param _name
097: */
098: public void setName(String _name) {
099: this ._name = _name;
100: }
101:
102: public Object saveState(FacesContext context) {
103: Object values[] = new Object[2];
104: values[0] = super .saveState(context);
105: values[1] = _name;
106: return (values);
107: }
108:
109: public void restoreState(FacesContext context, Object state) {
110:
111: Object values[] = (Object[]) state;
112: super .restoreState(context, values[0]);
113: _name = ((String) values[1]);
114: }
115:
116: }
|