/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Calculos_automaticos;

import java.beans.PropertyVetoException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author chuen
 */
public class prueba extends javax.swing.JInternalFrame {
    DefaultTreeModel model;
    DefaultMutableTreeNode selectednode;
    public prueba() {
        initComponents();
        model = (DefaultTreeModel) jTree1.getModel();
               try {
            this.setMaximum(true);
        } catch (PropertyVetoException ex) {
            Logger.getLogger(B_Composicion_puntasfilossup.class.getName()).log(Level.SEVERE, null, ex);
        }
    expandirtodo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popup = new javax.swing.JPopupMenu();
        general = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        Núcleo = new javax.swing.JMenuItem();
        Punta = new javax.swing.JMenuItem();
        Segmental_Estandard = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        Modificar = new javax.swing.JMenu();
        editar_datos = new javax.swing.JMenuItem();
        eliminar = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jTextField1 = new javax.swing.JTextField();
        txt2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        txt3 = new javax.swing.JTextField();
        nodo2 = new javax.swing.JButton();
        expandir = new javax.swing.JButton();
        path = new javax.swing.JTextField();
        ultimo = new javax.swing.JTextField();
        agregar = new javax.swing.JTextField();
        agregarenelpath = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        agregar_en_root = new javax.swing.JButton();
        recorrer = new javax.swing.JButton();

        popup.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        popup.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        popup.setMinimumSize(new java.awt.Dimension(10, 6));

        general.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        general.setText("Agregar ficha general");
        general.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generalActionPerformed(evt);
            }
        });
        popup.add(general);
        popup.add(jSeparator1);

        Núcleo.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        Núcleo.setText("Agregar ficha de núcleo");
        Núcleo.setToolTipText("");
        Núcleo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NúcleoActionPerformed(evt);
            }
        });
        popup.add(Núcleo);

        Punta.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        Punta.setText("Agregar ficha de punta de proyectil");
        popup.add(Punta);

        Segmental_Estandard.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        Segmental_Estandard.setText("Agregar ficha de descripción segmental estandar");
        popup.add(Segmental_Estandard);
        popup.add(jSeparator2);

        Modificar.setBackground(new java.awt.Color(255, 51, 0));
        Modificar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        Modificar.setText("Modificar datos");
        Modificar.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N

        editar_datos.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        editar_datos.setText("Editar datos");
        Modificar.add(editar_datos);

        eliminar.setFont(new java.awt.Font("Segoe UI", 1, 14)); // NOI18N
        eliminar.setText("Eliminar registro");
        Modificar.add(eliminar);

        popup.add(Modificar);

        popup.getAccessibleContext().setAccessibleParent(jTree1);

        setClosable(true);
        setMaximizable(true);
        setResizable(true);

        jLabel1.setText("jLabel1");

        jButton1.setText("Agregar otras fichas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTree1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("1001");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("General");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Nucleo");
        javax.swing.tree.DefaultMutableTreeNode treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Plataforma 1");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Plataforma 2");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("1002");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("General");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Muesca");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Punta de proyectil");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("1003");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("General");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Nucleo/1022");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setComponentPopupMenu(popup);
        jTree1.setExpandsSelectedPaths(false);
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jButton2.setText("Agregar pieza");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        nodo2.setText("Agregar Núcleo");
        nodo2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nodo2ActionPerformed(evt);
            }
        });

        expandir.setText("Expandir ramas");
        expandir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                expandirActionPerformed(evt);
            }
        });

        agregarenelpath.setText("agregar en el path");
        agregarenelpath.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarenelpathActionPerformed(evt);
            }
        });

        jLabel2.setText("Selection path");

        jLabel3.setText("getLastSelectedPathComponent");

        agregar_en_root.setText("Agregar en root");
        agregar_en_root.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregar_en_rootActionPerformed(evt);
            }
        });

        recorrer.setText("recorrer");
        recorrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recorrerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 280, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(expandir, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txt2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txt3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nodo2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(recorrer)
                        .addGap(33, 33, 33)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(path)
                    .addComponent(agregarenelpath, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(agregar)
                    .addComponent(ultimo)
                    .addComponent(jLabel2)
                    .addComponent(agregar_en_root, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(30, 30, 30))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(txt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addGap(45, 45, 45)
                        .addComponent(txt3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nodo2)
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(recorrer))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 313, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(expandir)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(path, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(16, 16, 16)
                        .addComponent(jLabel2)
                        .addGap(1, 1, 1)))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(ultimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(agregar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(agregarenelpath)))
                .addGap(18, 18, 18)
                .addComponent(agregar_en_root)
                .addContainerGap(244, Short.MAX_VALUE))
        );

        jScrollPane1.setViewportView(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 710, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
 //este agrega hojas a los nodos
        selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
 if(selectednode!=null){

 selectednode.insert (new DefaultMutableTreeNode(jTextField1.getText()),selectednode.getIndex(selectednode.getLastChild()));
 model.reload(selectednode);
 }        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//este crea nodos ppales
        
        DefaultTreeModel model = (DefaultTreeModel)jTree1.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
        DefaultMutableTreeNode c1 = new DefaultMutableTreeNode(txt2.getText());
        root.add(c1);                
        model.reload();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void nodo2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nodo2ActionPerformed
//este convierte en nodo una ultima rama
 selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
 if(selectednode!=null){
   DefaultMutableTreeNode c2 = new DefaultMutableTreeNode(txt3.getText());  
   selectednode.add(c2);
   model.reload();  

 }    
    }//GEN-LAST:event_nodo2ActionPerformed
private void expandirultimarama(){
    int ultimo =  jTree1.getRowCount();
       jTree1.expandRow(ultimo-1);

}
    private void expandirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_expandirActionPerformed
  expandirtodo();
    }//GEN-LAST:event_expandirActionPerformed
private void expandirtodo(){
         int j = jTree1.getRowCount();
       int i = 0;
       while(i < j) {
           jTree1.expandRow(i);
           i += 1;
           j = jTree1.getRowCount();
       }
}
    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
 //este convierte en nodo una ultima rama
 selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        path.setText(selectednode.toString());

 TreePath parentPath = jTree1.getSelectionPath();
ultimo.setText(parentPath.toString());
    }//GEN-LAST:event_jTree1MouseClicked

    private void generalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generalActionPerformed
selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        String Tipo_ficha = selectednode.toString()+"/General/Cod.General";
        selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
 if(selectednode!=null){

 selectednode.insert (new DefaultMutableTreeNode(Tipo_ficha),selectednode.getIndex(selectednode.getLastChild()));
 model.reload(selectednode);
 }        
 expandirultimarama();
    }//GEN-LAST:event_generalActionPerformed

    private void agregarenelpathActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarenelpathActionPerformed


DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();

DefaultMutableTreeNode parent = (DefaultMutableTreeNode)root.getChildAt(1);
model.insertNodeInto (new DefaultMutableTreeNode("root/N°/adentro"), parent, parent.getChildCount());

       

        
    }//GEN-LAST:event_agregarenelpathActionPerformed

    private void agregar_en_rootActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregar_en_rootActionPerformed
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();

model.insertNodeInto(new DefaultMutableTreeNode("nueva pieza"), root, root.getChildCount());
    }//GEN-LAST:event_agregar_en_rootActionPerformed

    private void recorrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recorrerActionPerformed
TreeNode raiz = (TreeNode)jTree1.getModel().getRoot();
        System.out.println("raiz"+ raiz );


System.out.println( raiz.toString());


if (raiz.getChildCount() >= 0) { 
    for (int i = 0; i < raiz.getChildCount(); i++) {
        if(raiz.getChildAt(i).toString().equals("Nucleo")){
            System.out.println("esto es un nucleo");   
        }
    }
}



    }//GEN-LAST:event_recorrerActionPerformed

    private void NúcleoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NúcleoActionPerformed
TreeNode raiz = (TreeNode)jTree1.getModel().getRoot();

selectednode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
  
Object cod = new Object();

cod = selectednode.toString();


////validacion para saber si es un nucleo
//if(selectednode.toString().equals("Nucleo")){
//            JOptionPane.showMessageDialog(null, "este es un núcleo, sí se pueden agregar plataformas");
//        }else{
//            JOptionPane.showMessageDialog(null, "Seleccione un núcleo para agregar plataformas");
//}

//Ver el código de ese nucleo
String string = cod.toString();
String[] parts = string.split("/");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

 JOptionPane.showMessageDialog(null, "el codigo es: " +part2);
    }//GEN-LAST:event_NúcleoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu Modificar;
    private javax.swing.JMenuItem Núcleo;
    private javax.swing.JMenuItem Punta;
    private javax.swing.JMenuItem Segmental_Estandard;
    private javax.swing.JTextField agregar;
    private javax.swing.JButton agregar_en_root;
    private javax.swing.JButton agregarenelpath;
    private javax.swing.JMenuItem editar_datos;
    private javax.swing.JMenuItem eliminar;
    private javax.swing.JButton expandir;
    private javax.swing.JMenuItem general;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
    private javax.swing.JButton nodo2;
    private javax.swing.JTextField path;
    private javax.swing.JPopupMenu popup;
    private javax.swing.JButton recorrer;
    private javax.swing.JTextField txt2;
    private javax.swing.JTextField txt3;
    private javax.swing.JTextField ultimo;
    // End of variables declaration//GEN-END:variables
}
