View Javadoc

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/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
23   */
24  public class ArrayDataModel<E> extends DataModel<E>
25  {
26      // FIELDS
27      private int _rowIndex = -1;
28      private E[] _data;
29  
30      // CONSTRUCTORS
31      public ArrayDataModel()
32      {
33          super();
34      }
35  
36      public ArrayDataModel(E[] array)
37      {
38          if (array == null)
39          {
40              throw new NullPointerException("array");
41          }
42          setWrappedData(array);
43      }
44  
45      // METHODS
46      @Override
47      public int getRowCount()
48      {
49          if (_data == null)
50          {
51              return -1;
52          }
53          return _data.length;
54      }
55  
56      @Override
57      public E getRowData()
58      {
59          if (_data == null)
60          {
61              return null;
62          }
63          if (!isRowAvailable())
64          {
65              throw new IllegalArgumentException("row is unavailable");
66          }
67          return _data[_rowIndex];
68      }
69  
70      @Override
71      public int getRowIndex()
72      {
73          return _rowIndex;
74      }
75  
76      @Override
77      public Object getWrappedData()
78      {
79          return _data;
80      }
81  
82      @Override
83      public boolean isRowAvailable()
84      {
85          return _data != null && _rowIndex >= 0 && _rowIndex < _data.length;
86      }
87  
88      @Override
89      public void setRowIndex(int rowIndex)
90      {
91          if (rowIndex < -1)
92          {
93              throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
94          }
95          int oldRowIndex = _rowIndex;
96          _rowIndex = rowIndex;
97          if (_data != null && oldRowIndex != _rowIndex)
98          {
99              Object data = isRowAvailable() ? getRowData() : null;
100             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
101             DataModelListener[] listeners = getDataModelListeners();
102             for (int i = 0; i < listeners.length; i++)
103             {
104                 listeners[i].rowSelected(event);
105             }
106         }
107     }
108 
109     @Override
110     public void setWrappedData(Object data)
111     {
112         if (data == null)
113         {
114             setRowIndex(-1);
115             _data = null;
116         }
117         else
118         {
119             _data = (E[])data;
120             _rowIndex = -1;
121             setRowIndex(0);
122         }
123     }
124 
125 }