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

net.sf.gluebooster.java.booster.basic.math.graph.GraphDisplayConfiguration Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.math.graph;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.beans.ExceptionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.swing.Icon;

import org.apache.commons.collections4.Transformer;

import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import net.sf.gluebooster.java.booster.basic.container.SimpleBoostedNode;
import net.sf.gluebooster.java.booster.basic.transformation.CallableToShape;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.meta.HasName;
import net.sf.gluebooster.java.booster.essentials.meta.Stringable;
import net.sf.gluebooster.java.booster.essentials.meta.objects.DisplayConfiguration;
import net.sf.gluebooster.java.booster.essentials.meta.objects.ObjectDescription;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;

/**
 * Configuration how a graph is to be displayed
 * 
 * @author CBauer
 *
 */
public class GraphDisplayConfiguration extends DisplayConfiguration {


	/**
	 * Should the vertex labels be displayed.
	 */
	private boolean displayVertexLabel = false;

	/**
	 * The position of the label relative to the center of the vertex.
	 */
	private Renderer.VertexLabel.Position vertexLabelPosition = Renderer.VertexLabel.Position.CNTR;

	/**
	 * Generates the label for a vertex
	 */
	private Callable vertexLabelTransformer;

	/**
	 * Generates the display of the vertex (if it should not just be a shape)
	 */
	private Callable vertexIconTransformer;
	/**
	 * Should the tooltips of vertices be displayed.
	 */
	private boolean displayVertexTooltip = true;

	/**
	 * Generates the tooltip for a vertex.
	 */
	private Callable vertexToolTipTransformer;

	/**
	 * Should the label of a vertex be the value or the name (default).
	 */
	private boolean displayVertexValueInsteadOfName = false;

	/**
	 * The level how detailed a vertex should be displayed.
	 */
	private int vertexDetailDisplayLevel;

	/**
	 * The vertices that should be highlighted.
	 */
	private Collection highlightedVertices;

	/**
	 * The colors of the vertices
	 */
	private Map vertexColors;

	/**
	 * Listeners for user keyboard interaction
	 */
	private List keyListeners = new ArrayList();

	/**
	 * Generates the shape of a vertex.
	 */
	private Callable vertexShapeTransformer;


	/**
	 * The background color of vertices.
	 */
	private Callable vertexBackgroundTransformer;

	/**
	 * Listener for user mouse interaction.
	 */
	private GraphMouseListener clickListener;


	/**
	 * if false an exception will occur if there is overlapping (of nodes)
	 */
	private ObjectDescription overlappingAllowed = ObjectDescription.createFlag(true, false);

	/**
	 * Listener to exceptions
	 */
	private ExceptionListener exceptionListener;

	/**
	 * a transformer that extracts information of a vertex (or an edge).
	 * 
	 * @author cbauer
	 *
	 */
	private class ToStringTransformer extends CallableAbstraction implements Transformer {

		@Override
		public String transform(Object vertexOrEdge) {
			try {
				// modified here
				// TODO do some refactoring
				// SimpleBoostedNode should inherit from BoostedObject
				// BoostedObject should get the objectAttributes and should
				// be
				// stringable
				// There should be an interface HasAttributes and the
				// objectattributes should be stringable

				Object objectToDisplay = vertexOrEdge;// default

				if (displayVertexValueInsteadOfName) {
					if (vertexOrEdge instanceof SimpleBoostedNode) {
						objectToDisplay = ((SimpleBoostedNode) vertexOrEdge).getAttributes().getValue();
					}
				}

				if (objectToDisplay == null) {
					return "null";
				} else if (objectToDisplay instanceof Stringable) {
					return ((Stringable) objectToDisplay).toString(vertexDetailDisplayLevel);
				} else if (objectToDisplay instanceof HasName) {
					return "" + ((HasName) objectToDisplay).getName();
				} else {
					return objectToDisplay.toString();
				}

				/*
				 * String name = ""; if (node instanceof HasName) name = name + ((HasName) node).getName(); else name = name + node; return name;
				 */
			} catch (Exception ex) {
				throw ThrowableBoostUtils.toRuntimeException(ex);
			}
		}

		@Override
		protected String callImpl(Object... parameters) throws Exception {

			return transform(parameters[0]);
		}
	};

	/**
	 * Creates a listener that logs the details of the clicked vertex
	 * 
	 * @return the created listener
	 */
	public GraphMouseListener createLoggingVertexDetailsListener() {
		Transformer vertexMaxDetailStringer = new ToStringTransformer();
		GraphMouseListener result = createLoggingVertexClickListener(vertexMaxDetailStringer);
		return result;
	}


	/**
	 * The background color of vertices.
	 * 
	 * @return the created transformer
	 */
	public Callable createDefaultNodesBackgroundTransformer() {
		return new CallableAbstraction() {

			@Override
			protected Paint callImpl(Vertex... nodes) throws Exception {
				Vertex node = nodes[0];
				if (highlightedVertices != null
						&& highlightedVertices.contains(node)) {
					return Color.GREEN;
				} else if (vertexColors != null
						&& vertexColors.containsKey(node)) {
					return vertexColors.get(node);
				} else {
					return Color.BLUE;
				}
			}
		};
	}

	/**
	 * Returns a transformer that creates a rectangle accoring the size of the label of the node.
	 * 
	 * @return the created transformer
	 */
	public Callable createRectangleForLabelTransformer() {
		return CallableToShape.createRectangleForString("TransformerToShape", vertexLabelTransformer);
	}



	/**
	 * Create a new transformer into a string
	 * 
	 * @return the transformer
	 */
	private ToStringTransformer createToStringTransformer() {
		return new ToStringTransformer();
	}

	/**
	 * Create a configuration for a graph with rectangular vertices and text as label.
	 * 
	 * @return the created configuration
	 */
	public static GraphDisplayConfiguration createRectangularNodesWithTextConfiguration() {
		GraphDisplayConfiguration result = new GraphDisplayConfiguration();

		result.setDisplayVertexLabel(true);
		result.setVertexLabelTransformer(result.createToStringTransformer());
		result.setVertexShapeTransformer(result
				.createRectangleForLabelTransformer());

		result.setClickListener(result.createLoggingVertexDetailsListener());
		return result;
	}
	

	/**
	 * Creates the configuration of a graph with colored nodes without labels but tooltips.
	 * 
	 * @return the created configuration
	 */
	public static GraphDisplayConfiguration createFilledNodesWithoutTextConfiguration() {
		GraphDisplayConfiguration result = new GraphDisplayConfiguration();
		result.vertexBackgroundTransformer = result.createDefaultNodesBackgroundTransformer();
		result.setVertexToolTipTransformer(result.createToStringTransformer());

		result.setClickListener(result.createLoggingVertexDetailsListener());
		return result;
	}
	
	/**
	 * Creates a configuration of a graph with simple display and tooltips.
	 * 
	 * @return the created configuration
	 */
	public static GraphDisplayConfiguration createUnadornedConfiguration() {
		GraphDisplayConfiguration result = new GraphDisplayConfiguration();
		result.setVertexToolTipTransformer(result.createToStringTransformer());
		result.setClickListener(result.createLoggingVertexDetailsListener());
		return result;
	}

	/**
	 * Private so that the create methods need to be used
	 */
	public GraphDisplayConfiguration() {
		setTitle("Graph");

	}

	public Callable getVertexBackgroundTransformer() {
		return vertexBackgroundTransformer;
	}

	public void setVertexBackgroundTransformer(
			Callable vertexBackgroundTransformer) {
		this.vertexBackgroundTransformer = vertexBackgroundTransformer;
	}

	public boolean isDisplayVertexValueInsteadOfName() {
		return displayVertexValueInsteadOfName;
	}

	public void setDisplayVertexValueInsteadOfName(
			boolean displayVertexValueInsteadOfName) {
		this.displayVertexValueInsteadOfName = displayVertexValueInsteadOfName;
	}

	public int getVertexDetailDisplayLevel() {
		return vertexDetailDisplayLevel;
	}

