001: /* BaseInit.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Aug 7, 2007 5:56:04 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.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Map.Entry;
027:
028: import javax.faces.context.FacesContext;
029:
030: import org.zkoss.lang.Classes;
031: import org.zkoss.zk.ui.util.Initiator;
032:
033: /**
034: * The skeletal class used to implement the Initiator
035: *
036: * @author Dennis.Chen
037: *
038: */
039: abstract public class BaseInit extends AbstractComponent {
040: private Map _compAttrMap;
041:
042: private String _useClass;
043:
044: /**
045: * Override Method, write a special mark denoting the component in this
046: * method
047: */
048: public void encodeEnd(FacesContext context) throws IOException {
049: if (!isRendered() || !isEffective())
050: return; //nothing to do
051: Map requestMap = context.getExternalContext().getRequestMap();
052:
053: Initiators inits = (Initiators) requestMap.get(Initiators.class
054: .getName());
055: if (inits == null)
056: requestMap.put(Initiators.class.getName(),
057: inits = new Initiators());
058:
059: Initiator init;
060: try {
061: init = (Initiator) Classes.forNameByThread(_useClass)
062: .newInstance();
063: } catch (Exception e) {
064: throw new RuntimeException(e.getMessage(), e);
065: }
066:
067: List args = new ArrayList();
068: for (Iterator itor = _compAttrMap.entrySet().iterator(); itor
069: .hasNext();) {
070: Map.Entry entry = (Entry) itor.next();
071: // process set value,
072: String prop = (String) entry.getKey();
073: Object value = _compAttrMap.get(prop);
074:
075: if (!prop.startsWith("arg"))
076: throw new IllegalArgumentException(
077: "Declared attribute:"
078: + prop
079: + " is illegal. Please use arg[int] instead.");
080: args.add(Integer.parseInt(prop.substring(3)), value);
081:
082: }
083:
084: inits.addInitiator(init, args);
085: }
086:
087: /**
088: * Returns the class name that is used to implement the Initiator
089: * <p>
090: * Default: null
091: *
092: * @return the class name used to implement the ZUL Component, or null to
093: * use the default
094: */
095: public String getUseClass() {
096: return _useClass;
097: }
098:
099: /**
100: * Sets the class that implements {@link Initiator}.
101: *
102: * @param useClass
103: * a class name with derived class which is implements
104: * {@link Initiator}
105: * @throws IllegalArgumentException
106: * if input class can't be found or is not implement Initiator
107: */
108: public void setUseClass(String useClass) {
109: this ._useClass = useClass;
110: }
111:
112: /**
113: * Set dynamic attribute of Initiator
114: *
115: * @param map
116: * the dynamic attributes.
117: */
118: public void setDynamicAttribute(Map map) {
119: _compAttrMap = map;
120: }
121:
122: // ----------------------------------------------------- StateHolder Methods
123:
124: /**
125: * Override Method, save the state of this component.
126: */
127: public Object saveState(FacesContext context) {
128: Object values[] = new Object[4];
129: values[0] = super .saveState(context);
130: values[1] = _useClass;
131: Object m[] = saveAttachedMapState(context, _compAttrMap);
132: values[2] = m[0];
133: values[3] = m[1];
134: return (values);
135:
136: }
137:
138: /**
139: * Override Method, restore the state of this component.
140: */
141: public void restoreState(FacesContext context, Object state) {
142: Object values[] = (Object[]) state;
143: super .restoreState(context, values[0]);
144: _useClass = (String) values[1];
145: _compAttrMap = (Map) restoreAttachedMapState(context,
146: values[2], values[3]);
147: }
148: }
|