/*
 * 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.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Rectangle2D;
import java.util.*;

import javax.swing.tree.*;
import java.io.*;
import javax.swing.*;

import gui.*;
/**
 * Insert the type's description here.
 *
 * @author: Yuriy Mikhaylovskiy
 */

public class _Point extends _Object implements Serializable{
  public float x,y,z;
  public Color color;
  public boolean selected = false;
  private Canvas3D J3D;
  private JTextField tx,ty,tz;
  private Object[] arr = new Object[1];
  
  private void writeObject(ObjectOutputStream out)  throws IOException{
    out.writeFloat(x);
    out.writeFloat(y);
    out.writeFloat(z);
    out.writeObject(color);
    out.writeObject(Id);
  }
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
    x = in.readFloat();
    y = in.readFloat();
    z = in.readFloat();
    color = (Color)in.readObject();
    Id = (String)in.readObject();
    selected = false;
  }

  public void addSelectedObjects(Vector v) {
      if (selected) v.add(this);
  }

  public void reset() {};
  public void mesh(int type, float size){}
  public boolean delete(){return selected;}

  public _Point(boolean add) {
    this(0,0,0,Color.black,add);
  }
  
  public _Point(float xx, float yy, float zz) {
    this(xx,yy,zz,Color.black,false);
  }
  
  public _Point(Vector3D v) {
      this(v.x,v.y,v.z,Color.black,false);
    }

  public _Point(_Point p) {
      this(p.x,p.y,p.z,p.color,false);
    }
  
  public _Point(float xx, float yy, float zz, Color cl,boolean add) {
    this.add = add;
    x=xx;
    y=yy;
    z=zz;
    color=cl;
  }

  public _Point(float xx, float yy, float zz, Color cl) {
      this(xx,yy,zz,cl,false);
    }
  
  public Object[] get_Array(Canvas3D j3d){
    arr = new Object[1];
    arr[0] = new shpPoint(x,y,z,selected ? j3d.SELECTCOLOR : color);
    for (int i=0; i<arr.length; i++)
        ((shp)arr[i]).setShow(show);
    return arr;
  }
  
  public boolean isPickPoint(int x, int y, boolean shw) {
      boolean check = false;

      if (show == shw)
      for (int i=0; i< arr.length; i++) 
      	check = (((shp)arr[i]).isPickPoint(x,y) == true ? true : check);

      return check;
  }
  
  public boolean isPickPoint(Rectangle2D r, boolean shw) {
      boolean check = false;

      if (show == shw)
      for (int i=0; i< arr.length; i++) 
      	check = (((shp)arr[i]).isPickPoint(r) == true ? true : check);

      return check;
  }
  
  
  public boolean isSelected(){return selected;}
  public void Select(String st){ selected=st!=null; }
  public String toString(){ return "Point ID="+Id;}
  public MutableTreeNode get_TreeNode(){
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(this);
    return node;
  }
  public void transform3D(Matrix3D t){
    if(!selected || processed)return;
    float xx=x*t.xx+y*t.xy+z*t.xz+t.xo;
    float yy=x*t.yx+y*t.yy+z*t.yz+t.yo;
    float zz=x*t.zx+y*t.zy+z*t.zz+t.zo;
    x=xx; y=yy; z=zz;
  }
  public JPanel getEditPanel(Canvas3D j3d, PreProcessor pp){
      J3D = j3d;
      JPanel p = new JPanel(new BorderLayout());
      JPanel p1 = new JPanel(new GridLayout(2,4));
      JPanel p2 = new JPanel(new BorderLayout());
      JPanel p3 = new JPanel(new BorderLayout());
      JPanel p4 = new JPanel(new VerticalFlowLayout());
      JPanel p5 = new JPanel(new BorderLayout());
      JPanel p6 = new JPanel(new BorderLayout());
      JPanel p7 = new JPanel(new BorderLayout());
      JPanel p8 = new JPanel(new BorderLayout());
      JPanel p9 = new JPanel(new BorderLayout());
      JPanel p10 = new JPanel(new BorderLayout());
      JPanel p11 = new JPanel(new BorderLayout());
      JPanel p12 = new JPanel(new BorderLayout());
      JPanel p13 = new JPanel(new BorderLayout());
      JLabel lb = new JLabel("Edit - "+toString());
      if (!add) {
          tx = new JTextField(x+"",3);
          ty = new JTextField(y+"",3);
          tz = new JTextField(z+"",3);
      } else
      {
          tx = new JTextField("",3);
          ty = new JTextField("",3);
          tz = new JTextField("",3);
      }
      lb.setForeground(Color.blue);
      JButton b_upd = new JButton(add == true?"Add":"Update");
      b_upd.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
        }
      });

      p.add(p1, BorderLayout.CENTER);
      p1.add(new JLabel());
      p1.add(new JLabel("X"));
      p1.add(new JLabel("Y"));
      p1.add(new JLabel("Z"));
      p1.add(new JLabel("Point:"));
      p1.add(tx, null);
      p1.add(ty, null);
      p1.add(tz, null);
      p.add(lb, BorderLayout.NORTH);
      p.add(p2,  BorderLayout.SOUTH);
      p2.add(b_upd,null);

      tx.addKeyListener(new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
             char c = e.getKeyChar();

             if (c == '\n') {
                 jButton1_actionPerformed(null);
                 tx.requestFocus();
                 tx.selectAll();
             }
             else
             if (c == ',') {
                  ty.requestFocus();
                  ty.selectAll();
                  e.consume();
              }

          }
          
      });

      ty.addKeyListener(new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
              char c = e.getKeyChar();

              if (c == '\n') {
                  jButton1_actionPerformed(null);
                  tx.requestFocus();
;
              }
              else
              if (c == ',') {
                  tz.requestFocus();
                  tz.selectAll();
                  e.consume();
              }
          }
      });

      tz.addKeyListener(new KeyAdapter() {
          public void keyTyped(KeyEvent e) {
              char c = e.getKeyChar();

              if (c == '\n') {
                  jButton1_actionPerformed(null);
                  tx.requestFocus();
                  tx.selectAll();
              }
              else
              if (c == ',') {
                  tx.requestFocus();
                  tx.selectAll();
                  e.consume();
              }
          }
      });
      
      return p;
}
  
  
  void jButton1_actionPerformed(ActionEvent e) {
      _Point tmp = this;      
      float xx1=Float.parseFloat(tx.getText());
      float yy1=Float.parseFloat(ty.getText());
      float zz1=Float.parseFloat(tz.getText());
      x=xx1; y=yy1; z=zz1;
      
      if(add == true) {
          try {
            tmp = (_Point)this.clone();
            J3D.add3D(tmp);
          } catch (CloneNotSupportedException e1) {
              e1.printStackTrace();
          }
          tmp.add = false;
          tx.requestFocus();
          tx.selectAll();
      } else
          tmp.reset();
      
      J3D.tree_reset();
      J3D.view_reset();
  }
  
  public void requestFocus() {
      
      tx.requestFocus();
      tx.selectAll();
      
  }
  
  public _Node[] get_Nodes(){return null;}
  public _Object[] get_Elements(){return null;}
  public float distance(_Point p){ return (float)Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z)); }

  public Vector3D getVector() {
      Vector3D v = new Vector3D();
      
      v.x = this.x;
      v.y = this.y;
      v.z = this.z;
      
      return v;
  }
  
  public void toLocal(Vector3D translate, Matrix3D rotate) {

      Vector3D temp = new Vector3D(this.x, this.y, this.z);
      
      temp.add(temp,translate);
      temp.mult(rotate,temp);
      
      this.x = temp.x;
      this.y = temp.y;
      this.z = temp.z;
      
  }

  public void toGlobal(Vector3D translate, Matrix3D rotate) {

      Vector3D temp = new Vector3D(this.x, this.y, this.z);
      
      temp.sub(temp,translate);
      temp.transMult(rotate,temp);
      
      this.x = temp.x;
      this.y = temp.y;
      this.z = temp.z;
      
  }

  public _Object duplicate(Canvas3D out, boolean add){ 
      _Point o = null;
      try {
        o = (_Point)this.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    
    if (add) out.add3D(o);
    
      return o; 
      }

  public Vector3D getCenter() {
      Vector3D s = new Vector3D(x,y,z);
      
      return s;
  }
  
}

