All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.webui.jsf.faces.TableDataProviderDataModel Maven / Gradle / Ivy

There is a newer version: 4.4.0.1
Show newest version
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at
 * https://woodstock.dev.java.net/public/CDDLv1.0.html.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at https://woodstock.dev.java.net/public/CDDLv1.0.html.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
 */
package com.sun.webui.jsf.faces;

import com.sun.data.provider.FieldKey;
import com.sun.data.provider.RowKey;
import com.sun.data.provider.TableDataProvider;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.faces.model.DataModel;
import javax.faces.model.DataModelEvent;
import javax.faces.model.DataModelListener;

/**
 * 

DataModel implementation that wraps a specified * {@link TableDataProvider} with the standard JavaServer Faces API. * Note that setting the rowIndex property of this * DataModel does NOT cause the cursor of * the wrapped {@link TableDataProvider} to be repositioned.

*/ public class TableDataProviderDataModel extends DataModel { // ------------------------------------------------------------ Constructors /** *

Construct an unitialized {@link TableDataProviderDataModel}.

*/ public TableDataProviderDataModel() { this(null); } /** *

Construct an {@link TableDataProviderDataModel} that wraps the * specified {@link TableDataProvider}.

*/ public TableDataProviderDataModel(TableDataProvider tdp) { setTableDataProvider(tdp); } // ------------------------------------------------------ Instance Variables /** *

The set of {@link FieldKey}s for the currently wrapped * {@link TableDataProvider}.

*/ private FieldKey fieldKeys[] = null; /** *

The row index to which this DataModel * is positioned.

*/ private int rowIndex = -1; // -------------------------------------------------------------- Properties /** *

The {@link TableDataProvider} that we are wrapping.

*/ private TableDataProvider tdp = null; /** *

Return the {@link TableDataProvider} we are wrapping.

*/ public TableDataProvider getTableDataProvider() { return this.tdp; } /** *

Set the {@link TableDataProvider} we are wrapping.

* * @param tdp The {@link TableDataProvider} to be wraapped */ public void setTableDataProvider(TableDataProvider tdp) { this.tdp = tdp; if (tdp == null) { this.fieldKeys = null; this.rowIndex = -1; } else { this.fieldKeys = tdp.getFieldKeys(); } } // ---------------------------------------------------- DataModel Properties /** *

Return true if the wrapped {@link TableDataProvider} * has an available row at the currently specified rowIndex.

*/ public boolean isRowAvailable() { if (getTableDataProvider() == null) { return false; } return getTableDataProvider().isRowAvailable(getRowKey()); } /** *

Return the number of rows available in the wrapped * {@link TableDataProvider}, or -1 if unknown.

*/ public int getRowCount() { if (getTableDataProvider() == null) { return -1; } return getTableDataProvider().getRowCount(); } /** *

Return a Map representing the data elements in the * current row, keyed by the canonical identifier for each element. * Any call to get() or put() operations on * this Map will be delegated to corresponding * getValue() and setValue() calls on the * wrapped {@link TableDataProvider}. Operations that attempt to add, * delete, or replace keys will be rejected.

*/ public Object getRowData() { if (getTableDataProvider() == null) { return null; } if (!getTableDataProvider().isRowAvailable(getRowKey())) { throw new IllegalArgumentException("" + getRowIndex()); } Map map = new TableDataProviderMap(); for (int i = 0; i < fieldKeys.length; i++) { map.put(fieldKeys[i].getFieldId(), tdp.getValue(fieldKeys[i], getRowKey())); } return map; } /** *

Return the currently selected rowIndex, or -1 for * no current position.

*/ public int getRowIndex() { return this.rowIndex; } public RowKey getRowKey() { int i = getRowIndex(); RowKey[] rks = tdp.getRowKeys(i + 1, null); if (rks.length > i) { return rks[i]; } return null; } /** *

Set the currently selected rowIndex. The cursor * position of the wrapped {@link TableDataProvider} is NOT * updated.

* * @param rowIndex The new selected row index, or -1 for no selection */ public void setRowIndex(int rowIndex) { if (rowIndex < -1) { throw new IllegalArgumentException("" + rowIndex); } int oldIndex = this.rowIndex; this.rowIndex = rowIndex; if (getTableDataProvider() == null) { return; } DataModelListener listeners[] = getDataModelListeners(); if ((oldIndex != rowIndex) && (listeners != null)) { Object rowData = null; if (isRowAvailable()) { rowData = getRowData(); } DataModelEvent event = new DataModelEvent(this, rowIndex, rowData); for (int i = 0; i < listeners.length; i++) { listeners[i].rowSelected(event); } } } /** *

Return the wrapped {@link TableDataProvider} instance, if any.

*/ public Object getWrappedData() { return getTableDataProvider(); } /** *

Set the wrapped {@link TableDataProvider} instance (if any).

* * @param data New {@link TableDataProvider} instance, or null * to disassociate from any instance */ public void setWrappedData(Object data) { setTableDataProvider((TableDataProvider) data); } // --------------------------------------------------------- Private Classes /** *

Private implementation of Map that delegates * get() and put() operations to * getValue() and setValue() calls on the * underlying {@link TableDataProvider}.

*/ private class TableDataProviderMap extends AbstractMap { public TableDataProviderMap() { this.rowIndex = TableDataProviderDataModel.this.getRowIndex(); this.rowKey = TableDataProviderDataModel.this.getRowKey(); } private int rowIndex; private RowKey rowKey; private FieldKey fieldKeys[] = TableDataProviderDataModel.this.fieldKeys; private TableDataProvider tdp = TableDataProviderDataModel.this.tdp; @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean containsValue(Object value) { Iterator keys = keySet().iterator(); while (keys.hasNext()) { Object key = keys.next(); Object contained = get(key); if (value == null) { if (contained == null) { return true; } } else { if (value.equals(contained)) { return true; } } } return false; } public Set entrySet() { return new TableDataProviderEntries(this); } @Override public Object get(Object key) { int columnIndex = index(key); if (columnIndex < 0) { return null; } return tdp.getValue(fieldKeys[columnIndex], rowKey); } @Override public Set keySet() { return new TableDataProviderKeys(this); } @Override public Object put(Object key, Object value) { int columnIndex = index(key); if (columnIndex < 0) { return null; } Object previous = tdp.getValue(fieldKeys[columnIndex], rowKey); tdp.setValue(fieldKeys[columnIndex], rowKey, value); return previous; } @Override public void putAll(Map map) { Iterator keys = map.keySet().iterator(); while (keys.hasNext()) { Object key = keys.next(); put(key, map.get(key)); } } @Override public Object remove(Object key) { throw new UnsupportedOperationException(); } @Override public Collection values() { return new TableDataProviderValues(this); } private int index(Object key) { int index = -1; for (int i = 0; i < fieldKeys.length; i++) { if (key.equals(fieldKeys[i].getFieldId())) { index = i; break; } } return index; } Object realKey(Object key) { return super.get(key); } Iterator realKeys() { return super.keySet().iterator(); } } /** *

Private implementation of Set for implementing the * entrySet() behavior of TableDataProviderMap.

*/ private class TableDataProviderEntries extends AbstractSet { public TableDataProviderEntries(TableDataProviderMap map) { this.map = map; } private TableDataProviderMap map = null; @Override public boolean add(Object o) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { if (o == null) { throw new NullPointerException(); } if (!(o instanceof Map.Entry)) { return false; } Map.Entry e = (Map.Entry) o; Object k = e.getKey(); Object v = e.getValue(); if (!map.containsKey(k)) { return false; } if (v == null) { return map.get(k) == null; } else { return v.equals(map.get(k)); } } @Override public boolean isEmpty() { return map.isEmpty(); } public Iterator iterator() { return new TableDataProviderIterator(map); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } public int size() { return map.size(); } } /** *

Private implementation of Iterator for the * Set returned by entrySet().

*/ private class TableDataProviderIterator implements Iterator { public TableDataProviderIterator(TableDataProviderMap map) { this.map = map; this.keys = map.keySet().iterator(); } private TableDataProviderMap map = null; private Iterator keys = null; public boolean hasNext() { return keys.hasNext(); } public Object next() { Object key = keys.next(); return (new TableDataProviderEntry(map, key)); } public void remove() { throw new UnsupportedOperationException(); } } /** *

Private implementation of Map.Entry that implements * the behavior for a single entry from the Set that is * returned by entrySet().

*/ private class TableDataProviderEntry implements Map.Entry { public TableDataProviderEntry(TableDataProviderMap map, Object key) { this.map = map; this.key = key; } private TableDataProviderMap map = null; private Object key = null; @Override public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof Map.Entry)) { return false; } Map.Entry e = (Map.Entry) o; if (key == null) { if (e.getKey() != null) { return false; } } else { if (!key.equals(e.getKey())) { return false; } } Object v = map.get(key); if (v == null) { if (e.getValue() != null) { return false; } } else { if (!v.equals(e.getValue())) { return false; } } return true; } public Object getKey() { return this.key; } public Object getValue() { return map.get(key); } @Override public int hashCode() { Object value = map.get(key); return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); } public Object setValue(Object value) { Object previous = map.get(key); map.put(key, value); return previous; } } /** *

Private implementation of Set that implements the * keySet() behavior.

*/ private class TableDataProviderKeys extends AbstractSet { public TableDataProviderKeys(TableDataProviderMap map) { this.map = map; } private TableDataProviderMap map = null; @Override public boolean add(Object o) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object o) { return map.containsKey(o); } @Override public boolean isEmpty() { return map.isEmpty(); } public Iterator iterator() { return new TableDataProviderKeysIterator(map); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } public int size() { return map.size(); } } /** *

Private implementation of Iterator that implements the * iterator() method returned by keySet().

*/ private class TableDataProviderKeysIterator implements Iterator { public TableDataProviderKeysIterator(TableDataProviderMap map) { this.map = map; this.keys = map.realKeys(); } private TableDataProviderMap map = null; private Iterator keys = null; public boolean hasNext() { return keys.hasNext(); } public Object next() { return keys.next(); } public void remove() { throw new UnsupportedOperationException(); } } /** *

Private implementation of Collection that implements * the values() method.

*/ private class TableDataProviderValues extends AbstractCollection { public TableDataProviderValues(TableDataProviderMap map) { this.map = map; } private TableDataProviderMap map = null; @Override public boolean add(Object o) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { throw new UnsupportedOperationException(); } @Override public boolean contains(Object value) { return map.containsValue(value); } public Iterator iterator() { return new TableDataProviderValuesIterator(map); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException(); } public int size() { return map.size(); } } /** *

Private implementation of Iterator that implements the * behavior for the Iterator returned by * values().iterator().

*/ private class TableDataProviderValuesIterator implements Iterator { public TableDataProviderValuesIterator(TableDataProviderMap map) { this.map = map; this.keys = map.keySet().iterator(); } private TableDataProviderMap map = null; private Iterator keys = null; public boolean hasNext() { return keys.hasNext(); } public Object next() { return map.get(keys.next()); } public void remove() { throw new UnsupportedOperationException(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy