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