package org.cade.ui;

import java.awt.Container;
import java.awt.Component;

public class ComponentFocusTree extends Thread {

    private Container container = null;

    public ComponentFocusTree(Container container) {
	System.out.println("ComponentFocusTree instantiated....");
	this.container = container;
    }

    public void run() {

	while (true) {
	    try {
		if (container != null) {
		    Component[] comp =  new Component[1];
		    comp[0] = container;
		    buildComponentTree(comp, -1);
		}
		System.out.println("=========\n\n\n\n");
		Thread.sleep(5000);
	    } catch (Exception e) {}
	} // while
    } // run()


    private void buildComponentTree(Component[] comp, int level) {
	
	level++;

	for (int i= 0; i < comp.length; i++) {
	    
	    System.out.print(getSpaces(level));
	    System.out.print("|_");
	    System.out.print(comp[i].getClass().getName());
	    System.out.print(": " + comp[i].hasFocus());
	    if (comp[i].hasFocus()) 
		System.out.println("  <--------");
	    else
		System.out.println();
	    
	    if (comp[i] instanceof java.awt.Container 
		&& ((Container) comp[i]).getComponentCount() > 0) {
		buildComponentTree(((Container)comp[i]).getComponents(), level); 
	    } // for(...)
	}
    }

    private String getSpaces(int spaces) {
	
	char[] spaceArray = new char[spaces*2];

	for (int i = 0; i < spaceArray.length; i++) {
	    spaceArray[i] = ' ';
	}
	
	return (new String(spaceArray));
    }

} // class ComponentFocusTree
