/*
 * 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_SurfRev 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 curve;
  private JLabel jLabel1 = new JLabel();
  private JPanel jPanel1 = new JPanel();
  private BorderLayout borderLayout2 = new BorderLayout();
  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 tfm = new JTextField();
  private JPanel jPanel6 = new JPanel();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JLabel jLabel4 = new JLabel();
  private JComboBox cb_type = new JComboBox(type);
  private JPanel jPanel7 = new JPanel();
  private BorderLayout borderLayout5 = new BorderLayout();
  private JLabel jLabel3 = new JLabel();
  private JComboBox cb_mat = new JComboBox();
  private JPanel jPanel8 = new JPanel();
  private BorderLayout borderLayout6 = new BorderLayout();
  private JLabel jLabel5 = new JLabel();
  private JTextField tft = new JTextField();
  private JPanel jPanel9 = new JPanel();
  private JLabel jLabel8 = new JLabel();
  private BorderLayout borderLayout7 = new BorderLayout();
  private JTextField tfa = new JTextField();
  private JPanel jPanel10 = new JPanel();
  private JPanel jPanel11 = new JPanel();
  private BorderLayout borderLayout8 = new BorderLayout();
  private JLabel jLabel9 = new JLabel();
  private JPanel jPanel12 = new JPanel();
  private GridLayout gridLayout3 = new GridLayout();
  private JTextField tf_x = new JTextField();
  private JTextField tf_y = new JTextField();
  private JTextField tf_z = new JTextField();
  private BorderLayout borderLayout9 = new BorderLayout();
  private JLabel jLabel10 = new JLabel();
  private JPanel jPanel13 = new JPanel();
  private GridLayout gridLayout4 = new GridLayout();
  private JRadioButton rb_ox = new JRadioButton();
  private JRadioButton rb_oy = new JRadioButton();
  private JRadioButton rb_oz = new JRadioButton();
  private JPanel jPanel14 = new JPanel();
  private BorderLayout borderLayout10 = new BorderLayout();
  private JLabel jLabel6 = new JLabel();
  private JTextField tf_curve = new JTextField();
  private JButton jButton2 = new JButton();
  private ButtonGroup ButtonGroup = new ButtonGroup();

  public PreProcessor_SurfRev(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 of revolving");
    jPanel1.setLayout(borderLayout2);
    jPanel4.setLayout(gridLayout2);
    gridLayout2.setColumns(1);
    gridLayout2.setRows(8);
    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");
    jLabel8.setText("Angle");
    jPanel9.setLayout(borderLayout7);
    tfa.setText("360");
    jPanel10.setLayout(borderLayout8);
    jLabel9.setText("Base point");
    jPanel12.setLayout(gridLayout3);
    gridLayout3.setColumns(3);
    tf_x.setText("0");
    tf_y.setText("0");
    tf_z.setText("0");
    jPanel11.setLayout(borderLayout9);
    jLabel10.setText("Axis of rotation");
    jPanel13.setLayout(gridLayout4);
    gridLayout4.setColumns(3);
    rb_ox.setSelected(true);
    rb_ox.setText("OX");
    rb_oy.setText("OY");
    rb_oz.setText("OZ");
    jPanel14.setLayout(borderLayout10);
    jLabel6.setText("Curve");
    tf_curve.setEditable(false);
    jButton2.setText("<<");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    this.add(jPanel2,  BorderLayout.SOUTH);
    jPanel2.add(jButton1, null);
    this.add(jLabel1, BorderLayout.NORTH);
    this.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jPanel4,  BorderLayout.CENTER);
    jPanel4.add(jPanel14, null);
    jPanel4.add(jPanel10, null);
    jPanel4.add(jPanel11, null);
    jPanel4.add(jPanel9, null);
    jPanel4.add(jPanel5, null);
    jPanel5.add(jLabel2,  BorderLayout.WEST);
    jPanel5.add(tfm,  BorderLayout.CENTER);
    jPanel4.add(jPanel6, null);
    jPanel6.add(jLabel4,  BorderLayout.WEST);
    jPanel6.add(cb_type,  BorderLayout.CENTER);
    jPanel4.add(jPanel7, null);
    jPanel7.add(jLabel3, BorderLayout.WEST);
    jPanel7.add(cb_mat, BorderLayout.CENTER);
    jPanel4.add(jPanel8, null);
    jPanel8.add(jLabel5, BorderLayout.WEST);
    jPanel8.add(tft, BorderLayout.CENTER);
    jPanel10.add(jLabel9, BorderLayout.WEST);
    jPanel10.add(jPanel12, BorderLayout.CENTER);
    jPanel12.add(tf_x, null);
    jPanel12.add(tf_y, null);
    jPanel12.add(tf_z, null);
    jPanel11.add(jLabel10, BorderLayout.WEST);
    jPanel11.add(jPanel13, BorderLayout.CENTER);
    jPanel13.add(rb_ox, null);
    jPanel13.add(rb_oy, null);
    jPanel13.add(rb_oz, null);
    jPanel14.add(jLabel6,  BorderLayout.WEST);
    jPanel14.add(tf_curve, BorderLayout.CENTER);
    jPanel14.add(jButton2,  BorderLayout.EAST);

    jPanel9.add(jLabel8,  BorderLayout.WEST);
    jPanel9.add(tfa, BorderLayout.CENTER);
    for(Enumeration en = mat.keys(); en.hasMoreElements();)  cb_mat.addItem(en.nextElement());
    this.validate();
    ButtonGroup.add(rb_ox);
    ButtonGroup.add(rb_oy);
    ButtonGroup.add(rb_oz);
  }


  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(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;
      float x = Float.parseFloat(tf_x.getText());
      float y = Float.parseFloat(tf_y.getText());
      float z = Float.parseFloat(tf_z.getText());
      int ax=-1;
      if(rb_ox.isSelected())ax=0;
      else if(rb_oy.isSelected())ax=1;
      else ax=2;
      J3D.add3D(new _SurfRev(curve,new _Point(x,y,z),ax,Float.parseFloat(tfa.getText()),Float.parseFloat(tfm.getText()),t,(Material)mat.get(cb_mat.getSelectedItem()+""),Float.parseFloat(tft.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)){
      curve=(_Object)obj;
      tf_curve.setText(obj.toString());
    }else{
      curve=null;
      tf_curve.setText("");
    }
  }



}


