1 /* 2 * $Id: ScalarDataModel.java,v 1.20 2007/04/27 22:00:09 ofung 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<E> extends DataModel<E> { 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(E 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 E 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 E getRowData() { 142 143 if (scalar == null) { 144 return (null); 145 } else if (!isRowAvailable()) { 146 throw new NoRowAvailableException(); 147 } else { 148 //noinspection unchecked 149 return (scalar); 150 } 151 152 } 153 154 155 /** 156 * @throws FacesException {@inheritDoc} 157 */ 158 public int getRowIndex() { 159 160 return (index); 161 162 } 163 164 165 /** 166 * @throws FacesException {@inheritDoc} 167 * @throws IllegalArgumentException {@inheritDoc} 168 */ 169 public void setRowIndex(int rowIndex) { 170 171 if (rowIndex < -1) { 172 throw new IllegalArgumentException(); 173 } 174 int old = index; 175 index = rowIndex; 176 if (scalar == null) { 177 return; 178 } 179 DataModelListener [] listeners = getDataModelListeners(); 180 if ((old != index) && (listeners != null)) { 181 Object rowData = null; 182 if (isRowAvailable()) { 183 rowData = getRowData(); 184 } 185 DataModelEvent event = 186 new DataModelEvent(this, index, rowData); 187 int n = listeners.length; 188 for (int i = 0; i < n; i++) { 189 if (null != listeners[i]) { 190 listeners[i].rowSelected(event); 191 } 192 } 193 } 194 195 } 196 197 198 public Object getWrappedData() { 199 200 return (this.scalar); 201 202 } 203 204 205 /** 206 * @throws ClassCastException {@inheritDoc} 207 */ 208 public void setWrappedData(Object data) { 209 210 if (data == null) { 211 scalar = null; 212 setRowIndex(-1); 213 } else { 214 scalar = (E) data; 215 index = -1; 216 setRowIndex(0); 217 } 218 219 } 220 221 222 }