home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
StringTableModel.java
< prev
next >
Wrap
Text File
|
1998-11-18
|
21KB
|
848 lines
package com.symantec.itools.swing.models;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Vector;
import java.util.StringTokenizer;
import com.sun.java.swing.event.TableModelEvent;
import java.math.*;
public class StringTableModel
extends com.sun.java.swing.table.AbstractTableModel
implements Serializable
{
/**
* The number of columns currently in the table.
* This is equal to the widest row of data.
* The column headers are pinned (padded or truncated) to the width of the data
*/
protected int colCount = 0;
/**
* A Vector of Vectors containing the table's data
*/
protected Vector dataVector = new Vector();
/**
* A vector of vectors containg booleans indicating editability of
* each cell in the table.
*/
protected Vector editableVector = new Vector();
/**
* A vector containing the column headers
*/
protected Vector headerVector = new Vector();
/**
* A boolean indicating whether the table in general is editable.
*/
protected boolean editable = false;
//
// com.sun.java.swing.table.TableModel implementation
//
/**
* Returns the number of rows in the table.
* @return The number of rows in the table.
*/
public int getRowCount()
{
return dataVector.size();
}
/**
* Returns the number of columns in the table.
* @return The number of columns in the table.
*/
public int getColumnCount()
{
return colCount;
}
/**
* Returns the header for specified column.
* May be space (padded)
* @param column The specified column
* @return The header for specified column.
*/
public String getColumnName(int column)
{
return (String)headerVector.elementAt(column);
}
/**
* Returns the class of the type of object held in this column.
* In this model, it it always String
* @param columnIndex The specified column
* @return String.class
*/
public Class getColumnClass(int columnIndex)
{
return String.class;
}
/**
* Returns the value in the specified cell
* @param row The specified row.
* @param column The specified column
* @return The value in the specified cell
*/
public Object getValueAt(int row, int column)
{
return ((String)((Vector)dataVector.elementAt(row)).elementAt(column));
}
/**
* Set the value for a specified cell
* @param aValue The object value to set the specified cell to.
* Should be a string for this model.
* @param row The specified row.
* @param column The specified column
*/
public void setValueAt(Object aValue, int row, int column)
{
((Vector)dataVector.elementAt(row)).setElementAt(aValue, column);
((Vector)editableVector.elementAt(row)).setElementAt(Boolean.TRUE, column);
fireTableCellUpdated(row, column);
}
/**
* Set the editability of the specified cell.
* @param rowIndex The specified row.
* @param columnIndex The specified column
* @param f True or false.
*/
public void setCellEditable(int rowIndex, int columnIndex, boolean f)
{
Vector data;
data = (Vector)editableVector.elementAt(rowIndex);
data.setElementAt(new Boolean(f), columnIndex);
}
/**
* Is the cell editable?
* @param rowIndex The specified row
* @param columnIndex The specified column
* @return true if cell is editable
*/
public boolean isCellEditable(int rowIndex, int columnIndex)
{
Vector data;
if (!editable)
return false;
data = (Vector)editableVector.elementAt(rowIndex);
return (((Boolean)data.elementAt(columnIndex)).booleanValue());
}
//
// Properties
//
/**
* Calls getItems(char)
* @return An array of comma-delimited strings representing the
* data in the table.
*/
public String[] getItems()
{
return (getItems(','));
}
/**
* Called by getItems(). Constructs the array of
* comma-delimited string from the data in dataVector.
* @param appendChar a comma
* @return An array of comma-delimited strings representing the
* data in the table.
*/
public String[] getItems(char appendChar)
{
int rowCount;
String[] items;
rowCount = dataVector.size();
items = new String[rowCount];
for(int i = 0; i < rowCount; i++)
{
StringBuffer row;
row = new StringBuffer();
for(int j = 0; j < colCount;j++)
{
row.append(((Vector)dataVector.elementAt(i)).elementAt(j));
row.append(appendChar);
}
// strip trailing appendChar off.
row.setLength(row.length() - 1);
items[i] = row.toString();
}
return items;
}
/**
* Calls setItems(String[],String).
* @param newItems An array of comma-delimited strings holding the
* data for the table.
*/
public void setItems(String[] newItems)
{
setItems(newItems, ",");
}
/**
* Parses the String array, one row at a time, via a stringtokenizer
* by calling setItemAt(String,int,boolean,String)
* @param newItems The string array of data
* @param sepChar The comma delimiter
*/
public void setItems(String[] newItems, String sepChar)
{
int rowCount;
colCount = 0;
dataVector.setSize(0);
// cycle through rows
for(int i = 0; i < newItems.length;i++)
{
setItemAt(newItems[i], i, false, sepChar);
}
rowCount = dataVector.size();
// pad any short rows
for(int k = 0; k < rowCount;k++)
{
int rowWidth = ((Vector)dataVector.elementAt(k)).size();
if(rowWidth < colCount)
{
int padding;
padding = colCount - rowWidth;
for(int l = 0; l < padding; l++)
{
((Vector)dataVector.elementAt(k)).addElement("");
}
}
}
// Generate notification
fireTableStructureChanged();
}
/**
* Calls setItemAt(String,int,boolean,String)
* @param item A comma-delimited string representing a row of data
* @param row The specified row.
* @param notifyOfChange Whether or not to broadcast an event.
*/
public void setItemAt(String item, int row, boolean notifyOfChange)
{
setItemAt(item, row, notifyOfChange, ",");
}
/**
* Puts the item value (row of data) into dataVector
* Pads any short rows to the widest row with a space.
* @param item A comma-delimited string representing a row of data
* @param row The specified row.
* @param notifyOfChange Whether or not to broadcast an event.
* @param sepChar The comma delimiter.
*/
public void setItemAt(String item, int row, boolean notifyOfChange, String sepChar)
{
StringTokenizer newRow;
dataVector.insertElementAt(new Vector(), row);
editableVector.insertElementAt(new Vector(), row);
newRow = new StringTokenizer(item, sepChar);
colCount = Math.max(colCount,newRow.countTokens());
// columns
while(newRow.hasMoreTokens())
{
int rowWidth;
String rowItem;
rowItem = newRow.nextToken();
((Vector)dataVector.elementAt(row)).addElement(rowItem);
((Vector)editableVector.elementAt(row)).addElement(Boolean.TRUE);
rowWidth = ((Vector)dataVector.elementAt(row)).size();
if(rowWidth > headerVector.size())
{
headerVector.addElement(" ");
}
}
if(notifyOfChange)
{
fireTableDataChanged();
}
}
/**
* Calls getColumnHeaders(char)
* @return A comma-delimited string containing the columnHeaders
*/
public String getColumnHeaders()
{
return (getColumnHeaders(','));
}
/**
* Constructs a comma-delimited string from headerVector
* @param sepChar String - The comma delimiter
* @return A comma-delimited string containing the columnHeaders
*/
public String getColumnHeaders(char sepChar)
{
StringBuffer colHeaders = new StringBuffer();
for (int j = 0; j < colCount;j++)
{
colHeaders.append((String)headerVector.elementAt(j));
colHeaders.append(sepChar);
}
// remove trailing sepChar
if (colHeaders.length() > 0)
colHeaders.setLength(colHeaders.length() - 1);
return colHeaders.toString();
}
/**
* Calls setcolumnHeaders(String,String)
* @param newHeaders A comma-delimited string containing the column headers
*/
public void setColumnHeaders(String newHeaders)
{
setColumnHeaders(newHeaders, ",");
}
/**
* Sets the column headers.
* If newHeaders is shorter than colCount
* then the headerVector is padded with spaces.
* If newHeaders is longer than colCount, they will be stored but
* not displayed by JTable.
* @param newHeaders A comma-delimited string containing the column headers
* @param sepChar String - The comma delimiter
*/
public void setColumnHeaders(String newHeaders, String sepChar)
{
headerVector = new Vector();
StringTokenizer headers = new StringTokenizer(newHeaders,sepChar);
int newHeadersWidth = headers.countTokens();
for (int j = 0; j < Math.max(newHeadersWidth,colCount); j++)
{
if(headers.hasMoreTokens())
{
headerVector.addElement(headers.nextToken());
}
else
{
headerVector.addElement(" ");
}
}
fireTableStructureChanged();
fireTableDataChanged();
}
/**
* Is the table in general editable?
* @return true if table is editable
*/
public boolean isEditable()
{
return editable;
}
/**
* Sets whether or not the table in general is editable.
* @param isEditable boolean
*/
public void setEditable(boolean isEditable)
{
editable = isEditable;
}
/**
* Calls getRow(int,char)
* Get a particular row of data.
* @param row The specified row.
* @return A comma delimited string containg the elements in the specified row.
*/
public String getRow(int row)
{
return getRow(row, ',');
}
/**
* Get a particular row of data.
* @param row The specified row.
* @param appendChar a comma
* @return A comma delimited string containing the elements in the specified row.
*/
public String getRow(int row, char appendChar)
{
StringBuffer buf;
buf = new StringBuffer();
for(Enumeration e = elements(row); e.hasMoreElements();)
{
buf.append((String)e.nextElement());
buf.append(appendChar);
}
// remove trailing appendChar
buf.setLength(buf.length() - 1);
return (buf.toString());
}
//
// helper methods
//
/**
* Add a row of data. Calls insertRowAt(String,int)
* @param data A comma-delimited string.
*/
public void addRow(String data)
{
insertRowAt(data, dataVector.size());
}
/**
* Calls setItemAt(string,int,boolean)
* @param data A comma-delimited string.
* @param row The specified row.
*/
public void insertRowAt(String data, int row)
{
setItemAt(data, row, false);
fireTableRowsInserted(row, row);
}
/**
* Removes the specified row from dataVector
* @param row The specified row.
*/
public void removeRow(int row)
{
dataVector.removeElementAt(row);
fireTableRowsDeleted(row, row);
}
/**
* Returns the number of rows in dataVector.
* @return The number of rows
*/
public int getNumRows()
{
return (dataVector.size());
}
//
// Vector like methods
//
/**
* Gets the value from a particular cell.
* @param row The specified row
* @param col The specified column
* @return The value from a particular cell.
*/
public Object getElementAt(int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.elementAt(col));
}
/**
* Fills anArray with corresponding values in dataVector by calling
* getElementAt(int,int)
* @param anArray Object array
*/
public void copyInto(Object anArray[][])
{
for(int i = 0; i < anArray.length; i++)
{
for(int j = 0; j < anArray[i].length; j++)
{
anArray[i][j] = elementAt(i, j);
}
}
}
/**
* Calls the Vector.trimToSize() on the specified row in dataVector
* @param row The specified row
*/
public void trimToSize(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.trimToSize();
}
/**
* Calls the Vector.ensureCapacity() on the specified row in dataVector
* @param minCapacity The required capacity
* @param row The specified row
*/
public void ensureCapacity(int minCapacity, int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.ensureCapacity(minCapacity);
}
/**
* Calls the Vector.setSize() on the specified row in dataVector
* @param newSize The new size
* @param row The specified row
*/
public void setSize(int newSize, int row)
{
int oldSize;
Vector data;
data = (Vector)dataVector.elementAt(row);
oldSize = data.size();
data.setSize(newSize);
if(oldSize != newSize)
{
fireTableRowsUpdated(row, row);
}
}
/**
* Calls the Vector.Capacity() on the specified row in dataVector
* @param row The specified row
* @return The current capacity of the specified row
*/
public int capacity(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.capacity());
}
/**
* Calls the Vector.Size() on dataVector
* @return The current size (number of rows) of dataVector.
*/
public int size()
{
return (dataVector.size());
}
/**
* Calls the Vector.Size() on the specified row in dataVector
* @param row The specified row
* @return The current size of the specified row
*/
public int size(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.size());
}
/**
* Calls the Vector.isEmpty() on the specified row in dataVector
* @param row The specified row
* @return Whether or not the specified row is empty.
*/
public boolean isEmpty(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.isEmpty());
}
/**
* Calls the Vector.elements() on the specified row in dataVector
* @param row The specified row
* @return The specified row as an enumeration
*/
public Enumeration elements(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.elements());
}
/**
* Calls the Vector.contains() on the specified row in dataVector
* @param elem The specified object
* @param row The specified row
* @return Whether or not the specified row contains the specified object
*/
public boolean rowContains(Object elem, int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.contains(elem));
}
/**
* Determines whether the specified column contains the specified object.
* @param elem The specified object
* @param col The specified column
* @return Whether the specified column contains the specified object.
*/
public boolean columnContains(Object elem, int col)
{
int size;
size = size();
for(int i = 0; i < size; i++)
{
if(((Vector)dataVector.elementAt(i)).elementAt(col).equals(elem))
{
return (true);
}
}
return (false);
}
/**
* Calls the Vector.indexOf(object) on the specified row in dataVector
* @param elem The specified object
* @param row The specified row
* @return The index of the spcified object in the specified row
*/
public int indexOf(Object elem, int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.indexOf(elem));
}
/**
* Calls the Vector.indexOf(object,int) on the specified row in dataVector
* @param elem The specified object
* @param row The specified row
* @param col The specified column
* @return The index of the spcified object in the specified row, but
* begins the search at the specified column.
*/
public int indexOf(Object elem, int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.indexOf(elem, col));
}
/**
* Calls the Vector.lastIndexOf(object) on the specified row in dataVector
* @param elem The specified object
* @param row The specified row
* @return The last occurrence of elem in the specified row.
*/
public int lastIndexOf(Object elem, int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.lastIndexOf(elem));
}
/**
* Calls the Vector.lastIndexOf(object,int) on the specified row in dataVector
* @param elem The specified object
* @param row The specified row
* @param col The specified column
* @return The last occurrence of elem in the specified row, searching backwards
* from the specified index
*/
public int lastIndexOf(Object elem, int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.lastIndexOf(elem, col));
}
/**
* Calls the Vector.elementAt() on the specified row in dataVector
* @param row The specified row
* @param col The specified column
* @return The object at the specified row and column.
*/
public Object elementAt(int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.elementAt(col));
}
/**
* Calls the Vector.firstElement() on the specified row in dataVector
* @param row The specified row
* @return The first element in the specified row of dataVector
*/
public Object firstElement(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.firstElement());
}
/**
* Calls the Vector.lastElement() on the specified row in dataVector
* @param row The specified row
* @return The last element in the specified row of dataVector
*/
public Object lastElement(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
return (data.lastElement());
}
/**
* Calls the Vector.setElementAt() on the specified row in dataVector
* @param obj object - the value to set
* @param row The specified row
* @param col The specified column
*/
public void setElementAt(Object obj, int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.setElementAt(obj, col);
fireTableRowsUpdated(row, row);
}
/**
* Calls the Vector.removeElementAt() on the specified row in dataVector
* @param row Removes the value at the specified cell row, and
* @param col the specified column
*/
public void removeElementAt(int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.removeElementAt(col);
fireTableRowsUpdated(row, row);
}
/**
* Calls the Vector.insertElementAt() on the specified row in dataVector
* @param obj The value to set.
* @param row the specified row
* @param col the specified column
*/
public void insertElementAt(Object obj, int row, int col)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.insertElementAt(obj, col);
fireTableRowsUpdated(row, row);
}
/**
* Calls the Vector.addElement() on the specified row in dataVector
* @param obj The value to add
* @param row The specified row
*/
public void addElement(Object obj, int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.addElement(obj);
fireTableRowsUpdated(row, row);
}
/**
* Calls the Vector.removeElement() on the specified row in dataVector
* @param obj The value to remove. Removes only the first occurence.
* @param row The specified row
* @return Whether or not the value was found
*/
public boolean removeElement(Object obj, int row)
{
Vector data;
boolean rv;
data = (Vector)dataVector.elementAt(row);
rv = data.removeElement(obj);
if(rv)
{
fireTableRowsUpdated(row, row);
}
return (rv);
}
/**
* Calls the Vector.removeAllElements() on the specified row in dataVector
* @param row The specified row
*/
public void removeAllElements(int row)
{
Vector data;
data = (Vector)dataVector.elementAt(row);
data.removeAllElements();
fireTableRowsUpdated(row, row);
}
}