/*
 * 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_Element2 extends JPanel {
  Canvas3D J3D;
  Hashtable mat;
  BorderLayout borderLayout1 = new BorderLayout();
  String[] type = {"Rod_2","Beam_2","Contact_Line"};
  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 JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JComboBox jComboBox1 = new JComboBox(type);
  private _Node[] nodes = new _Node[2];
  private JPanel jPanel4 = new JPanel();
  private JPanel jPanel3 = new JPanel();
  private JPanel jPanel2 = new JPanel();
  private JButton jButton1 = new JButton();
  private JLabel jLabel3 = new JLabel();
  private BorderLayout borderLayout3 = new BorderLayout();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JComboBox jComboBox2 = new JComboBox();
  private JPanel jPanel5 = new JPanel();
  private BorderLayout borderLayout4 = new BorderLayout();
  private JLabel jLabel1 = new JLabel();
  private JTextField jTextField3 = new JTextField();

  public PreProcessor_Element2(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(2);
    jLabel6.setText("Node 1");
    jLabel7.setText("Node 2");
    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);
      }
    });
    jComboBox1.setForeground(Color.blue);
    jTextField1.setEditable(false);
    jTextField2.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("Diameter");
    jComboBox2.setActionCommand("");
    jComboBox2.setLightWeightPopupEnabled(false);
    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);
    this.add(jComboBox1, BorderLayout.NORTH);
    this.add(jPanel2,  BorderLayout.SOUTH);
    jPanel4.add(jLabel3, BorderLayout.WEST);
    jPanel4.add(jComboBox2, BorderLayout.CENTER);
    jPanel2.add(jPanel5,  BorderLayout.CENTER);
    jPanel5.add(jLabel1, BorderLayout.WEST);
    jPanel5.add(jTextField3, BorderLayout.CENTER);
    jPanel2.add(jPanel3,  BorderLayout.SOUTH);
    jPanel3.add(jButton1, null);
    jPanel2.add(jPanel4, BorderLayout.NORTH);
    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{
      if(nodes[0]==null || nodes[1]==null)return;
      int t=-1;
      if(jComboBox1.getSelectedIndex()==0) t = Canvas3D.MESH_Rod_2;
      if(jComboBox1.getSelectedIndex()==1) t = Canvas3D.MESH_Beam_2;
      if(jComboBox1.getSelectedIndex()==2) t = Canvas3D.MESH_Contact_Line;
      J3D.add3D(new _Element2(nodes[0],nodes[1],t,(Material)mat.get(jComboBox2.getSelectedItem()+""),Float.parseFloat(jTextField3.getText())));
      J3D.repaint();
    }catch(Exception e1){error(e1);}
  }

  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("");
    }
  }

}


