📄 cascadedtable.java
字号:
// ------------------------------------------------------------------------
// Table Metadata
/**
* @see prefuse.data.Table#getColumnCount()
*/
public int getColumnCount() {
return m_columns.size() + m_pnames.size();
}
/**
* Get the number of columns explicitly stored by this table (i.e., all
* columns that are not inherited from the parent table).
* @return the number of locally stored columns
*/
public int getLocalColumnCount() {
return m_columns.size();
}
// ------------------------------------------------------------------------
// Parent Table Methods
/**
* Get the parent table from which this cascaded table inherits values.
* @return the parent table
*/
public Table getParentTable() {
return m_parent;
}
/**
* Given a row in this table, return the corresponding row in the parent
* table.
* @param row a row in this table
* @return the corresponding row in the parent table
*/
public int getParentRow(int row) {
return ((CascadedRowManager)m_rows).getParentRow(row);
}
/**
* Given a row in the parent table, return the corresponding row, if any,
* in this table.
* @param prow a row in the parent table
* @return the corresponding row in this table, or -1 if the given parent
* row is not inherited by this table
*/
public int getChildRow(int prow) {
return ((CascadedRowManager)m_rows).getChildRow(prow);
}
// ------------------------------------------------------------------------
// Row Operations
/**
* @see prefuse.data.Table#addRow()
*/
public int addRow() {
if ( m_parent != null ) {
throw new IllegalStateException(
"Add row not supported for CascadedTable.");
} else {
return super.addRow();
}
}
/**
* @see prefuse.data.Table#addRows(int)
*/
public void addRows(int nrows) {
if ( m_parent != null ) {
throw new IllegalStateException(
"Add rows not supported for CascadedTable.");
} else {
super.addRows(nrows);
}
}
/**
* @see prefuse.data.Table#removeRow(int)
*/
public boolean removeRow(int row) {
if ( m_parent != null ) {
throw new IllegalStateException(
"Remove row not supported for CascadedTable.");
} else {
return super.removeRow(row);
}
}
/**
* Internal method for adding a new cascaded row backed by
* the given parent row.
* @param prow the parent row to inherit
* @return the row number ofr the newly added row in this table
*/
protected int addCascadedRow(int prow) {
int r = m_rows.addRow();
((CascadedRowManager)m_rows).put(r, prow);
updateRowCount();
fireTableEvent(r, r, TableModelEvent.ALL_COLUMNS,
TableModelEvent.INSERT);
return r;
}
/**
* Internal method for removing a cascaded row from this table.
* @param row the row to remove
* @return true if the row was successfully removed, false otherwise
*/
protected boolean removeCascadedRow(int row) {
boolean rv = super.removeRow(row);
if ( rv )
((CascadedRowManager)m_rows).remove(row);
return rv;
}
// ------------------------------------------------------------------------
// Column Operations
/**
* @see prefuse.data.Table#getColumnName(int)
*/
public String getColumnName(int col) {
int local = m_names.size();
if ( col >= local ) {
return (String)m_pnames.get(col-local);
} else {
return (String)m_names.get(col);
}
}
/**
* @see prefuse.data.Table#getColumnNumber(prefuse.data.column.Column)
*/
public int getColumnNumber(Column col) {
int idx = m_columns.indexOf(col);
if ( idx == -1 && m_parent != null ) {
idx = m_parent.getColumnNumber(col);
if ( idx == -1 ) return idx;
String name = m_parent.getColumnName(idx);
idx = m_pnames.indexOf(name);
if ( idx != -1 ) idx += m_columns.size();
}
return idx;
}
/**
* @see prefuse.data.Table#getColumn(int)
*/
public Column getColumn(int col) {
m_lastCol = col;
int local = m_names.size();
if ( col >= local && m_parent != null ) {
return m_parent.getColumn((String)m_pnames.get(col-local));
} else {
return (Column)m_columns.get(col);
}
}
/**
* @see prefuse.data.Table#hasColumn(java.lang.String)
*/
protected boolean hasColumn(String name) {
int idx = getColumnNumber(name);
return idx >= 0 && idx < getLocalColumnCount();
}
/**
* @see prefuse.data.Table#getColumnNames()
*/
protected Iterator getColumnNames() {
if ( m_parent == null ) {
return m_names.iterator();
} else {
return new CompositeIterator(m_names.iterator(),
m_pnames.iterator());
}
}
/**
* Invalidates this table's cached schema. This method should be called
* whenever columns are added or removed from this table.
*/
protected void invalidateSchema() {
super.invalidateSchema();
this.filterColumns();
}
// ------------------------------------------------------------------------
// Listener Methods
/**
* Internal listener class handling updates from the backing parent table,
* the column projection, or the row selection predicate.
*/
private class Listener
implements TableListener, ProjectionListener, ExpressionListener
{
public void tableChanged(Table t, int start, int end, int col, int type) {
// must come from parent
if ( t != m_parent )
return;
CascadedRowManager rowman = (CascadedRowManager)m_rows;
// switch on the event type
switch ( type ) {
case EventConstants.UPDATE:
{
// do nothing if update on all columns, as this is only
// used to indicate a non-measurable update.
if ( col == EventConstants.ALL_COLUMNS ) {
break;
}
// process each update, check if filtered state changes
for ( int r=start, cr=-1; r<=end; ++r ) {
if ( (cr=rowman.getChildRow(r)) != -1 ) {
// the parent row has a corresponding row in this table
if ( m_rowFilter.getBoolean(m_parent.getTuple(r)) ) {
// row still passes the filter, check the column
int idx = getColumnNumber(m_parent.getColumnName(col));
if ( idx >= getLocalColumnCount() )
fireTableEvent(cr, cr, idx, EventConstants.UPDATE);
} else {
// row no longer passes the filter, remove it
removeCascadedRow(cr);
}
} else {
// does it now pass the filter due to the update?
if ( m_rowFilter.getBoolean(m_parent.getTuple(r)) ) {
if ( (cr=rowman.getChildRow(r)) < 0 )
addCascadedRow(r);
}
}
}
break;
}
case EventConstants.DELETE:
{
if ( col == EventConstants.ALL_COLUMNS ) {
// entire rows deleted
for ( int r=start, cr=-1; r<=end; ++r ) {
if ( (cr=rowman.getChildRow(r)) != -1 )
removeCascadedRow(cr);
}
} else {
// column deleted
filterColumns();
}
break;
}
case EventConstants.INSERT:
if ( col == EventConstants.ALL_COLUMNS ) {
// entire rows added
for ( int r=start; r<=end; ++r ) {
if ( m_rowFilter.getBoolean(m_parent.getTuple(r)) ) {
if ( rowman.getChildRow(r) < 0 )
addCascadedRow(r);
}
}
} else {
// column added
filterColumns();
}
break;
}
}
public void projectionChanged(ColumnProjection projection) {
if ( projection == m_colFilter )
filterColumns();
}
public void expressionChanged(Expression expr) {
if ( expr == m_rowFilter )
filterRows();
}
}
} // end of class CascadedTable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -