All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d.SceneGraphObjectState Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 */

package com.sun.j3d.utils.scenegraph.io.state.javax.media.j3d;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Constructor;

import javax.media.j3d.SceneGraphObject;

import com.sun.j3d.utils.scenegraph.io.retained.Controller;
import com.sun.j3d.utils.scenegraph.io.retained.SGIORuntimeException;
import com.sun.j3d.utils.scenegraph.io.retained.SymbolTableData;

public abstract class SceneGraphObjectState {

    protected SceneGraphObject node;
    protected SymbolTableData symbol;
    protected Controller control;
    protected String nodeClassName;

    /**
     * Create a new State object
     *
     * During Saveing
     *  SymbolTableData will have the nodeID and j3dNode fields set
     *
     * During loading
     *  SymbolTableData be null, symbol will be created and added to the
     * symbol data during readObject()
     */
    public SceneGraphObjectState( SymbolTableData symbol, Controller control ) {
        this.symbol = symbol;
        this.control = control;

        if (symbol!=null) {
            this.node = symbol.j3dNode;

            // This consistancy check is for debugging purposes
            //if (symbol.j3dNode==null || symbol.nodeID==0)
            //    throw new RuntimeException( "Bad Symbol in State creation");
        }


        if (node!=null) {
            nodeClassName = node.getClass().getName();

	    try {
                if (node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO)
                    ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO)node).createSceneGraphObjectReferences( control.getSymbolTable() );
            } catch(Exception e) {
                System.err.println("Exception in createSceneGraphObjectReferences");
                e.printStackTrace();
            }

        }

    }

    /**
     * DO NOT call symbolTable.addReference in writeObject as this (may)
     * result in a concurrentModificationException.
     *
     * All references should be created in the constructor
     */
    public void writeObject( DataOutput out ) throws IOException {
        boolean sgIO = node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO;
        out.writeBoolean( sgIO );
        out.writeInt( symbol.nodeID );


        int nodeClassID = control.getNodeClassID( node );

        out.writeShort( nodeClassID );

        if (nodeClassID==-1)
            out.writeUTF( nodeClassName );

        writeConstructorParams( out );

        if (sgIO) {
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
	    DataOutputStream tmpOut = new DataOutputStream( byteStream );
            ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO)node).writeSceneGraphObject( tmpOut );
	    tmpOut.close();
	    out.writeInt( byteStream.size() );
	    out.write( byteStream.toByteArray() );
        }

        writeUserData( out );
        writeString(node.getName(), out);

        writeCapabilities( out );
    }

    public void readObject( DataInput in ) throws IOException {

        boolean sgIO = in.readBoolean();
        int nodeID = in.readInt();

	int nodeClassID = in.readShort();

	nodeClassName = null;

	if ( nodeClassID==-1 )
	    nodeClassName = in.readUTF();

	readConstructorParams( in );

	if ( nodeClassID!=-1 ) {
	    node = createNode();
	    nodeClassName = node.getClass().getName();
	} else
	    node = createNode(nodeClassName);


        if ( sgIO ) {
	    if (control.getCurrentFileVersion()==1)
                ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO)node).readSceneGraphObject( in );
            else {
	        int size = in.readInt();
                if (node instanceof com.sun.j3d.utils.scenegraph.io.SceneGraphIO) {
	            byte[] bytes = new byte[size];
		    in.readFully( bytes );
                    ByteArrayInputStream byteStream = new ByteArrayInputStream( bytes );
                    DataInputStream tmpIn = new DataInputStream( byteStream );
                    ((com.sun.j3d.utils.scenegraph.io.SceneGraphIO)node).readSceneGraphObject( tmpIn );
		    tmpIn.close();
                } else {
	    	    in.skipBytes( size );
                }
            }
        }

        symbol = control.getSymbolTable().createSymbol( this, node, nodeID );
        readUserData( in );
        if (control.getCurrentFileVersion()>2) {
            node.setName(readString(in));
        }

        readCapabilities( in );
    }

    public SceneGraphObject getNode() {
        return node;
    }

    public int getNodeID() {
        return symbol.nodeID;
    }

    public SymbolTableData getSymbol() {
        return symbol;
    }

    private void readUserData( DataInput in ) throws IOException {

        node.setUserData( control.readSerializedData( in ));
    }

    private void writeUserData( DataOutput out ) throws IOException {
        Object obj = node.getUserData();
        if (obj != null && !(obj instanceof java.io.Serializable)) {
            System.err.println("UserData is not Serializable and will not be saved");
            obj = null;
        }

        control.writeSerializedData( out, (Serializable)obj );
    }

    /*
     * NOTE:  This implementation assumes a maximum of 64 capability
     * bits per node class.  If this changes in the future, this
     * implementation will need to be updated.
     */
    private void writeCapabilities( DataOutput out ) throws IOException {
	long capabilities = 0;
	long frequentCapabilities = 0;

	for ( int i=0;i<64;i++ ) {
	    if ( node.getCapability( i ) ) capabilities |= (1L << i);
	    if ( !(node.getCapabilityIsFrequent( i )) ) frequentCapabilities |= (1L << i);
	}
        out.writeLong( capabilities );
        out.writeLong( frequentCapabilities );
    }

    private void readCapabilities( DataInput in ) throws IOException {
	long capabilities = in.readLong();
	long frequentCapabilities = in.readLong();

	for ( int i=0;i<64;i++ ) {
	    if ( (capabilities&(1L<




© 2015 - 2024 Weber Informatics LLC | Privacy Policy