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.List;
019:
020: /**
021: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
022: *
023: * @author Thomas Spiegl (latest modification by $Author: mbr $)
024: * @version $Revision: 512227 $ $Date: 2007-02-27 13:25:16 +0100 (Di, 27 Feb 2007) $
025: */
026: public class ListDataModel extends DataModel {
027:
028: // FIELDS
029: private int _rowIndex = -1;
030: private List _data;
031:
032: // CONSTRUCTORS
033: public ListDataModel() {
034: super ();
035: }
036:
037: public ListDataModel(List list) {
038: if (list == null)
039: throw new NullPointerException("list");
040: setWrappedData(list);
041: }
042:
043: // METHODS
044: public int getRowCount() {
045: if (_data == null) {
046: return -1;
047: }
048: return _data.size();
049: }
050:
051: public Object getRowData() {
052: if (_data == null) {
053: return null;
054: }
055: if (!isRowAvailable()) {
056: throw new IllegalArgumentException("row is unavailable");
057: }
058: return _data.get(_rowIndex);
059: }
060:
061: public int getRowIndex() {
062: return _rowIndex;
063: }
064:
065: public Object getWrappedData() {
066: return _data;
067: }
068:
069: public boolean isRowAvailable() {
070: if (_data == null) {
071: return false;
072: }
073: return _rowIndex >= 0 && _rowIndex < _data.size();
074: }
075:
076: public void setRowIndex(int rowIndex) {
077: if (rowIndex < -1) {
078: throw new IllegalArgumentException("illegal rowIndex "
079: + rowIndex);
080: }
081: int oldRowIndex = _rowIndex;
082: _rowIndex = rowIndex;
083: if (_data != null && oldRowIndex != _rowIndex) {
084: Object data = isRowAvailable() ? getRowData() : null;
085: DataModelEvent event = new DataModelEvent(this , _rowIndex,
086: data);
087: DataModelListener[] listeners = getDataModelListeners();
088: for (int i = 0; i < listeners.length; i++) {
089: listeners[i].rowSelected(event);
090: }
091: }
092: }
093:
094: public void setWrappedData(Object data) {
095: _data = (List) data;
096: int rowIndex = _data != null ? 0 : -1;
097: setRowIndex(rowIndex);
098: }
099:
100: }
|