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 javax.servlet.jsp.jstl.sql.Result;
22  import java.util.SortedMap;
23  
24  /**
25   * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/api/index.html">JSF Specification</a>
26   *
27   * @author Thomas Spiegl (latest modification by $Author: skitching $)
28   * @version $Revision: 676278 $ $Date: 2008-07-13 03:35:04 -0500 (Sun, 13 Jul 2008) $
29   */
30  public class ResultDataModel extends DataModel
31  {
32      // FIELDS
33      private int _rowIndex = -1;
34      private Result _data;
35  
36      // CONSTRUCTORS
37      public ResultDataModel()
38      {
39          super();
40      }
41  
42      public ResultDataModel(Result result)
43      {
44          if (result == null) throw new NullPointerException("result");
45          setWrappedData(result);
46      }
47  
48      // METHODS
49      public int getRowCount()
50      {
51          if (getRows() == null)
52          {
53              return -1;
54          }
55          return getRows().length;
56      }
57  
58      public Object getRowData()
59      {
60          if (getRows() == null)
61          {
62              return null;
63          }
64          if (!isRowAvailable())
65          {
66              throw new IllegalArgumentException("row is unavailable");
67          }
68          return getRows()[_rowIndex];
69      }
70  
71      public int getRowIndex()
72      {
73          return _rowIndex;
74      }
75  
76      public Object getWrappedData()
77      {
78          return _data;
79      }
80  
81      public boolean isRowAvailable()
82      {
83          if (getRows() == null)
84          {
85              return false;
86          }
87          return _rowIndex >= 0 && _rowIndex < getRows().length;
88      }
89  
90      public void setRowIndex(int rowIndex)
91      {
92          if (rowIndex < -1)
93          {
94              throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
95          }
96          int oldRowIndex = _rowIndex;
97          _rowIndex = rowIndex;
98          if (getRows() != null && oldRowIndex != _rowIndex)
99          {
100             Object data = isRowAvailable() ? getRowData() : null;
101             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
102             DataModelListener[] listeners = getDataModelListeners();
103             for (int i = 0; i < listeners.length; i++)
104             {
105                 listeners[i].rowSelected(event);
106             }
107         }
108     }
109 
110     private SortedMap[] getRows()
111     {
112         if(_data==null)
113             return null;
114 
115         return _data.getRows();
116     }
117 
118     public void setWrappedData(Object data)
119     {
120         if (data == null)
121         {
122             setRowIndex(-1);
123         }
124         else
125         {
126             _data = ((Result)data);
127             setRowIndex(0);
128         }
129     }
130 
131 }