// Copyright 2004, FreeHEP.
package hep.graphics.heprep1.adapter;

import java.util.*;

import hep.graphics.heprep.HepRepAttValue;
import hep.graphics.heprep.HepRepInstance;
import hep.graphics.heprep.HepRepInstanceTree;
import hep.graphics.heprep.HepRepPoint;
import hep.graphics.heprep.HepRepSelectFilter;
import hep.graphics.heprep.HepRepType;
import hep.graphics.heprep.HepRepTypeTree;

/**
 *
 * @author Mark Donszelmann
 * @version $Id: AbstractHepRepInstanceAdapter.java,v 1.4 2004/07/30 22:32:13 duns Exp $
 */
public abstract class AbstractHepRepInstanceAdapter extends HepRepAttributeAdapter implements HepRepInstance {

    private HepRepInstance parent;
    private HepRepType type;
    private List/*<HepRepInstance>*/ instances;

    private transient String layer;
    private transient boolean hasFrame;

    public AbstractHepRepInstanceAdapter(hep.graphics.heprep1.HepRepAttribute attribute,
                                         HepRepInstance parent,
                                         HepRepType type) {
        this(null, attribute, parent, type);                                   
    }
    
    public AbstractHepRepInstanceAdapter(hep.graphics.heprep1.HepRepAttribute parentAttribute,
                                         hep.graphics.heprep1.HepRepAttribute attribute,
                                         HepRepInstance parent,
                                         HepRepType type) {
        super(parentAttribute, attribute);
        this.parent = parent;
        this.type = type;
        this.instances = new ArrayList();
        layer = null;
        hasFrame = false;
    }

    public void overlay(HepRepInstance instance) {
        throw new UnsupportedOperationException();
    }

    public void addInstance(HepRepInstance instance) {
        instances.add(instance);
    }
            
    public void removeInstance(HepRepInstance instance) {
        throw new UnsupportedOperationException();
    }
  
    public List/*<HepRepInstance>*/ getInstances() {
        return instances;
    }
        
    public void addPoint(HepRepPoint point) {
        throw new UnsupportedOperationException();
    }

    public HepRepType getType() {
        return type;
    }
            
    public int getPoints(double[][] xyz) {
        return -1;
    }
        
    public HepRepInstance getSuperInstance() {
        return parent;
    }
        
    public HepRepInstance copy(HepRepTypeTree typeTree, HepRepInstance parent) throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
    
    public HepRepInstance copy(HepRepTypeTree typeTree, HepRepInstance parent, HepRepSelectFilter filter) throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    public HepRepInstance copy(HepRepTypeTree typeTree, HepRepInstanceTree parent) throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    public HepRepInstance copy(HepRepTypeTree typeTree, HepRepInstanceTree parent, HepRepSelectFilter filter) throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
        
    public void setUserObject(Object object) {
    }   

    public Object getUserObject() {
        return null;
    }

    /**
     * search for attribute on node, then search on type
     */
    public HepRepAttValue getAttValue(String name) {
        String lowerCaseName = name.toLowerCase();
        HepRepAttValue value = getAttValueFromNode(lowerCaseName);
        return (value != null) ? value : type.getAttValue(lowerCaseName);
    }

    public String getLayer() {
        validate();
        return layer;
    }
    
    public boolean hasFrame() {
        validate();
        return hasFrame;
    }
    
    private void validate() {
        if (layer != null) return;
        layer = getAttValue("layer").getString().intern();
        hasFrame = getAttValue("hasframe").getBoolean();
    }     
}
