/*
 * A GUI element, this is essentially an extension of a TextFieldGridPanel, 
 * with the addition of a title centered above the grid; designed to be used
 * in conjunction with the matrix class for input and output of matrix 
 * values.
 *
 * Author : Tom Kimber
 *
 */
import java.awt.* ;
import java.util.* ; 
public class MatrixPanel extends Panel
{
   public Label lblTitle = new Label("" , Label.CENTER ) ;
   private TextFieldGridPanel grid ;
   private int num_rows , num_cols ; 
   private Matrix m ;
   public double getValue( int i , int j )
   {
      return grid.getValue( i , j ) ;
   }
   public int getNumRows()
   {
      return num_rows ;
   }
   public int getNumCols()
   {
      return num_cols ; 
   }
   private void setNumRows( int i )
   {
      num_rows = i ;
   }
   private void setNumCols( int j )
   {
      num_cols = j ;
   }
   public void setValue( int i , int j , double x ) 
   {
      grid.setValue( i , j , x ) ;      
   }
   public void setValue( int i , int j , int x )
   {
      grid.setValue( i , j , x ) ;
   }
   public void updatePanel( Matrix m ) 
   {
      for ( int i = 0 ; i < num_rows ; i ++ )
      {
         for( int j = 0 ; j < num_cols ; j++ ) 
         {
             setValue( i , j , m.getEntry( i , j ) ) ;
         } 
      }   
   }
   public MatrixPanel( String title , Matrix mat )
   {
      m = mat ;
      num_rows = m.getNumRows( ) ;
      num_cols = m.getNumCols( ) ;
      setNumRows( num_rows ) ;
      setNumCols( num_cols ) ;
      setLayout( new BorderLayout() ) ;
      setFont( new Font( "Monospaced" , Font.PLAIN , 12 ) ) ;
      lblTitle.setText( title ) ;
      Panel pnlTitle = new Panel() ;
      pnlTitle.add( lblTitle ) ;
      add( BorderLayout.NORTH , lblTitle ) ;
      grid = new TextFieldGridPanel( num_rows , num_cols ) ;
      Panel gridDisplay = new Panel() ;
      gridDisplay.add( grid ) ;
      add( BorderLayout.CENTER , gridDisplay ) ;
       this.updatePanel( m ) ;
   }
}