1 /*
2 * $Id: ListDataModel.java,v 1.20.4.1 2008/04/20 20:48:29 rlubke Exp $
3 */
4
5 /*
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
7 *
8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
9 *
10 * The contents of this file are subject to the terms of either the GNU
11 * General Public License Version 2 only ("GPL") or the Common Development
12 * and Distribution License("CDDL") (collectively, the "License"). You
13 * may not use this file except in compliance with the License. You can obtain
14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
16 * language governing permissions and limitations under the License.
17 *
18 * When distributing the software, include this License Header Notice in each
19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
20 * Sun designates this particular file as subject to the "Classpath" exception
21 * as provided by Sun in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the License
23 * Header, with the fields enclosed by brackets [] replaced by your own
24 * identifying information: "Portions Copyrighted [year]
25 * [name of copyright owner]"
26 *
27 * Contributor(s):
28 *
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package javax.faces.model;
42
43
44 import java.util.List;
45 import javax.faces.FacesException;
46
47
48 /**
49 * <p><strong>ListDataModel</strong> is a convenience implementation of
50 * {@link DataModel} that wraps an <code>List</code> of Java objects.</p>
51 */
52
53 public class ListDataModel extends DataModel {
54
55
56 // ------------------------------------------------------------ Constructors
57
58
59 /**
60 * <p>Construct a new {@link ListDataModel} with no specified
61 * wrapped data.</p>
62 */
63 public ListDataModel() {
64
65 this(null);
66
67 }
68
69
70 /**
71 * <p>Construct a new {@link ListDataModel} wrapping the specified
72 * list.</p>
73 *
74 * @param list List to be wrapped (if any)
75 */
76 public ListDataModel(List list) {
77
78 super();
79 setWrappedData(list);
80
81 }
82
83
84 // ------------------------------------------------------ Instance Variables
85
86
87 // The current row index (zero relative)
88 private int index = -1;
89
90
91 // The list we are wrapping
92 private List list;
93
94
95 // -------------------------------------------------------------- Properties
96
97
98 /**
99 * <p>Return <code>true</code> if there is <code>wrappedData</code>
100 * available, and the current value of <code>rowIndex</code> is greater
101 * than or equal to zero, and less than the size of the list. Otherwise,
102 * return <code>false</code>.</p>
103 *
104 * @throws FacesException if an error occurs getting the row availability
105 */
106 public boolean isRowAvailable() {
107
108 if (list == null) {
109 return (false);
110 } else if ((index >= 0) && (index < list.size())) {
111 return (true);
112 } else {
113 return (false);
114 }
115
116 }
117
118
119 /**
120 * <p>If there is <code>wrappedData</code> available, return the
121 * length of the list. If no <code>wrappedData</code> is available,
122 * return -1.</p>
123 *
124 * @throws FacesException if an error occurs getting the row count
125 */
126 public int getRowCount() {
127
128 if (list == null) {
129 return (-1);
130 }
131 return (list.size());
132
133 }
134
135
136 /**
137 * <p>If row data is available, return the array element at the index
138 * specified by <code>rowIndex</code>. If no wrapped data is available,
139 * return <code>null</code>.</p>
140 *
141 * @throws FacesException if an error occurs getting the row data
142 * @throws IllegalArgumentException if now row data is available
143 * at the currently specified row index
144 */
145 public Object getRowData() {
146
147 if (list == null) {
148 return (null);
149 } else if (!isRowAvailable()) {
150 throw new NoRowAvailableException();
151 } else {
152 return (list.get(index));
153 }
154
155 }
156
157
158 /**
159 * @throws FacesException {@inheritDoc}
160 */
161 public int getRowIndex() {
162
163 return (index);
164
165 }
166
167
168 /**
169 * @throws FacesException {@inheritDoc}
170 * @throws IllegalArgumentException {@inheritDoc}
171 */
172 public void setRowIndex(int rowIndex) {
173
174 if (rowIndex < -1) {
175 throw new IllegalArgumentException();
176 }
177 int old = index;
178 index = rowIndex;
179 if (list == null) {
180 return;
181 }
182 DataModelListener [] listeners = getDataModelListeners();
183 if ((old != index) && (listeners != null)) {
184 Object rowData = null;
185 if (isRowAvailable()) {
186 rowData = getRowData();
187 }
188 DataModelEvent event =
189 new DataModelEvent(this, index, rowData);
190 int n = listeners.length;
191 for (int i = 0; i < n; i++) {
192 if (null != listeners[i]) {
193 listeners[i].rowSelected(event);
194 }
195 }
196 }
197
198 }
199
200
201 public Object getWrappedData() {
202
203 return (this.list);
204
205 }
206
207
208 /**
209 * @throws ClassCastException if <code>data</code> is
210 * non-<code>null</code> and is not a <code>List</code>
211 */
212 public void setWrappedData(Object data) {
213
214 if (data == null) {
215 list = null;
216 setRowIndex(-1);
217 } else {
218 list = (List) data;
219 index = -1;
220 setRowIndex(0);
221 }
222
223 }
224
225
226 }