All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metacsp.utility.UI.GraphFrame Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-2013 Federico Pecora 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
package org.metacsp.utility.UI;

/*
 * 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.
 *
 */


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

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

import org.metacsp.framework.ConstraintNetwork;

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.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.algorithms.layout.util.Relaxer;
import edu.uci.ics.jung.algorithms.layout.util.VisRunner;
import edu.uci.ics.jung.algorithms.util.IterativeContext;
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.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.layout.LayoutTransition;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.util.Animator;

/**
 * A variation of AddNodeDemo that animates transitions between graph states.
 *
 * @author Tom Nelson
 */
public class GraphFrame extends JFrame {
	
	private static final Logger logger = Logger.getLogger(GraphFrame.class.getPackage().getName());
	
	private static final long serialVersionUID = 3896624751126451811L;

	private ObservableGraph g = null;

    private VisualizationViewer vv = null;

    private AbstractLayout layout = null;

    //Timer timer;

    boolean done;

    protected JButton switchLayout;
    
    //private int layer = 0;

    //public static final int EDGE_LENGTH = 100;
    
//	private class STNTransformer implements Transformer {
//		@Override
//		public Point2D transform(Variable arg0) {
//			Point ret = new Point((int) ((Long)arg0.getDomain().chooseValue("ET")*20), layer);
//			layer+=100;
//			return ret;
//		}
//	}


    
    public GraphFrame(ObservableGraph graph, String title) {
    	super(title);

        g = graph;
        g.addGraphEventListener(new GraphEventListener() {

			@Override
			public void handleGraphEvent(GraphEvent evt) {
				logger.log(Level.FINE, "Got " + evt);
				
				/****/
		    	vv.getRenderContext().getPickedVertexState().clear();
		    	vv.getRenderContext().getPickedEdgeState().clear();
		        try {

		                layout.initialize();

		                try {
		        			Relaxer relaxer = new VisRunner((IterativeContext)layout);
		        			relaxer.stop();
		        			relaxer.prerelax();
		                } catch (java.lang.ClassCastException e) {}
		                
		        		StaticLayout staticLayout =
		        			new StaticLayout(g, layout);
						LayoutTransition lt =
							new LayoutTransition(vv, vv.getGraphLayout(),
									staticLayout);
						Animator animator = new Animator(lt);
						animator.start();
//						vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
						vv.repaint();

		        } catch (Exception e) {
		            System.out.println(e);

		        }				
				/****/
			}});

        //create a graphdraw
        //layout = new FRLayout(g);
        //layout = new SpringLayout(g);
        //layout = new StaticLayout(g,new STNTransformer());
        layout = new FRLayout2(g);
        //layout = new CircleLayout(g);
        //layout = new ISOMLayout(g);
        //layout = new KKLayout(g);
        layout.setSize(new Dimension(600,600));
        
        
        try {
			Relaxer relaxer = new VisRunner((IterativeContext)layout);
			relaxer.stop();
			relaxer.prerelax();
        } catch (java.lang.ClassCastException e) {}
        
        
        Layout staticLayout =
			new StaticLayout(g, layout);

        vv = new VisualizationViewer(staticLayout, 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.setGraphMouse(new DefaultModalGraphMouse());

        vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.S);
        //vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vv.setForeground(Color.black);
        
        vv.addComponentListener(new ComponentAdapter() {

			/**
			 * @see java.awt.event.ComponentAdapter#componentResized(java.awt.event.ComponentEvent)
			 */
			@Override
			public void componentResized(ComponentEvent arg0) {
				super.componentResized(arg0);
				System.err.println("resized");
				layout.setSize(arg0.getComponent().getSize());
			}});

        getContentPane().add(vv);
        switchLayout = new JButton("Switch to SpringLayout");
        switchLayout.addActionListener(new ActionListener() {
        	
        	@Override
            public void actionPerformed(ActionEvent ae) {
            	Dimension d = vv.getSize();//new Dimension(600,600);
                if (switchLayout.getText().indexOf("Spring") > 0) {
                    switchLayout.setText("Switch to FRLayout");
                    //layout = new SpringLayout(g, new ConstantTransformer(EDGE_LENGTH));
                    layout = new SpringLayout(g);
                    layout.setSize(d);
                    
                    try {
            			Relaxer relaxer = new VisRunner((IterativeContext)layout);
            			relaxer.stop();
            			relaxer.prerelax();
                    } catch (java.lang.ClassCastException e) {}
            		
            		StaticLayout staticLayout =
            			new StaticLayout(g, layout);
    				LayoutTransition lt =
    					new LayoutTransition(vv, vv.getGraphLayout(),
    							staticLayout);
    				Animator animator = new Animator(lt);
    				animator.start();
    			//	vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
    				vv.repaint();

                } else {
                    switchLayout.setText("Switch to SpringLayout");
                    layout = new FRLayout(g, d);
                    layout.setSize(d);
                    
                    try {
            			Relaxer relaxer = new VisRunner((IterativeContext)layout);
            			relaxer.stop();
            			relaxer.prerelax();
                    } catch (java.lang.ClassCastException e) {}
                    
            		StaticLayout staticLayout =
            			new StaticLayout(g, layout);
    				LayoutTransition lt =
    					new LayoutTransition(vv, vv.getGraphLayout(),
    							staticLayout);
    				Animator animator = new Animator(lt);
    				animator.start();
    			//	vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
    				vv.repaint();

                }
            }
        });

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

    	this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    	this.pack();
    	this.setVisible(true);

    }

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy