/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOUSE. See the GNU
 * General Public License for more details.
 *
 * You should have recieved a copy of the GNU General Public License
 * along with this program; if not write to the Free Software
 * Foundation, inc., 59 Temple Place, Suite 330, Boston MA 02111-1307
 * USA
 */

package gui;

import javax.swing.*;
import java.awt.*;
import j3d.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
/**
 * Insert the type's description here.
 *
 * @author: Yuriy Mikhaylovskiy
 */

public class PreProcessor_NurbSurface extends JPanel {
  Canvas3D J3D;
  Hashtable mat;
  String[] type = {"Contact_Triangle","Shell_C0_3","Shell_BT_4"};
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JPanel jPanel1 = new JPanel();
  int U=3,V=3;
  private JTextField tf_u = new JTextField("3");
  private JTextField tf_v = new JTextField("3");
  Vector dat = new Vector();
  String[] hdr = {"U,V","X","Y","Z","W"};
  private TableModel tableModel = new DefaultTableModel() {
    public int getColumnCount() { return 5; }
    public int getRowCount() {
      try {
        return U*V;
      }catch(Exception e) { return 0;}
    }
    public Object getValueAt(int row, int col){
      if(col==0){ return ((int)row/V)+","+(row-((int)row/V)*V); }
      if(dat==null || dat.size()<row*4+col) return null;
      if(dat.elementAt(row*4+col-1)==null) return null; else return (Float)dat.elementAt(row*4+col-1);
    }
    public void setValueAt(Object aValue, int row, int col) {
      dat.setSize(getRowCount()*4);
      dat.setElementAt(aValue,row*4+col-1);
    }
    public String getColumnName(int col) {return hdr[col];}
    public boolean isCellEditable(int row, int col) {if(col==0)return false; else return true;}
    public Class getColumnClass(int c) {if(c==0)return "".getClass(); else return new Float(0).getClass();}
  };
  JTable Table = new JTable(tableModel);
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel2 = new JPanel();
  private JButton jButton1 = new JButton();
  private JPanel jPanel3 = new JPanel();
  private BorderLayout borderLayout3 = new BorderLayout();
  private JLabel jLabel1 = new JLabel();
  private JPanel jPanel4 = new JPanel();
  private GridLayout gridLayout1 = new GridLayout();
  private JLabel jLabel2 = new JLabel("U",JLabel.RIGHT);
  private JLabel jLabel3 = new JLabel("V",JLabel.RIGHT);
  private JPanel jPanel5 = new JPanel();
  private GridLayout gridLayout2 = new GridLayout();
  private JPanel jPanel6 = new JPanel();
  private JPanel jPanel7 = new JPanel();
  private JPanel jPanel8 = new JPanel();
  private JPanel jPanel9 = new JPanel();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JLabel jLabel4 = new JLabel();
  private JTextField tf_size = new JTextField();
  private BorderLayout borderLayout5 = new BorderLayout();
  private JLabel jLabel5 = new JLabel();
  private JComboBox cb_type = new JComboBox(type);
  private BorderLayout borderLayout6 = new BorderLayout();
  private JLabel jLabel6 = new JLabel();
  private JComboBox cb_mat = new JComboBox();
  private JLabel jLabel7 = new JLabel();
  private BorderLayout borderLayout7 = new BorderLayout();
  private JTextField tf_t = new JTextField();

