import rock.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.URL;

import javax.naming.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 * Creates a table container to hold properties of phases present in the equilibrium assemblage.
 * This table is displayed in the main window of the GUI.
 * @version 1.0 (August 2007)
 * @author Mark S. Ghiorso, OFM-Research Inc.
 */
public class PhaseTableModel extends AbstractTableModel {
  /** Quantity of each phase is displayed in grams. */
  public static int UNIT_GRAMS          = 0;
  /** Quantity of each phase is displayed in wt %. */
  public static int UNIT_WEIGHT_PERCENT = 1;
  /** Quantity of each phase is displayed in volume %. */
  public static int UNIT_VOLUME_PERCENT = 2;

  private String columnNames[] = {
    "phase", "grams", "affinity (kJ)", "formula", 
    "component", "mol frac", "component", "mol frac", "component", "mol frac", 
    "component", "mol frac", "component", "mol frac", "component", "mol frac",
    "component", "mol frac", "component", "mol frac", "component", "mol frac",
    "component", "mol frac" };
  private java.lang.Object[][] data;
  private int units;
  
  /**
   * Creates the table and initializes the class.
   */
  PhaseTableModel() {
    data = new java.lang.Object[100][24];
    for (int i=0; i<100; i++) {
      data[i][0] = "";
      for (int j=1; j<3; j++) data[i][j] = new Double(0.0);
      data[i][3] = "";
      for (int j=4; j<24; j=j+2) {
        data[i][j  ] = "";
        data[i][j+1] = new Double(0.0);
      }
    }
    units = UNIT_GRAMS;
  }
  
  /**
   * Sets display of the quantity property for all phases in the table.
   * @param units Permissible values are UNIT_GRAMS, UNIT_WEIGHT_PERCENT, and UNIT_VOLUME_PERCENT.
   */
  public void setDisplayUnit(int units) {
    this.units = units;
    if        (units == UNIT_GRAMS)          {
      System.out.println("Units set to *grams*");
      columnNames[1] = "grams";
    } else if (units == UNIT_WEIGHT_PERCENT) {
      System.out.println("Units set to *wt %*");
      columnNames[1] = "wt %";
    } else if (units == UNIT_VOLUME_PERCENT) {
      System.out.println("Units set to *vol %*");
      columnNames[1] = "vol %";
    } else                                   {
      System.out.println("ERROR Bad call to setDisplayUnit");
    }
    fireTableStructureChanged();
  }
 
  /**
   * Get number of columns in the table.
   * @return Number of columns in table.
   */
  public int getColumnCount() {
    return columnNames.length;
  }
  
  /**
   * Get number of rows in the table.
   * @return Number of rows in table.
   */
  public int getRowCount() {
    return data.length;
  }
  
  /**
   * Get column header label.
   * @param col Index number of table column.
   * @return Header label for column.
   */
  public String getColumnName(int col) {
    return columnNames[col];
  }
  
  /**
   * Get value stored in a table cell.
   * @param row Index number of table row.
   * @param col Index number of table column.
   * @return Value stored in specified cell. Retrieved as an object that must be caste to a java type for display.
   */
  public java.lang.Object getValueAt(int row, int col) {
    return data[row][col];
  }
  
  /**
   * Get class of objects stored in a table column.
   * @param c Index number of table column.
   * @return Specific class of objects stored in this table column.
   */
  public Class getColumnClass(int c) {
    return getValueAt(0, c).getClass();
  }
  
  /**
   * Determine if a particular table cell may be edited by the user on the GUI.
   * @param row Index number of table row.
   * @param col Index number of table column.
   * @return True if editable; false otherwise. Always returns false.
   */
  public boolean isCellEditable(int row, int col) {
    return false;
  }
  
  /**
   * Set the value of a cell in the table.
   * @param value Java object. 
   * @param row Index number of table row.
   * @param col Index number of table column.
   */
  public void setValueAt(java.lang.Object value, int row, int col) {
    data[row][col] = value;
    fireTableCellUpdated(row,col);
  }
  
}
  
