001: /* ListDataModelWrapper.java
002: {{IS_NOTE
003: Purpose:
004:
005: Description:
006:
007: History:
008: Jul 25, 2007 10:03:38 AM , Created by Dennis Chen
009: }}IS_NOTE
010:
011: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
012:
013: {{IS_RIGHT
014: This program is distributed under GPL Version 2.0 in the hope that
015: it will be useful, but WITHOUT ANY WARRANTY.
016: }}IS_RIGHT
017: */
018: package org.zkoss.seam;
019:
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.util.Collections;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: import javax.faces.model.DataModelEvent;
030: import javax.faces.model.DataModelListener;
031:
032: import org.jboss.seam.jsf.ListDataModel;
033: import org.zkoss.zkplus.databind.BindingListModel;
034: import org.zkoss.zul.event.ListDataEvent;
035: import org.zkoss.zul.event.ListDataListener;
036:
037: /**
038: * You should not create this class directly.<br/>
039: * This Calss is for wrapper JSF's DataModel to ZK's DataModel.
040: * @author Dennis.Chen
041: *
042: */
043: public class ListDataModelWrapper extends ListDataModel implements
044: BindingListModel {
045:
046: private ListDataModel _ldm;
047:
048: private HashMap indexCache = new HashMap();
049: private List listeners = Collections
050: .synchronizedList(new LinkedList());
051:
052: public int indexOf(Object obj) {
053: if (obj == null)
054: return -1;
055: Integer i = (Integer) indexCache.get(obj);
056: if (i == null)
057: return -1;
058: return i.intValue();
059: }
060:
061: public ListDataModelWrapper(ListDataModel ldm) {
062: _ldm = ldm;
063: _ldm.addDataModelListener(new MyDataModelListener());
064: }
065:
066: public void addListDataListener(ListDataListener listener) {
067: if (listeners.indexOf(listener) == -1) {
068: listeners.add(listener);
069: }
070: }
071:
072: public Object getElementAt(int i) {
073: _ldm.setRowIndex(i);
074: Object obj = _ldm.getRowData();
075: if (obj != null) {
076: indexCache.put(obj, new Integer(i));
077: }
078: return obj;
079: }
080:
081: public int getSize() {
082: return _ldm.getRowCount();
083: }
084:
085: public void removeListDataListener(ListDataListener listener) {
086: listeners.remove(listener);
087: }
088:
089: /**
090: * Fire Contents Changed Event to ListDataListener
091: * @param start
092: * @param end
093: */
094: public void fireRowChanged(int start, int end) {
095: new ListDataEvent(this , ListDataEvent.CONTENTS_CHANGED, start,
096: end);
097: }
098:
099: /**
100: * Fire Contents Added Event to ListDataListener
101: * @param start
102: * @param end
103: */
104: public void fireRowAdded(int start, int end) {
105: new ListDataEvent(this , ListDataEvent.INTERVAL_ADDED, start,
106: end);
107: }
108:
109: /**
110: * Fire Contents Removed Event to ListDataListener
111: * @param start
112: * @param end
113: */
114: public void fireRowDeleted(int start, int end) {
115: new ListDataEvent(this , ListDataEvent.INTERVAL_REMOVED, start,
116: end);
117: }
118:
119: protected void fireEvent(ListDataEvent event) {
120: int i = listeners.size();
121: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
122: ((ListDataListener) iter.next()).onChange(event);
123: }
124: }
125:
126: class MyDataModelListener implements DataModelListener {
127: public void rowSelected(DataModelEvent arg0) {
128: // TODO , nothing can do now.
129: }
130: }
131:
132: /**
133: * @return inner ListDataModel
134: */
135: public ListDataModel getInner() {
136: return _ldm;
137: }
138:
139: // Delegate org.jboss.seam.jsf.ListDataModel
140: public void addDataModelListener(DataModelListener arg0) {
141: _ldm.addDataModelListener(arg0);
142: }
143:
144: public DataModelListener[] getDataModelListeners() {
145: return _ldm.getDataModelListeners();
146: }
147:
148: public int getRowCount() {
149: return _ldm.getRowCount();
150: }
151:
152: public Object getRowData() {
153: return _ldm.getRowData();
154: }
155:
156: public int getRowIndex() {
157: return _ldm.getRowIndex();
158: }
159:
160: public Object getWrappedData() {
161: return _ldm.getWrappedData();
162: }
163:
164: public int hashCode() {
165: return _ldm.hashCode();
166: }
167:
168: public boolean isRowAvailable() {
169: return _ldm.isRowAvailable();
170: }
171:
172: public void removeDataModelListener(DataModelListener arg0) {
173: _ldm.removeDataModelListener(arg0);
174: }
175:
176: public void setRowIndex(int arg0) {
177: _ldm.setRowIndex(arg0);
178: }
179:
180: public void setWrappedData(Object arg0) {
181: _ldm.setWrappedData(arg0);
182: }
183:
184: private void writeObject(ObjectOutputStream oos) throws IOException {
185: oos.writeObject(getWrappedData());
186: oos.writeInt(getRowIndex());
187: }
188:
189: private void readObject(ObjectInputStream ois) throws IOException,
190: ClassNotFoundException {
191: this .setWrappedData(ois.readObject());
192: this .setRowIndex(ois.readInt());
193: }
194: //end of Delegate
195:
196: }
|