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

public class _Node implements _Object, Serializable{
  private Canvas3D J3D;
  private PreProcessor PreP;
  public float x,y,z;
  public Color color;
  private String Id;
  public boolean selected = false;
  public Constraints constraint;
  public Loads load;
  public Float M,Ixx,Iyy,Izz,Ixy,Iyz,Ixz;
  public JTextField tx,ty,tz;
  public JComboBox cb_constraints,cb_loads;
  public JTextField tmass,txi,tyi,tzi,txyi,tyzi,txzi;

  private void writeObject(ObjectOutputStream out)  throws IOException{
    out.writeFloat(x);
    out.writeFloat(y);
    out.writeFloat(z);
    out.writeObject(color);
    out.writeObject(constraint);
    out.writeObject(load);
    out.writeObject(M);
    out.writeObject(Ixx);
    out.writeObject(Iyy);
    out.writeObject(Izz);
    out.writeObject(Ixy);
    out.writeObject(Iyz);
    out.writeObject(Ixz);
  }
  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{
    x = in.readFloat();
    y = in.readFloat();
    z = in.readFloat();
    color = (Color)in.readObject();
    selected = false;
    constraint = (Constraints)in.readObject();
    load = (Loads)in.readObject();
    M = (Float)in.readObject();
    Ixx = (Float)in.readObject();
    Iyy = (Float)in.readObject();
    Izz = (Float)in.readObject();
    Ixy = (Float)in.readObject();
    Iyz = (Float)in.readObject();
    Ixz = (Float)in.readObject();
  }


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

