/*
 *      @(#)Node.java 1.30 98/11/05 20:34:46
 *
 * Copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 *
 * 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 AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE 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 SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */

/*
 * @Author: Rick Goldberg
 * @Author: Doug Gehringer
 *
 */

package com.sun.j3d.loaders.vrml97.impl;
import vrml.InvalidFieldException;
import vrml.InvalidExposedFieldException;
import vrml.InvalidEventInException;
import vrml.InvalidEventOutException;

import java.util.Hashtable;
import java.util.Enumeration;

import java.io.PrintStream;
import java.io.Writer;

public abstract class Node extends BaseNode implements Cloneable, Notifier {

    Hashtable FieldSpec = new Hashtable(4);
    int refCount = 0; // used to count USE's in writeX3D

    Node(Loader loader) {
        super(loader);
    }

    // Exposed fields may be either input ( eventIn )
    // output ( eventOut ) or both
    public Field getExposedField(String fieldName)
    throws InvalidExposedFieldException {
        Field f;
        if( ( f = (Field) FieldSpec.get(fieldName)) == null )
            throw new InvalidExposedFieldException("No field named "+fieldName);
        return(f);
    }

    public ConstField getEventOut(String eventOutName)
    throws InvalidEventOutException {
        Object cf;
        Field f;

        String fieldName = Field.baseName(eventOutName);
        f = (Field)FieldSpec.get(fieldName);
        if( f == null ) {
            throw new InvalidEventOutException("No field named " + fieldName);
        } else if( (f.fieldType & Field.EVENT_OUT) != Field.EVENT_OUT ) {
            throw new InvalidEventOutException("Field is not an EVENT_OUT");
        }
        if( !(f instanceof ConstField) ) {
            f = ((Field)f).constify();
        }
        return(ConstField)f;
    }


    // the default action is to connect a route by accessing the fields
    // through these calls, and a setValue on the input side automatically
    // causes a getValue on the output.


    public Field getEventIn(String eventInName) 	
		throws InvalidEventInException {
        String fieldName = Field.baseName(eventInName);
        Field f = (Field) FieldSpec.get(fieldName);
        if( f == null ) {
            throw new InvalidEventInException("No field named " + eventInName);
        } else if( (f.fieldType & Field.EVENT_IN) != Field.EVENT_IN ) {
            throw new InvalidEventInException("Field is not an EVENT_IN");
        } 

	if( f instanceof ConstField ) {
            throw new InvalidEventInException("Field is an ConstField");
        }
        return(f);
    }

    public Field getField(String fieldName) 
		throws InvalidFieldException {
        Field f;
        fieldName = Field.baseName(fieldName);
        //if(loader.debug) {
        //    System.out.println(toStringId()+" getField "+fieldName);
        //}
        if( ( f = (Field) FieldSpec.get(fieldName)) == null )
            throw new InvalidFieldException("No field named " + fieldName);
        return(f);
    }

    public abstract void notifyMethod(String eventInName, double time);
    abstract void initFields();

    public vrml.BaseNode wrap() {
        return(new vrml.node.Node(this));
    }

    void writeX3D(Writer w, int depth) throws java.io.IOException {
	tab(w,depth);
	w.write("<"+getType()+" ");
	if (defName != null) {
	    if ( refCount++ < 1 ) {
	        w.write(" DEF=\""+defName+"\" ");
	    } else {
		w.write(" USE=\""+defName+"\"/>\n");
		return;
	    }
	}
	java.util.Vector saveNodes = new java.util.Vector();
	java.util.Enumeration e = FieldSpec.elements();
        while(e.hasMoreElements()) {
            Field f = (Field) e.nextElement();
	    if (( f instanceof SFNode )||( f instanceof MFNode ))
		//if(!(getType().equals("Inline")))
		saveNodes.add(f);
	    else if (!f.fieldName.equals("examine")) {
		w.write("\n");
		tab(w,depth);
		w.write("  "+f.fieldName+"=\""+f.getX3DValue()+"\"");
	    }
	} if (saveNodes.size() == 0) {
	    w.write("/>\n");
	    return;
	} else {
	    w.write(">\n");
	    e = saveNodes.elements();
	    while(e.hasMoreElements()&&!getType().equals("Inline")){
	    Field f = (Field) e.nextElement();
	    if (f instanceof SFNode ) {
		if(((SFNode)f).node != null)
		    (((SFNode)f).node).writeX3D(w,depth+1);
		} else {
		    BaseNode bn[] = ((MFNode)f).nodes;
		    for(int b = 0; b<bn.length; b++ )
		        if(bn[b] != null)bn[b].writeX3D(w,depth+1);
		}	
	    }
	 }
	 tab(w,depth);
	 // terminate with the end tag, not a 1 liner
	 w.write("</"+getType()+">\n");
	 return;
    }

    // todo: be hardnosed about this and make tab() be a special case
    // of writer. then you have w.tab(depth).
    void tab(Writer w,int depth) throws java.io.IOException {
	String t ="";
	for(int d=0; d<depth; d++) {
	   t+="\t";
	}
	w.write(t);
    }

    public void causeEvent(String eventOutName, Field field) 
    throws InvalidEventOutException {
	Field f;

        String fieldName = Field.baseName(eventOutName);
        f = (Field)FieldSpec.get(fieldName);
        if( f == null ) {
            throw new InvalidEventOutException("No field named " + fieldName);
        } else if( (f.fieldType & Field.EVENT_OUT) != Field.EVENT_OUT ) {
            throw new InvalidEventOutException("Field is not an EVENT_OUT");
        }
        f.update(field);       
    }	
}
