org.ujoframework.swing.UjoTableModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ujo-orm Show documentation
Show all versions of ujo-orm Show documentation
Quick ORM implementation based on the UJO objects.
/*
* Copyright 2007-2010 Pavel Ponec
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ujoframework.swing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import org.ujoframework.Ujo;
import org.ujoframework.UjoProperty;
import org.ujoframework.core.UjoComparator;
import org.ujoframework.core.UjoManager;
import org.ujoframework.extensions.UjoCloneable;
/**
* An Ujo implementation of TableModel.
* @author Pavel Ponec
*/
public class UjoTableModel extends AbstractTableModel {
/** A repaint header event. */
public static final int EVENT_REPAINT_HEADER = 1000;
/** Columns definition */
protected UjoProperty[] columns;
/** A data store */
protected List rows;
/**
* Creates a new instance of UjoTableModel
* @param columns Columns in a required order.
*/
public UjoTableModel(UjoProperty ... columns) {;
this.columns = columns;
initData();
}
/** Creates a new instance of UjoTableModel for all attributes. */
public UjoTableModel(Class ujoType) {
this(UjoManager.getInstance().readProperties(ujoType).toArray());
}
/** An Initialization */
protected void initData() {
rows = new ArrayList();
}
/** Returns an UjoManager */
protected UjoManager getUjoManager() {
return UjoManager.getInstance();
}
/** Assign a table rows */
public void setRows(List rows) {
if (rows==null) { throw new IllegalArgumentException("Can't assign a null value"); }
this.rows = rows;
fireTableDataChanged();
}
/** Get Row Count */
public int getRowCount() {
return rows.size();
}
/** Count of table columns. */
public int getColumnCount() {
return columns.length;
}
/** Convert columnIndex to a UjoProperty. */
public UjoProperty getColumn(int columnIndex) {
return columns[columnIndex];
}
/** Set columns into table */
public void setColumns(UjoProperty ... columns) {
this.columns = columns;
fireTableStructureChanged();
}
/** Get value from cell.
* @deprecated Use a method with column type of UjoProperty instead of.
*/
final public Object getValueAt(int rowIndex, int columnIndex) {
return getValueAt(rowIndex, columns[columnIndex]);
}
/** Set value to cell.
* @deprecated Use a method with column type of UjoProperty instead of.
*/
final public void setValueAt(Object value, int rowIndex, int columnIndex) {
setValueAt(value, rowIndex, columns[columnIndex]);
}
/** Column Name */
final public String getColumnName(int columnIndex) {
return getColumnName(columns[columnIndex]);
}
/** Is the Cell Editable?
* @deprecated Use a method with column type of UjoProperty instead of.
*/
final public boolean isCellEditable(int rowIndex, int columnIndex) {
return isCellEditable(rowIndex, columns[columnIndex]);
}
/**
* Returns the most specific superclass for all the cell values in the column.
* @return the common ancestor class of the object values in the model.
* @deprecated Use a method with column type of UjoProperty instead of.
*/
final public Class getColumnClass(int columnIndex) {
return getColumnClass(columns[columnIndex]);
}
// ================= NEW Ujo API ====================
/**
* Returns a row by a parameter. The mothod can throw an exception called "IndexOutOfBoundsException"
* @param rowIndex
*/
public ROW getRow(int rowIndex) throws IndexOutOfBoundsException {
return rows.get(rowIndex);
}
/**
* Returns a row by a parameter. The result can be a null value if a rowIndex is out of a range.
* @param rowIndex
*/
public ROW getRowNullable(int rowIndex) {
return rowIndex>=0 && rowIndex=0; i--) {
if (columns[i]==column) {
return i;
}
}
throw new IllegalArgumentException("Bad column: " + column);
}
/** Returns a table column index or -1 if the row was not found. */
public int getRowIndex(ROW row) {
int count = rows.size();
for (int i=0 ; i comp = new UjoComparator(properties);
sort(comp);
}
/** Sort data by a Comparator object. */
public void sort(Comparator comparator) {
Collections.sort(rows, comparator);
fireTableAllRowUpdated();
}
/** Fire an sing, that all rows was updated. */
public void fireTableAllRowUpdated() {
fireTableRowsUpdated(0, getRowCount()-1);
}
/** Fire an sing, that column was updated. */
public void fireTableColumnUpdated(UjoProperty columnProp) {
int column = getColumnIndex(columnProp);
for (int i=getRowCount()-1; i>=0; i--) {
fireTableCellUpdated(i, column);
}
}
}