package cl.jtv;
/*
 * NodeTreeTrace.java
 *
 * Created on 9 de octubre de 2001, 02:46 AM
 */


/**
 *
 * @author  Marco Mora Godoy
 * @version
 */
//import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.*;
import javax.swing.JTree;
import java.util.*;


public class NodeTreeTrace extends javax.swing.tree.DefaultMutableTreeNode implements Runnable{
    public Maquina maquina;
    public ContextoSubmaquina contextoSubmaquina;
    public Maquina plyMachine,parentPlayMachine;
    public TreeNode[] parentTreePath;
    //public boolean contextChange=false;
    public static Boolean contextChange=Boolean.FALSE;
    public static Boolean runningValueChange=Boolean.FALSE;
    public boolean flagAOT0=true;
    //public boolean isSetRoot=false;
    public NodeTreeTrace parentRootTreeTrace;
    public Cinta arrayOfTapes0[];
    public Cinta arrayOfTapes1[];
    public DiccionarioMetavalores dicMetavalores;
    Thread thread;
    private ThreadGroup traceGroup;
    //private TestTree testTree;
    private Maquina machine,machine0,auxMachine,auxMachine2;
    private Cinta tape;
    public Cinta arrayOfTapes[];
    public Simbolo simboloEscrito;
    private SharedInteger numOfNodes;  //Objeto compartido
    private boolean isRoot;
    private int I;
    private NodeTreeTrace rootTreeTrace;
    public NodeTreeTrace choosedNodeND=null;
    //private DefaultMutableTreeNode rootTreeTrace;
    private DefaultTreeModel treeModel;
    private JTree jtree;
    private int iconValue;
    int estado=Maquina.ESTADO_DORMIDA;
    //private static int numOfNodes=0;
    
    public DefaultMutableTreeNode realTree;
    /** Creates new NodeTreeTrace */
    public NodeTreeTrace(String name,SharedInteger nof,boolean _isRoot,NodeTreeTrace _rootTreeTrace,DefaultTreeModel _treeModel,JTree _jtree) {
        super(name);
        
        numOfNodes = nof;
        thread = new Thread(this);
        isRoot = _isRoot;
        I = 0;
        rootTreeTrace = _rootTreeTrace;
        treeModel = _treeModel;
        this.dicMetavalores = new DiccionarioMetavalores();
        jtree = _jtree;
    }
    
    
    public NodeTreeTrace(SharedInteger nof,boolean _isRoot,NodeTreeTrace _rootTreeTrace,DefaultTreeModel _treeModel,JTree _jtree) {
        super();
        
        //testTree = _testTree;
        numOfNodes = nof;
        thread = new Thread(this);
        isRoot = _isRoot;
        I = 0;
        rootTreeTrace = _rootTreeTrace;
        treeModel = _treeModel;
        jtree = _jtree;
        //this.setUserObject(" ");
    }
    
    public NodeTreeTrace(){
        
        super();
        //this.setUserObject(" ");
    }
    
    
    
    
    public NodeTreeTrace(Object child){
        
        super(child);
        if(child instanceof NodeTreeTrace){
            this.setJTree(((NodeTreeTrace)child).getJTree());
            this.setTreeModel(((NodeTreeTrace)child).getTreeModel());
            this.setRootTreeTrace(((NodeTreeTrace)child).getRootTreeTrace());
        }
        
        //this.setUserObject(" ");
        
    }
    
    public void setNumOfNodes(SharedInteger _numOfNodes){
        
        numOfNodes = _numOfNodes;
    }
    
    public  SharedInteger getNumOfNodes(){
        
        return numOfNodes;
    }
    
    public void setRootTreeTrace(NodeTreeTrace _rootTreeTrace){
        
        rootTreeTrace = _rootTreeTrace;
        
    }
    
    public void setTreeModel(DefaultTreeModel _treeModel){
        
        treeModel = _treeModel;
        
    }
    
