001: /* BaseComponentDefinition.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: 2007/11/21 , 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.util.Iterator;
023: import java.util.LinkedHashMap;
024: import java.util.Map;
025:
026: import javax.faces.context.FacesContext;
027:
028: import org.zkoss.zk.ui.Page;
029: import org.zkoss.zk.ui.UiException;
030: import org.zkoss.zk.ui.metainfo.ComponentDefinition;
031: import org.zkoss.zk.ui.metainfo.impl.ComponentDefinitionImpl;
032:
033: /**
034: * A Implement of component definition tag<br/>
035: * @author Dennis.Chen
036: *
037: */
038: public class BaseComponentDefinition extends AbstractComponent {
039:
040: private String _macroURI;
041:
042: private String _extends;
043:
044: private String _useClass;
045:
046: private String _name;
047:
048: private boolean _inline;
049:
050: private String _moldName;
051:
052: private String _moldURI;
053:
054: private Map _compAttrMap;
055:
056: public String getMacroURI() {
057: return _macroURI;
058: }
059:
060: public void setMacroURI(String macroURI) {
061: this ._macroURI = macroURI;
062: }
063:
064: public String getExtends() {
065: return _extends;
066: }
067:
068: public void setExtends(String _extends) {
069: this ._extends = _extends;
070: }
071:
072: public String getUseClass() {
073: return _useClass;
074: }
075:
076: public void setUseClass(String useClass) {
077: this ._useClass = useClass;
078: }
079:
080: public String getName() {
081: return _name;
082: }
083:
084: public void setName(String name) {
085: this ._name = name;
086: }
087:
088: public boolean isInline() {
089: return _inline;
090: }
091:
092: public void setInline(boolean inline) {
093: this ._inline = inline;
094: }
095:
096: public String getMoldName() {
097: return _moldName;
098: }
099:
100: public void setMoldName(String moldName) {
101: this ._moldName = moldName;
102: }
103:
104: public String getMoldURI() {
105: return _moldURI;
106: }
107:
108: public void setMoldURI(String moldURI) {
109: this ._moldURI = moldURI;
110: }
111:
112: /**
113: * Set dynamic attribute
114: * @param map the dynamic attributes.
115: */
116: public void setDynamicAttribute(Map map) {
117: _compAttrMap = map;
118: }
119:
120: // ----------------------------------------------------- StateHolder Methods
121:
122: /**
123: * Override Method, save the state of this component.
124: */
125: public Object saveState(FacesContext context) {
126: Object values[] = new Object[10];
127: values[0] = super .saveState(context);
128: Object m[] = saveAttachedMapState(context, _compAttrMap);
129: values[1] = m[0];
130: values[2] = m[1];
131: values[3] = _macroURI;
132: values[4] = _extends;
133: values[5] = _useClass;
134: values[6] = _name;
135: values[7] = _inline ? Boolean.TRUE : Boolean.FALSE;
136: values[8] = _moldName;
137: values[9] = _moldURI;
138: return (values);
139:
140: }
141:
142: /**
143: * Override Method, restore the state of this component.
144: */
145: public void restoreState(FacesContext context, Object state) {
146: Object values[] = (Object[]) state;
147: super .restoreState(context, values[0]);
148: _compAttrMap = (Map) restoreAttachedMapState(context,
149: values[1], values[2]);
150: _macroURI = (String) values[3];
151: _extends = (String) values[4];
152: _useClass = (String) values[5];
153: _name = (String) values[6];
154: _inline = ((Boolean) values[7]).booleanValue();
155: _moldName = (String) values[8];
156: _moldURI = (String) values[9];
157: }
158:
159: public void encodeBegin(FacesContext context) throws IOException {
160: if (!isRendered() || !isEffective())
161: return; //nothing to do
162: super .encodeBegin(context);
163: Map requestMap = (Map) context.getExternalContext()
164: .getRequestMap();
165: Map macros = (Map) requestMap.get(BaseComponentDefinition.class
166: .getName());
167: if (macros == null)
168: requestMap.put(BaseComponentDefinition.class.getName(),
169: macros = new LinkedHashMap());
170: macros.put(_name, this );
171: }
172:
173: /** Helper method, check a string is null or empty. */
174: private static boolean isEmpty(String s) {
175: return s == null || s.length() == 0;
176: }
177:
178: /**
179: * register component definition to page.
180: * @param page a zul page instance
181: */
182: /*public*/void registerComponentDefinition(Page page) {
183: ComponentDefinition compdef;
184: if (_macroURI != null) {
185: compdef = page.getLanguageDefinition().getMacroDefinition(
186: _name, _macroURI, _inline, null);
187: if (!isEmpty(_useClass)) {
188: if (_inline)
189: throw new UiException(
190: "class not allowed with inline macros, ");
191: compdef.setImplementationClass(_useClass);
192: }
193: } else if (_extends != null) {
194: final ComponentDefinition ref = page
195: .getLanguageDefinition().getComponentDefinition(
196: _extends);
197: if (ref.isMacro())
198: throw new UiException(
199: "Unable to extend from a macro component, "
200: + _extends);
201: compdef = ref.clone(null, _name);
202: if (!isEmpty(_useClass)) {
203: compdef.setImplementationClass(_useClass);
204: //Resolve later since might defined in zscript
205: }
206: } else {
207: if (isEmpty(_useClass))
208: throw new UiException(
209: " The Macro-define's property: 'use' cannot be empty! ");
210: final ComponentDefinitionImpl cdi = new ComponentDefinitionImpl(
211: page.getLanguageDefinition(), null, _name,
212: (Class) null);
213: cdi.setCurrentDirectory(null);
214: //mold URI requires it
215: compdef = cdi;
216: compdef.setImplementationClass(_useClass);
217: }
218:
219: if (!isEmpty(_moldURI))
220: compdef.addMold(isEmpty(_moldName) ? "default" : _moldName,
221: _moldURI);
222: for (Iterator e = _compAttrMap.entrySet().iterator(); e
223: .hasNext();) {
224: final Map.Entry me = (Map.Entry) e.next();
225: compdef.addProperty((String) me.getKey(), (String) me
226: .getValue());
227: }
228:
229: page.getComponentDefinitionMap().add(compdef);
230: }
231:
232: }
|