📄 table.java
字号:
protected void updateRowCount() {
int maxrow = m_rows.getMaximumRow() + 1;
// update columns
Iterator cols = getColumns();
while ( cols.hasNext() ) {
Column c = (Column)cols.next();
c.setMaximumRow(maxrow);
}
}
/**
* Removes a row from this table.
* @param row the row to delete
* @return true if the row was successfully deleted, false if the
* row was already invalid
*/
public boolean removeRow(int row) {
if ( m_rows.isValidRow(row) ) {
// the order of operations here is extremely important
// otherwise listeners may end up with corrupted state.
// fire update *BEFORE* clearing values
// allow listeners (e.g., indices) to perform clean-up
fireTableEvent(row, row, TableModelEvent.ALL_COLUMNS,
TableModelEvent.DELETE);
// invalidate the tuple
m_tuples.invalidate(row);
// release row with row manager
// do this before clearing column values, so that any
// listeners can determine that the row is invalid
m_rows.releaseRow(row);
// now clear column values
for ( Iterator cols = getColumns(); cols.hasNext(); ) {
Column c = (Column)cols.next();
c.revertToDefault(row);
}
return true;
}
return false;
}
/**
* Clear this table, removing all rows.
* @see prefuse.data.tuple.TupleSet#clear()
*/
public void clear() {
IntIterator rows = rows(true);
while ( rows.hasNext() ) {
removeRow(rows.nextInt());
}
}
/**
* Indicates if the given row number corresponds to a valid table row.
* @param row the row number to check for validity
* @return true if the row is valid, false if it is not
*/
public boolean isValidRow(int row) {
return m_rows.isValidRow(row);
}
// ------------------------------------------------------------------------
// Column Operations
/**
* Internal method indicating if the given data field is included as a
* data column.
*/
protected boolean hasColumn(String name) {
return getColumnNumber(name) != -1;
}
/**
* Get the data field name of the column at the given column number.
* @param col the column number
* @return the data field name of the column
*/
public String getColumnName(int col) {
return (String)m_names.get(col);
}
/**
* Get the column number for a given data field name.
* @param field the name of the column to lookup
* @return the column number of the column, or -1 if the name is not found
*/
public int getColumnNumber(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
return ( e==null ? -1 : e.colnum );
}
/**
* Get the column number for the given Column instance.
* @param col the Column instance to lookup
* @return the column number of the column, or -1 if the name is not found
*/
public int getColumnNumber(Column col) {
return m_columns.indexOf(col);
}
/**
* Get the column at the given column number.
* @param col the column number
* @return the Column instance
*/
public Column getColumn(int col) {
m_lastCol = col;
return (Column)m_columns.get(col);
}
/**
* Get the column with the given data field name
* @param field the data field name of the column
* @return the Column instance
*/
public Column getColumn(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
return ( e != null ? e.column : null );
}
/**
* Add a column with the given name and data type to this table.
* @param name the data field name for the column
* @param type the data type, as a Java Class, for the column
* @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.Class)
*/
public void addColumn(String name, Class type) {
addColumn(name, type, null);
}
/**
* Add a column with the given name and data type to this table.
* @param name the data field name for the column
* @param type the data type, as a Java Class, for the column
* @param defaultValue the default value for column data values
* @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.Class, java.lang.Object)
*/
public void addColumn(String name, Class type, Object defaultValue) {
Column col = ColumnFactory.getColumn(type,
m_rows.getMaximumRow()+1, defaultValue);
addColumn(name, col);
}
/**
* Add a derived column to this table, using an Expression instance to
* dynamically calculate the column data values.
* @param name the data field name for the column
* @param expr a String expression in the prefuse expression language, to
* be parsed into an {@link prefuse.data.expression.Expression} instance.
* The string is parsed by the
* {@link prefuse.data.expression.parser.ExpressionParser}. If an error
* occurs during parsing, an exception will be thrown.
* @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, java.lang.String)
*/
public void addColumn(String name, String expr) {
Expression ex = ExpressionParser.parse(expr);
Throwable t = ExpressionParser.getError();
if ( t != null ) {
throw new RuntimeException(t);
} else {
addColumn(name, ex);
}
}
/**
* Add a derived column to this table, using an Expression instance to
* dynamically calculate the column data values.
* @param name the data field name for the column
* @param expr the Expression that will determine the column values
* @see prefuse.data.tuple.TupleSet#addColumn(java.lang.String, prefuse.data.expression.Expression)
*/
public void addColumn(String name, Expression expr) {
addColumn(name, ColumnFactory.getColumn(this, expr));
}
/**
* Add a constant column to this table, which returns one constant value
* for all column rows.
* @param name the data field name for the column
* @param type the data type, as a Java Class, for the column
* @param dflt the default value for column data values
*/
public void addConstantColumn(String name, Class type, Object dflt) {
addColumn(name, ColumnFactory.getConstantColumn(type, dflt));
}
/**
* Internal method for adding a column.
* @param name the name of the column
* @param col the actual Column instance
*/
protected void addColumn(String name, Column col) {
int idx = getColumnNumber(name);
if ( idx >= 0 && idx < m_columns.size() ) {
throw new IllegalArgumentException(
"Table already has column with name \""+name+"\"");
}
// add the column
m_columns.add(col);
m_names.add(name);
m_lastCol = m_columns.size()-1;
ColumnEntry entry = new ColumnEntry(m_lastCol, col,
new ColumnMetadata(this, name));
// add entry, dispose of an overridden entry if needed
ColumnEntry oldEntry = (ColumnEntry)m_entries.put(name, entry);
if ( oldEntry != null ) oldEntry.dispose();
invalidateSchema();
// listen to what the column has to say
col.addColumnListener(this);
// fire notification
fireTableEvent(m_rows.getMinimumRow(), m_rows.getMaximumRow(),
m_lastCol, TableModelEvent.INSERT);
}
/**
* Internal method for removing a column.
* @param idx the column number of the column to remove
* @return the removed Column instance
*/
protected Column removeColumn(int idx) {
// make sure index is legal
if ( idx < 0 || idx >= m_columns.size() ) {
throw new IllegalArgumentException("Column index is not legal.");
}
String name = (String)m_names.get(idx);
((ColumnEntry)m_entries.get(name)).dispose();
Column col = (Column)m_columns.remove(idx);
m_entries.remove(name);
m_names.remove(idx);
renumberColumns();
m_lastCol = -1;
invalidateSchema();
// ignore what the old column has to say
col.removeColumnListener(this);
// fire notification
fireTableEvent(m_rows.getMinimumRow(), m_rows.getMaximumRow(),
idx, TableModelEvent.DELETE);
return col;
}
/**
* Remove a data field from this table
* @param field the name of the data field / column to remove
* @return the removed Column instance
*/
public Column removeColumn(String field) {
int idx = m_names.indexOf(field);
if ( idx < 0 ) {
throw new IllegalArgumentException("No such column.");
}
return removeColumn(idx);
}
/**
* Remove a column from this table
* @param c the column instance to remove
*/
public void removeColumn(Column c) {
int idx = m_columns.indexOf(c);
if ( idx < 0 ) {
throw new IllegalArgumentException("No such column.");
}
removeColumn(idx);
}
/**
* Internal method that re-numbers columns upon column removal.
*/
protected void renumberColumns() {
Iterator iter = m_names.iterator();
for ( int idx=0; iter.hasNext(); ++idx ) {
String name = (String)iter.next();
ColumnEntry e = (ColumnEntry)m_entries.get(name);
e.colnum = idx;
}
}
/**
* Internal method that returns an iterator over columns
* @return an iterator over columns
*/
protected Iterator getColumns() {
return m_columns.iterator();
}
/**
* Internal method that returns an iterator over column names
* @return an iterator over column name
*/
protected Iterator getColumnNames() {
return m_names.iterator();
}
// ------------------------------------------------------------------------
// Column Metadata
/**
* Return a metadata instance providing summary information about a column.
* @param field the data field name of the column
* @return the columns' associated ColumnMetadata instance
*/
public ColumnMetadata getMetadata(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
if ( e == null ) {
throw new IllegalArgumentException("Unknown column name: "+field);
}
return e.metadata;
}
// ------------------------------------------------------------------------
// Index Methods
/**
* Create (if necessary) and return an index over the given data field.
* The first call to this method with a given field name will cause the
* index to be created and stored. Subsequent calls will simply return
* the stored index. To attempt to retrieve an index without triggering
* creation of a new index, use the {@link #getIndex(String)} method.
* @param field the data field name of the column to index
* @return the index over the specified data column
*/
public Index index(String field) {
ColumnEntry e = (ColumnEntry)m_entries.get(field);
if ( e == null ) {
throw new IllegalArgumentException("Unknown column name: "+field);
} else if ( e.index != null ) {
return e.index; // already indexed
}
Column col = e.column;
try {
e.index = new TreeIndex(this, m_rows, col, null);
} catch ( IncompatibleComparatorException ice ) { /* can't happen */ }
return e.index;
}
/**
* Retrieve, without creating, an index for the given data field.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -