package KinoDB;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.Serializable;
import java.io.*;
import javax.swing.border.*;
import com.borland.jbcl.layout.*;
/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */

public class createTable extends JFrame {
  JLabel jLabel1 = new JLabel();
  int weiter = 0;
  int ready = 0;
  String tableName = "";
  String columnName[] = new String[20];
  String columnType[] = new String[20];
  String columnLenght[] = new String[20];
  int isAuto[] = new int[20];
  int isPrim[] = new int[20];
  int isPrimSet = 0;
  int cindex = 0;
  //String names[] = { "Integer", "Char" ,"Varchar", "Date" , "Float"};
  JPanel jPanel1 = new JPanel();
  JLabel ltablename = new JLabel();
  XYLayout xYLayout1 = new XYLayout();
  JTextField tableText = new JTextField();
  createTable2 tab2;
  nexthandler handler = new nexthandler();
  String createString = "";

  public createTable() {
    super("Table Creation Wizard (Step 1)");

    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  //types.

    tableText.addActionListener(
         new ActionListener() {
            public void actionPerformed( ActionEvent e )
            {
              hide();
              tab2 = new createTable2();
              tab2.next.addActionListener(handler);
              tab2.end.addActionListener(handler);
              tab2.cbox2.addActionListener(handler);


            }
         }
      );

  setSize(300,200);
   show();
  }


  private void jbInit() throws Exception {
    jLabel1.setFont(new java.awt.Font("Dialog", 0, 30));
    jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
    jLabel1.setText("Create Table");
    ltablename.setText("Enter Table name:");
    jPanel1.setLayout(xYLayout1);
    tableText.setToolTipText("Please Enter the name of the new Table");
    this.getContentPane().add(jLabel1, BorderLayout.NORTH);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(ltablename, new XYConstraints(17, 36, -1, -1));
    jPanel1.add(tableText,  new XYConstraints(127, 35, 108, 20));
  }

 public class nexthandler implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
          if (e.getSource() == tab2.next)
          {

            columnName[cindex]   = new String(tab2.Columntext.getText());
            columnType[cindex]   = new String(tab2.names[tab2.jComboBox1.getSelectedIndex()]);
            columnLenght[cindex] = new String(tab2.Lenghttext.getText());
            //JOptionPane.showMessageDialog(null,columnName[cindex]);
            if (tab2.cbox.isSelected()) isAuto[cindex] = 1;
            if (tab2.cbox2.isSelected() && isPrimSet == 0) {isPrim[cindex] = 1;isPrimSet = 1;}
            tab2.hide();
            tab2 = new createTable2();

            if (isPrimSet == 1)
            {
              tab2.cbox2.setEnabled(false);
              tab2.cbox2.setToolTipText("Only 1 primary Key is possible");
            }

            tab2.next.addActionListener(this);
            tab2.end.addActionListener(this);
            cindex++;
          }


          if (e.getSource() == tab2.end)
          {
            columnName[cindex]   = new String(tab2.Columntext.getText());
            columnType[cindex]   = new String(tab2.names[tab2.jComboBox1.getSelectedIndex()]);
            columnLenght[cindex] = new String(tab2.Lenghttext.getText());
            if (tab2.cbox.isSelected()) isAuto[cindex] = 1;
            if (tab2.cbox2.isSelected() && isPrimSet == 0) {isPrim[cindex] = 1;isPrimSet = 1;}
            cindex++;
            makeCreateString();

            JOptionPane.showMessageDialog(null,"I think you successfully created a table");
            tab2.hide();
            ready = 1;
            dispose();


            //System.exit(0);
          }
          //if (e.getSource() == tab2.cbox2 && isPrimSet == 1)
         // {
            //tab2.cbox2.setEnabled(false);
         // }

    }

  }

 public String getCreateString()
 {return createString;}

 private void makeCreateString()
 {
   int i = 0;
   createString = "create table "+tableText.getText() + "(" ;
   while (i < cindex)
   {
     createString += columnName[i] + " " + columnType[i] + "(" + columnLenght[i] + ") ";
     if (isPrim[i] == 1) createString += "Primary Key ";
     if (isAuto[i] == 1) createString += "Auto_Increment";
     //createString +=
     if (i != cindex - 1)     createString += ",";
     else createString += ")";
     i++;

   }

 }

}
