/*
 * 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 java.awt.event.*;
import java.io.*;
import run.*;
import j3d.*;
import java.util.*;
/**
 * Insert the type's description here.
 *
 * @author: Yuriy Mikhaylovskiy
 */

public class Processor_View extends JDialog {
  public String model_in = "";
  public Hashtable db_node = new Hashtable();
  public Hashtable db_element = new Hashtable();
  BorderLayout borderLayout1 = new BorderLayout();
  JToolBar jToolBar1 = new JToolBar();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4 = new JButton();
  JButton jButton5 = new JButton();
  JCheckBox jCheckBox1 = new JCheckBox();
  JCheckBox jCheckBox2 = new JCheckBox();
  public Canvas3D J3D = new Canvas3D(null);
  JPanel jPanel1 = new JPanel();
  VerticalFlowLayout VFlowLayout = new VerticalFlowLayout();
  JCheckBox jCheckBox3 = new JCheckBox();
  JCheckBox jCheckBox4 = new JCheckBox();
  JCheckBox jCheckBox5 = new JCheckBox();
  JCheckBox jCheckBox6 = new JCheckBox();
  JCheckBox jCheckBox7 = new JCheckBox();
  JCheckBox jCheckBox8 = new JCheckBox();
  JCheckBox jCheckBox9 = new JCheckBox();
  JCheckBox jCheckBox10 = new JCheckBox();
  JScrollPane jScrollPane1 = new JScrollPane();
  JList jList1 = new JList();

  public Processor_View(Frame f) {
    super(f,false);
    try { jbInit(); } catch(Exception e) { e.printStackTrace(); }
  }
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);
    setBounds(50,50,600,450);
    jButton1.setText("jButton1");
    jButton2.setText("jButton2");
    jButton3.setText("jButton3");
    jButton4.setText("jButton4");
    jButton5.setText("jButton5");
    jCheckBox1.setText("Nodes ");
    jCheckBox2.setText("Elements");
    this.setTitle("Model");
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowActivated(WindowEvent e) {
        this_windowActivated(e);
      }
      public void windowDeactivated(WindowEvent e) {
        this_windowDeactivated(e);
      }
    });
    jToolBar1.setFloatable(false);
    jPanel1.setLayout(VFlowLayout);
    jCheckBox3.setText("Nodes");
    jCheckBox4.setText("Elements");
    jCheckBox5.setText("Materials");
    jCheckBox6.setText("Constraints");
    jCheckBox7.setText("Loads");
    jCheckBox8.setText("Trackers");
    jCheckBox9.setText("Nodes label");
    jCheckBox10.setText("Elements label");
    this.getContentPane().add(jToolBar1, BorderLayout.NORTH);
    jToolBar1.add(jButton1, null);
    jToolBar1.add(jButton2, null);
    jToolBar1.add(jButton3, null);
    jToolBar1.add(jButton4, null);
    jToolBar1.add(jButton5, null);
    jToolBar1.add(jCheckBox1, null);
    jToolBar1.add(jCheckBox2, null);
    this.getContentPane().add(J3D, BorderLayout.CENTER);
    this.getContentPane().add(jPanel1, BorderLayout.WEST);
    jPanel1.add(jCheckBox3, null);
    jPanel1.add(jCheckBox9, null);
    jPanel1.add(jCheckBox4, null);
    jPanel1.add(jCheckBox10, null);
    jPanel1.add(jCheckBox5, null);
    jPanel1.add(jCheckBox6, null);
    jPanel1.add(jCheckBox7, null);
    jPanel1.add(jCheckBox8, null);
    jPanel1.add(jScrollPane1, null);
    jScrollPane1.getViewport().add(jList1, null);

  }

  void this_windowActivated(WindowEvent e) {
    J3D.remove_all();
    db_node.clear();
    db_element.clear();
    StringTokenizer stt = new StringTokenizer(model_in,"\n");
    String st;
    while (stt.hasMoreTokens()) {
      st = stt.nextToken().trim().toUpperCase();
      if(st.equals("NODES")){
        try{
          while (stt.hasMoreTokens()) {
            st = stt.nextToken().trim().toUpperCase();
            if(st.startsWith("#") || st.length()==0)continue;
            StringTokenizer stt1 = new StringTokenizer(st," =XYZ\t");
            db_node.put(stt1.nextToken(),new _Point(Float.parseFloat(stt1.nextToken()),Float.parseFloat(stt1.nextToken()),Float.parseFloat(stt1.nextToken())));
          }
        }catch(Exception e1){}
      }
    }
    stt = new StringTokenizer(model_in,"\n");
    while (stt.hasMoreTokens()) {
      st = stt.nextToken().trim().toUpperCase();
      if(st.startsWith("ELEMENTS")){
        try{
          while (stt.hasMoreTokens()) {
            st = stt.nextToken().trim().toUpperCase();
            if(st.startsWith("#") || st.length()==0 || st.startsWith("ELEMENTS"))continue;
            StringTokenizer stt1 = new StringTokenizer(st,"[]");
            String key = stt1.nextToken();
            key = key.substring(0,key.indexOf(" "));
            String st_nd = stt1.nextToken();
            StringTokenizer stt2 = new StringTokenizer(st_nd,", ");
            int[] arr = new int[stt2.countTokens()];
            int i=0;
            while (stt2.hasMoreTokens()){
              arr[i]=Integer.parseInt(stt2.nextToken());
              i++;
            }
            db_element.put(key,arr);
          }
        }catch(Exception e1){}
      }
    }
    for(Enumeration en = db_node.keys(); en.hasMoreElements() ;) {
      J3D.add3D((_Point)db_node.get(en.nextElement()));
    }
    for(Enumeration en = db_element.keys(); en.hasMoreElements() ;) {
      int[] arr = (int[])db_element.get(en.nextElement());
      if(arr.length==2)J3D.add3D(new _Line((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[1]+"")));
      else if(arr.length==3)J3D.add3D(new _Triangle((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[1]+""),(_Point)db_node.get(arr[2]+"")));
      else if(arr.length==4)J3D.add3D(new _Quad((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[1]+""),(_Point)db_node.get(arr[2]+""),(_Point)db_node.get(arr[3]+"")));
      else if(arr.length==8){
        J3D.add3D(new _Quad((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[1]+""),(_Point)db_node.get(arr[2]+""),(_Point)db_node.get(arr[3]+"")));
        J3D.add3D(new _Quad((_Point)db_node.get(arr[4]+""),(_Point)db_node.get(arr[5]+""),(_Point)db_node.get(arr[6]+""),(_Point)db_node.get(arr[7]+"")));
        J3D.add3D(new _Quad((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[1]+""),(_Point)db_node.get(arr[5]+""),(_Point)db_node.get(arr[4]+"")));
        J3D.add3D(new _Quad((_Point)db_node.get(arr[1]+""),(_Point)db_node.get(arr[2]+""),(_Point)db_node.get(arr[6]+""),(_Point)db_node.get(arr[5]+"")));
        J3D.add3D(new _Quad((_Point)db_node.get(arr[2]+""),(_Point)db_node.get(arr[3]+""),(_Point)db_node.get(arr[7]+""),(_Point)db_node.get(arr[6]+"")));
        J3D.add3D(new _Quad((_Point)db_node.get(arr[0]+""),(_Point)db_node.get(arr[3]+""),(_Point)db_node.get(arr[7]+""),(_Point)db_node.get(arr[4]+"")));
      }
    }
  }

  void this_windowDeactivated(WindowEvent e) {
    this.setVisible(false);
  }

}
