1 /*
2 * $Id: ScalarDataModel.java,v 1.20.4.1 2008/04/20 20:48:30 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 javax.faces.FacesException;
45
46
47 /**
48 * <p><strong>ScalarDataModel</strong> is a convenience implementation of
49 * {@link DataModel} that wraps an individual Java object.</p>
50 */
51
52 public class ScalarDataModel extends DataModel {
53
54
55 // ------------------------------------------------------------ Constructors
56
57
58 /**
59 * <p>Construct a new {@link ScalarDataModel} with no specified
60 * wrapped data.</p>
61 */
62 public ScalarDataModel() {
63
64 this(null);
65
66 }
67
68
69 /**
70 * <p>Construct a new {@link ScalarDataModel} wrapping the specified
71 * scalar object.</p>
72 *
73 * @param scalar Scalar to be wrapped (if any)
74 */
75 public ScalarDataModel(Object scalar) {
76
77 super();
78 setWrappedData(scalar);
79
80 }
81
82
83 // ------------------------------------------------------ Instance Variables
84
85
86 // The currently selected row index (zero-relative)
87 private int index;
88
89
90 // The scalar we are wrapping
91 private Object scalar;
92
93
94 // -------------------------------------------------------------- Properties
95
96
97 /**
98 * <p>Return <code>true</code> if there is <code>wrappedData</code>
99 * available, and the current value of <code>rowIndex</code> is zero.
100 * Otherwise, return <code>false</code>.</p>
101 *
102 * @throws FacesException if an error occurs getting the row availability
103 */
104 public boolean isRowAvailable() {
105
106 if (scalar == null) {
107 return (false);
108 } else if (index == 0) {
109 return (true);
110 } else {
111 return (false);
112 }
113
114 }
115
116
117 /**
118 * <p>If there is <code>wrappedData</code> available, return 1.
119 * If no <code>wrappedData</code> is available, return -1.</p>
120 *
121 * @throws FacesException if an error occurs getting the row count
122 */
123 public int getRowCount() {
124
125 if (scalar == null) {
126 return (-1);
127 }
128 return (1);
129
130 }
131
132
133 /**
134 * <p>If wrapped data is available, return the wrapped data instance.
135 * Otherwise, return <code>null</code>.</p>
136 *
137 * @throws FacesException if an error occurs getting the row data
138 * @throws IllegalArgumentException if now row data is available
139 * at the currently specified row index
140 */
141 public Object getRowData() {
142
143 if (scalar == null) {
144 return (null);
145 } else if (!isRowAvailable()) {
146 throw new NoRowAvailableException();
147 } else {
148 return (scalar);
149 }
150
151 }
152
153
154 /**
155 * @throws FacesException {@inheritDoc}
156 */
157 public int getRowIndex() {
158
159 return (index);
160
161 }
162
163
164 /**
165 * @throws FacesException {@inheritDoc}
166 * @throws IllegalArgumentException {@inheritDoc}
167 */
168 public void setRowIndex(int rowIndex) {
169
170 if (rowIndex < -1) {
171 throw new IllegalArgumentException();
172 }
173 int old = index;
174 index = rowIndex;
175 if (scalar == null) {
176 return;
177 }
178 DataModelListener [] listeners = getDataModelListeners();
179 if ((old != index) && (listeners != null)) {
180 Object rowData = null;
181 if (isRowAvailable()) {
182 rowData = getRowData();
183 }
184 DataModelEvent event =
185 new DataModelEvent(this, index, rowData);
186 int n = listeners.length;
187 for (int i = 0; i < n; i++) {
188 if (null != listeners[i]) {
189 listeners[i].rowSelected(event);
190 }
191 }
192 }
193
194 }
195
196
197 public Object getWrappedData() {
198
199 return (this.scalar);
200
201 }
202
203
204 /**
205 * @throws ClassCastException {@inheritDoc}
206 */
207 public void setWrappedData(Object data) {
208
209 if (data == null) {
210 scalar = null;
211 setRowIndex(-1);
212 } else {
213 scalar = data;
214 index = -1;
215 setRowIndex(0);
216 }
217
218 }
219
220
221 }