Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package javax.faces.model;
 
 
 import java.util.List;

DataModel is an abstraction around arbitrary data binding technologies that can be used to adapt a variety of data sources for use by JavaServer Faces components that support per-row processing for their child components (such as javax.faces.component.UIData.

The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a zero-relative cursor (row index). The APIs provide mechanisms to position to a specified zero-relative row index, and to retrieve an object that represents the data that corresponds to the current row index.

A concrete DataModel instance is attached to a particular collection of underlying data by calling the setWrappedData() method. It can be detached from that underlying data collection by passing a null parameter to this method.

Concrete DataModel implementations must provide a public zero-arguments constructor that calls setWrappedData(null). A convenience constructor that takes a wrapped object of the appropriate type (and passes it on via a call to setWrappedData(), should also be provided.

Event listeners may be registered to receive notifications of when a new row index is selected.

 
 
 public abstract class DataModel<E> implements Iterable<E> {
 
 
     private static final DataModelListener[] EMPTY_DATA_MODEL_LISTENER =
          new DataModelListener[0];
 
     // -------------------------------------------------------------- Properties
 

    

Return a flag indicating whether there is rowData available at the current rowIndex. If no wrappedData is available, return false.

Throws:
javax.faces.FacesException if an error occurs getting the row availability
 
     public abstract boolean isRowAvailable();


    

Return the number of rows of data objects represented by this DataModel. If the number of rows is unknown, or no wrappedData is available, return -1.

Throws:
javax.faces.FacesException if an error occurs getting the row count
    public abstract int getRowCount();


    

Return an object representing the data for the currenty selected row index. If no wrappedData is available, return null.

Throws:
javax.faces.FacesException if an error occurs getting the row data
java.lang.IllegalArgumentException if now row data is available at the currently specified row index
    public abstract E getRowData();


    

Return the zero-relative index of the currently selected row. If we are not currently positioned on a row, or no wrappedData is available, return -1.

Throws:
javax.faces.FacesException if an error occurs getting the row index
    public abstract int getRowIndex();


    

Set the zero-relative index of the currently selected row, or -1 to indicate that we are not positioned on a row. It is possible to set the row index at a value for which the underlying data collection does not contain any row data. Therefore, callers may use the isRowAvailable() method to detect whether row data will be available for use by the getRowData() method.

If there is no wrappedData available when this method is called, the specified rowIndex is stored (and may be retrieved by a subsequent call to getRowData()), but no event is sent. Otherwise, if the currently selected row index is changed by this call, a DataModelEvent will be sent to the rowSelected() method of all registered DataModelListeners.

Parameters:
rowIndex The new zero-relative index (must be non-negative)
Throws:
javax.faces.FacesException if an error occurs setting the row index
java.lang.IllegalArgumentException if rowIndex is less than -1
    public abstract void setRowIndex(int rowIndex);


    

Return the object representing the data wrapped by this DataModel, if any.

    public abstract Object getWrappedData();


    

Set the object representing the data collection wrapped by this DataModel. If the specified data is null, detach this DataModel from any previously wrapped data collection instead.

If data is non-null, the currently selected row index must be set to zero, and a DataModelEvent must be sent to the rowSelected() method of all registered DataModelListeners indicating that this row is now selected.

Parameters:
data Data collection to be wrapped, or null to detach from any previous data collection
Throws:
java.lang.ClassCastException if data is not of the appropriate type for this DataModel implementation
    public abstract void setWrappedData(Object data);
    // ------------------------------------------------------ Instance Variables


    

The list of registered DataModelListeners for this DataModel. This variable will be null unless there is at least one registered listener.

    private List<DataModelListenerlisteners = null;
    // --------------------------------------------- Event Listener Registration


    

Add a new DataModelListener to the set interested in notifications from this DataModel.

Parameters:
listener The new DataModelListener to be registered
Throws:
java.lang.NullPointerException if listener is null
    public void addDataModelListener(DataModelListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        if ( == null) {
            //noinspection CollectionWithoutInitialCapacity
             = new ArrayList<DataModelListener>();
        }
        .add(listener);
    }


    

Return the set of DataModelListeners interested in notifications from this DataModel. If there are no such listeners, an empty array is returned.

	if ( == null) {
else {
	    return .toArray
		    (new DataModelListener[.size()]);
	}
    }


    

Remove an existing DataModelListener from the set interested in notifications from this DataModel.

Parameters:
listener The old DataModelListener to be deregistered
Throws:
java.lang.NullPointerException if listener is null
    public void removeDataModelListener(DataModelListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        if ( != null) {
            .remove(listener);
            if (.isEmpty()) {
                 = null;
            }
        }
    }

    

Return a read-only Iterator over the row data for this model.

Since:
2.0
    public Iterator<E> iterator() {
        return new DataModelIterator<E>(this);
        
    }
    
    // ---------------------------------------------------------- Nested Classes
    @SuppressWarnings({"unchecked"})
    private static final class DataModelIterator<T> implements Iterator<T> {
        private DataModel<T> model;
        private int index;
        
        // -------------------------------------------------------- Constructors
        DataModelIterator(DataModel<T> model) {
            this. = model;
            this..setRowIndex();
        }
        // ----------------------------------------------- Methods from Iterator


        
        public boolean hasNext() {
            return .isRowAvailable();
        }


        
        public T next() {
            if (!.isRowAvailable()) {
                throw new NoSuchElementException();
            }
            Object o = .getRowData();
            .setRowIndex(++);
            return (T) o;
        }


        
Unsupported.

        public void remove() {
            throw new UnsupportedOperationException();
        }
    } // END DataModelIterator
New to GrepCode? Check out our FAQ X