001: /* BranchOutput.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Aug 9, 2007 12:42:46 PM 2007, Created by Dennis.Chen
010: }}IS_NOTE
011:
012: Some code of this file is refer to Apache License Version 2.0
013: the license file is http://www.apache.org/licenses/LICENSE-2.0
014:
015: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
016:
017: {{IS_RIGHT
018: This program is distributed under GPL Version 2.0 in the hope that
019: it will be useful, but WITHOUT ANY WARRANTY.
020: }}IS_RIGHT
021: */
022: package org.zkoss.jsf.zul.impl;
023:
024: import javax.faces.component.EditableValueHolder;
025: import javax.faces.component.ValueHolder;
026: import javax.faces.context.FacesContext;
027: import javax.faces.convert.Converter;
028: import javax.faces.el.ValueBinding;
029:
030: import org.zkoss.lang.reflect.Fields;
031: import org.zkoss.zk.ui.Component;
032:
033: /**
034: * The skeletal class used to implement the ZULJSF components
035: * which needs to support {@link javax.faces.component.ValueHolder}.<br/>
036: * Components should be declared nested under {@link org.zkoss.jsf.zul.Page}.
037: *
038: * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
039: *
040: * @author Dennis.Chen
041: */
042: abstract public class BranchOutput extends BranchComponent implements
043: ValueHolder, ValueHolderSupport {
044:
045: private boolean _localValueSet;
046:
047: public boolean isLocalValueSet() {
048: return _localValueSet;
049: }
050:
051: public void setLocalValueSet(boolean localValueSet) {
052: this ._localValueSet = localValueSet;
053: }
054:
055: //------------code refer to Apache MyFaces-----------------
056:
057: public Object getLocalValue() {
058: return (this ._value);
059: }
060:
061: private Converter _converter;
062: private Object _value;
063:
064: public Converter getConverter() {
065: if (this ._converter != null) {
066: return this ._converter;
067: }
068: ValueBinding vb = getValueBinding("converter");
069: if (vb != null) {
070: return (Converter) vb.getValue(getFacesContext());
071: } else {
072: return null;
073: }
074: }
075:
076: public void setConverter(Converter converter) {
077: this ._converter = converter;
078: }
079:
080: public Object getValue() {
081: //modify dennis, maybe use set null to value,
082: if (_localValueSet/*_value != null*/) {
083: return this ._value;
084: }
085: ValueBinding vb = getValueBinding("value");
086: if (vb != null) {
087: return vb.getValue(getFacesContext());
088: } else {
089: return null;
090: }
091:
092: }
093:
094: public void setValue(Object value) {
095: this ._value = value;
096: _localValueSet = true;
097: }
098:
099: // ----------------------------------------------------- StateHolder Methods
100: /**
101: * Override Method, save the state of this component.
102: */
103: public Object saveState(FacesContext context) {
104:
105: Object values[] = new Object[4];
106: values[0] = super .saveState(context);
107: values[1] = saveAttachedState(context, _converter);
108: values[2] = _value;
109: values[3] = _localValueSet ? Boolean.TRUE : Boolean.FALSE;
110: return (values);
111:
112: }
113:
114: /**
115: * Override Method, restore the state of this component.
116: */
117: public void restoreState(FacesContext context, Object state) {
118:
119: Object values[] = (Object[]) state;
120: super .restoreState(context, values[0]);
121: _converter = (Converter) restoreAttachedState(context,
122: values[1]);
123: _value = values[2];
124: _localValueSet = ((Boolean) values[3]).booleanValue();
125:
126: }
127:
128: // ------------end of code refer--------------------
129:
130: /**
131: * Override Method,
132: * if instance implements {@link ValueHolderSupport} (always for now),
133: * then i will try to set the value from ValueHolder to ZUL Component.
134: *
135: */
136: protected void afterZULComponentComposed(Component zulcomp) {
137: super .afterZULComponentComposed(zulcomp);
138:
139: //check if need to put value attribute of ValueHolder to ZUL
140: if (this instanceof ValueHolderSupport
141: && (isLocalValueSet() || getValueBinding("value") != null)) {
142: Object value = null;
143: String att = ((ValueHolderSupport) this )
144: .getMappedAttributeName();
145: if (att != null) {
146: if (this instanceof EditableValueHolder
147: && !((EditableValueHolder) this ).isValid()) {
148: value = ((EditableValueHolder) this )
149: .getSubmittedValue();
150: try {
151: if (value != null && value instanceof String) {
152: value = transferValueForAttribute((String) value);
153: }
154: Fields.setField(zulcomp, att, value, true);
155: return;
156: } catch (Exception x) {
157: //when invalid, I ignore the exception and set to original value by getValue.
158: }
159: }
160:
161: value = getValue();
162: Converter converter = getConverter();
163: if (converter != null) {
164: value = converter.getAsString(getFacesContext(),
165: this , value);
166: }
167:
168: try {
169: if (value != null && value instanceof String) {
170: value = transferValueForAttribute((String) value);
171: }
172: Fields.setField(zulcomp, att, value, true);
173: } catch (Exception x) {
174: throw new RuntimeException(
175: "set Field fail at attribute :" + att, x);
176: }
177: }
178: }
179: }
180:
181: /**
182: * Return ZUL Component attribute to map to value attribute of ValueHolder,
183: * delivering class might override this method to return corresponding name.
184: *
185: * Note : Default is "value"
186: *
187: * @see ValueHolderSupport
188: */
189: public String getMappedAttributeName() {
190: return "value";
191: }
192:
193: /**
194: * Default implementation return original String
195: * @see ValueHolderSupport
196: */
197: public Object transferValueForAttribute(String value) {
198: return value;
199: }
200:
201: }
|