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