package gui;
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import run.*;

import uka.karmi.rmi.RemoteException;

import java.util.*;

import j3d.*;

import javax.swing.table.*;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

import jp.lang.DistributedRuntime;

import jp.sync.Barrier;

import jp.sync.BarrierFactory;

public class Processor extends JPanel {
   public static final String ver = "Processor (2005.03.17)";
   public Hashtable db_node = new Hashtable();
   public Hashtable db_node_constraints = new Hashtable();
   public Hashtable db_constraints = new Hashtable();
   public Hashtable db_node_loads = new Hashtable();
   public Hashtable db_loads = new Hashtable();
   public Hashtable db_element = new Hashtable();
   public Hashtable db_element_loads = new Hashtable();
   public String path = "";
   JDialog dialog;
   ImageIcon img_open = new ImageIcon(Processor.class.getResource("open.gif"));
   ImageIcon img_import = new ImageIcon(Processor.class.getResource("import.gif"));
   ImageIcon img_save = new ImageIcon(Processor.class.getResource("save.gif"));
   ImageIcon img_run = new ImageIcon(Processor.class.getResource("run.gif"));
   ImageIcon img_stop = new ImageIcon(Processor.class.getResource("stop.gif"));
   ImageIcon img_find = new ImageIcon(Processor.class.getResource("find.gif"));
   ImageIcon img_solid = new ImageIcon(Processor.class.getResource("solid.gif"));
   ImageIcon img_zoomin = new ImageIcon(Processor.class.getResource("zoomin.gif"));
   ImageIcon img_zoomout = new ImageIcon(Processor.class.getResource("zoomout.gif"));
   ImageIcon img_view_top = new ImageIcon(Processor.class.getResource("view_top.gif"));
   ImageIcon img_view_bottom = new ImageIcon(Processor.class.getResource("view_bottom.gif"));
   ImageIcon img_view_left = new ImageIcon(Processor.class.getResource("view_left.gif"));
   ImageIcon img_view_right = new ImageIcon(Processor.class.getResource("view_right.gif"));
   ImageIcon img_view_front = new ImageIcon(Processor.class.getResource("view_front.gif"));
   ImageIcon img_view_back = new ImageIcon(Processor.class.getResource("view_back.gif"));
   ImageIcon img_view_sw = new ImageIcon(Processor.class.getResource("view_sw.gif"));
   ImageIcon img_view_se = new ImageIcon(Processor.class.getResource("view_se.gif"));
   ImageIcon img_view_ne = new ImageIcon(Processor.class.getResource("view_ne.gif"));
   ImageIcon img_view_nw = new ImageIcon(Processor.class.getResource("view_nw.gif"));
   ImageIcon img_config = new ImageIcon(Processor.class.getResource("config.gif"));
   Thread tsolve;
   boolean isSolve = false;
   JCheckBox cb_node = new JCheckBox("Nodes label", true);
   JCheckBox cb_element = new JCheckBox("Elements label", true);
   JCheckBox cb_constraints = new JCheckBox("Constraints", true);
   JCheckBox cb_loads = new JCheckBox("Loads", true);
   JCheckBox cb_trackers = new JCheckBox("Trackers", true);
   JCheckBox cb_materials = new JCheckBox("Materials", true);
   Object[][] table_materials = new Object[0][4];
   String[] table_hdr_materials = {"Show", "Color", "Material"};
   private TableModel tableModel = new AbstractTableModel(){
      
      public int getColumnCount() {
         return 3;
      }
      public int getRowCount() {
         return gui.Processor.this.table_materials.length;
      }
      public Object getValueAt(int row, int col) {
         return gui.Processor.this.table_materials[row][col] + "";
      }
      public String getColumnName(int col) {
         return gui.Processor.this.table_hdr_materials[col];
      }
      public boolean isCellEditable(int row, int col) {
         return false;
      }
   };
   BorderLayout borderLayout1 = new BorderLayout();
   JSplitPane jSplitPane1 = new JSplitPane();
   JTextArea result = new JTextArea();
   JScrollPane jScrollPane1 = new JScrollPane();
   JPanel jPanel3 = new JPanel();
   BorderLayout borderLayout3 = new BorderLayout();
   JSplitPane jSplitPane2 = new JSplitPane();
   JPanel jPanel4 = new JPanel();
   JPanel jPanel5 = new JPanel();
   JScrollPane jScrollPane2 = new JScrollPane();
   JTextArea problem = new JTextArea();
   BorderLayout borderLayout4 = new BorderLayout();
   BorderLayout borderLayout5 = new BorderLayout();
   JToolBar jToolBar1 = new JToolBar();
   JButton b_zoomin = new JButton();
   JButton b_zoomout = new JButton();
   Canvas3D J3D = new Canvas3D(null);
   JButton b_viewbottom = new JButton();
   JButton b_viewright = new JButton();
   JButton b_viewfront = new JButton();
   JButton b_viewleft = new JButton();
   JButton b_viewtop = new JButton();
   JButton b_viewback = new JButton();
   JButton b_viewsw = new JButton();
   JButton b_viewse = new JButton();
   JButton b_viewne = new JButton();
   JButton b_viewnw = new JButton();
   JToolBar jToolBar2 = new JToolBar();
   JButton b_open = new JButton();
   JButton b_model = new JButton();
   JButton b_start = new JButton();
   JButton b_config = new JButton();
   JButton b_save = new JButton();
   JButton b_find = new JButton();
   JButton b_import = new JButton();
   JSlider js = new JSlider(JSlider.HORIZONTAL, 1, 8, 1);
   public Processor() {
      super();
      try {
         jbInit();
      }  catch (Exception e) {
         e.printStackTrace();
      }
   }
   private void jbInit() throws Exception {
      this.J3D.RENDERMODE = this.J3D.RENDERMODE_HIDE;
      this.setLayout(this.borderLayout1);
      this.jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
      this.jSplitPane1.setLastDividerLocation(0);
      this.jSplitPane1.setOneTouchExpandable(true);
      this.result.setEditable(false);
      this.jPanel3.setLayout(this.borderLayout3);
      this.jPanel4.setLayout(this.borderLayout4);
      this.jPanel5.setLayout(this.borderLayout5);
      this.jToolBar1.setFloatable(false);
      this.b_zoomin.setToolTipText("Zoom in");
      this.b_zoomin.setIcon(this.img_zoomin);
      this.b_zoomin.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_zoomin_actionPerformed(e);
         }
      });
      this.b_zoomout.setToolTipText("Zoom out");
      this.b_zoomout.setIcon(this.img_zoomout);
      this.b_zoomout.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_zoomout_actionPerformed(e);
         }
      });
      this.b_viewtop.setToolTipText("Top View");
      this.b_viewtop.setIcon(this.img_view_top);
      this.b_viewtop.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewtop_actionPerformed(e);
         }
      });
      this.b_viewbottom.setToolTipText("Bottom View");
      this.b_viewbottom.setIcon(this.img_view_bottom);
      this.b_viewbottom.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewbottom_actionPerformed(e);
         }
      });
      this.b_viewleft.setToolTipText("Left View");
      this.b_viewleft.setIcon(this.img_view_left);
      this.b_viewleft.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewleft_actionPerformed(e);
         }
      });
      this.b_viewright.setToolTipText("Right View");
      this.b_viewright.setIcon(this.img_view_right);
      this.b_viewright.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewright_actionPerformed(e);
         }
      });
      this.b_viewfront.setToolTipText("Front View");
      this.b_viewfront.setIcon(this.img_view_front);
      this.b_viewfront.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewfront_actionPerformed(e);
         }
      });
      this.b_viewback.setToolTipText("Back View");
      this.b_viewback.setIcon(this.img_view_back);
      this.b_viewback.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewback_actionPerformed(e);
         }
      });
      this.b_viewsw.setToolTipText("SW Isometric View");
      this.b_viewsw.setIcon(this.img_view_sw);
      this.b_viewsw.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewsw_actionPerformed(e);
         }
      });
      this.b_viewse.setToolTipText("SE Isometric View");
      this.b_viewse.setIcon(this.img_view_se);
      this.b_viewse.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewse_actionPerformed(e);
         }
      });
      this.b_viewne.setToolTipText("NE Isometric View");
      this.b_viewne.setIcon(this.img_view_ne);
      this.b_viewne.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewne_actionPerformed(e);
         }
      });
      this.b_viewnw.setToolTipText("NW Isometric View");
      this.b_viewnw.setIcon(this.img_view_nw);
      this.b_viewnw.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_viewnw_actionPerformed(e);
         }
      });
      this.jSplitPane2.setOneTouchExpandable(true);
      this.b_open.setToolTipText("Open model *.in");
      this.b_open.setIcon(this.img_open);
      this.b_open.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_open_actionPerformed(e);
         }
      });
      this.b_start.setToolTipText("Start / Stop");
      this.b_start.setIcon(this.img_run);
      this.b_start.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_start_actionPerformed(e);
         }
      });
      this.b_model.setToolTipText("Reload view");
      this.b_model.setIcon(this.img_solid);
      this.b_model.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_model_actionPerformed(e);
         }
      });
      this.jToolBar2.setFloatable(false);
      this.b_config.setToolTipText("Configure");
      this.b_config.setIcon(this.img_config);
      this.b_config.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_config_actionPerformed(e);
         }
      });
      this.b_save.setToolTipText("Save");
      this.b_save.setIcon(this.img_save);
      this.b_save.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_save_actionPerformed(e);
         }
      });
      this.b_find.setIcon(this.img_find);
      this.b_find.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_find_actionPerformed(e);
         }
      });
      this.b_import.setToolTipText("Import model (NASTRAN, GMSH)");
      this.b_import.setIcon(this.img_import);
      this.b_import.addActionListener(new java.awt.event.ActionListener(){
         
         public void actionPerformed(ActionEvent e) {
            b_import_actionPerformed(e);
         }
      });
      this.js.setMajorTickSpacing(1);
      this.js.setMinorTickSpacing(1);
      this.js.setPaintTicks(true);
      this.js.setPaintLabels(true);
      this.js.setSnapToTicks(true);
      this.js.addChangeListener(new ChangeListener(){
         
         public void stateChanged(ChangeEvent arg0) {
            js_stateChanged(arg0);
         }
      });
      this.add(this.jSplitPane1, BorderLayout.CENTER);
      this.jSplitPane1.add(this.jScrollPane1, JSplitPane.LEFT);
      this.jSplitPane1.add(this.jPanel3, JSplitPane.RIGHT);
      this.jPanel3.add(this.jSplitPane2, BorderLayout.CENTER);
      this.jSplitPane2.add(this.jPanel4, JSplitPane.LEFT);
      this.jPanel4.add(this.jScrollPane2, BorderLayout.CENTER);
      this.jPanel4.add(this.jToolBar2, BorderLayout.NORTH);
      this.jToolBar2.add(this.b_open, null);
      this.jToolBar2.add(this.b_import, null);
      this.jToolBar2.add(this.b_save, null);
      this.jToolBar2.addSeparator();
      this.jToolBar2.add(this.b_start, null);
      this.jToolBar2.addSeparator();
      this.jToolBar2.add(this.b_find, null);
      this.jToolBar2.add(this.b_model, null);
      this.jToolBar2.addSeparator();
      this.jToolBar2.add(this.js);
      this.jScrollPane2.getViewport().add(this.problem);
      this.jSplitPane2.add(this.jPanel5, JSplitPane.RIGHT);
      this.jPanel5.add(this.jToolBar1, BorderLayout.NORTH);
      this.jToolBar1.add(this.b_zoomin, null);
      this.jToolBar1.add(this.b_zoomout, null);
      this.jToolBar1.addSeparator();
      this.jToolBar1.add(this.b_viewtop, null);
      this.jToolBar1.add(this.b_viewbottom, null);
      this.jToolBar1.add(this.b_viewleft, null);
      this.jToolBar1.add(this.b_viewright, null);
      this.jToolBar1.add(this.b_viewfront, null);
      this.jToolBar1.add(this.b_viewback, null);
      this.jToolBar1.add(this.b_viewsw, null);
      this.jToolBar1.add(this.b_viewse, null);
      this.jToolBar1.add(this.b_viewne, null);
      this.jToolBar1.add(this.b_viewnw, null);
      this.jToolBar1.addSeparator();
      this.jToolBar1.add(this.b_config, null);
      this.jPanel5.add(this.J3D, BorderLayout.CENTER);
      this.jScrollPane1.getViewport().add(this.result);
      this.jSplitPane1.setDividerLocation(100);
      this.jSplitPane2.setDividerLocation(400);
   }
   void b_open_actionPerformed(ActionEvent e) {
      try {
         FileDialog fd = new FileDialog(getFrame(this), "Open model", FileDialog.LOAD);
         fd.setDirectory(".");
         fd.setFile("*.in");
         fd.show();
         this.path = fd.getDirectory() + fd.getFile();
         if (fd.getFile() != null) {
            RandomAccessFile in = new RandomAccessFile(this.path, "r");
            this.problem.setText("");
            String st;
            while ((st = in.readLine()) != null) this.problem.append(st + "\n");
            in.close();
            this.result.setText("Selected problem - " + this.path);
            show_model();
         }
      }  catch (Exception e1) {
         error(e1);
      }
   }
   void b_import_actionPerformed(ActionEvent e) {
      try {
         FileDialog fd = new FileDialog(getFrame(this), "Import model", FileDialog.LOAD);
         fd.setDirectory(".");
         fd.show();
         if (fd.getFile() != null) {
            this.path = fd.getDirectory() + fd.getFile().substring(0, fd.getFile().lastIndexOf(".")) + ".in";
            Translator translatorinstance = new Translator();
            translatorinstance.initialize(fd.getDirectory() + fd.getFile(), this.path);
            translatorinstance.post();
            RandomAccessFile in = new RandomAccessFile(this.path, "r");
            this.problem.setText("");
            String st;
            while ((st = in.readLine()) != null) this.problem.append(st + "\n");
            in.close();
            this.result.setText("Selected problem - " + this.path);
            show_model();
         }
      }  catch (Exception e1) {
         error(e1);
      }
   }
   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);
   }
   public static Frame getFrame(Component component) {
      for (; component != null && !(component instanceof Frame); component = component.getParent()) {
      }
      return (Frame)component;
   }
   void b_start_actionPerformed(ActionEvent e) {
      try {
         if (this.isSolve) {
            this.tsolve.interrupt();
            this.tsolve = null;
            this.isSolve = false;
            this.b_start.setIcon(this.img_run);
            this.b_open.setEnabled(true);
            this.b_import.setEnabled(true);
            this.js.setEnabled(true);
            this.result.append("\n*** Interrupted Solver ***");
            this.result.setCaretPosition(this.result.getText().length());
         } else {
            File f = new File(this.path);
            f.delete();
            RandomAccessFile o = new RandomAccessFile(this.path, "rw");
            o.writeBytes(this.problem.getText());
            o.close();
            System.setOut(new PrintStream(System.out){
               
               public void println(String x) {
                  super.println(x);
                  gui.Processor.this.result.append(x + "\n");
                  gui.Processor.this.result.setCaretPosition(gui.Processor.this.result.getText().length());
               }
            });
            System.setErr(new PrintStream(System.err){
               
               public void println(Object x) {
                  super.println(x);
                  gui.Processor.this.result.append(x + "\n");
                  gui.Processor.this.isSolve = false;
                  gui.Processor.this.b_start.setIcon(gui.Processor.this.img_run);
                  gui.Processor.this.b_open.setEnabled(true);
                  gui.Processor.this.b_import.setEnabled(true);
                  gui.Processor.this.js.setEnabled(true);
                  gui.Processor.this.result.append("\n*** Interrupted Solver ***");
                  gui.Processor.this.result.setCaretPosition(gui.Processor.this.result.getText().length());
               }
            });
            this.tsolve = new Thread(){
               
               public void run() {
                  gui.Processor.this.result.setText("");
                  int nr_CPU = DistributedRuntime.getMachineCnt();
                  if (nr_CPU > 0) {
                     Thread[] workers;
                     final ModelCluster[] nodes;
                     try {
                        System.out.println("********************************");
                        System.out.println("*** Cluster has been invoked ***");
                        System.out.println("********************************\n\n");
                        System.out.println("Running on " + nr_CPU + " clients\n\n");
                        nodes = new ModelCluster[nr_CPU];
                        System.out.println("*** Creating cluster processes ***");
                        for (int n = 0; n < nr_CPU; n++) {
                           nodes[n] =  /** @at n */ new ModelCluster(jp.lang.Node.getNode(n), nr_CPU, n);
                        }
                        final Barrier barrier = BarrierFactory.createBarrier(nodes.length);
                        final String filename = gui.Processor.this.path;
                        workers = new Thread[nr_CPU];
                        for (int n = 0; n < workers.length; n++) {
                           final ModelCluster node = nodes[n];
                           workers[n] = new Thread(new Runnable(){
                              
                              public void run() {
                                 try {
                                    node.initialize(gui.Processor.this.path, nodes);
                                    node.assembleMassMatrix();
                                    node.setInitialConditions();
                                    barrier.sync();
                                    node.passReferences();
                                    barrier.sync();
                                    node.determineDistribution();
                                    node.referenceNodes();
                                    barrier.sync();
                                    node.optimize();
                                    node.solve(barrier);
                                 }  catch (Exception e) {
                                    System.out.println("Hej");
                                    e.printStackTrace();
                                    return;
                                 }
                              }
                           });
                        }
                        System.out.println("*** Starting the solution ***");
                        System.out.println("See terminal window for printout");
                        for (int n = 0; n < workers.length; n++) {
                           workers[n].start();
                        }
                        long stime = System.currentTimeMillis();
                        for (int n = 0; n < workers.length; n++) {
                           try {
                              workers[n].join();
                           }  catch (InterruptedException e) {
                              System.out.println("Shutting down clients");
                              for (int m = 0; m < workers.length; m++) nodes[m].setTerminate(true);
                           }
                        }
                        System.out.println("Solving took " + (System.currentTimeMillis() - stime) + " ms");
                     }  catch (Error e) {
                        e.printStackTrace();
                        return;
                     }
                  } else {
                     nr_CPU = gui.Processor.this.js.getValue();
                     try {
                        System.out.println("*******************************");
                        System.out.println("*** Solver has been invoked ***");
                        System.out.println("*******************************\n\n");
                        System.out.println("Running on " + nr_CPU + " clients\n\n");
                        ModelSmp siminstance = new ModelSmp(nr_CPU);
                        System.out.println("Processing file: " + gui.Processor.this.path);
                        System.out.println("*** Initializing ***");
                        siminstance.initialize(gui.Processor.this.path);
                        System.out.println("*** Assembling the Mass Matrix ***");
                        siminstance.assembleMassMatrix();
                        System.out.println("*** Setting Initial Conditions ***");
                        siminstance.setInitialConditions();
                        long stime = System.currentTimeMillis();
                        System.out.println("*** Initiating Solver ***");
                        try {
                           siminstance.solve();
                        }  catch (InterruptedException e1) {
                        }
                        System.out.println("Solving took " + (System.currentTimeMillis() - stime) + " ms");
                        siminstance.post();
                     }  catch (Error e) {
                        System.out.println(e);
                        return;
                     } catch (RemoteException e) {
                        e.printStackTrace();
                     }
                  }
                  gui.Processor.this.isSolve = false;
                  gui.Processor.this.b_start.setIcon(gui.Processor.this.img_run);
                  gui.Processor.this.b_open.setEnabled(true);
                  gui.Processor.this.b_import.setEnabled(true);
                  gui.Processor.this.js.setEnabled(true);
                  gui.Processor.this.result.append("*** End ***");
                  gui.Processor.this.result.setCaretPosition(gui.Processor.this.result.getText().length());
               }
            };
            this.tsolve.setPriority(this.tsolve.MIN_PRIORITY);
            this.tsolve.start();
            this.isSolve = true;
            this.b_start.setIcon(this.img_stop);
            this.b_open.setEnabled(false);
            this.b_import.setEnabled(false);
            this.js.setEnabled(false);
         }
      }  catch (IOException e1) {
         error(e1);
         e1.printStackTrace();
         this.result.append("\n\n" + e1);
      }
   }
   public void show_model() {
      this.J3D.remove_all();
      this.db_node.clear();
      this.db_element.clear();
      this.db_constraints.clear();
      this.db_node_constraints.clear();
      this.db_loads.clear();
      this.db_node_loads.clear();
      this.db_element_loads.clear();
      StringTokenizer stt = new StringTokenizer(this.problem.getText().replaceAll("\t", " "), "\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");
                  String key = stt1.nextToken();
                  this.db_node.put(key, new _Point(Float.parseFloat(stt1.nextToken()), Float.parseFloat(stt1.nextToken()), Float.parseFloat(stt1.nextToken())));
                  if (st.indexOf("CONSTRAINT =") != -1) {
                     String st1 = st.substring(st.indexOf("CONSTRAINT") + 10);
                     stt1 = new StringTokenizer(st1, " \t");
                     stt1.nextToken();
                     this.db_node_constraints.put(key, stt1.nextToken());
                  }
                  if (st.indexOf("LOAD =") != -1) {
                     String st1 = st.substring(st.indexOf("LOAD") + 4);
                     stt1 = new StringTokenizer(st1, " \t");
                     stt1.nextToken();
                     this.db_node_loads.put(key, stt1.nextToken());
                  }
               }
            }  catch (Exception e1) {
            }
         }
      }
      stt = new StringTokenizer(this.problem.getText(), "\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 = new StringTokenizer(key, " \t").nextToken();
                  String st_nd = stt1.nextToken();
                  String st_load = stt1.nextToken();
                  StringTokenizer stt2 = new StringTokenizer(st_nd, ", \t");
                  int[] arr = new int[stt2.countTokens()];
                  int i = 0;
                  while (stt2.hasMoreTokens()) {
                     arr[i] = Integer.parseInt(stt2.nextToken());
                     i++;
                  }
                  this.db_element.put(key, arr);
                  if (st_load.toUpperCase().indexOf("LOAD") != -1) {
                     st_load = st_load.substring(st_load.toUpperCase().indexOf("LOAD"));
                     StringTokenizer stt3 = new StringTokenizer(st_load, "= \t");
                     stt3.nextToken();
                     this.db_element_loads.put(key, stt3.nextToken());
                  }
               }
            }  catch (Exception e1) {
            }
         }
      }
      stt = new StringTokenizer(this.problem.getText(), "\n");
      while (stt.hasMoreTokens()) {
         st = stt.nextToken().trim().toUpperCase();
         if (st.toUpperCase().indexOf("BOUNDARY_CONDITION") != -1 || st.toUpperCase().indexOf("RIGID_BODY") != -1) {
            try {
               while (stt.hasMoreTokens()) {
                  st = stt.nextToken().trim().toUpperCase();
                  if (st.startsWith("#") || st.length() == 0) continue;
                  if (st.startsWith("NODES") || st.startsWith("ELEMENTS") || st.startsWith("CONTROLS") || st.startsWith("TRACKERS") || st.startsWith("MATERIALS") || st.startsWith("LOADS")) break;
                  StringTokenizer stt1 = new StringTokenizer(st, " \t");
                  String key = stt1.nextToken();
                  this.db_constraints.put(key, key);
               }
            }  catch (Exception e1) {
            }
         }
      }
      stt = new StringTokenizer(this.problem.getText(), "\n");
      while (stt.hasMoreTokens()) {
         st = stt.nextToken().trim().toUpperCase();
         if (st.toUpperCase().indexOf("LOADS") != -1) {
            try {
               while (stt.hasMoreTokens()) {
                  st = stt.nextToken().trim().toUpperCase();
                  if (st.startsWith("#") || st.length() == 0) continue;
                  if (st.startsWith("NODES") || st.startsWith("ELEMENTS") || st.startsWith("CONTROLS") || st.startsWith("TRACKERS") || st.startsWith("MATERIALS") || st.indexOf("BOUNDARY_CONDITION") != -1 || st.indexOf("RIGID_BODY") != -1) break;
                  StringTokenizer stt1 = new StringTokenizer(st, " \t");
                  String key = stt1.nextToken();
                  this.db_loads.put(key, key);
               }
            }  catch (Exception e1) {
            }
         }
      }
      for (Enumeration en = this.db_node.keys(); en.hasMoreElements(); ) {
         String key = en.nextElement() + "";
         _Point p = (_Point)this.db_node.get(key);
         if (this.cb_node.isSelected()) this.J3D.add3D(new _OrientedText(key, p.x, p.y, p.z));
         this.J3D.add3D(p);
      }
      if (this.cb_constraints.isSelected()) for (Enumeration en = this.db_node_constraints.keys(); en.hasMoreElements(); ) {
         String key = en.nextElement() + "";
         String text = this.db_constraints.get((this.db_node_constraints.get(key) + "").toUpperCase()) + "";
         _Point p = (_Point)this.db_node.get(key);
         this.J3D.add3D(new _OrientedText(text, p.x, p.y, p.z, Color.blue));
      }
      if (this.cb_loads.isSelected()) {
         for (Enumeration en = this.db_node_loads.keys(); en.hasMoreElements(); ) {
            String key = en.nextElement() + "";
            String text = this.db_loads.get((this.db_node_loads.get(key) + "").toUpperCase()) + "";
            _Point p = (_Point)this.db_node.get(key);
            this.J3D.add3D(new _OrientedText(text, p.x, p.y, p.z, Color.red));
         }
         for (Enumeration en = this.db_element_loads.keys(); en.hasMoreElements(); ) {
            String key = "" + en.nextElement();
            int[] arr = (int[])this.db_element.get(key);
            String val = this.db_element_loads.get(key) + "";
            if (arr.length == 3) {
               _Point p1 = (_Point)this.db_node.get(arr[0] + "");
               _Point p2 = (_Point)this.db_node.get(arr[1] + "");
               _Point p3 = (_Point)this.db_node.get(arr[2] + "");
               this.J3D.add3D(new _Triangle(p1, p2, p3));
               if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(val, (p1.x + p2.x + p3.x) / 3, (p1.y + p2.y + p3.y) / 3, (p1.z + p2.z + p3.z) / 3, Color.red));
            } else if (arr.length == 4) {
               _Point p1 = (_Point)this.db_node.get(arr[0] + "");
               _Point p2 = (_Point)this.db_node.get(arr[1] + "");
               _Point p3 = (_Point)this.db_node.get(arr[2] + "");
               _Point p4 = (_Point)this.db_node.get(arr[3] + "");
               this.J3D.add3D(new _Quad(p1, p2, p3, p4));
               if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(val, (p1.x + p2.x + p3.x + p4.x) / 4, (p1.y + p2.y + p3.y + p4.y) / 4, (p1.z + p2.z + p3.z + p4.z) / 4, Color.red));
            }
         }
      }
      for (Enumeration en = this.db_element.keys(); en.hasMoreElements(); ) {
         String key = "" + en.nextElement();
         int[] arr = (int[])this.db_element.get(key);
         if (arr.length == 2) {
            _Point p1 = (_Point)this.db_node.get(arr[0] + "");
            _Point p2 = (_Point)this.db_node.get(arr[1] + "");
            this.J3D.add3D(new _Line(p1, p2));
            if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(key, (p1.x + p2.x) / 2, (p1.y + p2.y) / 2, (p1.z + p2.z) / 2, Color.green));
         } else if (arr.length == 3) {
            _Point p1 = (_Point)this.db_node.get(arr[0] + "");
            _Point p2 = (_Point)this.db_node.get(arr[1] + "");
            _Point p3 = (_Point)this.db_node.get(arr[2] + "");
            this.J3D.add3D(new _Triangle(p1, p2, p3));
            if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(key, (p1.x + p2.x + p3.x) / 3, (p1.y + p2.y + p3.y) / 3, (p1.z + p2.z + p3.z) / 3, Color.green));
         } else if (arr.length == 4) {
            _Point p1 = (_Point)this.db_node.get(arr[0] + "");
            _Point p2 = (_Point)this.db_node.get(arr[1] + "");
            _Point p3 = (_Point)this.db_node.get(arr[2] + "");
            _Point p4 = (_Point)this.db_node.get(arr[3] + "");
            this.J3D.add3D(new _Quad(p1, p2, p3, p4));
            if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(key, (p1.x + p2.x + p3.x + p4.x) / 4, (p1.y + p2.y + p3.y + p4.y) / 4, (p1.z + p2.z + p3.z + p4.z) / 4, Color.green));
         } else if (arr.length == 8) {
            _Point p1 = (_Point)this.db_node.get(arr[0] + "");
            _Point p2 = (_Point)this.db_node.get(arr[1] + "");
            _Point p3 = (_Point)this.db_node.get(arr[2] + "");
            _Point p4 = (_Point)this.db_node.get(arr[3] + "");
            _Point p5 = (_Point)this.db_node.get(arr[4] + "");
            _Point p6 = (_Point)this.db_node.get(arr[5] + "");
            _Point p7 = (_Point)this.db_node.get(arr[6] + "");
            _Point p8 = (_Point)this.db_node.get(arr[7] + "");
            this.J3D.add3D(new _Quad(p1, p2, p3, p4));
            this.J3D.add3D(new _Quad(p5, p6, p7, p8));
            this.J3D.add3D(new _Quad(p1, p2, p6, p5));
            this.J3D.add3D(new _Quad(p2, p3, p7, p6));
            this.J3D.add3D(new _Quad(p3, p4, p8, p7));
            this.J3D.add3D(new _Quad(p1, p4, p8, p5));
            if (this.cb_element.isSelected()) this.J3D.add3D(new _OrientedText(key, (p1.x + p2.x + p3.x + p4.x + p5.x + p6.x + p7.x + p8.x) / 8, (p1.y + p2.y + p3.y + p4.y + p5.y + p6.y + p7.y + p8.y) / 8, (p1.z + p2.z + p3.z + p4.z + p5.z + p6.z + p7.z + p8.z) / 8, Color.green));
         }
      }
   }
   void b_zooma_actionPerformed(ActionEvent e) {
      this.J3D.view_all();
   }
   void b_zoomin_actionPerformed(ActionEvent e) {
      this.J3D.view_scale(1.2F);
   }
   void b_zoomout_actionPerformed(ActionEvent e) {
      this.J3D.view_scale(0.8F);
   }
   void b_viewtop_actionPerformed(ActionEvent e) {
      this.J3D.view_top();
   }
   void b_viewbottom_actionPerformed(ActionEvent e) {
      this.J3D.view_bottom();
   }
   void b_viewleft_actionPerformed(ActionEvent e) {
      this.J3D.view_left();
   }
   void b_viewright_actionPerformed(ActionEvent e) {
      this.J3D.view_right();
   }
   void b_viewfront_actionPerformed(ActionEvent e) {
      this.J3D.view_front();
   }
   void b_viewback_actionPerformed(ActionEvent e) {
      this.J3D.view_back();
   }
   void b_viewsw_actionPerformed(ActionEvent e) {
      this.J3D.view_sw();
   }
   void b_viewse_actionPerformed(ActionEvent e) {
      this.J3D.view_se();
   }
   void b_viewne_actionPerformed(ActionEvent e) {
      this.J3D.view_ne();
   }
   void b_viewnw_actionPerformed(ActionEvent e) {
      this.J3D.view_nw();
   }
   void b_model_actionPerformed(ActionEvent e) {
      show_model();
   }
   void b_config_actionPerformed(ActionEvent e) {
      JPanel p = new JPanel(new VerticalFlowLayout());
      p.add(this.cb_node);
      p.add(this.cb_element);
      p.add(this.cb_constraints);
      p.add(this.cb_loads);
      p.add(this.cb_trackers);
      p.add(this.cb_materials);
      JOptionPane.showMessageDialog(this, p, "Show", JOptionPane.PLAIN_MESSAGE, null);
      show_model();
   }
   void b_save_actionPerformed(ActionEvent e) {
      try {
         FileDialog fd = new FileDialog(getFrame(this), "Save model as", FileDialog.SAVE);
         fd.setDirectory(".");
         fd.setFile("*.in");
         fd.show();
         String st = fd.getDirectory() + fd.getFile();
         if (fd.getFile() != null) {
            if (!st.endsWith(".in")) st += ".in";
            File f = new File(st);
            f.delete();
            RandomAccessFile o = new RandomAccessFile(st, "rw");
            o.writeBytes(this.problem.getText());
            o.close();
            this.result.append("\nSaved problem as - " + st + "\n");
            this.path = st;
         }
      }  catch (Exception e1) {
         error(e1);
      }
   }
   void b_find_actionPerformed(ActionEvent e) {
      if (this.dialog == null) this.dialog = new JDialog(getFrame(this));
      this.dialog.setTitle("Find/Replace");
      this.dialog.getContentPane().removeAll();
      this.dialog.getContentPane().add(new Processor_Find(this.problem));
      this.dialog.pack();
      this.dialog.setVisible(true);
   }
   void js_stateChanged(ChangeEvent arg) {
      this.result.setText("Number of CPU:s set to:" + this.js.getValue());
   }
   public void exit() {
      System.out.println("Processor finalize.");
   }
}
