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