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

edu.uci.ics.jung.visualization.VisualizationViewer Maven / Gradle / Ivy

The newest version!
/*
* Copyright (c) 2003, The JUNG Authors 
*
* All rights reserved.
*
* This software is open-source under the BSD license; see either
* "license.txt" or
* https://github.com/jrtom/jung/blob/master/LICENSE for a description.
*/
package edu.uci.ics.jung.visualization;

import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Point2D;

import javax.swing.ToolTipManager;

import com.google.common.base.Function;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.control.MouseListenerTranslator;

/**
 * Adds mouse behaviors and tooltips to the graph visualization base class
 * 
 * @author Joshua O'Madadhain
 * @author Tom Nelson 
 * @author Danyel Fisher
 */
@SuppressWarnings("serial")
public class VisualizationViewer extends BasicVisualizationServer {

	protected Function vertexToolTipTransformer;
	protected Function edgeToolTipTransformer;
	protected Function mouseEventToolTipTransformer;
	
    /**
     * provides MouseListener, MouseMotionListener, and MouseWheelListener
     * events to the graph
     */
    protected GraphMouse graphMouse;
    
    protected MouseListener requestFocusListener = new MouseAdapter() {
		public void mouseClicked(MouseEvent e) {
			requestFocusInWindow();
		}
    };


	public VisualizationViewer(Layout layout) {
	    this(new DefaultVisualizationModel(layout));
	}
	
	public VisualizationViewer(Layout layout, Dimension preferredSize) {
	    this(new DefaultVisualizationModel(layout, preferredSize), preferredSize);
	}
	
	public VisualizationViewer(VisualizationModel model) {
	    this(model, new Dimension(600,600));
	}

    public VisualizationViewer(VisualizationModel model,
	        Dimension preferredSize) {
        super(model, preferredSize);
		setFocusable(true);
        addMouseListener(requestFocusListener);
	}
	
	/**
	 * a setter for the GraphMouse. This will remove any
	 * previous GraphMouse (including the one that
	 * is added in the initMouseClicker method.
	 * @param graphMouse new value
	 */
	public void setGraphMouse(GraphMouse graphMouse) {
	    this.graphMouse = graphMouse;
	    MouseListener[] ml = getMouseListeners();
	    for(int i=0; iGraphMouse
	 */
	public GraphMouse getGraphMouse() {
	    return graphMouse;
	}

	/**
	 * This is the interface for adding a mouse listener. The GEL
	 * will be called back with mouse clicks on vertices.
	 * @param gel the mouse listener to add
	 */
	public void addGraphMouseListener( GraphMouseListener gel ) {
		addMouseListener( new MouseListenerTranslator( gel, this ));
	}
	
	/** 
	 * Override to request focus on mouse enter, if a key listener is added
	 * @see java.awt.Component#addKeyListener(java.awt.event.KeyListener)
	 */
	@Override
	public synchronized void addKeyListener(KeyListener l) {
		super.addKeyListener(l);
	}
	
	/**
	 * @param edgeToolTipTransformer the edgeToolTipTransformer to set
	 */
	public void setEdgeToolTipTransformer(
			Function edgeToolTipTransformer) {
		this.edgeToolTipTransformer = edgeToolTipTransformer;
		ToolTipManager.sharedInstance().registerComponent(this);
	}

	/**
	 * @param mouseEventToolTipTransformer the mouseEventToolTipTransformer to set
	 */
	public void setMouseEventToolTipTransformer(
			Function mouseEventToolTipTransformer) {
		this.mouseEventToolTipTransformer = mouseEventToolTipTransformer;
		ToolTipManager.sharedInstance().registerComponent(this);
	}

	/**
	 * @param vertexToolTipTransformer the vertexToolTipTransformer to set
	 */
	public void setVertexToolTipTransformer(
			Function vertexToolTipTransformer) {
		this.vertexToolTipTransformer = vertexToolTipTransformer;
		ToolTipManager.sharedInstance().registerComponent(this);
	}

    /**
     * called by the superclass to display tooltips
     */
    public String getToolTipText(MouseEvent event) {
        Layout layout = getGraphLayout();
        Point2D p = null;
        if(vertexToolTipTransformer != null) {
            p = event.getPoint();
            	//renderContext.getBasicTransformer().inverseViewTransform(event.getPoint());
            V vertex = getPickSupport().getVertex(layout, p.getX(), p.getY());
            if(vertex != null) {
            	return vertexToolTipTransformer.apply(vertex);
            }
        }
        if(edgeToolTipTransformer != null) {
        	if(p == null) p = renderContext.getMultiLayerTransformer().inverseTransform(Layer.VIEW, event.getPoint());
            E edge = getPickSupport().getEdge(layout, p.getX(), p.getY());
            if(edge != null) {
            	return edgeToolTipTransformer.apply(edge);
            }
        }
        if(mouseEventToolTipTransformer != null) {
        	return mouseEventToolTipTransformer.apply(event);
        }
        return super.getToolTipText(event);
    }

    /**
     * a convenience type to represent a class that
     * processes all types of mouse events for the graph
     */
    public interface GraphMouse extends MouseListener, MouseMotionListener, MouseWheelListener {}
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy