/*
 * 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 java.io.IOException;





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

    public static void main(String[] argv) {
        Graph graph = null;
        try {
            graph = MatrixIn.getGraphfromFile();
        } catch (IOException e) {
        }

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

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

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

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


        //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));

        ActionList color = new ActionList();
        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());

        //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(split);
        mainframe.pack();
        mainframe.setVisible(true);

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