001: /* DataModelUtil.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 javax.faces.model.DataModel;
021:
022: import org.jboss.seam.Component;
023:
024: /**
025: * This class helps developers to do thing with Seam's DataModel
026: * @author Dennis.Chen
027: */
028: public class DataModelUtil {
029:
030: /**
031: * Put selectedItem into Seam's context for DataModelSelection.<br/>
032: * JSF use row index to identify the selected row in action.However, if data was changed between first request and secondary request (ex, delete or insert).
033: * when secondary request, that's say a selection clicked, then rowIndex may not equals to actual selectedItem's index.
034: *
035: * @param dataModelName name of DataModel
036: * @param selectionName name of DataModelSelection
037: * @param selectedItem new or updated instance of bean.
038: * @param selectedIndex selected index of selection
039: * @return true if selection is consistent.
040: */
041: static public boolean select(String dataModelName,
042: String selectionName, Object selectedItem, int selectedIndex) {
043: //prepare data model (cause datamode outject by seam)
044: Object obj = Component.getInstance(dataModelName);
045: if (obj != null) {
046: boolean setsel = checkSelection(obj, selectedItem,
047: selectedIndex);
048: if (!setsel)
049: return false;
050: //put selected item in to context, then seam can inject to some bean
051: ContextUtil.updateToContext(selectionName, selectedItem);
052: return true;
053: }
054: return false;
055:
056: }
057:
058: /**
059: * Put selectedItem into Seam's context for DataModelSelection.
060: * This method will find index of selectedItem in DataModel which named dataModelName)<br/>
061: * JSF use row index to identify the selected row in action.However, if data was changed between first request and secondary request (ex, delete or insert).
062: * when secondary request, that's say a selection clicked, then rowIndex may not equals to actual selectedItem's index.
063: *
064: * @param dataModelName name of DataModel
065: * @param selectionName name of DataModelSelection
066: * @param selectedItem new or updated instance of bean.
067: * @return true if selection is consistent.
068: */
069: static public boolean select(String dataModelName,
070: String selectionName, Object selectedItem) {
071:
072: //prepare data model (cause datamode outject by seam)
073: Object obj = Component.getInstance(dataModelName);
074: if (obj != null) {
075: boolean findsel = findSelection(obj, selectedItem);
076: if (!findsel)
077: return false;
078: //put selected item in to context,then seam can inject to some bean
079: ContextUtil.updateToContext(selectionName, selectedItem);
080: return true;
081: }
082: return false;
083: }
084:
085: static private boolean findSelection(Object model,
086: Object selectedItem) {
087:
088: //We Can support fast finding method in some Implement if it have faster index search mechanism
089: if (model instanceof DataModel) {
090: DataModel m = ((DataModel) model);
091: int size = m.getRowCount();
092: for (int i = 0; i < size; i++) {
093: m.setRowIndex(i);
094: if (m.isRowAvailable()) {
095: if (m.getRowData().equals(selectedItem)) {
096: m.setRowIndex(i);//why set again?
097: return true;
098: }
099: }
100: }
101: }
102: return false;
103: }
104:
105: static private boolean checkSelection(Object model,
106: Object selectedItem, int rowIndex) {
107: if (model instanceof DataModel) {
108: DataModel m = ((DataModel) model);
109: int oindex = m.getRowIndex();
110: m.setRowIndex(rowIndex);
111: if (m.isRowAvailable()) {
112: Object data = m.getRowData();
113: if (data.equals(selectedItem)) {
114: return true;
115: }
116: }
117: m.setRowIndex(oindex);
118: }
119: return false;
120: }
121:
122: }
|