// Copyright 2000-2004, FreeHEP.
package hep.graphics.heprep.test;

import java.awt.Color;
import java.io.IOException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Iterator;

import hep.graphics.heprep.*;
import hep.graphics.heprep.xml.*;
import hep.graphics.heprep.util.*;

/**
 * Tests iteration of a HepRep file. Prints out the changes to all subscribed
 * attributes as they should be set in the target system.
 *
 * @author M.Donszelmann
 *
 * @version $Id: TestIterator.java,v 1.18 2004/08/30 23:14:00 duns Exp $
 */

public class TestIterator implements HepRepAttributeListener {
    public static final String cvsId = "$Id: TestIterator.java,v 1.18 2004/08/30 23:14:00 duns Exp $";

    public void run(String[] args) throws Throwable {
        String fname = args[0];

        HepRep hepRep;
        // read File
        hepRep = HepRepIO.readHepRep(fname);
        
        List layers;
        if (args.length == 1) {
            layers = null;
        } else {
            layers = new ArrayList();
            for (int j=1; j<args.length; j++) {
                layers.add(args[j]);
            }
        }
        HepRepIterator it = HepRepUtil.getInstances(hepRep.getInstanceTreeList(), layers, null, false);
        
        it.addHepRepAttributeListener("Layer", this);
        it.addHepRepAttributeListener("DrawAs", this);
        it.addHepRepAttributeListener("Color", this);
            
        while (it.hasNext()) {
            HepRepInstance instance = (HepRepInstance)it.next();
            System.out.print("Instance of ");
            System.out.print(instance.getType());
            System.out.print(" as ");
            System.out.println(instance.getAttValue("drawas"));
        }
    }

    public void setAttribute(HepRepInstance instance, String key, String value, String lowerCaseValue, int showLabel) {
        if (key.equals("layer")) System.out.println("============================================");
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void setAttribute(HepRepInstance instance, String key, Color value, int showLabel) {
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void setAttribute(HepRepInstance instance, String key, long value, int showLabel) {
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void setAttribute(HepRepInstance instance, String key, int value, int showLabel) {
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void setAttribute(HepRepInstance instance, String key, double value, int showLabel) {
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void setAttribute(HepRepInstance instance, String key, boolean value, int showLabel) {
        System.out.println(key+"="+value+":"+showLabel);
    }

    public void removeAttribute(HepRepInstance instance, String key) {
        System.out.println("*"+key);
    }

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("Usage: TestIterator filename [layernames]");
            System.exit(1);
        }

        try {
            new TestIterator().run(args);
        } catch (Throwable e) {
	        System.out.println(e);
	        e.printStackTrace();
        }
    }


}
