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

dk.eobjects.metamodel.data.DataSetTableModel Maven / Gradle / Ivy

/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel.data;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import dk.eobjects.metamodel.query.SelectItem;

/**
 * TableModel specialized for getting data from a DataSet
 */
class DataSetTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 267644807447629777L;
	private List _materializedRows = new ArrayList();
	private DataSet _dataSet;
	private SelectItem[] _selectItems;

	public DataSetTableModel(DataSet dataSet) {
		_dataSet = dataSet;
		_selectItems = dataSet.getSelectItems();
	}

	public int getColumnCount() {
		return _selectItems.length;
	}

	public int getRowCount() {
		materialize();
		return _materializedRows.size();
	}

	private void materialize() {
		if (_dataSet != null) {
			while (_dataSet.next()) {
				_materializedRows.add(_dataSet.getRow());
			}
			_dataSet.close();
			_dataSet = null;
		}
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		materialize();
		return _materializedRows.get(rowIndex).getValue(columnIndex);
	}

	@Override
	public String getColumnName(int column) {
		return _selectItems[column].getSuperQueryAlias(false);
	}

	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		throw new UnsupportedOperationException(
				"DataSetTableModels are immutable, so setValueAt() method is unsupported.");
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy