/*
 * 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.*;

public class PreProcessor_Element3 extends JPanel {
  Canvas3D J3D;
  Hashtable mat;
  BorderLayout borderLayout1 = new BorderLayout();
  String[] type = {"Contact_Triangle","Shell_C0_3"};
  private JPanel jPanel1 = new JPanel();
  private GridLayout gridLayout1 = new GridLayout();
  private JLabel jLabel6 = new JLabel();
  private JTextField jTextField1 = new JTextField();
  private JLabel jLabel7 = new JLabel();
  private JTextField jTextField2 = new JTextField();
  private JLabel jLabel8 = new JLabel();
  private JTextField jTextField3 = new JTextField();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JComboBox cb_type = new JComboBox(type);
  private _Node[] nodes = new _Node[3];
  private JPanel jPanel4 = new JPanel();
  private JPanel jPanel3 = new JPanel();
  private JPanel jPanel2 = new JPanel();
  private JButton jButton1 = new JButton();
  private JComboBox cb_mat = new JComboBox();
  private JLabel jLabel3 = new JLabel();
  private BorderLayout borderLayout3 = new BorderLayout();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel5 = new JPanel();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JLabel jLabel1 = new JLabel();
  private JTextField tft = new JTextField();

  public PreProcessor_Element3(Canvas3D j3d, Hashtable m) {
    try {
      J3D=j3d;
      mat=m;
      jbInit();
    }catch(Exception e) { e.printStackTrace();}
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jPanel1.setLayout(gridLayout1);
    gridLayout1.setColumns(3);
    gridLayout1.setRows(3);
    jLabel6.setText("Node 1");
    jLabel7.setText("Node 2");
    jLabel8.setText("Node 3");
    jButton2.setText("<<");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    jButton3.setText("<<");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
    jButton4.setText("<<");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    });
    cb_type.setForeground(Color.blue);
    jTextField1.setEditable(false);
    jTextField2.setEditable(false);
    jTextField3.setEditable(false);
    jPanel4.setLayout(borderLayout3);
    jPanel2.setLayout(borderLayout2);
    jButton1.setText("add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jLabel3.setText("Material");
    jPanel5.setLayout(borderLayout4);
    jLabel1.setText("Thickness");
    this.add(jPanel1, BorderLayout.CENTER);
    jPanel1.add(jLabel6, null);
    jPanel1.add(jTextField1, null);
    jPanel1.add(jButton2, null);
    jPanel1.add(jLabel7, null);
    jPanel1.add(jTextField2, null);
    jPanel1.add(jButton3, null);
    jPanel1.add(jLabel8, null);
    jPanel1.add(jTextField3, null);
    jPanel1.add(jButton4, null);
    this.add(cb_type, BorderLayout.NORTH);
    this.add(jPanel2,  BorderLayout.SOUTH);
    jPanel4.add(jLabel3, BorderLayout.WEST);
    jPanel4.add(cb_mat, BorderLayout.CENTER);
    jPanel4.add(jPanel5,  BorderLayout.SOUTH);
    jPanel5.add(jLabel1, BorderLayout.WEST);
    jPanel5.add(tft, BorderLayout.CENTER);
    jPanel2.add(jPanel3, BorderLayout.CENTER);
    jPanel3.add(jButton1, null);
    jPanel2.add(jPanel4, BorderLayout.NORTH);
    for(Enumeration en = mat.keys(); en.hasMoreElements();)  cb_mat.addItem(en.nextElement());
    this.validate();
  }


  public void error(Object st){
    JOptionPane.showMessageDialog(null,"Error: "+st,"Error!",JOptionPane.ERROR_MESSAGE);
    System.out.println("Error: "+st);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    if(nodes[0]==null || nodes[1]==null || nodes[2]==null)return;
    J3D.add3D(new _Element3(nodes[0],nodes[1],nodes[2],cb_type.getSelectedIndex()==0 ? Canvas3D.MESH_Contact_Triangle : Canvas3D.MESH_Shell_C0_3,(Material)mat.get(cb_mat.getSelectedItem()+""),Float.parseFloat(tft.getText())));
    J3D.repaint();
  }

  void jButton2_actionPerformed(ActionEvent e) {
    Object obj = J3D.getSelectedObject3D();
    if(obj!=null && obj instanceof _Node){
      nodes[0]=(_Node)obj;
      jTextField1.setText(obj.toString());
    }else{
      nodes[0]=null;
      jTextField1.setText("");
    }
  }

  void jButton3_actionPerformed(ActionEvent e) {
    Object obj = J3D.getSelectedObject3D();
    if(obj!=null && obj instanceof _Node){
      nodes[1]=(_Node)obj;
      jTextField2.setText(obj.toString());
    }else{
      nodes[1]=null;
      jTextField2.setText("");
    }
  }

  void jButton4_actionPerformed(ActionEvent e) {
    Object obj = J3D.getSelectedObject3D();
    if(obj!=null && obj instanceof _Node){
      nodes[2]=(_Node)obj;
      jTextField3.setText(obj.toString());
    }else{
      nodes[2]=null;
      jTextField3.setText("");
    }
  }


}


