001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package javax.faces.model;
017:
018: import java.util.SortedMap;
019:
020: import javax.servlet.jsp.jstl.sql.Result;
021:
022: //import javax.servlet.jsp.
023: /**
024: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
025: *
026: * @author Thomas Spiegl (latest modification by $Author: mbr $)
027: * @version $Revision: 512227 $ $Date: 2007-02-27 13:25:16 +0100 (Di, 27 Feb 2007) $
028: */
029: public class ResultDataModel extends DataModel {
030: // FIELDS
031: private int _rowIndex = -1;
032: private Result _data;
033:
034: // CONSTRUCTORS
035: public ResultDataModel() {
036: super ();
037: }
038:
039: public ResultDataModel(Result result) {
040: if (result == null)
041: throw new NullPointerException("result");
042: setWrappedData(result);
043: }
044:
045: // METHODS
046: public int getRowCount() {
047: if (getRows() == null) {
048: return -1;
049: }
050: return getRows().length;
051: }
052:
053: public Object getRowData() {
054: if (getRows() == null) {
055: return null;
056: }
057: if (!isRowAvailable()) {
058: throw new IllegalArgumentException("row is unavailable");
059: }
060: return getRows()[_rowIndex];
061: }
062:
063: public int getRowIndex() {
064: return _rowIndex;
065: }
066:
067: public Object getWrappedData() {
068: return _data;
069: }
070:
071: public boolean isRowAvailable() {
072: if (getRows() == null) {
073: return false;
074: }
075: return _rowIndex >= 0 && _rowIndex < getRows().length;
076: }
077:
078: public void setRowIndex(int rowIndex) {
079: if (rowIndex < -1) {
080: throw new IllegalArgumentException("illegal rowIndex "
081: + rowIndex);
082: }
083: int oldRowIndex = _rowIndex;
084: _rowIndex = rowIndex;
085: if (getRows() != null && oldRowIndex != _rowIndex) {
086: Object data = isRowAvailable() ? getRowData() : null;
087: DataModelEvent event = new DataModelEvent(this , _rowIndex,
088: data);
089: DataModelListener[] listeners = getDataModelListeners();
090: for (int i = 0; i < listeners.length; i++) {
091: listeners[i].rowSelected(event);
092: }
093: }
094: }
095:
096: private SortedMap[] getRows() {
097: if (_data == null)
098: return null;
099:
100: return _data.getRows();
101: }
102:
103: public void setWrappedData(Object data) {
104: if (data == null) {
105: setRowIndex(-1);
106: } else {
107: _data = ((Result) data);
108: setRowIndex(0);
109: }
110: }
111:
112: }
|