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