/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plab.prefuseTests;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JSplitPane;

import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.action.layout.graph.BalloonTreeLayout;
import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.action.layout.graph.RadialTreeLayout;
import prefuse.action.layout.graph.FruchtermanReingoldLayout;
import prefuse.activity.Activity;
import prefuse.controls.DragControl;
import prefuse.controls.PanControl;
import prefuse.controls.ZoomControl;
import prefuse.data.Graph;
import prefuse.data.io.DataIOException;
import prefuse.data.io.GraphMLReader;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
import prefuse.util.ui.UILib;
import prefuse.util.force.ForceSimulator;
import prefuse.action.ActionList;
import prefuse.action.CompositeAction;
import prefuse.util.ui.JForcePanel;
import prefuse.util.ui.JValueSlider;
import prefuse.action.layout.CircleLayout;
import prefuse.util.PrefuseLib;
import prefuse.render.Renderer;
import prefuse.render.ShapeRenderer;
import prefuse.render.PolygonRenderer;

import java.io.IOException;






/**
 * The prefuse tutorial, reproduced.
 *
 * @author BCR
 */
public class Example {

    public static void main(String[] argv) {
        Graph graph = null;

        System.out.println( PrefuseLib.getMemoryUsageInMB() );
        //Load data;
        try {
            graph = new GraphMLReader().readGraph("/lambda100.xml"); //HERE
        } catch (DataIOException exception) {
            System.err.println("Error loading graph from file. Aborting.");
            System.exit(1);
        }

        System.out.println( PrefuseLib.getMemoryUsageInMB() );

        //Visualization
        Visualization viz = new Visualization();
        viz.add("graph",graph);

        System.out.println( PrefuseLib.getMemoryUsageInMB() );

        //Initialize the Renderers
       // LabelRenderer labRenderer = new LabelRenderer(); //HERE
        // labRenderer.setRoundedCorner(8, 8);

        Renderer labRenderer = new ShapeRenderer(20);


        //Renderer polyR = new PolygonRenderer(Constants.POLY_TYPE_CURVE);
        //((PolygonRenderer)polyR).setCurveSlack(0.15f);

        DefaultRendererFactory drf = new DefaultRendererFactory(labRenderer);
       // drf.add(new InGroupPredicate("graph.nodes"), labRenderer);
        viz.setRendererFactory(drf);

        viz.setValue("graph.nodes", null, VisualItem.SHAPE, new Integer(Constants.SHAPE_ELLIPSE));

        //set up Actions
        //create pallete for male/female colors
        int[] palette = new int[] {
            ColorLib.rgb(255, 180, 180), ColorLib.rgb(190, 190, 255),
            ColorLib.rgb(100,255,200), ColorLib.rgb(0, 155, 155)
        };

        DataColorAction dataFill = new DataColorAction("graph.nodes", "label",
                Constants.NOMINAL, VisualItem.FILLCOLOR, palette);

        ColorAction text = new ColorAction("graph.nodes",
                VisualItem.TEXTCOLOR, ColorLib.gray(0));

        ColorAction edges = new ColorAction("graph.edges",
                VisualItem.STROKECOLOR, ColorLib.gray(200));

        ColorAction fill = new ColorAction("graph.edges",
                VisualItem.FILLCOLOR, ColorLib.gray(200));

        ColorAction stroke = new ColorAction("graph.nodes",
                VisualItem.STROKECOLOR, ColorLib.gray(0));

        ActionList color = new ActionList();
        color.add(stroke);
        color.add(fill);
        color.add(dataFill);
        color.add(edges);
        color.add(text);



        ActionList layout = new ActionList(ActionList.INFINITY);
        //layout.add(new ForceDirectedLayout("graph"));
        layout.add(new ForceDirectedLayout("graph"));
        

        layout.add(new RepaintAction());

        viz.putAction("layout", layout);
        viz.putAction("color", color);

        Display display = new Display(viz);
        display.setSize(720, 500);
        display.addControlListener(new DragControl());
        display.addControlListener(new PanControl());
        display.addControlListener(new ZoomControl());

        display.setHighQuality(true);

        //UILib.setPlatformLookAndFeel();
/*
        ForceSimulator fsim = ((ForceDirectedLayout)layout.get(0)).getForceSimulator();
        JForcePanel jfp = new JForcePanel(fsim);

        final JValueSlider slider = new JValueSlider("Distance", 0, 2.0, 1.0);
        

        jfp.add(slider);

      

        JSplitPane split = new JSplitPane();
        split.setLeftComponent(display);
        split.setRightComponent(jfp);
        split.setOneTouchExpandable(true);
        split.setContinuousLayout(false);
        split.setDividerLocation(700);
*/

        JFrame mainframe = new JFrame("p r e f u s e | e x a m p l e");
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.add(display);
        mainframe.pack();
        mainframe.setVisible(true);

        viz.run("color");
        viz.run("layout"); 
        
    }
}
