///////////////////////////////////////////////////////////////////////////////
//                   ALL STUDENTS COMPLETE THESE SECTIONS
// Title:            Satisfiability Test
// Files:            ABMatrix.java, CdbTuple.java, Dialog1.java, Frame1.java,
//                   JTableHeaderToolTips.java, Mytest.java, RowNumberHeader.java
// Semester:         Spring 2007
//
// Author:           Hang Yue, yuehang366@gmail.com
// CS Login:         hyue
// Lecturer's Name:  Dr.Peter Revesz
// Lab Section:      (your lab section number)
//
//                   PAIR PROGRAMMERS COMPLETE THIS SECTION
// Pair Partner:     (name of your pair programming partner)
// CS Login:         (partner's login name)
// Lecturer's Name:  (name of your partner's lecturer)
// Lab Section:      (your partner's lab section number)
//
//                   STUDENTS WHO GET HELP FROM ANYONE OTHER THAN THEIR PARTNER
// Credits:          Scot Anderson
//////////////////////////// 80 columns wide //////////////////////////////////
package satisfiablitytest;

import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.XYConstraints;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
//import junit.framework.Assert;
import junit.framework.TestCase;

public class Dialog1 extends JDialog implements Serializable
{
	/**
	 * for Serializable interface
	 */
	 private static final long serialVersionUID = 5334533793567570867L;
	 
     private JPanel panel1;
     private JLabel jLabel2;
     private XYLayout xYLayout1;
     /**
 	 * This is the table rows
 	 */
     protected int rows; 
     /**
 	 * This is the table columns
 	 */
     protected int cols; 
     public DefaultTableModel model;
     protected JTable table;
     private JButton btnExt;
     private JButton btnTest;
     protected JPanel innerPanelsouth;
     private int f1;
     private boolean flag;
     private Frame1 fram;
     public Object rowDate[];

     public Dialog1()
     {
          this(null, "", true);
     }
     public Dialog1(Frame frame, String title, boolean modal)  
     {    
    	  super(frame, title, modal);
          panel1 = new JPanel();
          xYLayout1 = new XYLayout();
          model = new DefaultTableModel();
          table = new JTable(model);
          btnExt = new JButton();
          btnTest = new JButton();
          jLabel2 = new JLabel();
         // rowDate = new Object[cols];
          panel1.setLayout(xYLayout1);
          table.setFont(new Font("Dialog", 0, 15));
          table.setCellSelectionEnabled(true);
          table.setColumnSelectionAllowed(true);
          table.setGridColor(UIManager.getColor("PasswordField.selectionBackground"));
          table.setRowHeight(23);
          table.setRowMargin(2);
          innerPanelsouth = new JPanel();
          innerPanelsouth.add(btnTest);
          innerPanelsouth.add(btnExt);
          getContentPane().add(panel1, "First");
          getContentPane().add(innerPanelsouth, "Last");
          panel1.add(table, new XYConstraints(11, 29, 464, 274));
          table.setAutoResizeMode(0);
          panel1.add(jLabel2);
          btnExt.setText("CANCEL");
          btnExt.setSize(1, 10);
          btnTest.setText("TEST");
          JTableHeaderToolTips jthtt = new JTableHeaderToolTips(table.getColumnModel());
          table.setTableHeader(jthtt);
          JScrollPane jsp = new JScrollPane(table);
          JViewport jvp = new JViewport();
          jvp.setView(new RowNumberHeader(table));
          jsp.setRowHeader(jvp);
          getContentPane().add(jsp, "Center");
          pack();
          addWindowListener(new WindowAdapter()   
          {   
          public void windowClosing(WindowEvent   w)   
            {   
        	  model.setNumRows(0);
              model.setColumnCount(0);
              jLabel2.setText("");
              dispose(); 
            }   
          });
          
          btnExt.addActionListener(new  ActionListener(){
        	  public void actionPerformed (ActionEvent e)
              {    model.setNumRows(0);
     	           model.setColumnCount(0);
     	           jLabel2.setText("");
            	   dispose();
              }
          });
         /**
      	  *  show the results after pressing Button "test"
      	  */
          btnTest.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e)
           {
        	  try{
        		  Test test =new Test();
        	      test.testSimpleSat();	 
        	    }
        	    catch (NullPointerException e5){
        	    	System.out.print("NullPointer");
        	    }  	    
        	    if (flag==true)
        			 jLabel2.setText("Matrix >> Satisfiable");
        		else
        			 jLabel2.setText("Matrix >> Unsatisfiable");
        	    
          }
        });
     
     }

     public class Test extends TestCase{
    		/**
    		 * This method transfers the table values to the matrix, then transfer 
    		 * satisfiable method in CdbTuple class to return satisfiable or unsatifiable
    		 * @param filename
    		 */
    	   public void testSimpleSat()
    		{
    			ABMatrix m = new ABMatrix(); 
    			fram = new Frame1();
    			f1= table.getRowCount();
    			m.matrix = new double[f1][f1];
    			try{
    				
    			  for (int i=0; i<f1; i++)
    				for (int j=0; j<f1; j++)
    				{   
    					if(table.getValueAt(i,j).toString()=="-¡Þ")
    					{
    						m.matrix[i][j] = Double.NEGATIVE_INFINITY;
    					}
    				    else
    					m.matrix[i][j] = Double.parseDouble(table.getValueAt(i,j).toString());
    				}
    			}
    			catch(Exception e3){
    			    System.out.println(e3.getMessage());
    			    System.out.print("Please input the correct number format");
    			}
    		
    			CdbTuple t = new CdbTuple(m, f1,fram.columnNames);
    			flag = t.satisfiable();
    			//Assert.assertTrue(flag);
    		} 
      }  
}
