/*
 */
//package treemap;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.JFrame;

import org.graffiti.graph.AdjListGraph;
import org.graffiti.graph.Graph;
import org.graffiti.graph.Node;
import org.graffiti.plugins.ios.importers.graphml.GraphMLReader;

/**
 * @author Paul Holleis
 */
public class TreeMapTemplate extends JFrame {
        
    private final int width = 400;
    private final int height = 330;
    
    Node root = null;
    
    
    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println
                ("Specify filename of graph as command line parameter.");
            System.exit(1);
        }
        String fileName = args[0];
        if (fileName.equals("")) {
            System.out.println
                ("Specify filename of graph as command line parameter.");
            System.exit(1);
        }
        
        TreeMapTemplate main = new TreeMapTemplate(fileName);
    }
    
    
    public TreeMapTemplate(String fileName) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("TreeMap Application");
        
        /* read graph */
        GraphMLReader xmlReader = new GraphMLReader();
        Graph graph = new AdjListGraph();
        try {
            xmlReader.read(fileName, graph);
        } catch (IOException e) {
            System.out.println
                ("ERROR: could not load graph: " + e.getMessage());
            System.exit(1);
        }
        
        /* get root node */
        root = (Node)graph.getNodes().get(0);

        if (root == null) {
            System.out.println("ERROR: graph has no root.");
            System.exit(1);
        }

        this.setSize(width+50, height+50);
        this.setVisible(true);
        
        //*** EXAMPLE CODE TO RUN THROUGH TREE ***//
        
        System.out.println("root has " + root.getOutDegree() + " children");
        System.out.print("these have / ");
        Iterator nodeIter = root.getOutNeighbors().iterator();
        while (nodeIter.hasNext()) {
            Node node = (Node)nodeIter.next();
            System.out.print(node.getOutDegree() + " / ");
        }
        System.out.println("children, respectively.");
     }
    
    
    /* 
     * @see java.awt.Component#paint(java.awt.Graphics)
     */
    public void paint(Graphics g) {
        super.paint(g);
        
        Graphics2D g2d = (Graphics2D)g;
        
        //*** INSERT CODE HERE, SHOWING "graph" ON the frame ***//
        //*** "root" is the root node of the graph             ***//

    }
}