  public PreProcessor_NurbSurface(Canvas3D j3d, Hashtable m) {
    try {
      J3D=j3d;
      mat=m;
      jbInit();
    }catch(Exception e) { e.printStackTrace();}
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jScrollPane1.setPreferredSize(new Dimension(200, 200));
    jPanel1.setLayout(borderLayout2);
    jButton1.setText("add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jPanel3.setLayout(borderLayout3);
    jLabel1.setForeground(Color.blue);
    jLabel1.setText("Nurb Surface");
    jPanel4.setLayout(gridLayout1);
    gridLayout1.setColumns(4);
    tf_v.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        tf_v_keyReleased(e);
      }
    });
    tf_u.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        tf_u_keyReleased(e);
      }
    });
    jPanel5.setLayout(gridLayout2);
    gridLayout2.setColumns(1);
    gridLayout2.setRows(4);
    jPanel6.setLayout(borderLayout4);
    jLabel4.setText("Mesh size");
    jPanel9.setLayout(borderLayout5);
    jLabel5.setText("Mesh type");
    jPanel8.setLayout(borderLayout6);
    jLabel6.setText("Material");
    jLabel7.setText("Thickness");
    jPanel7.setLayout(borderLayout7);
    this.add(jScrollPane1, BorderLayout.CENTER);
    this.add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(jPanel2, BorderLayout.CENTER);
    jPanel2.add(jButton1, null);
    jPanel1.add(jPanel5, BorderLayout.NORTH);
    jPanel5.add(jPanel6, null);
    jPanel6.add(jLabel4, BorderLayout.WEST);
    jPanel6.add(tf_size, BorderLayout.CENTER);
    jPanel5.add(jPanel9, null);
    jPanel9.add(jLabel5, BorderLayout.WEST);
    jPanel9.add(cb_type, BorderLayout.CENTER);
    jPanel5.add(jPanel8, null);
    jPanel8.add(jLabel6, BorderLayout.WEST);
    jPanel8.add(cb_mat, BorderLayout.CENTER);
    jPanel5.add(jPanel7, null);
    jPanel7.add(jLabel7,  BorderLayout.WEST);
    jPanel7.add(tf_t, BorderLayout.CENTER);
    this.add(jPanel3, BorderLayout.NORTH);
    jPanel3.add(jLabel1, BorderLayout.WEST);
    jPanel3.add(jPanel4, BorderLayout.CENTER);
    jPanel4.add(jLabel2, null);
    jPanel4.add(tf_u, null);
    jPanel4.add(jLabel3, null);
    jPanel4.add(tf_v, null);
    jScrollPane1.getViewport().add(Table, null);
    for(Enumeration en = mat.keys(); en.hasMoreElements();)  cb_mat.addItem(en.nextElement());
    this.validate();
  }


  public void error(Object st){
    JOptionPane.showMessageDialog(this,"Error: "+st,"Error!",JOptionPane.ERROR_MESSAGE);
    if(st instanceof Exception) ((Exception)st).printStackTrace(); else System.out.println("Error: "+st);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    try {
      _CtrlPoint[] points = new _CtrlPoint[U*V];
      int k=0;
      for(int i=0; i<dat.size()/4; i++) points[i] = new _CtrlPoint(((Float)dat.elementAt(i*4)).floatValue(),((Float)dat.elementAt(i*4+1)).floatValue(),((Float)dat.elementAt(i*4+2)).floatValue(),((Float)dat.elementAt(i*4+3)).floatValue(),Color.GRAY);
      /*
      points = new _CtrlPoint[16];
      points[0] = new _CtrlPoint(-90,  -20,   80,  1, Color.GRAY);
      points[1] = new _CtrlPoint(-40,   10,   80,  1, Color.GRAY);
      points[2] = new _CtrlPoint(40,  -30,   60,  1, Color.GRAY);
      points[3] = new _CtrlPoint(100,  -10,   80,  1, Color.GRAY);
      points[4] = new _CtrlPoint(-60,   30,   40,  1, Color.GRAY);
      points[5] = new _CtrlPoint(0,  -10,   40,  1, Color.GRAY);
      points[6] = new _CtrlPoint(20,  -10,   40,  1, Color.GRAY);
      points[7] = new _CtrlPoint(60,   20,   40,  1, Color.GRAY);
      points[8] = new _CtrlPoint(-100,  -20,  -20,  1, Color.GRAY);
      points[9] = new _CtrlPoint(-40,  -40,  -20,  1, Color.GRAY);
      points[10] = new _CtrlPoint(40,  -10,  -20,  1, Color.GRAY);
      points[11] = new _CtrlPoint(100,   0,  -20,  1, Color.GRAY);
      points[12] = new _CtrlPoint(-90,   20,  -60,  1, Color.GRAY);
      points[13] = new _CtrlPoint(-40,  -40,  -50,  1, Color.GRAY);
      points[14] = new _CtrlPoint(40,   30,  -50,  1, Color.GRAY);
      points[15] = new _CtrlPoint(90,  -20,  -60,  1, Color.GRAY);
      //J3D.add3D(new _QSurface(points,4,4,60,30));
      */
      int t=-1;
      if(cb_type.getSelectedIndex()==0)t=Canvas3D.MESH_Contact_Triangle;
      else if(cb_type.getSelectedIndex()==1)t=Canvas3D.MESH_Shell_C0_3;
      else if(cb_type.getSelectedIndex()==2)t=Canvas3D.MESH_Shell_BT_4;
      J3D.add3D(new _NurbSurface(points,U,V,Float.parseFloat(tf_size.getText()), t, (Material)mat.get(cb_mat.getSelectedItem()+""), Float.parseFloat(tf_t.getText())));
      //J3D.add3D(new _NurbSurface(points,4,4,Float.parseFloat(tf_size.getText()), t, (Material)mat.get(cb_mat.getSelectedItem()+""), Float.parseFloat(tf_t.getText())));
      J3D.repaint();
    }catch(Exception e1) {error(e1);}
  }


  void tf_v_keyReleased(KeyEvent e) {
    try {
       V=Integer.parseInt(tf_v.getText());
    }catch(Exception e1) {return;}
    Table.revalidate();
  }

  void tf_u_keyReleased(KeyEvent e) {
    try {
       U=Integer.parseInt(tf_u.getText());
    }catch(Exception e1) {return;}
    Table.revalidate();
  }
}


