import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

/**
 * Written by Jaymin Patel
 * Modified by Peter McBrien to permit snapshot view
 */
class PrintRes extends JFrame implements ActionListener {
  private Table tab;
  private boolean showInterval;
  private int timePoint;
  private String title;
  ButtonGroup isTemporal;
  JTextField snapshotTime;
  JRadioButton allTimes;
  JRadioButton snapshot;
  JPanel timePanel;
  JScrollPane scrollPane;

  // PJM Added version of PrintRes that returns only a snapshot view
  // (ie. results  for one time point)
  public PrintRes(Table tab, String title, int timePoint) {
    this.tab=tab;
    this.title=title;
    showInterval=false;
    this.timePoint=timePoint;
    createContents();
  }

  public PrintRes(Table tab, String title) {
    this.tab=tab;
    this.title=title;
    showInterval=true;
    this.timePoint=0;
    createContents();
  }

  private void processTable() {
    Vector columnNames=getColumnNames(tab.getCol(),showInterval);
    Vector data=getTableData(tab,showInterval,timePoint);
    JTable table=new JTable(data, columnNames);
    scrollPane.setViewportView(table);
  }

  public void actionPerformed (ActionEvent evt) {  
    Object source=evt.getSource();

    if (source==allTimes) {
      showInterval=true;
      snapshotTime.setEnabled(!showInterval);
      processTable();
    }

    if (source==snapshot) {
      showInterval=false;
      snapshotTime.setEnabled(!showInterval);
      timePoint=Integer.parseInt(snapshotTime.getText());
      processTable();
    }

    if (source==snapshotTime) {
      if (showInterval==false) {
	timePoint=Integer.parseInt(snapshotTime.getText());
	processTable();
      }
    }
  }

  private void createContents() {
    timePanel=new JPanel();
    timePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
    allTimes=new JRadioButton("All times",showInterval);
    allTimes.addActionListener(this);
    snapshot=new JRadioButton("Snapshot",!showInterval);
    snapshot.addActionListener(this);
    timePanel.add(allTimes);
    timePanel.add(snapshot);
    snapshotTime=new JTextField(10);
    snapshotTime.setText(""+timePoint); 
    snapshotTime.addActionListener(this);
    snapshotTime.setEnabled(!showInterval);
    timePanel.add(snapshotTime);
    isTemporal=new ButtonGroup();
    isTemporal.add(allTimes);
    isTemporal.add(snapshot);
    getContentPane().add(timePanel,BorderLayout.NORTH);


    //Create the scroll pane and add the table to it. 
    scrollPane=new JScrollPane();
    processTable();

    //Add the scroll pane to this window.
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    // Make window appear
    setSize(500, 300);
    setTitle(title);
    setVisible(true);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  }

  private static Vector getColumnNames(Column col, boolean showInterval) {
    Vector cols=new Vector();
    int noCols = col.getColNumber();

    /* get all the column titles */
    for (int i=0; i<noCols; i++)
      cols.add(col.getPosName(i));

    /* provide the column titles for the time components */
    if (showInterval) {
      cols.add("st");
      cols.add("et");
    }

    return cols;
  }

  private static Vector getTableData(Table tab, boolean showInterval,
					 int timePoint) {
    int noRows = tab.getNoRowSet();
    int noCols = tab.getNoCols();
		
    /* hold all row data in table
     * 'noCols+2' => store st and et as well
     */
    Vector data=new Vector();
    
    for (int i=0; i<noRows; i++) { /* go through all rows */
      Row r=(Row)tab.getRow(i);

      if (showInterval || 
	  (r.getRow_st()<=timePoint && timePoint<=r.getRow_et())) {
	Object[] rowVals=r.getRowValues();
	Vector row=new Vector();
	
	/* go through all columns of each row */
	for (int j=0; j<noCols; j++) 
	  /* place each column value of row in 'data' */
	  row.add(rowVals[j]);
	
	/* place the time components (st & et) in 'data' */
	if (showInterval) {
	  row.add(new Integer (r.getRow_st()));
	  row.add(new Integer (r.getRow_et()));
	}

	data.add(row);
      }

    }
    
    return data;
  }
}
