| |
"Java Design Patterns for Long Lists"
Vol. 9, Issue 8, p. 50
Listing 1
/** Custom ArrayList model */
private class CustomArrayListModel extends AbstractListModel
{
/** @serial List */
private ArrayList _list;
/**
* Returns specified object.
* <p>
* @param i index
* @return object
*/
public Object getElementAt( int i )
{ return( _list.get( i ));
}
/**
* Returns size of list.
* <p>
* @return size
*/
public int getSize()
{ return( _list.size());
}
/**
* Adds the specified element to the end of the list.
* <p>
* @param o Object
*/
public void addElement( Object o )
{ _list.add( o );
fireIntervalAdded( this,
_list.size() - 1, _list.size() - 1 );
}
/**
* Removes the specified element.
* <p>
* @param i index
*/
public void removeElement( int i )
{ _list.remove( i );
fireIntervalRemoved( this, i, i );
}
/**
* Adds all of the specified elements.
* <p>
* @param objects Array of Objects
*/
public void addAll( Object[] objects )
{ for( int i = 0; ( i < objects.length ); i++ )
_list.add( objects[ i ]);
fireIntervalAdded( this, _list.size() - objects.length, _list.size() - 1 );
}
/**
* Removes all objects from the list.
*/
public void clear()
{ int size = _list.size();
_list.clear();
fireIntervalRemoved( this, 0, size - 1 );
}
/**
* Returns list as array.
* <p>
* @return list
*/
public Object[] toArray()
{ return( _list.toArray());
}
/**
* Constructor.
* <p>
* @param size size of list
*/
public CustomArrayListModel( int size )
{ _list = new ArrayList( size );
}
}
Listing 2
/** Custom array model */
private class CustomArrayModel extends AbstractTableModel
{
/** @serial Data vector */
private Object[][] _table = new Object[ 0 ][];
/** @serial Column names */
private Object[] _columnNames = new Object[ 0 ];
/** @serial Number of rows */
private int _rows = 0;
/**
* Returns specified value.
* <p>
* @param row row index
* @param column column index
* @return value
*/
public Object getValueAt( int row, int column )
{ return( _table[ row ][ column ]);
}
/**
* Returns number of rows.
* <p>
* @return number of rows
*/
public int getRowCount()
{ return( _rows );
}
/**
* Returns number of columns.
* <p>
* @return number of columns
*/
public int getColumnCount()
{ return( _columnNames.length );
}
/**
* Returns row as array.
* <p>
* @param i row index
* @return row
*/
public Object[] getRow( int i )
{ return( _table[ i ]);
}
/**
* Returns column names.
* <p>
* @return column names
*/
public Object[] getColumnNames()
{ return( _columnNames );
}
/**
* Assigns data and column names.
* <p>
* @param objects data vector
* @param columnNames column names
*/
public void setDataVector(
Object[][] objects, Object[] columnNames )
{ System.arraycopy( objects, 0,
_table, 0, objects.length );
_rows = objects.length;
_columnNames = columnNames;
fireTableRowsInserted( 0, _rows - 1 );
}
/**
* Adds the specified row to the end of the table.
* <p>
* @param row row
*/
public void addRow( Object[] row )
{ _table[ _rows ] = row;
_rows++;
fireTableRowsInserted( _rows - 1, _rows - 1 );
}
/**
* Removes the specified row.
* <p>
* @param i index
*/
public void removeRow( int i )
{ System.arraycopy( _table, i + 1,
_table, i, _rows - i - 1 );
_rows--;
fireTableRowsDeleted( i, i );
}
/**
* Adds all of the specified rows.
* <p>
* @param objects Array of rows
*/
public void addAll( Object[][] objects )
{ System.arraycopy( objects, 0,
_table, _rows, objects.length );
_rows += objects.length;
fireTableRowsInserted(
_rows - objects.length, _rows - 1 );
}
/**
* Removes all rows.
*/
public void clear()
{ int rows = _rows;
_rows = 0;
fireTableRowsDeleted( 0, rows - 1 );
}
/**
* Returns table as array.
* <p>
* @return size
*/
public Object[][] toArray()
{ Object[][] objects = new Object[ _rows ][];
System.arraycopy( _table, 0, objects, 0, _rows );
return( objects );
}
/**
* Constructor.
* <p>
* @param rows maximum number of rows
* @param objects data vector
* @param columnNames column names
*/
public CustomArrayModel( int rows,
Object[][] objects, Object[] columnNames )
{ _table = new Object[ rows ][];
System.arraycopy( objects, 0,
_table, 0, objects.length );
_rows = objects.length;
_columnNames = columnNames;
}
}
|
|