javax.faces.model.ScalarDataModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jakarta.faces Show documentation
Show all versions of jakarta.faces Show documentation
EE4J Compatible Implementation for Jakarta Faces API
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package javax.faces.model;
/**
* ScalarDataModel is a convenience implementation of
* {@link DataModel} that wraps an individual Java object.
*/
public class ScalarDataModel extends DataModel {
// ------------------------------------------------------------ Constructors
/**
* Construct a new {@link ScalarDataModel} with no specified
* wrapped data.
*/
public ScalarDataModel() {
this(null);
}
/**
* Construct a new {@link ScalarDataModel} wrapping the specified
* scalar object.
*
* @param scalar Scalar to be wrapped (if any)
*/
public ScalarDataModel(E scalar) {
super();
setWrappedData(scalar);
}
// ------------------------------------------------------ Instance Variables
// The currently selected row index (zero-relative)
private int index;
// The scalar we are wrapping
private E scalar;
// -------------------------------------------------------------- Properties
/**
* Return true
if there is wrappedData
* available, and the current value of rowIndex
is zero.
* Otherwise, return false
.
*
* @throws javax.faces.FacesException if an error occurs getting the row availability
*/
@Override
public boolean isRowAvailable() {
if (scalar == null) {
return (false);
} else if (index == 0) {
return (true);
} else {
return (false);
}
}
/**
* If there is wrappedData
available, return 1.
* If no wrappedData
is available, return -1.
*
* @throws javax.faces.FacesException if an error occurs getting the row count
*/
@Override
public int getRowCount() {
if (scalar == null) {
return (-1);
}
return (1);
}
/**
* If wrapped data is available, return the wrapped data instance.
* Otherwise, return null
.
*
* @throws javax.faces.FacesException if an error occurs getting the row data
* @throws IllegalArgumentException if now row data is available
* at the currently specified row index
*/
@Override
public E getRowData() {
if (scalar == null) {
return (null);
} else if (!isRowAvailable()) {
throw new NoRowAvailableException();
} else {
//noinspection unchecked
return (scalar);
}
}
/**
* @throws javax.faces.FacesException {@inheritDoc}
*/
@Override
public int getRowIndex() {
return (index);
}
/**
* @throws javax.faces.FacesException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
*/
@Override
public void setRowIndex(int rowIndex) {
if (rowIndex < -1) {
throw new IllegalArgumentException();
}
int old = index;
index = rowIndex;
if (scalar == null) {
return;
}
DataModelListener [] listeners = getDataModelListeners();
if ((old != index) && (listeners != null)) {
Object rowData = null;
if (isRowAvailable()) {
rowData = getRowData();
}
DataModelEvent event =
new DataModelEvent(this, index, rowData);
int n = listeners.length;
for (int i = 0; i < n; i++) {
if (null != listeners[i]) {
listeners[i].rowSelected(event);
}
}
}
}
@Override
public Object getWrappedData() {
return (this.scalar);
}
/**
* @throws ClassCastException {@inheritDoc}
*/
@Override
public void setWrappedData(Object data) {
if (data == null) {
scalar = null;
setRowIndex(-1);
} else {
scalar = (E) data;
index = -1;
setRowIndex(0);
}
}
}