    public void setJTree(JTree _jtree){
        jtree = _jtree;
        
    }
    
    
    public NodeTreeTrace getRootTreeTrace(){
        
        return rootTreeTrace;
        
    }
    
    public DefaultTreeModel getTreeModel(){
        
        return treeModel;
        
    }
    
    public JTree getJTree(){
        
        return jtree;
        
    }
    
    public void setMachine(Maquina _machine){
        
        machine = machine0 = _machine;
        
    }
    
    public Maquina getMachine(){
        
        return machine;
    }
    
    public Maquina getMachine0(){
        
        return machine0;
    }
    
    public void setTape(Cinta _tape){
        
        tape = _tape;
    }
    
    public Cinta getTape(){
        
        return tape;
    }
    
    public void setArrayOfTapes(Cinta _arrayOfTapes[]){
        
        arrayOfTapes = _arrayOfTapes;
    }
    
    public Cinta[] getArrayOfTapes(){
        
        //return arrayOfTapes;
        Cinta[] arrayOut = new Cinta[arrayOfTapes.length];
        for(int i=0;i<arrayOfTapes.length;i++)
            arrayOut[i] = new Cinta(arrayOfTapes[i],true);
        
        return arrayOut;
    }
    
    public void setIconValue(int _iconValue){
        
        iconValue = _iconValue;
    }
    
    public int getIconValue(){
        
        return iconValue;
        
    }
    
    public void start(){
        //thread = new Thread(this);
        ////System.out.println("START");
        I = 0;
        this.auxMachine = this.machine;
        thread.start();
    }
    
    
    
    public void start(javax.swing.tree.DefaultMutableTreeNode _selectedNode){
        
        /*NodeTreeTrace selectedNode = (NodeTreeTrace) _selectedNode;
        NodeTreeTrace rootNode = selectedNode.getMyRoot();
         
        arrayOfTapes = rootNode.getArrayOfTapes();
        for(int i=0;i<arrayOfTapes.length;i++)
           arrayOfTapes[i].setValoresIniciales();
         
        NodeTreeTrace auxNode = rootNode;
         while(true){
              if(!auxNode.isRoot())
                    auxNode.getMachine().accion(arrayOfTapes);
                    if(auxNode==selectedNode) break;
                    auxNode = (NodeTreeTrace)auxNode.getNextNode();
         }
         */
        I = this.getIndex(_selectedNode)+1;
        ////System.out.println("I==" + I);
        //I = 5;
        //rootTreeTrace = (NodeTreeTrace) _selectedNode;
        //this.machine = ((NodeTreeTrace)_selectedNode).getMachine().proxima;
        this.auxMachine = ((NodeTreeTrace)_selectedNode).getMachine().proxima;
        thread.start();
        
    }
    
    public void start2(javax.swing.tree.DefaultMutableTreeNode _selectedNode){
        
        rootTreeTrace = (NodeTreeTrace) _selectedNode;
        this.auxMachine = this.machine;
        thread.start();
        
    }
    
    public void start(javax.swing.tree.DefaultMutableTreeNode childNode,javax.swing.tree.DefaultMutableTreeNode parentNode){
        
        I = parentNode.getIndex(childNode)+1;
        rootTreeTrace = (NodeTreeTrace) childNode;
        this.auxMachine = this.machine;
        thread.start();
        
    }
    
    public void join() throws InterruptedException{
        
        thread.join();
        
    }
    
    
    
    public void stop(){
        //System.out.println("STOP");
        //TODO
        //thread.stop();
        thread = null;
    }
    
    public void yield(){
        //System.out.println("YIELD");
        thread.yield();
    }
    
    public void suspend(){
        //System.out.println("SUSPEND");
        //TODO
        //thread.suspend();
    }
    
    public void resume(){
        //System.out.println("RESUME");
        //TODO
        //thread.resume();
    }
    
    public void setThread(Thread _thread){
        
        thread = _thread;
    }
    
    public synchronized void waitUntilCondition(int limON) throws InterruptedException{
        
        if(numOfNodes.getValue()>1){
            this.wait();
        }
        
        if(numOfNodes.getValue()>=limON)
            this.stop();
    }
    
    public synchronized void incNumOfNodes(int limON,javax.swing.tree.DefaultMutableTreeNode _newChild) throws InterruptedException{
        
        
        //if(numOfNodes.getValue()>1){
        //     this.wait();
        // }
        synchronized(numOfNodes){
            if(numOfNodes.getValue()>=limON){
                if(this.getChildCount()==0)
                    this.removeFromParent();
                this.stop();
                //this.suspend();
                ////System.out.println("Reausumo en 0");
            }
            //this.add(_newChild);
            //this.addObject(nodeParent,nodeTT,this.rootTreeTrace,jTree1,treeModel);
            //this.addObject(this,_newChild,rootTreeTrace,jtree,treeModel);
            //treeModel.insertNodeInto(_newChild,rootTreeTrace,rootTreeTrace.getChildCount());
            treeModel.insertNodeInto(_newChild,this,this.getChildCount());
            //this.addObject(_newChild);
            //this.addObject("HOLA");
            //this.addObject(_newChild,jtree,rootTreeTrace,treeModel);
            numOfNodes.incrValue();
            ////System.out.println("Number of Nodes=" + numOfNodes.getValue());
        }
        /*if(!this.isRoot){
            this.wait();
            this.notifyAll();
        }*/
        /*if(numOfNodes.getValue()>=limON)
            this.stop();
        else{
            notify();
        } */
    }
    
    public synchronized boolean isLimitOfNodes(int limON){
        return numOfNodes.getValue()>=limON;
    }
    
    
    
    public void run(){
        
        char c;
        boolean hasAutoEnlace;
        boolean isSingle;
        boolean flag;
        Enlace enlace=null;
        Maquina mainMachine = auxMachine;
        /*if(this.arrayOfTapes1==null){*/
        if(flagAOT0){
            this.arrayOfTapes0 = new Cinta[arrayOfTapes.length];
            for(int i=0;i<arrayOfTapes.length;i++)
                this.arrayOfTapes0[i] = new Cinta(arrayOfTapes[i],true);
        }
        
        
        //**************************DESCOMENTAR***********************
        //System.out.println("INDICE 1="+arrayOfTapes[0].getIndiceCabezal());
        //System.out.println("CINTA 1="+arrayOfTapes[0].getCaracteres().toString());
        
        int p=0;
        while(true){
            String asignaciones = " ";
            //synchronized(treeModel){
            synchronized(this.numOfNodes){
                if(this.auxMachine==null){
                    
                    NodeTreeTrace newChild = new NodeTreeTrace();
                    newChild.setJTree(this.jtree);
                    newChild.setTreeModel(this.treeModel);
                    newChild.setRootTreeTrace(this.rootTreeTrace);
                    //newChild.setTestTree(this.testTree);
                    newChild.setNumOfNodes(numOfNodes);
                    newChild.setIconValue(JTV.MyRenderer.ICONO_STOP);
                    newChild.setUserObject(" ");
                    this.estado = Maquina.ESTADO_HALT;
                    newChild.setMachine(auxMachine2);
                    treeModel.insertNodeInto(newChild,this,this.getChildCount());
                    
                    break;
                }
                
                
                
                
                
                if(this.auxMachine instanceof Submaquina){
                    
                    NodeTreeTrace newChild = new NodeTreeTrace();
                    newChild.contextoSubmaquina = new ContextoSubmaquina(((Submaquina)this.auxMachine).getNombreLargo());
                    
                    
                    Submaquina submaquina = (Submaquina)auxMachine;
                    Vector2 vector2 = (Vector2)submaquina.machineComponents;
                    int numeroCintas = vector2.getNumeroCintas();
                    newChild.contextoSubmaquina.rootTreeTrace.arrayOfTapes = new Cinta[numeroCintas];
                    for(int i=1;i<numeroCintas;i++)
                        newChild.contextoSubmaquina.rootTreeTrace.arrayOfTapes[i] = new Cinta(38+Cinta.H,20*i,i+1);
                    
                    if((!vector2.isMonocinta())&&submaquina.getIndiceCinta()==0){
                        newChild.contextoSubmaquina.rootTreeTrace.arrayOfTapes[0] = this.arrayOfTapes[0];
                        //System.out.println("Indice Cinta == 0");
                    } else if(vector2.isMonocinta()||submaquina.getIndiceCinta()>0){
                        newChild.contextoSubmaquina.rootTreeTrace.arrayOfTapes[0] = this.arrayOfTapes[submaquina.getIndiceCinta()];
                        //System.out.println("Indice Cinta > 0");
                    }
                    
                    
                    
                    
                    newChild.contextoSubmaquina.rootTreeTrace.parentRootTreeTrace = this; //this.rootTreeTrace;
                    
                    newChild.contextoSubmaquina.rootTreeTrace.parentPlayMachine = plyMachine; //mainMachine; //((Submaquina)this.auxMachine).submaquina;
                    
                    
                    newChild.contextoSubmaquina.rootTreeTrace.setThread(new Thread(newChild.contextoSubmaquina.rootTreeTrace,"SUM"));
                    
                    newChild.contextoSubmaquina.rootTreeTrace.setAuxMachine(((Vector2)((Submaquina)this.auxMachine).machineComponents).inicial);  //AGREGADO 12-12-2002
                    newChild.contextoSubmaquina.rootTreeTrace.plyMachine = ((Vector2)((Submaquina)this.auxMachine).machineComponents).inicial;    //AGREGADO 12-12-2002
                    
                    newChild.contextoSubmaquina.rootTreeTrace.setTreeModel(this.treeModel);
                    newChild.contextoSubmaquina.rootTreeTrace.dicMetavalores = new DiccionarioMetavalores(((Submaquina)this.auxMachine).variables,((Submaquina)this.auxMachine).parametros);
                    newChild.contextoSubmaquina.rootTreeTrace.numOfNodes = new SharedInteger();
                    newChild.contextoSubmaquina.rootTreeTrace.thread.start();
                    
                    try{
                        //Thread.sleep(5000);
                        newChild.contextoSubmaquina.rootTreeTrace.thread.join();
                    } catch(InterruptedException ex){
                        //System.out.println("InterruptedException");
                        ex.printStackTrace();
                        javax.swing.JOptionPane.showMessageDialog(null, "Se produjo un error durante la ejecución","JTV",javax.swing.JOptionPane.ERROR_MESSAGE);
                        
                        synchronized(this.numOfNodes){
                            try{
                                this.numOfNodes.notifyAll();
                            } catch(IllegalMonitorStateException imse){
                                imse.printStackTrace();
                            }
                        }
                        return;
                    }
                    
                    
                    
                    newChild.setTreeModel(this.treeModel);
                    newChild.setNumOfNodes(numOfNodes);
                    newChild.setIconValue(this.auxMachine.getIconValue());
                    //if(newChild.getIconValue()==JTV.MyRenderer.ICONO_SUBMAQUINA)
                    newChild.setUserObject((((Submaquina)auxMachine).getNombreLargo())+"()");
                    newChild.setMachine(auxMachine);
                    
                    treeModel.insertNodeInto(newChild,this,this.getChildCount());
                    
                    newChild.contextoSubmaquina.rootTreeTrace.parentTreePath = newChild.getPath();
                    
                    if(newChild.contextoSubmaquina.rootTreeTrace.estado==Maquina.ESTADO_COLGADA){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_SUBMAQUINA_COLGADA);
                        newChild.setUserObject(" ");
                        this.estado = Maquina.ESTADO_COLGADA;
                        
                        break;
                    }
                    
                    if(newChild.contextoSubmaquina.rootTreeTrace.estado==Maquina.ESTADO_RECHAZA){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_SUBMAQUINA_ERROR);
                        this.estado = Maquina.ESTADO_RECHAZA;
                        
                        break;
                    }
                    
                    if(newChild.contextoSubmaquina.rootTreeTrace.estado==Maquina.ESTADO_ND){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_SUBMAQUINA_ND);
                        newChild.setUserObject(" "); //NUEVO
                        this.estado = Maquina.ESTADO_ND;
                        
                        break;
                    }
                    
                    if(newChild.contextoSubmaquina.rootTreeTrace.estado==Maquina.ESTADO_EXPANDIBLE){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_SUBMAQUINA_EXPANDIR);
                        this.estado = Maquina.ESTADO_EXPANDIBLE;
                        
                        break;
                    }
                    
