/*
 * OrbitBehavior.java
 *
 * Created on November 1, 2000, 2:13 PM
 */

package com.sun.j3d.demos.utils.scenegraph.io.test;

import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.demos.utils.scenegraph.io.*;

/**
 * A branchGroup that contains a reference to another object
 *
 * Used to test bug reported by Boris Zeldin
 *
 * @author  root
 * @version 
 */
public class CrossReferenceBG extends javax.media.j3d.BranchGroup implements SceneGraphIO {

    private SceneGraphObject obj;
    private int targetRef;
    
    /** Creates new OrbitBehavior */
    public CrossReferenceBG( SceneGraphObject obj ) {
        this.obj = obj;
    }
    
    /** 
     * Paramaterless constructor for SceneGraphIO
     */
    public CrossReferenceBG() {
    }
    
    /**
 * This method will be called during the writing of the object.
 * The method is called before writeSGObject and gives the user the chance
 * to create references to other Nodes and NodeComponents.
 *
 * References take the form of a nodeID, of type integer. Every SceneGraphObject
 * is assigned a unique ID.
 *
 * The user must save the reference information in writeSGObject
 */
    public void createSceneGraphObjectReferences(SceneGraphObjectReferenceControl ref) {
        targetRef = ref.addReference( obj );
    }
    
    /**
 * This method is called once the all objects in the scenegraph have been loaded.
 *
 * Within this method the user should restore references to the SceneGraphObjects
 * whose nodeID's were created with createSceneGraphObjectReferences
 */
    public void restoreSceneGraphObjectReferences(SceneGraphObjectReferenceControl ref) {
        obj = ref.resolveReference( targetRef );
        
        System.out.println("CrossReferenceBG has reference to "+obj );
    }
    
    /**
 * This is called after data for the parent SceneGraphObject has been written to
 * the <code>out</code>. The user should store any state that is declared in
 * this object
 */
    public void writeSceneGraphObject(java.io.DataOutput out) throws java.io.IOException {
        out.writeInt( targetRef );
    }
    
    /**
 * This is called after the object has been constructed and the superclass SceneGraphObject
 * data has been read from <code>in</code>.
 *
 * The user should restore all state infomation written in writeSGObject
 */
    public void readSceneGraphObject(java.io.DataInput in) throws java.io.IOException {
        targetRef = in.readInt();
    }
    
    /**
 * This method only has an effect if this is a subclass of Group.
 *
 * If this returns true then all children of this Group will be saved.
 *
 * If it returns false then the children are not saved.
 */
    public boolean saveChildren() {
        return true;
    }
        
}
