/*
 * 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.util.*;
/**
 * Insert the type's description here.
 *
 * @author: Yuriy Mikhaylovskiy
 */

public class ImpactGUI extends JFrame {
  String ver = "Impact GUI (2004.10.17)";
  PreProcessor pre = new PreProcessor();
  Processor processor = new Processor();
  PostProcessor post = new PostProcessor();
  Help help = new Help();
  Graph graph = new Graph(){
    void new_action() {
      if(post.time_step.getModel().getSize()<1){error("Can not read data!"); return;}
      Graph_add p = new Graph_add();
      int result = JOptionPane.showOptionDialog(this,p,"Create new Chart",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE,null,null,null);
      if(result==0){
        String st = p.FromTo.getText();
        while(st.indexOf('-')!=-1){
          int i = st.indexOf('-');
          String st1 = st.substring(0,i);
          String st2 = st.substring(i+1);
          StringTokenizer st_t1 = new StringTokenizer(st1,", ");
          StringTokenizer st_t2 = new StringTokenizer(st2,", ");
          String s1="";
          String s2="";
          String s3="";
          while(st_t1.hasMoreTokens())s1=st_t1.nextToken();
          s2=st_t2.nextToken();
          int i1 = Integer.parseInt(s1);
          int i2 = Integer.parseInt(s2);
          for(int j=i1+1; j<i2; j++)s3+=" "+j;
          st=st1+s3+" "+st2;
        }
        StringTokenizer st_t = new StringTokenizer(st,", ");
        while(st_t.hasMoreTokens()){
          String nd_el = st_t.nextToken();
          try{
            post.db_graph_preparation(p.AxisY.getSelectedIndex(),nd_el);
            graph.addXY(post.db_graph_tmp,post.db_graph_inf);
          }catch(Exception e1){error("Result for node/element '"+nd_el+"' not fount!");}
        }
      }else return;
    }
  };
  BorderLayout borderLayout1 = new BorderLayout();
  JTabbedPane TabbedPane = new JTabbedPane();


  public ImpactGUI() {
    try { jbInit(); }catch(Exception e) {e.printStackTrace();}
  }
  public static void main(String[] args) {
    //new ImpactGUIS();
    //System.out.println(java.awt.geom.Line2D.Float.ptLineDist(0,0,0,10,0,10));
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(Exception e) { e.printStackTrace(); }
    new ImpactGUI();
  }
  private void jbInit() throws Exception {
    this.setIconImage(new ImageIcon(ImpactGUI.class.getResource("Impact.gif")).getImage());
    getContentPane().setLayout(borderLayout1);
    super.setTitle(ver);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(50,50,screenSize.width-100, screenSize.height-100);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        pre.exit();
        processor.exit();
        post.exit();
        graph.exit();
        System.exit(0);
      }
    });
    show();
    this.getContentPane().add(TabbedPane, BorderLayout.CENTER);
    TabbedPane.add(pre, pre.ver);
    TabbedPane.add(processor, processor.ver);
    TabbedPane.add(post, post.ver);
    TabbedPane.add(graph, graph.ver);
    TabbedPane.add(help, help.ver);

  }
  public void setTitle(String st){
    super.setTitle(ver+"   "+st);
  }

}
