| /*=============================================================================# |
| # Copyright (c) 2012, 2021 Original NatTable authors and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # Original NatTable authors and others - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ecommons.waltable.data; |
| |
| import java.util.List; |
| |
| import org.eclipse.core.runtime.IProgressMonitor; |
| |
| |
| /** |
| * Enables the use of a {@link List} containing POJO(s) as a backing data source. |
| * |
| * By default a bean at position 'X' in the list is displayed in |
| * row 'X' in the table. The properties of the bean are used to |
| * populate the columns. A {@link IColumnPropertyResolver} is used to |
| * retrieve column data from the bean properties. |
| * |
| * @param <T> type of the Objects in the backing list. |
| * @see IColumnPropertyResolver |
| */ |
| public class ListDataProvider<T> implements IRowDataProvider<T> { |
| |
| protected List<T> list; |
| protected IColumnAccessor<T> columnAccessor; |
| |
| |
| public ListDataProvider(final List<T> list, final IColumnAccessor<T> columnAccessor) { |
| this.list= list; |
| this.columnAccessor= columnAccessor; |
| } |
| |
| @Override |
| public long getColumnCount() { |
| return this.columnAccessor.getColumnCount(); |
| } |
| |
| @Override |
| public long getRowCount() { |
| return this.list.size(); |
| } |
| |
| @Override |
| public Object getDataValue(final long columnIndex, final long rowIndex, final int flags, final IProgressMonitor monitor) { |
| if (rowIndex >= Integer.MAX_VALUE) { |
| throw new IndexOutOfBoundsException(); |
| } |
| final T rowObj= this.list.get((int) rowIndex); |
| return this.columnAccessor.getDataValue(rowObj, columnIndex); |
| } |
| |
| @Override |
| public void setDataValue(final long columnIndex, final long rowIndex, final Object newValue) { |
| if (rowIndex >= Integer.MAX_VALUE) { |
| throw new IndexOutOfBoundsException(); |
| } |
| final T rowObj= this.list.get((int) rowIndex); |
| this.columnAccessor.setDataValue(rowObj, columnIndex, newValue); |
| } |
| |
| @Override |
| public T getRowObject(final long rowIndex) { |
| if (rowIndex >= Integer.MAX_VALUE) { |
| throw new IndexOutOfBoundsException(); |
| } |
| return this.list.get((int) rowIndex); |
| } |
| |
| @Override |
| public long indexOfRowObject(final T rowObject) { |
| return this.list.indexOf(rowObject); |
| } |
| |
| public List<T> getList() { |
| return this.list; |
| } |
| |
| } |