  public _Node() {
    this(0,0,0,Color.black);
  }
  public _Node(float xx, float yy, float zz) {
    this(xx,yy,zz,Color.black);
  }
  public _Node(float xx, float yy, float zz, Color cl) {
    this(xx,yy,zz,cl,null,null);
  }
  public _Node(float xx, float yy, float zz, Color cl, Constraints con, Loads ld) {
    x=xx;
    y=yy;
    z=zz;
    color=cl;
    constraint=con;
    load=ld;
  }
  public Object[] get_Array(Canvas3D j3d){
    int k = 0;
    Object[] arr = new Object[1+(j3d.SHOW_ID_NODE?1:0)+((j3d.SHOW_ID_CONSTRAINTS && constraint!=null)?1:0)+((j3d.SHOW_ID_LOADS && load!=null)?1:0)];
    arr[k++]=new shpPoint(x,y,z,selected ? j3d.SELECTCOLOR : color);
    if(j3d.SHOW_ID_NODE)arr[k++]=new shpOrientedText(/*Id+""*/get_Id(),x,y,z,Color.black);
    if(j3d.SHOW_ID_CONSTRAINTS && constraint!=null)arr[k++]=new shpOrientedText(constraint.name,x,y,z,constraint.color);
    if(j3d.SHOW_ID_LOADS && load!=null)arr[k++]=new shpOrientedText(load.name,x,y,z,load.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 "Node ID="+Id;}
  public MutableTreeNode get_TreeNode(){
    DefaultMutableTreeNode node = new DefaultMutableTreeNode(this);
    node.add(new DefaultMutableTreeNode("X,Y,Z - ("+x+","+y+","+z+")"));
    node.add(new DefaultMutableTreeNode("Constraint - "+constraint));
    node.add(new DefaultMutableTreeNode("Load - "+load));
    node.add(new DefaultMutableTreeNode("Mass - "+M));
    node.add(new DefaultMutableTreeNode("X inertia - "+Ixx));
    node.add(new DefaultMutableTreeNode("Y inertia - "+Iyy));
    node.add(new DefaultMutableTreeNode("Z inertia - "+Izz));
    node.add(new DefaultMutableTreeNode("XY inertia - "+Ixy));
    node.add(new DefaultMutableTreeNode("YZ inertia - "+Iyz));
    node.add(new DefaultMutableTreeNode("XZ inertia - "+Iyz));
    return node;
  }
  public void transform3D(Matrix3D t){
    if(!selected)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;
     PreP = pp;
     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());
     lb.setForeground(Color.blue);
     JButton b_upd = new JButton("Update");
     b_upd.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
         try{
           float xx1=Float.parseFloat(tx.getText());
           float yy1=Float.parseFloat(ty.getText());
           float zz1=Float.parseFloat(tz.getText());
           constraint=(Constraints)PreP.ConstDB.get(cb_constraints.getSelectedItem()+"");
           load=(Loads)PreP.LoadDB.get(cb_loads.getSelectedItem()+"");
           if(tmass.getText().trim().length()!=0)M=new Float(tmass.getText());else M=null;
           if(txi.getText().trim().length()!=0)Ixx=new Float(txi.getText());else Ixx=null;
           if(tyi.getText().trim().length()!=0)Iyy=new Float(tyi.getText());else Iyy=null;
           if(tzi.getText().trim().length()!=0)Izz=new Float(tzi.getText());else Izz=null;
           if(txyi.getText().trim().length()!=0)Ixy=new Float(txyi.getText());else Ixy=null;
           if(tyzi.getText().trim().length()!=0)Iyz=new Float(tyzi.getText());else Iyz=null;
           if(txzi.getText().trim().length()!=0)Ixz=new Float(txzi.getText());else Ixz=null;
           x=xx1; y=yy1; z=zz1;
           J3D.tree_reset();
           J3D.view_reset();
         }catch(Exception e1) { error(e1);}
       }
     });
     tx = new JTextField(x+"",3);
     ty = new JTextField(y+"",3);
     tz = new JTextField(z+"",3);
     cb_constraints = new JComboBox();
     cb_loads = new JComboBox();
     cb_constraints.addItem("");
     cb_loads.addItem("");
     for(Enumeration en = pp.ConstDB.keys(); en.hasMoreElements();)  cb_constraints.addItem(en.nextElement());
     for(Enumeration en = pp.LoadDB.keys(); en.hasMoreElements();)  cb_loads.addItem(en.nextElement());
     if(constraint!=null)cb_constraints.setSelectedItem(constraint.name);
     if(load!=null)cb_loads.setSelectedItem(load.name);
     tmass = new JTextField(M==null?"":M+"");
     txi = new JTextField(Ixx==null?"":Ixx+"");
     tyi = new JTextField(Iyy==null?"":Iyy+"");
     tzi = new JTextField(Izz==null?"":Izz+"");
     txyi = new JTextField(Ixy==null?"":Ixy+"");
     tyzi = new JTextField(Iyz==null?"":Iyz+"");
     txzi = new JTextField(Ixz==null?"":Ixz+"");
     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(p3,  BorderLayout.SOUTH);
     p3.add(b_upd, null);
     p2.add(p4, BorderLayout.NORTH);
     p4.add(p5, null);
     p5.add(new JLabel("Constraints"),  BorderLayout.WEST);
     p5.add(cb_constraints, BorderLayout.CENTER);
     p4.add(p6, null);
     p6.add(new JLabel("Loads"), BorderLayout.WEST);
     p6.add(cb_loads, BorderLayout.CENTER);
     p4.add(p7, null);
     p7.add(new JLabel("Mass"), BorderLayout.WEST);
     p7.add(tmass, BorderLayout.CENTER);
     p4.add(p8, null);
     p8.add(new JLabel("X inertia"), BorderLayout.WEST);
     p8.add(txi, BorderLayout.CENTER);
     p4.add(p9, null);
     p9.add(new JLabel("Y inertia"), BorderLayout.WEST);
     p9.add(tyi, BorderLayout.CENTER);
     p4.add(p10, null);
     p10.add(new JLabel("Z inertia"), BorderLayout.WEST);
     p10.add(tzi, BorderLayout.CENTER);
     p4.add(p11, null);
     p11.add(new JLabel("XY inertia"), BorderLayout.WEST);
     p11.add(txyi, BorderLayout.CENTER);
     p4.add(p12, null);
     p12.add(new JLabel("YZ inertia"), BorderLayout.WEST);
     p12.add(tyzi, BorderLayout.CENTER);
     p4.add(p13, null);
     p13.add(new JLabel("XZ inertia"),  BorderLayout.WEST);
     p13.add(txzi, BorderLayout.CENTER);
     return p;
  }
  public void error(Object st){
    JOptionPane.showMessageDialog(null,"Error: "+st,"Error!",JOptionPane.ERROR_MESSAGE);
    System.out.println("Error: "+st);
    if(st instanceof Exception) ((Exception)st).printStackTrace();
  }
  public _Node[] get_Nodes(){
    _Node[] arr = {this};
    return arr;
  }
  public _Object[] get_Elements(){return null;}

}