	public void setVertexDetailDisplayLevel(int vertexDetailDisplayLevel) {
		this.vertexDetailDisplayLevel = vertexDetailDisplayLevel;
	}


	public Collection getHighlightedVertices() {
		return highlightedVertices;
	}

	public void setHighlightedVertices(Collection highlightedVertices) {
		this.highlightedVertices = highlightedVertices;
	}

	public Map getVertexColors() {
		return vertexColors;
	}

	public void setVertexColors(Map vertexColors) {
		this.vertexColors = vertexColors;
	}

	public List getKeyListeners() {
		return keyListeners;
	}

	public Callable getVertexShapeTransformer() {
		return vertexShapeTransformer;
	}

	public void setVertexShapeTransformer(
			Callable vertexShapeTransformer) {
		this.vertexShapeTransformer = vertexShapeTransformer;
	}

	public boolean isDisplayVertexLabel() {
		return displayVertexLabel;
	}

	public void setDisplayVertexLabel(boolean displayVertexLabel) {
		this.displayVertexLabel = displayVertexLabel;
	}

	public boolean isDisplayVertexTooltip() {
		return displayVertexTooltip;
	}

	public void setDisplayVertexTooltip(boolean displayVertexTooltip) {
		this.displayVertexTooltip = displayVertexTooltip;
	}

	public Renderer.VertexLabel.Position getVertexLabelPosition() {
		return vertexLabelPosition;
	}

	public void setVertexLabelPosition(
			Renderer.VertexLabel.Position vertexLabelPosition) {
		this.vertexLabelPosition = vertexLabelPosition;
	}

	public Callable getVertexLabelTransformer() {
		return vertexLabelTransformer;
	}

	public void setVertexLabelTransformer(
			Callable vertexLabelTransformer) {
		this.vertexLabelTransformer = vertexLabelTransformer;
	}


	public Callable getVertexToolTipTransformer() {
		return vertexToolTipTransformer;
	}

	public void setVertexToolTipTransformer(
			Callable vertexToolTipTransformer) {
		this.vertexToolTipTransformer = vertexToolTipTransformer;
	}

	/**
	 * Creates a listener that logs the nodes that are clicked.
	 * 
	 * @param logTextCreator
	 *            creates the text from the vertices
	 * @return the created listener
	 */
	public static  GraphMouseListener createLoggingVertexClickListener(
			final Transformer logTextCreator) {

		GraphMouseListener clickListener = new GraphMouseListener() {

			@Override
			public void graphClicked(Vertex v, MouseEvent me) {
				String text = logTextCreator.transform(v);
				System.out.println(text);

			}

			@Override
			public void graphPressed(Vertex v, MouseEvent me) {

			}

			@Override
			public void graphReleased(Vertex v, MouseEvent me) {

			}
		};

		return clickListener;
	}

	public void setKeyListeners(List keyListeners) {
		this.keyListeners = keyListeners;
	}

	public GraphMouseListener getClickListener() {
		return clickListener;
	}

	public void setClickListener(GraphMouseListener clickListener) {
		this.clickListener = clickListener;
	}

	/**
	 * Get the dimension of the graph.
	 * 
	 * @return the computed dimension
	 */
	public Dimension getLayoutDimension() {
		return new Dimension(getPreferredSize().width - 80, getPreferredSize().height - 80);
		// subtract a bit for the borders
	}

	public ObjectDescription getOverlappingAllowed() {
		return overlappingAllowed;
	}

	public void setOverlappingAllowed(ObjectDescription overlappingAllowed) {
		this.overlappingAllowed = overlappingAllowed;
	}

	public Callable getVertexIconTransformer() {
		return vertexIconTransformer;
	}

	public void setVertexIconTransformer(Callable vertexIconTransformer) {
		this.vertexIconTransformer = vertexIconTransformer;
	}


	public ExceptionListener getExceptionListener() {
		return exceptionListener;
	}

	public void setExceptionListener(ExceptionListener exceptionListener) {
		this.exceptionListener = exceptionListener;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy