01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package javax.faces.model;
17:
18: /**
19: * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
20: *
21: * @author Thomas Spiegl (latest modification by $Author: mbr $)
22: * @version $Revision: 512227 $ $Date: 2007-02-27 13:25:16 +0100 (Di, 27 Feb 2007) $
23: */
24: public class ScalarDataModel extends DataModel {
25: // FIELDS
26: private int _rowIndex = -1;
27: private Object _data;
28:
29: // CONSTRUCTORS
30: public ScalarDataModel() {
31: super ();
32: }
33:
34: public ScalarDataModel(Object scalar) {
35: setWrappedData(scalar);
36: }
37:
38: // METHODS
39: public int getRowCount() {
40: return _data != null ? 1 : -1;
41: }
42:
43: public Object getRowData() {
44: if (_data == null) {
45: return null;
46: }
47: if (!isRowAvailable()) {
48: throw new IllegalArgumentException("row is unavailable");
49: }
50: return _data;
51: }
52:
53: public int getRowIndex() {
54: return _rowIndex;
55: }
56:
57: public Object getWrappedData() {
58: return _data;
59: }
60:
61: public boolean isRowAvailable() {
62: if (_data == null) {
63: return false;
64: }
65: return _rowIndex == 0;
66: }
67:
68: public void setRowIndex(int rowIndex) {
69: if (rowIndex < -1) {
70: throw new IllegalArgumentException("illegal rowIndex "
71: + rowIndex);
72: }
73: int oldRowIndex = _rowIndex;
74: _rowIndex = rowIndex;
75: if (_data != null && oldRowIndex != _rowIndex) {
76: Object data = isRowAvailable() ? getRowData() : null;
77: DataModelEvent event = new DataModelEvent(this , _rowIndex,
78: data);
79: DataModelListener[] listeners = getDataModelListeners();
80: for (int i = 0; i < listeners.length; i++) {
81: listeners[i].rowSelected(event);
82: }
83: }
84: }
85:
86: public void setWrappedData(Object data) {
87: _data = data;
88: int rowIndex = _data != null ? 0 : -1;
89: setRowIndex(rowIndex);
90: }
91:
92: }
|