001: /* BaseListbox.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.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.faces.context.FacesContext;
027:
028: import org.zkoss.jsf.zul.impl.BranchInput;
029: import org.zkoss.zk.ui.Component;
030: import org.zkoss.zk.ui.event.Event;
031: import org.zkoss.zk.ui.event.EventListener;
032: import org.zkoss.zk.ui.event.Events;
033: import org.zkoss.zk.ui.event.SelectEvent;
034: import org.zkoss.zul.ListModel;
035: import org.zkoss.zul.Listbox;
036: import org.zkoss.zul.Listitem;
037:
038: /**
039: * The Base implementation of Listbox.
040: * This component should be declared nested under {@link org.zkoss.jsf.zul.Page}.
041: * @author Dennis.Chen
042: *
043: */
044: abstract public class BaseListbox extends BranchInput {
045:
046: /**
047: * Override it , and return null, It means that do not map value of ValueHolder to zul component. i
048: * will take case value of ValueHolder to selecteItem of listbox.
049: *
050: * @return always null
051: */
052: public String getMappedAttributeName() {
053: return null;
054: }
055:
056: protected void afterZULComponentComposed(Component zulcomp) {
057: super .afterZULComponentComposed(zulcomp);
058:
059: if (isLocalValueSet() || getValueBinding("value") != null) {
060:
061: zulcomp.addEventListener("onProcessZULJSFSelection",
062: new ProcessSelection());
063:
064: //send onProcessZULJSFSelection as late as possible.
065: Events.postEvent("onProcessZULJSFSelection", zulcomp,
066: getValue());
067: }
068: }
069:
070: private class ProcessSelection implements EventListener {
071: public void onEvent(Event event) throws Exception {
072: Listbox listbox = (Listbox) event.getTarget();
073: Object value = event.getData();
074: processSelection(listbox, value);
075: }
076: }
077:
078: /**
079: * set selected item of listbox by value,
080: */
081: private void processSelection(Listbox listbox, Object value) {
082: HashMap selections = new HashMap();
083: listbox.clearSelection();
084:
085: Object multi = getAttributeValue("multiple");
086: boolean isMulti = (multi == null) ? false : "true"
087: .equalsIgnoreCase(multi.toString()) ? true : false;
088: if (value == null) {
089: return;
090: } else if (isMulti) {
091: if (value instanceof Collection) {
092: Iterator iter = ((Collection) value).iterator();
093: while (iter.hasNext()) {
094: selections.put(iter.next(), "");
095: }
096: } else if (value instanceof Object[]) {
097: Object[] s = (Object[]) value;
098: for (int i = 0; i < s.length; i++) {
099: selections.put(s[i], "");
100: }
101: } else {
102: selections.put(value, "");
103: }
104: } else {
105: selections.put(value, "");
106: }
107:
108: ListModel model = listbox.getModel();
109: int size = -1;
110: Object data = null;
111:
112: if (model == null) {
113: size = listbox.getItemCount();
114: } else {
115: size = model.getSize();
116: }
117: boolean hitSel = false;
118: for (int i = 0; i < size; i++) {
119: data = null;
120: Listitem item = listbox.getItemAtIndex(i);
121: if (model == null) {
122: data = item.getValue();
123: } else {
124: data = model.getElementAt(i);
125: }
126:
127: if (data != null && selections.get(data) != null) {
128: //if(!item.isLoaded()){
129: // listbox.renderItem(item);
130: //}
131: listbox.addItemToSelection(item);
132: hitSel = true;
133: selections.remove(data);
134: if (selections.isEmpty())
135: break;
136: }
137: }
138: if (hitSel) {
139: Events.postEvent(new SelectEvent("onSelect", listbox,
140: listbox.getSelectedItems()));
141: }
142: }
143:
144: /**
145: * decode parameter in request,
146: * @param context
147: */
148: protected void clientInputDecode(FacesContext context) {
149: String clientId = this .getClientId(context);
150: Map requestMap = context.getExternalContext()
151: .getRequestParameterMap();
152: if (requestMap.containsKey(clientId)) {
153:
154: String[] newValue = (String[]) context.getExternalContext()
155: .getRequestParameterValuesMap().get(clientId);
156:
157: Object multi = getAttributeValue("multiple");
158: boolean isMulti = (multi == null) ? false : "true"
159: .equalsIgnoreCase(multi.toString()) ? true : false;
160:
161: if (newValue != null) {
162: if (newValue.length > 0) {
163: //JDK 1.5 BUG ? 5043241 on , incompatible types for ?: neither is a subtype of the other
164: //setSubmittedValue(isMulti?newValue:newValue[0]);
165: Object value;
166: if (isMulti) {
167: value = newValue;
168: } else {
169: value = newValue[0];
170: }
171: setSubmittedValue(value);
172: }
173: }
174: }
175: }
176:
177: }
|