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