/*
 * 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_PLine extends JPanel {
  Canvas3D J3D;
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JPanel jPanel1 = new JPanel();
  Object[][] dat = new Object[100][3];
  String[] hdr = {"X","Y","Z"};
  private TableModel tableModel = new AbstractTableModel() {
    public int getColumnCount() { return 3; }
    public int getRowCount() { return 100; }
    public Object getValueAt(int row, int col){ return dat[row][col];}
    public String getColumnName(int col) {return hdr[col];}
    public boolean isCellEditable(int row, int col) {return true;}
    public void setValueAt(Object aValue, int row, int col) { dat[row][col]=aValue;}
    public Class getColumnClass(int c) {return new Float(0).getClass();}
  };
  JTable jTable1 = new JTable(tableModel);
  private JCheckBox jCheckBox1 = new JCheckBox();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JPanel jPanel2 = new JPanel();
  private JButton jButton1 = new JButton();
  private float[][] nodes;
  private JLabel jLabel1 = new JLabel();

  public PreProcessor_PLine(Canvas3D j3d) {
    try {
      J3D=j3d;
      jbInit();
    }catch(Exception e) { e.printStackTrace();}
  }
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    jScrollPane1.setPreferredSize(new Dimension(200, 200));
    jCheckBox1.setText("Close Polyline");
    jPanel1.setLayout(borderLayout2);
    jButton1.setText("add");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jLabel1.setForeground(Color.blue);
    jLabel1.setText("Polyline");
    this.add(jScrollPane1, BorderLayout.CENTER);
    this.add(jPanel1, BorderLayout.SOUTH);
    jPanel1.add(jCheckBox1,  BorderLayout.WEST);
    jPanel1.add(jPanel2, BorderLayout.CENTER);
    jPanel2.add(jButton1, null);
    this.add(jLabel1, BorderLayout.NORTH);
    jScrollPane1.getViewport().add(jTable1, null);
    this.validate();
  }


  public void error(Object st){
    JOptionPane.showMessageDialog(null,"Error: "+st,"Error!",JOptionPane.ERROR_MESSAGE);
    System.out.println("Error: "+st);
  }

  void jButton1_actionPerformed(ActionEvent e) {
    int n = 0;
    for(n=0; (n<dat.length && dat[n][0]!=null && dat[n][1]!=null && dat[n][2]!=null); n++){}
    nodes = new float[n][3];
    for(int i=0; i<n; i++){
      nodes[i][0]=Float.parseFloat(""+dat[i][0]);
      nodes[i][1]=Float.parseFloat(""+dat[i][1]);
      nodes[i][2]=Float.parseFloat(""+dat[i][2]);
    }
    if(n==0)return;
    J3D.add3D(new _PLine(nodes,jCheckBox1.isSelected()));
    J3D.repaint();
  }
}


