1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package javax.faces.model;
20
21 /**
22 * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
23 *
24 * @author Thomas Spiegl (latest modification by $Author: skitching $)
25 * @version $Revision: 676278 $ $Date: 2008-07-13 03:35:04 -0500 (Sun, 13 Jul 2008) $
26 */
27 public class ArrayDataModel extends DataModel
28 {
29 // FIELDS
30 private int _rowIndex = -1;
31 private Object[] _data;
32
33 // CONSTRUCTORS
34 public ArrayDataModel()
35 {
36 super();
37 }
38
39 public ArrayDataModel(Object[] array)
40 {
41 if (array == null) throw new NullPointerException("array");
42 setWrappedData(array);
43 }
44
45 // METHODS
46 public int getRowCount()
47 {
48 if (_data == null)
49 {
50 return -1;
51 }
52 return _data.length;
53 }
54
55 public Object getRowData()
56 {
57 if (_data == null)
58 {
59 return null;
60 }
61 if (!isRowAvailable())
62 {
63 throw new IllegalArgumentException("row is unavailable");
64 }
65 return _data[_rowIndex];
66 }
67
68 public int getRowIndex()
69 {
70 return _rowIndex;
71 }
72
73 public Object getWrappedData()
74 {
75 return _data;
76 }
77
78 public boolean isRowAvailable()
79 {
80 if (_data == null)
81 {
82 return false;
83 }
84 return _rowIndex >= 0 && _rowIndex < _data.length;
85 }
86
87 public void setRowIndex(int rowIndex)
88 {
89 if (rowIndex < -1)
90 {
91 throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
92 }
93 int oldRowIndex = _rowIndex;
94 _rowIndex = rowIndex;
95 if (_data != null && oldRowIndex != _rowIndex)
96 {
97 Object data = isRowAvailable() ? getRowData() : null;
98 DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
99 DataModelListener[] listeners = getDataModelListeners();
100 for (int i = 0; i < listeners.length; i++)
101 {
102 listeners[i].rowSelected(event);
103 }
104 }
105 }
106
107 public void setWrappedData(Object data)
108 {
109 _data = (Object[])data;
110 int rowIndex = _data != null ? 0 : -1;
111 setRowIndex(rowIndex);
112 }
113
114 }