FileDocCategorySizeDatePackage
MarketDataModel.javaAPI DocExample1870Thu Oct 24 20:14:24 BST 2002None

MarketDataModel

public class MarketDataModel extends AbstractTableModel implements Runnable

Fields Summary
Thread
runner
MYOSM
market
int
delay
Stock[]
stocks
int[]
stockIndices
String[]
headers
Constructors Summary
public MarketDataModel(int initialDelay)

    market = new MYOSM();
    delay = initialDelay * 1000;
    Thread runner = new Thread(this);
    runner.start();
  
Methods Summary
public intgetColumnCount()

 return headers.length; 
public java.lang.StringgetColumnName(int c)

 return headers[c]; 
public intgetRowCount()


      return stocks.length; 
public java.lang.ObjectgetValueAt(int r, int c)

    switch(c) {
    case 0:
      return stocks[r].symbol;
    case 1:
      return new Double(stocks[r].price);
    case 2:
      return new Double(stocks[r].delta);
    case 3:
      return stocks[r].lastUpdate;
    }
    throw new IllegalArgumentException("Bad cell (" + r + ", " + c +")");
  
public voidrun()

    while(true) {
      // Blind update . . . we could check for real deltas if necessary
      updateStocks();

      // We know there are no new columns, so don't fire a data change, only
      // fire a row update . . . this keeps the table from flashing
      fireTableRowsUpdated(0, stocks.length - 1);
      try { Thread.sleep(delay); }
      catch(InterruptedException ie) {}
    }
  
public voidsetDelay(int seconds)

 delay = seconds * 1000; 
public voidsetStocks(int[] indices)

    stockIndices = indices;
    updateStocks();
    fireTableDataChanged();
  
public voidupdateStocks()

    stocks = new Stock[stockIndices.length];
    for (int i = 0; i < stocks.length; i++) {
      stocks[i] = market.getQuote(stockIndices[i]);
    }