FileDocCategorySizeDatePackage
PlayerInfoModel.javaAPI DocExample2947Tue Dec 12 18:58:44 GMT 2000bingo.shared

PlayerInfoModel

public class PlayerInfoModel extends AbstractTableModel

Fields Summary
protected static int
NUM_COLUMNS
protected static int
START_NUM_ROWS
protected int
nextEmptyRow
protected int
numRows
public static final String
idName
public static final String
playerName
public static final String
cardNumName
public static final String
wolfNumName
protected Vector
data
Constructors Summary
public PlayerInfoModel()


      
        data = new Vector();
    
Methods Summary
public synchronized voidclear()

	int oldNumRows = numRows;

        numRows = START_NUM_ROWS;
	data.removeAllElements();
        nextEmptyRow = 0;

	if (oldNumRows > START_NUM_ROWS) {
	    fireTableRowsDeleted(START_NUM_ROWS, oldNumRows - 1);
	}
	fireTableRowsUpdated(0, START_NUM_ROWS - 1);
    
public synchronized intgetColumnCount()

        return NUM_COLUMNS;
    
public java.lang.StringgetColumnName(int column)

	switch (column) {
	  case 0:
	    return idName;
	  case 1:
	    return playerName;
	  case 2:
	    return cardNumName;
	  case 3:
	    return wolfNumName;
	}
	return "";
    
public synchronized intgetRowCount()

        if (numRows < START_NUM_ROWS) {
            return START_NUM_ROWS;
        } else {
            return numRows;
        }
    
public synchronized java.lang.ObjectgetValueAt(int row, int column)

	try {
            PlayerRecord p = (PlayerRecord)data.elementAt(row);
            switch (column) {
              case 0:
                return new Integer(p.ID);
              case 1:
                return p.name;
              case 2:
                return new Integer(p.numCards);
              case 3:
                return new Integer(p.wolfCries);
            }
	} catch (Exception e) {
	}
	return "";
    
public synchronized voidupdatePlayer(PlayerRecord playerRecord)

        int ID = playerRecord.ID; //find the ID
        PlayerRecord p = null;
        int index = -1; 
        boolean found = false;
	boolean addedRow = false;
        
        int i = 0;
        while (!found && (i < nextEmptyRow)) {
            p = (PlayerRecord)data.elementAt(i);
            if (p.ID == ID) {
                found = true;
                index = i;
            } else {
                i++;
            }
        }

        if (found) { //update old player
	    data.setElementAt(playerRecord, index);
        } else { //add new player
	    if (numRows <= nextEmptyRow) {
		//add a row
                numRows++;
		addedRow = true;
            }
            index = nextEmptyRow;
	    data.addElement(playerRecord);
	}
    
        nextEmptyRow++;

	//Notify listeners that the data changed.
	if (addedRow) {
	    fireTableRowsInserted(index, index);
	} else {
	    fireTableRowsUpdated(index, index);
	}