01: /* ListModelConverter.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Fri Dec 1 16:55:36 2006, Created by Henri Chen
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.zkplus.databind;
18:
19: import org.zkoss.zul.ListModelSet;
20: import org.zkoss.zul.ListModelMap;
21: import org.zkoss.zul.ListModelList;
22: import org.zkoss.zk.ui.UiException;
23: import org.zkoss.zk.ui.Component;
24:
25: import java.util.Set;
26: import java.util.Map;
27: import java.util.List;
28: import java.util.ArrayList;
29:
30: /**
31: * The {@link TypeConverter} implmentation for converting collection to ListModel and vice versa.
32: *
33: * @author Henri Chen
34: */
35: public class ListModelConverter implements TypeConverter {
36: /** Convert a Set, Map, List, Object[], Enum, or other kind of BindingListModel to associated {@link BindingListModel}.
37: * @param val must be instanceof Set, Map, List, Object[], Enum Class, or other kind of BindingListModel implementation.
38: */
39: public Object coerceToUi(Object val, Component comp) {
40: if (val == null) {
41: val = new ArrayList();
42: }
43: if (val instanceof BindingListModel) {
44: return val;
45: } else if (val instanceof Set) {
46: return new BindingListModelSet((Set) val, true);
47: } else if (val instanceof List) {
48: return new BindingListModelList((List) val, true);
49: } else if (val instanceof Map) {
50: return new BindingListModelMap((Map) val, true);
51: } else if (val instanceof Object[]) {
52: return new BindingListModelArray((Object[]) val, true);
53: } else if ((val instanceof Class)
54: && Enum.class.isAssignableFrom((Class) val)) {
55: return new BindingListModelArray((Object[]) ((Class) val)
56: .getEnumConstants(), true);
57: } else {
58: throw new UiException(
59: "Expects java.util.Set, java.util.List, java.util.Map, Object[], Enum Class, or BindingListModel only. "
60: + val.getClass());
61: }
62: }
63:
64: /** Convert a {@link BindingListModel} to Set, Map, List, or BindingListModel (itself).
65: * @param val must be BindingListModelSet, BindingListModelList, BindingListModelMap, or other kind of BindingListModel
66: */
67: public Object coerceToBean(Object val, Component comp) {
68: if (val == null) {
69: throw new NullPointerException("val");
70: }
71: if (val instanceof BindingListModelSet) {
72: return ((BindingListModelSet) val).getInnerSet();
73: } else if (val instanceof BindingListModelList) {
74: return ((BindingListModelList) val).getInnerList();
75: } else if (val instanceof BindingListModelMap) {
76: return ((BindingListModelMap) val).getInnerMap();
77: } else if (val instanceof BindingListModelArray) {
78: return ((BindingListModelArray) val).getInnerArray();
79: } else if (val instanceof BindingListModel) {
80: return val;
81: } else {
82: throw new UiException(
83: "Expects BindingListModelSet, BindingListModelList, BindingListModelMap, or BindingListModel only."
84: + val.getClass());
85: }
86: }
87: }
|