/*
 * 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 java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import j3d.*;

public class PreProcessor_SurfDir extends JPanel {
  Canvas3D J3D;
  Hashtable mat;
  BorderLayout borderLayout1 = new BorderLayout();
  String[] type = {"Contact_Triangle","Shell_C0_3","Shell_BT_4"};
  private JButton jButton1 = new JButton();
  private JPanel jPanel2 = new JPanel();
  private _Object[] curves = new _Object[2];
  private JLabel jLabel1 = new JLabel();
  private JPanel jPanel1 = new JPanel();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel3 = new JPanel();
  private JTextField jTextField2 = new JTextField();
  private JTextField jTextField1 = new JTextField();
  private JLabel jLabel7 = new JLabel();
  private GridLayout gridLayout1 = new GridLayout();
  private JLabel jLabel6 = new JLabel();
  private JButton jButton3 = new JButton();
  private JButton jButton2 = new JButton();
  private JPanel jPanel4 = new JPanel();
  private GridLayout gridLayout2 = new GridLayout();
  private JPanel jPanel5 = new JPanel();
  private BorderLayout borderLayout3 = new BorderLayout();
  private JLabel jLabel2 = new JLabel();
  private JTextField jTextField3 = new JTextField();
  private JPanel jPanel6 = new JPanel();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JLabel jLabel4 = new JLabel();
  private JComboBox jComboBox1 = new JComboBox(type);
  private JPanel jPanel7 = new JPanel();
  private BorderLayout borderLayout5 = new BorderLayout();
  private JLabel jLabel3 = new JLabel();
  private JComboBox jComboBox2 = new JComboBox();
  private JPanel jPanel8 = new JPanel();
  private BorderLayout borderLayout6 = new BorderLayout();
  private JLabel jLabel5 = new JLabel();
  private JTextField jTextField4 = new JTextField();

  public PreProcessor_SurfDir(Canvas3D j3d, Hashtable m) {
    try {
      J3D=j3d;
      mat=m;
      jbInit();
    }catch(Exception e) { e.printStackTrace();}
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jButton1.setText("add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jLabel1.setForeground(Color.blue);
    jLabel1.setText("Surface on a direction");
    jPanel1.setLayout(borderLayout2);
    jPanel3.setLayout(gridLayout1);
    jTextField2.setEditable(false);
    jTextField1.setEditable(false);
    jLabel7.setText("Direction");
    gridLayout1.setColumns(3);
    gridLayout1.setRows(2);
    jLabel6.setText("Curve");
    jButton3.setText("<<");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton2.setText("<<");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jPanel4.setLayout(gridLayout2);
    gridLayout2.setColumns(1);
    gridLayout2.setRows(4);
    jPanel5.setLayout(borderLayout3);
    jLabel2.setText("Mesh Size");
    jPanel6.setLayout(borderLayout4);
    jLabel4.setText("Mesh Type");
    jPanel7.setLayout(borderLayout5);
    jLabel3.setText("Material");
    jPanel8.setLayout(borderLayout6);
    jLabel5.setText("Thickness");
    this.add(jPanel2,  BorderLayout.SOUTH);
    jPanel2.add(jButton1, null);
    this.add(jLabel1, BorderLayout.NORTH);
    this.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jPanel3,  BorderLayout.CENTER);
    jPanel3.add(jLabel6, null);
    jPanel3.add(jTextField1, null);
    jPanel3.add(jButton2, null);
    jPanel3.add(jLabel7, null);
    jPanel3.add(jTextField2, null);
    jPanel3.add(jButton3, null);
    jPanel1.add(jPanel4,  BorderLayout.SOUTH);
    jPanel4.add(jPanel5, null);
    jPanel5.add(jLabel2,  BorderLayout.WEST);
    jPanel5.add(jTextField3,  BorderLayout.CENTER);
    jPanel4.add(jPanel6, null);
    jPanel6.add(jLabel4,  BorderLayout.WEST);
    jPanel6.add(jComboBox1,  BorderLayout.CENTER);
    jPanel4.add(jPanel7, null);
    jPanel7.add(jLabel3, BorderLayout.WEST);
    jPanel7.add(jComboBox2, BorderLayout.CENTER);
    jPanel4.add(jPanel8, null);
    jPanel8.add(jLabel5, BorderLayout.WEST);
    jPanel8.add(jTextField4, BorderLayout.CENTER);
    for(Enumeration en = mat.keys(); en.hasMoreElements();)  jComboBox2.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{
      int t=-1;
      if(jComboBox1.getSelectedIndex()==0)t=Canvas3D.MESH_Contact_Triangle;
      else if(jComboBox1.getSelectedIndex()==1)t=Canvas3D.MESH_Shell_C0_3;
      else if(jComboBox1.getSelectedIndex()==2)t=Canvas3D.MESH_Shell_BT_4;
      J3D.add3D(new _SurfDir(curves[0],curves[1],Float.parseFloat(jTextField3.getText()),t,(Material)mat.get(jComboBox2.getSelectedItem()+""),Float.parseFloat(jTextField4.getText())));
      J3D.repaint();
    }catch(Exception e1){error(e1);}
  }

  void jButton2_actionPerformed(ActionEvent e) {
    Object obj = J3D.getSelectedObject3D();
    if(obj!=null && (obj instanceof _Line || obj instanceof _PLine || obj instanceof _NurbCurve)){
      curves[0]=(_Object)obj;
      jTextField1.setText(obj.toString());
    }else{
      curves[0]=null;
      jTextField1.setText("");
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    Object obj = J3D.getSelectedObject3D();
    if(obj!=null && (obj instanceof _Line || obj instanceof _PLine || obj instanceof _NurbCurve)){
      curves[1]=(_Object)obj;
      jTextField2.setText(obj.toString());
    }else{
      curves[1]=null;
      jTextField2.setText("");
    }
  }

}


