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