001: /* BaseUi.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: 2007/11/21 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.util.HashMap;
022: import java.util.Map;
023:
024: import javax.faces.context.FacesContext;
025:
026: import org.zkoss.zk.ui.Component;
027: import org.zkoss.zk.ui.metainfo.ComponentDefinition;
028:
029: /**
030: * The Base implementation of ZScript.
031: * This component should be declared nested under {@link org.zkoss.jsf.zul.Page}.
032: * @author Dennis.Chen
033: */
034: public class BaseUi extends BranchComponent {
035:
036: private String _tag;
037:
038: private ComponentDefinition _compDef;
039:
040: /**
041: * Components of this component,
042: * if the definition of _tag set to inline, then _comps is the created components base on macroURI
043: * if desn't inline, then _comps is the created HtmlMacroComponent.
044: */
045: private Component[] _comps;
046:
047: public void setTag(String tag) {
048: _tag = tag;
049: }
050:
051: public String getTag() {
052: return _tag;
053: }
054:
055: public Object saveState(FacesContext context) {
056: Object values[] = new Object[2];
057: values[0] = super .saveState(context);
058: values[1] = _tag;
059: return (values);
060: }
061:
062: public void restoreState(FacesContext context, Object state) {
063:
064: Object values[] = (Object[]) state;
065: super .restoreState(context, values[0]);
066: _tag = ((String) values[1]);
067: }
068:
069: void initComponent(org.zkoss.zk.ui.Page page) {
070: if (_rootcomp == null)
071: throw new IllegalStateException(
072: "Must be nested inside the page component: " + this );
073: if (_zulcomp != null) {
074: _zulcomp.detach();
075: _zulcomp = null;
076: }
077:
078: _compDef = page.getComponentDefinition(_tag, true);
079: if (_compDef == null) {
080: throw new RuntimeException(
081: "Component Definition not found :" + _tag);
082: }
083: try {
084: final Object useClass = _compDef.getImplementationClass();
085: final String use = getUse();
086:
087: if (_compDef.isInlineMacro()) {// the tag holds multiple components.
088: final Map props = new HashMap();
089: BranchComponent parent = this .getParentComponent();
090: Component comp = null;
091: if (parent != null) {
092: comp = parent.getZULComponent();
093: props.put("includer", comp);
094: } else {
095: props.put("includer", page);
096: }
097: _compDef.evalProperties(props, page, comp);
098: props.putAll(_compAttrMap);
099: if (use != null)
100: props.put("use", this .getUse());
101: _comps = page
102: .getDesktop()
103: .getExecution()
104: .createComponents(_compDef.getMacroURI(), props);
105: } else {// the tag hold only one component.
106: if (use != null) {
107: _zulcomp = _compDef.newInstance(page, use);
108: } else if (useClass instanceof Class) {
109: _zulcomp = _compDef.newInstance(page,
110: ((Class) useClass).getName());
111: } else {
112: _zulcomp = _compDef.newInstance(page, useClass
113: .toString());
114: }
115:
116: _comps = new Component[] { _zulcomp };
117: _composer.doBeforeComposeChildren(_zulcomp);
118: _zulcomp.getDefinition().applyProperties(_zulcomp);
119: }
120: } catch (Exception e) {
121: if (!_compDef.isInlineMacro()) {
122: if (!_composer.doCatch(e)) {
123: throw new RuntimeException(e.getMessage(), e);
124: }
125: } else {
126: throw new RuntimeException(e.getMessage(), e);
127: }
128: } finally {
129: if (!_compDef.isInlineMacro()) {
130: try {
131: _composer.doFinally();
132: } catch (Exception e) {
133: throw new RuntimeException(e.getMessage(), e);
134: }
135: }
136: }
137:
138: if (_idSet && _zulcomp != null) {
139: _zulcomp.setId(getId());
140: }
141:
142: if (_parentcomp != null) {
143: _parentcomp.addChildZULComponent(this );
144: } else {
145: _rootcomp.addChildZULComponent(this );
146: }
147: }
148:
149: void afterComposeComponent() {
150: if (!_compDef.isInlineMacro()) {
151: //it is HtmlMacroComponent, so we need to call afterComposeComponent,
152: //but we must skip evaluateDynaAttributes in afterComposeComponent
153: super .afterComposeComponent();
154: }
155: }
156:
157: public Component[] getComponent() {
158: return _comps;
159: }
160:
161: }
|