                    this.auxMachine2 = this.auxMachine;
                    this.auxMachine = this.auxMachine.proxima;
                    Thread.yield();
                    continue;
                }
                
                
                hasAutoEnlace=false;
                isSingle=false;
                flag=false;
                if(this.auxMachine instanceof EnlaceCompuesto){
                    
                    Enumeration en0 = ((EnlaceCompuesto) auxMachine).getEnlaces();
                    Vector vectorResultadosPosibles = new Vector();
                    while(en0.hasMoreElements()){
                        Enlace enlace0 = (Enlace) en0.nextElement();
                        ResultadoEvaluacion resultadoEvaluacion = null;
                        try{
                            resultadoEvaluacion = enlace0.condicionEnlace.evaluar(arrayOfTapes,dicMetavalores);
                        } catch(Exception ex){
                            System.out.println("ERROR EN EVALUACION");
                        }
                        if(resultadoEvaluacion.isResultado()){
                            resultadoEvaluacion.setEnlace(enlace0);
                            vectorResultadosPosibles.add(resultadoEvaluacion);
                        }
                    }
                    
                    Enumeration en = vectorResultadosPosibles.elements();
                    
                    //int enlacesPosibles = ((EnlaceCompuesto) auxMachine).getEnlacesPosibles(this.arrayOfTapes);
                    
                    if(vectorResultadosPosibles.size()==0){
                        NodeTreeTrace newChild = new NodeTreeTrace();
                        newChild.setJTree(this.jtree);
                        newChild.setTreeModel(this.treeModel);
                        newChild.setRootTreeTrace(this.rootTreeTrace);
                        //newChild.setTestTree(this.testTree);
                        newChild.setNumOfNodes(numOfNodes);
                        newChild.setIconValue(JTV.MyRenderer.ICONO_STOP);
                        newChild.setUserObject(" ");
                        this.estado = Maquina.ESTADO_HALT;
                        newChild.setMachine(auxMachine.previa);
                        treeModel.insertNodeInto(newChild,this,this.getChildCount());
                        break;
                    }
                    //DiccionarioMetavalores cloneDicMetavalores = (DiccionarioMetavalores)dicMetavalores.clone();
                    if(vectorResultadosPosibles.size()==1)
                        isSingle=true;
                    while(en.hasMoreElements()){
                        ResultadoEvaluacion resultadoEvaluacion = (ResultadoEvaluacion)en.nextElement();
                        enlace = resultadoEvaluacion.getEnlace();
                        
                        if(!resultadoEvaluacion.isError()){
                            try{
                                if(isSingle){
                                    asignaciones = resultadoEvaluacion.asignar(dicMetavalores);
                                    dicMetavalores.setValoresIniciales0();
                                } else{
                                    asignaciones = resultadoEvaluacion.asignar((DiccionarioMetavalores)dicMetavalores.clone());
                                    resultadoEvaluacion.dicMetavalores.setValoresIniciales0();
                                }
                                //asignaciones = resultadoEvaluacion.asignar(this.dicMetavalores);
                            } catch(Exception ex){
                                System.out.println("ERROR EN ASIGNACION");
                            }
                        } else{
                            
                            NodeTreeTrace newChild = new NodeTreeTrace();
                            newChild.setJTree(this.jtree);
                            newChild.setTreeModel(this.treeModel);
                            newChild.setRootTreeTrace(this.rootTreeTrace);
                            Cinta _arrayOfTapes[] = new Cinta[arrayOfTapes.length];
                            for(int j=0;j<arrayOfTapes.length;j++)
                                _arrayOfTapes[j] = new Cinta(arrayOfTapes[j]);
                            //newChild.setArrayOfTapes(arrayOfTapes);
                            newChild.setArrayOfTapes(_arrayOfTapes);
                            //newChild.setTestTree(this.testTree);
                            newChild.setNumOfNodes(numOfNodes);
                            newChild.dicMetavalores = this.dicMetavalores;
                            //newChild.setIconValue(JTV.MyRenderer.ICONO_RECHAZA);
                            this.estado = Maquina.ESTADO_RECHAZA;
                            newChild.setMachine(enlace);
                            //newChild.setUserObject(resultadoEvaluacion.getStrError());
                            treeModel.insertNodeInto(newChild,this,this.getChildCount());
                            if(isSingle){
                                newChild.setIconValue(JTV.MyRenderer.ICONO_RECHAZA);
                                newChild.setUserObject(resultadoEvaluacion.getStrError());
                                synchronized(this.numOfNodes){
                                    try{
                                        this.numOfNodes.notifyAll();
                                    } catch(IllegalMonitorStateException imse){
                                        imse.printStackTrace();
                                    }
                                }
                                return;
                            }
                            NodeTreeTrace newChild1 = new NodeTreeTrace();
                            newChild1.setJTree(this.jtree);
                            newChild1.setTreeModel(this.treeModel);
                            newChild1.setRootTreeTrace(this.rootTreeTrace);
                            newChild1.setArrayOfTapes(_arrayOfTapes);
                            //newChild1.setTestTree(this.testTree);
                            newChild1.setNumOfNodes(numOfNodes);
                            newChild1.setIconValue(JTV.MyRenderer.ICONO_RECHAZA);
                            newChild1.setMachine(enlace);
                            newChild1.setUserObject(resultadoEvaluacion.getStrError());
                            newChild1.dicMetavalores = this.dicMetavalores;
                            treeModel.insertNodeInto(newChild1,newChild,newChild.getChildCount());
                            
                            continue;
                            
                        }
                        
                        if(enlace.esCiclico()||isSingle){
                            //hasAutoEnlace = true;
                            flag = true;
                            auxMachine = enlace;
                            if(enlace instanceof AutoEnlace)
                                enlace.setIconValue(JTV.MyRenderer.ICONO_AUTOENLACE);
                            else
                                enlace.setIconValue(JTV.MyRenderer.ICONO_ENLACE);
                        } else{
                            
                            //NodeTreeTrace newChild1 = new NodeTreeTrace(testTree,numOfNodes,true,this.rootTreeTrace,treeModel,jtree);
                            NodeTreeTrace newChild1 = new NodeTreeTrace(numOfNodes,true,this.rootTreeTrace,treeModel,jtree);
                            newChild1.setMachine(enlace);
                            //if(c!=0) newChild1.setUserObject("" + c);
                            if(asignaciones.trim().length()!=0) 
                                newChild1.setUserObject(asignaciones);
                            else
                                newChild1.setUserObject(" ");
                            //newChild1.setUserObject(asignaciones.trim().length()==0?"":asignaciones);
                            //newChild1.setTape(new Cinta(tape));
                            Cinta _arrayOfTapes[] = new Cinta[arrayOfTapes.length];
                            for(int j=0;j<arrayOfTapes.length;j++)
                                _arrayOfTapes[j] = new Cinta(arrayOfTapes[j]);
                            newChild1.setArrayOfTapes(_arrayOfTapes);
                            newChild1.setNumOfNodes(this.numOfNodes);
                            newChild1.dicMetavalores = resultadoEvaluacion.dicMetavalores;
                            //newChild1.dicMetavalores = this.dicMetavalores;
                            newChild1.parentPlayMachine = plyMachine;
                            newChild1.plyMachine = plyMachine;
                            this.estado = Maquina.ESTADO_ND;
                            treeModel.insertNodeInto(newChild1,this,this.getChildCount());
                            newChild1.start2(newChild1);
                            ////System.out.println("AGREGANDO RAMA");
                        }
                        //}
                    }
                    
                    //if(!hasAutoEnlace) break;
                    if(!flag) break;
                }
                
                
                
                NodeTreeTrace newChild = new NodeTreeTrace();
                if(asignaciones.trim().length()!=0) 
                    newChild.setUserObject(asignaciones);
                else
                    newChild.setUserObject(" ");
                newChild.setJTree(this.jtree);
                newChild.setTreeModel(this.treeModel);
                
                newChild.setRootTreeTrace(this.rootTreeTrace);
                //newChild.setTestTree(this.testTree);
                newChild.setNumOfNodes(numOfNodes);
                newChild.setIconValue(this.auxMachine.getIconValue());
                if(newChild.getIconValue()==JTV.MyRenderer.ICONO_L || newChild.getIconValue()==JTV.MyRenderer.ICONO_R) newChild.setUserObject(" ");
                if(newChild.getIconValue()==JTV.MyRenderer.ICONO_ESCRITORA) newChild.setUserObject(auxMachine.getNombre());
                newChild.setMachine(auxMachine);
                
                //this.auxMachine.accion(this.tape);
                
                if(!this.auxMachine.accion(this.arrayOfTapes,this.dicMetavalores)){
                    if(newChild.getIconValue()==JTV.MyRenderer.ICONO_ESCRITORA){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_RECHAZA);
                        newChild.setUserObject(newChild.getUserObject()+"="+"INDEFINIDO");
                        this.estado = Maquina.ESTADO_RECHAZA;
                    } else{
                        newChild.setIconValue(JTV.MyRenderer.ICONO_WARNING);
                        this.estado = Maquina.ESTADO_COLGADA;
                    }
                    treeModel.insertNodeInto(newChild,this,this.getChildCount());
                    break;
                }
                
                
                try{
                    
                    if(newChild.getIconValue()==JTV.MyRenderer.ICONO_ESCRITORA && !CondicionEnlace.isNombreSimbolo(auxMachine.getNombre())){
                        
                        String metaValue = ((this.dicMetavalores.getMetavalor((String)newChild.getUserObject())).getValor()).getNombre(); //+"->"+this.arrayOfTapes[auxMachine.getIndiceCinta()].getIndiceCabezal();
                        newChild.setUserObject(newChild.getUserObject()+"="+metaValue);
                    }
                }catch(Exception ex){
                    //System.out.println("Excepcion");
                    ex.printStackTrace();
                }
                
                try{
                    //synchronized(treeModel){
                    int childCount = this.getChildCount();
                    //Thread.wait(100);
                    treeModel.insertNodeInto(newChild,this,childCount);
                    //treeModel.notifyAll();
                    //}
                } catch(Exception ex){
                    ex.printStackTrace();
                    javax.swing.JOptionPane.showMessageDialog(null, "Se produjo un error durante la ejecución","JTV",javax.swing.JOptionPane.ERROR_MESSAGE);
                    synchronized(this.numOfNodes){
                        try{
                            this.numOfNodes.notifyAll();
                        } catch(IllegalMonitorStateException imse){
                            imse.printStackTrace();
                        }
                    }
                    return;
                }
                //treeModel.insertNodeInto(newChild,this,this.getChildCount());
                this.auxMachine2 = this.auxMachine;
                this.auxMachine = this.auxMachine.proxima;
                
                if(numOfNodes.getValue()>numOfNodes.getMaxValue()){
                    
                    newChild = new NodeTreeTrace();
                    newChild.setJTree(this.jtree);
                    newChild.setTreeModel(this.treeModel);
                    newChild.setRootTreeTrace(this.rootTreeTrace);
                    //newChild.setTestTree(this.testTree);
                    newChild.setNumOfNodes(numOfNodes);
                    if(auxMachine==null){
                        newChild.setIconValue(JTV.MyRenderer.ICONO_STOP);
                        newChild.setUserObject(" ");
                        this.estado=Maquina.ESTADO_HALT;
                    } else{
                        newChild.setIconValue(JTV.MyRenderer.ICONO_EXPANDIR);
                        newChild.setUserObject(" ");
                        this.estado=Maquina.ESTADO_EXPANDIBLE;
                    }
                    
                    newChild.setMachine(this.auxMachine2);
                    try{
                        treeModel.insertNodeInto(newChild,this,this.getChildCount());
                    } catch(Exception ex){
                        ex.printStackTrace();
                        javax.swing.JOptionPane.showMessageDialog(null, "Se produjo un error durante la ejecución","JTV",javax.swing.JOptionPane.ERROR_MESSAGE);
                        synchronized(this.numOfNodes){
                            try{
                                this.numOfNodes.notifyAll();
                            } catch(IllegalMonitorStateException imse){
                                imse.printStackTrace();
                            }
                        }
                    }
                    //treeModel.insertNodeInto(newChild,this,this.getChildCount());
                    break;
                } else
                    numOfNodes.incrValue();
                
                Thread.yield();
            }
            //}
        }
        
        this.arrayOfTapes1 = new Cinta[arrayOfTapes.length];
        for(int i=0;i<arrayOfTapes.length;i++)
            this.arrayOfTapes1[i] = new Cinta(arrayOfTapes[i],true);
        
        synchronized(this.numOfNodes){
            try{
                this.numOfNodes.notifyAll();
            } catch(IllegalMonitorStateException imse){
                imse.printStackTrace();
            }
        }
    }
    
    
    public NodeTreeTrace getMyRoot(){
        
        if(this.isLeaf()){
            ////System.out.println("SOY HOJA");
            DefaultMutableTreeNode node = this;
            while(true){
                if(node!=null && node.isLeaf())
                    node = node.getPreviousNode();
                else
                    return (NodeTreeTrace)node;
            }
            
        } else{
            ////System.out.println("NO SOY HOJA");
            return this;
        }
    }
    
    /** Getter for property auxMachine.
     * @return Value of property auxMachine.
     */
    public Maquina getAuxMachine() {
        return auxMachine;
    }
    
    /** Setter for property auxMachine.
     * @param auxMachine New value of property auxMachine.
     */
    public void setAuxMachine(Maquina auxMachine) {
        this.auxMachine = auxMachine;
    }
    
    public static Vector getCompletePathVector(DefaultMutableTreeNode node){
        
        Vector vector = new Vector();
        Object obj[] = node.getPath();
        for(int i=0;i<obj.length;i++){
            vector.add(obj[i]);
            if(obj.length==1) {
                vector.add(node);
                break;
            } else if(i==obj.length-1){
                vector.remove(obj[i]);
                vector.add(node);
                break;
            }
            Enumeration en = ((DefaultMutableTreeNode)obj[i]).children();
            while(en.hasMoreElements()){
                DefaultMutableTreeNode children = (DefaultMutableTreeNode)en.nextElement();
                if(children.isLeaf() && children!=node)
                    vector.add(children);
                else if(children.isLeaf() && children==node){
                    vector.add(children);
                    return vector;
                } else if(children==obj[i+1])
                    break;
            }
        }
        
        return vector;
    }
    
    
}
