main.AddNodeDemo.java Source code

Java tutorial

Introduction

Here is the source code for main.AddNodeDemo.java

Source

/*
 * Copyright (c) 2003, the JUNG Project and the Regents of the University of
 * California All rights reserved.
 *
 * This software is open-source under the BSD license; see either "license.txt"
 * or http://jung.sourceforge.net/license.txt for a description.
 *
 * Created on May 10, 2004
 */

package main;

import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.FRLayout2;
import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.ObservableGraph;
import edu.uci.ics.jung.graph.event.GraphEvent;
import edu.uci.ics.jung.graph.event.GraphEventListener;
import edu.uci.ics.jung.graph.util.Graphs;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import model.VertexTopology;
import model.TestCase;

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.ConstantTransformer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRootPane;

/**
 * Demonstrates visualization of a graph being actively updated.
 *
 * @author danyelf
 */
public class AddNodeDemo extends javax.swing.JApplet {

    /**
    *
    */
    private static final long serialVersionUID = -5345319851341875800L;

    private Graph<String, Number> g = null;

    private VisualizationViewer<String, Number> vv = null;

    private AbstractLayout<String, Number> layout = null;

    Timer timer;

    boolean done;

    protected JButton switchLayout;

    //    public static final LengthFunction<Number> UNITLENGTHFUNCTION = new SpringLayout.UnitLengthFunction<Number>(
    //            100);
    public static final int EDGE_LENGTH = 100;

    @Override
    public void init() {

        //create a graph
        Graph<String, Number> ig = Graphs
                .<String, Number>synchronizedDirectedGraph(new DirectedSparseMultigraph<String, Number>());

        ObservableGraph<String, Number> og = new ObservableGraph<String, Number>(ig);
        og.addGraphEventListener(new GraphEventListener<String, Number>() {

            public void handleGraphEvent(GraphEvent<String, Number> evt) {
                System.err.println("got " + evt);

            }
        });
        this.g = og;
        //create a graphdraw
        //        layout = new FRLayout2<String,Number>(g);
        //        layout = new SpringLayout<String,Number>(g);
        //        ((FRLayout)layout).setMaxIterations(200);
        layout = new KKLayout<String, Number>(g);

        vv = new VisualizationViewer<String, Number>(layout, new Dimension(600, 600));

        JRootPane rp = this.getRootPane();
        rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().setBackground(java.awt.Color.lightGray);
        getContentPane().setFont(new Font("Serif", Font.PLAIN, 12));

        vv.getModel().getRelaxer().setSleepTime(500);
        vv.setGraphMouse(new DefaultModalGraphMouse<Number, Number>());

        vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
        vv.setForeground(Color.white);

        FontMetrics fm = vv.getFontMetrics(vv.getFont());
        int width = fm.stringWidth(g.toString());

        Transformer<String, Shape> vertexSize = new Transformer<String, Shape>() {
            public Shape transform(String i) {

                Ellipse2D circle = new Ellipse2D.Double(-20, -20, 40, 40);
                // in this case, the vertex is twice as large                
                return circle;
            }

        };

        //        vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller() {
        //            @Override
        //            public String transform(Object v) {
        //
        //                return ((VertexTopology)v).screenName;
        //            }});

        vv.getRenderContext().setVertexShapeTransformer(vertexSize);

        getContentPane().setPreferredSize(new Dimension(1400, 900));
        getContentPane().add(vv);
        switchLayout = new JButton("Switch to SpringLayout");
        //        switchLayout.addActionListener(new ActionListener() {
        //
        //            @SuppressWarnings("unchecked")
        //            public void actionPerformed(ActionEvent ae) {
        //               Dimension d = new Dimension(600,600);
        //                if (switchLayout.getText().indexOf("Spring") > 0) {
        //                    switchLayout.setText("Switch to FRLayout");
        //                    layout = new SpringLayout<String,Number>(g,
        //                        new ConstantTransformer(EDGE_LENGTH));
        //                    layout.setSize(d);
        //                    vv.getModel().setGraphLayout(layout, d);
        //                } else {
        //                    switchLayout.setText("Switch to SpringLayout");
        //                    layout = new FRLayout<String,Number>(g, d);
        //                    vv.getModel().setGraphLayout(layout, d);
        //                }
        //            }
        //        });

        getContentPane().add(switchLayout, BorderLayout.SOUTH);

        timer = new Timer();
    }

    @Override
    public void start() {
        createTree();
    }

    Integer v_prev = null;

    //    public void process() {
    //
    //        try {
    //
    //            if (g.getVertexCount() < 100) {
    //               layout.lock(true);
    //                //add a vertex
    //                Integer v1 = new Integer(g.getVertexCount());
    //
    //                Relaxer relaxer = vv.getModel().getRelaxer();
    //                relaxer.pause();
    //                g.addVertex(v1);
    //                System.err.println("added node " + v1);
    //
    //                // wire it to some edges
    //                if (v_prev != null) {
    //                    g.addEdge(g.getEdgeCount(), v_prev, v1);
    //                    // let's connect to a random vertex, too!
    //                    int rand = (int) (Math.random() * g.getVertexCount());
    //                    g.addEdge(g.getEdgeCount(), v1, rand);
    //                }
    //
    //                v_prev = v1;
    //
    //                layout.initialize();
    //                relaxer.resume();
    //                layout.lock(false);
    //            } else {
    //               done = true;
    //            }
    //
    //        } catch (Exception e) {
    //            System.out.println(e);
    //
    //        }
    //    }

    class RemindTask extends TimerTask {

        @Override
        public void run() {
            //            process();
            if (done)
                cancel();

        }
    }

    public static void main(String[] args) {
        AddNodeDemo and = new AddNodeDemo();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(and);

        and.init();
        and.start();
        frame.pack();
        frame.setVisible(true);
    }

    private void createTree() {
        //       g.addVertex("V22");
        //       g.addVertex("V1");
        //       g.addEdge(edgeFactory.create(), "V0", "V1");

        g.addEdge(edgeFactory.create(), "Carbon", "Calypso");
        g.addEdge(edgeFactory.create(), "Carbon2", "Calypso");
        g.addEdge(edgeFactory.create(), "Carbon3", "Calypso");
        g.addEdge(edgeFactory.create(), "mx300g2", "asterix-ref");
        g.addEdge(edgeFactory.create(), "sx20", "asterix-ref");
        g.addEdge(edgeFactory.create(), "drishti", "asterix-ref");
        g.addEdge(edgeFactory.create(), "dx70", "asterix-ref");
        g.addEdge(edgeFactory.create(), "dx80", "asterix-ref");

    }

    Factory<Integer> edgeFactory = new Factory<Integer>() {
        int i = 0;

        public Integer create() {
            return i++;
        }
    };

}