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

Save This Page
Home » mojarra-1.2_09-b02-FCS-source » javax.faces.model » [javadoc | source]