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

public class _PLine implements _Object, Serializable{
  public float[][] nodes;
  public Color color;
  public boolean close;
  private String Id;
  private boolean selected = false;
  private Canvas3D J3D;
  private Object[][] dat;
  private String[] hdr;
  private TableModel tableModel;
  private JTable table;
  private JCheckBox cb;

  private void writeObject(ObjectOutputStream out)  throws IOException{
    out.writeObject(nodes);
    out.writeObject(color);
    out.writeBoolean(close);
  }
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
    nodes=(float[][])in.readObject();
    color=(Color)in.readObject();
    close=in.readBoolean();
    selected = false;
  }
  public void mesh(int type, float size){}
  public boolean delete(){return selected;}

  public _PLine(float[][] p, boolean cl, Color c){
    nodes=p;
    close=cl;
    color=c;
  }
  public _PLine(float[][] p, boolean cl){
    this(p,cl,Color.black);
  }
  public Object[] get_Array(Canvas3D j3d){
    int n = nodes.length-1;
    if(close)n+=1;
    //if(n<1)n=0;
    Object[] arr = new Object[n];
    for(int i=0; i<nodes.length-1; i++){
      arr[i]=new shpLine(nodes[i][0],nodes[i][1],nodes[i][2],nodes[i+1][0],nodes[i+1][1],nodes[i+1][2], selected ? j3d.SELECTCOLOR : color);
    }
    if(close)arr[nodes.length-1]=new shpLine(nodes[0][0],nodes[0][1],nodes[0][2],nodes[nodes.length-1][0],nodes[nodes.length-1][1],nodes[nodes.length-1][2], selected ? j3d.SELECTCOLOR : color);
    return arr;
  }
  public String get_Id(){
    return Id;
  }
  public void set_Id(String id){
    Id=id;
  }
  public boolean isSelected(){return selected;}
  public void Select(String st){ selected=st!=null; }
  public String toString(){ return "Polyline ID="+Id;}
  public MutableTreeNode get_TreeNode(){
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(this);
    //node.add(new DefaultMutableTreeNode("Color"));
    for(int i=0; i<nodes.length; i++) node.add(new DefaultMutableTreeNode("X,Y,Z ("+nodes[i][0]+","+nodes[i][1]+","+nodes[i][2]+")"));
    node.add(new DefaultMutableTreeNode("Close "+close));
    return node;
  }
  public void transform3D(Matrix3D t){
    if(!selected)return;
    for(int i=0; i<nodes.length; i++){
      float xx=nodes[i][0]*t.xx+nodes[i][1]*t.xy+nodes[i][2]*t.xz+t.xo;
      float yy=nodes[i][0]*t.yx+nodes[i][1]*t.yy+nodes[i][2]*t.yz+t.yo;
      float zz=nodes[i][0]*t.zx+nodes[i][1]*t.zy+nodes[i][2]*t.zz+t.zo;
      nodes[i][0]=xx; nodes[i][1]=yy; nodes[i][2]=zz;
    }
  }
  public JPanel getEditPanel(Canvas3D j3d, PreProcessor pp){
    J3D=j3d;
    dat = new Object[100][3];
    hdr = new String[3];
    hdr[0]="X"; hdr[1]="Y"; hdr[2]="Z";
    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();}
    };
    table = new JTable(tableModel);
    cb = new JCheckBox();
    for(int i=0; i<nodes.length; i++) for(int j=0; j<3; j++)dat[i][j]=new Float(nodes[i][j]+"");
    JPanel p = new JPanel(new BorderLayout());
    JPanel p1 = new JPanel(new BorderLayout());
    JPanel p2 = new JPanel();
    JScrollPane sp = new JScrollPane();
    sp.setPreferredSize(new Dimension(180, 200));
    cb.setSelected(close);
    cb.setText("Close Polyline");
    JButton b_up = new JButton("Update");
    b_up.addActionListener(new ActionListener() {
      public void 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]);
        }
        close=cb.isSelected();
        J3D.tree_reset();
        J3D.view_reset();
      }
    });
    JLabel lb = new JLabel("Edit - "+toString());
    lb.setForeground(Color.blue);
    p.add(sp, BorderLayout.CENTER);
    p.add(p1, BorderLayout.SOUTH);
    p1.add(cb,  BorderLayout.WEST);
    p1.add(p2, BorderLayout.CENTER);
    p2.add(b_up, null);
    p.add(lb, BorderLayout.NORTH);
    sp.getViewport().add(table, null);
    p.validate();
    return p;
  }
  public _Node[] get_Nodes(){return null;}
  public _Object[] get_Elements(){return null;}
}