/*
 * 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_Mesh extends JPanel {
  BorderLayout borderLayout1 = new BorderLayout();
  String[] type = {"Rod_2","Beam_2","Beam_Spring_2","Contact_Line","Contact_Triangle","Shell_C0_3","Shell_BT_4","Solid_Iso_6"};
  private JButton jButton1 = new JButton();
  private JPanel jPanel2 = new JPanel();
  private JLabel jLabel1 = new JLabel();
  private JPanel jPanel1 = new JPanel();
  private JComboBox cb_type = new JComboBox(type);
  private Canvas3D J3D;
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel3 = new JPanel();
  private GridLayout gridLayout1 = new GridLayout();
  private JTextField tf_size = new JTextField();
  private JPanel jPanel4 = new JPanel();
  private JCheckBox cb_div = new JCheckBox();
  private JCheckBox cb_size = new JCheckBox();

  public PreProcessor_Mesh(Canvas3D j3d) {
    try {
      J3D=j3d;
      jbInit();
    }catch(Exception e) { e.printStackTrace();}
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jButton1.setText("Mesh");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jLabel1.setForeground(Color.blue);
    jLabel1.setText("Mesh");
    jPanel1.setLayout(borderLayout2);
    jPanel3.setLayout(gridLayout1);
    gridLayout1.setColumns(2);
    cb_div.setText("Dividing selected elements");
    cb_div.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        cb_div_itemStateChanged(e);
      }
    });
    cb_size.setSelected(true);
    cb_size.setText("Element size");
    cb_size.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        cb_size_itemStateChanged(e);
      }
    });
    this.add(jPanel2,  BorderLayout.SOUTH);
    jPanel2.add(jButton1, null);
    this.add(jLabel1, BorderLayout.NORTH);
    this.add(jPanel1,  BorderLayout.CENTER);
    jPanel1.add(cb_type,  BorderLayout.CENTER);
    jPanel1.add(jPanel3,  BorderLayout.SOUTH);
    jPanel3.add(cb_size, null);
    jPanel3.add(tf_size, null);
    jPanel1.add(jPanel4, BorderLayout.NORTH);
    jPanel4.add(cb_div, null);
    this.validate();
  }


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

  void jButton1_actionPerformed(ActionEvent e) {
    try{
      if(cb_div.isSelected()) J3D.mesh(-1,-1); else J3D.mesh(cb_type.getSelectedIndex(),Float.parseFloat(tf_size.getText()));
    }catch(Exception ioe){ error(ioe); }
  }

  void cb_div_itemStateChanged(ItemEvent e) {
    cb_type.setEnabled(!cb_div.isSelected());
    tf_size.setEnabled(!cb_div.isSelected());
    cb_size.setEnabled(!cb_div.isSelected());
  }

  void cb_size_itemStateChanged(ItemEvent e) {
    tf_size.setEnabled(cb_size.isSelected());
  }
}


