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

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

/*
 * 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 com.google.common.graph.Network;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.decorators.ParallelEdgeShapeFunction;
import edu.uci.ics.jung.visualization.layout.NetworkElementAccessor;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.DefaultNodeLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.EdgeLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.NodeLabelRenderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;
import edu.uci.ics.jung.visualization.util.ArrowFactory;
import edu.uci.ics.jung.visualization.util.Context;
import edu.uci.ics.jung.visualization.util.EdgeIndexFunction;
import edu.uci.ics.jung.visualization.util.ParallelEdgeIndexFunction;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.Ellipse2D;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.swing.CellRendererPane;
import javax.swing.Icon;
import javax.swing.JComponent;

public class PluggableRenderContext implements RenderContext {

  protected float arrowPlacementTolerance = 1;
  protected Predicate nodeIncludePredicate = n -> true;
  protected Function nodeStrokeFunction = n -> new BasicStroke(1.0f);

  protected Function nodeShapeFunction =
      n -> new Ellipse2D.Float(-10, -10, 20, 20);

  protected Function nodeLabelFunction = n -> null;
  protected Function nodeIconFunction;
  protected Function nodeFontFunction = n -> new Font("Helvetica", Font.PLAIN, 12);

  protected Function nodeDrawPaintFunction = n -> Color.BLACK;
  protected Function nodeFillPaintFunction = n -> Color.RED;
  protected Function nodeLabelDrawPaintFunction = n -> Color.BLACK;

  protected Function edgeLabelFunction = e -> null;
  protected Function edgeStrokeFunction = e -> new BasicStroke(1.0f);
  protected Function edgeArrowStrokeFunction = e -> new BasicStroke(1.0f);

  private static final int EDGE_ARROW_LENGTH = 10;
  private static final int EDGE_ARROW_WIDTH = 8;
  private static final int EDGE_ARROW_NOTCH_DEPTH = 4;
  protected Shape edgeArrow;
  protected boolean renderEdgeArrow;

  protected Predicate edgeIncludePredicate = n -> true;
  protected Function edgeFontFunction = n -> new Font("Helvetica", Font.PLAIN, 12);

  private static final float DIRECTED_EDGE_LABEL_CLOSENESS = 0.65f;
  private static final float UNDIRECTED_EDGE_LABEL_CLOSENESS = 0.65f;
  protected float edgeLabelCloseness;

  protected Function, E>, Shape> edgeShapeFunction;
  protected Function edgeFillPaintFunction = n -> null;
  protected Function edgeDrawPaintFunction = n -> Color.black;
  protected Function arrowFillPaintFunction = n -> Color.black;
  protected Function arrowDrawPaintFunction = n -> Color.black;

  protected EdgeIndexFunction parallelEdgeIndexFunction;

  protected MultiLayerTransformer multiLayerTransformer = new BasicTransformer();

  /** pluggable support for picking graph elements by finding them based on their coordinates. */
  protected NetworkElementAccessor pickSupport;

  protected int labelOffset = LABEL_OFFSET;

  /** the JComponent that this Renderer will display the graph on */
  protected JComponent screenDevice;

  protected PickedState pickedNodeState;
  protected PickedState pickedEdgeState;

  /**
   * The CellRendererPane is used here just as it is in JTree and JTable, to allow a pluggable
   * JLabel-based renderer for Node and Edge label strings and icons.
   */
  protected CellRendererPane rendererPane = new CellRendererPane();

  /** A default GraphLabelRenderer - picked Node labels are blue, picked edge labels are cyan */
  protected NodeLabelRenderer nodeLabelRenderer = new DefaultNodeLabelRenderer(Color.blue);

  protected EdgeLabelRenderer edgeLabelRenderer = new DefaultEdgeLabelRenderer(Color.cyan);

  protected GraphicsDecorator graphicsContext;

  private EdgeShape edgeShape;

  PluggableRenderContext(Network graph) {
    this.edgeShapeFunction = new EdgeShape.QuadCurve();
    this.parallelEdgeIndexFunction = new ParallelEdgeIndexFunction<>();
    if (graph.isDirected()) {
      this.edgeArrow =
          ArrowFactory.getNotchedArrow(EDGE_ARROW_WIDTH, EDGE_ARROW_LENGTH, EDGE_ARROW_NOTCH_DEPTH);
      this.renderEdgeArrow = true;
      this.edgeLabelCloseness = DIRECTED_EDGE_LABEL_CLOSENESS;
    } else {
      this.edgeArrow = ArrowFactory.getWedgeArrow(EDGE_ARROW_WIDTH, EDGE_ARROW_LENGTH);
      this.renderEdgeArrow = false;
      this.edgeLabelCloseness = UNDIRECTED_EDGE_LABEL_CLOSENESS;
    }
  }

  /** @return the nodeShapeFunction */
  public Function getNodeShapeFunction() {
    return nodeShapeFunction;
  }

  /** @param nodeShapeFunction the nodeShapeFunction to set */
  public void setNodeShapeFunction(Function nodeShapeFunction) {
    this.nodeShapeFunction = nodeShapeFunction;
  }

  /** @return the nodeStrokeFunction */
  public Function getNodeStrokeFunction() {
    return nodeStrokeFunction;
  }

  /** @param nodeStrokeFunction the nodeStrokeFunction to set */
  public void setNodeStrokeFunction(Function nodeStrokeFunction) {
    this.nodeStrokeFunction = nodeStrokeFunction;
  }

  public static float[] getDashing() {
    return dashing;
  }

  public static float[] getDotting() {
    return dotting;
  }

  public float getArrowPlacementTolerance() {
    return arrowPlacementTolerance;
  }

  public void setArrowPlacementTolerance(float arrow_placement_tolerance) {
    this.arrowPlacementTolerance = arrow_placement_tolerance;
  }

  public Shape getEdgeArrow() {
    return edgeArrow;
  }

  public void setEdgeArrow(Shape shape) {
    this.edgeArrow = shape;
  }

  public boolean renderEdgeArrow() {
    return this.renderEdgeArrow;
  }

  public void setRenderEdgeArrow(boolean render) {
    this.renderEdgeArrow = render;
  }

  public Function getEdgeFontFunction() {
    return edgeFontFunction;
  }

  public void setEdgeFontFunction(Function edgeFontFunction) {
    this.edgeFontFunction = edgeFontFunction;
  }

  public Predicate getEdgeIncludePredicate() {
    return edgeIncludePredicate;
  }

  public void setEdgeIncludePredicate(Predicate edgeIncludePredicate) {
    this.edgeIncludePredicate = edgeIncludePredicate;
  }

  public float getEdgeLabelCloseness() {
    return edgeLabelCloseness;
  }

  public void setEdgeLabelCloseness(float closeness) {
    this.edgeLabelCloseness = closeness;
  }

  public EdgeLabelRenderer getEdgeLabelRenderer() {
    return edgeLabelRenderer;
  }

  public void setEdgeLabelRenderer(EdgeLabelRenderer edgeLabelRenderer) {
    this.edgeLabelRenderer = edgeLabelRenderer;
  }

  public Function getEdgeFillPaintFunction() {
    return edgeFillPaintFunction;
  }

  public void setEdgeDrawPaintFunction(Function edgeDrawPaintFunction) {
    this.edgeDrawPaintFunction = edgeDrawPaintFunction;
  }

  public Function getEdgeDrawPaintFunction() {
    return edgeDrawPaintFunction;
  }

  public void setEdgeFillPaintFunction(Function edgeFillPaintFunction) {
    this.edgeFillPaintFunction = edgeFillPaintFunction;
  }

  public Function, E>, Shape> getEdgeShapeFunction() {
    return edgeShapeFunction;
  }

  public void setEdgeShapeFunction(Function, E>, Shape> edgeShapeFunction) {
    this.edgeShapeFunction = edgeShapeFunction;
    if (edgeShapeFunction instanceof ParallelEdgeShapeFunction) {
      @SuppressWarnings("unchecked")
      ParallelEdgeShapeFunction function =
          (ParallelEdgeShapeFunction) edgeShapeFunction;
      function.setEdgeIndexFunction(this.parallelEdgeIndexFunction);
    }
  }

  public Function getEdgeLabelFunction() {
    return edgeLabelFunction;
  }

  public void setEdgeLabelFunction(Function edgeLabelFunction) {
    this.edgeLabelFunction = edgeLabelFunction;
  }

  public Function edgeStrokeFunction() {
    return edgeStrokeFunction;
  }

  public void setEdgeStrokeFunction(Function edgeStrokeFunction) {
    this.edgeStrokeFunction = edgeStrokeFunction;
  }

  public Function getEdgeArrowStrokeFunction() {
    return edgeArrowStrokeFunction;
  }

  public void setEdgeArrowStrokeFunction(Function edgeArrowStrokeFunction) {
    this.edgeArrowStrokeFunction = edgeArrowStrokeFunction;
  }

  public GraphicsDecorator getGraphicsContext() {
    return graphicsContext;
  }

  public void setGraphicsContext(GraphicsDecorator graphicsContext) {
    this.graphicsContext = graphicsContext;
  }

  public int getLabelOffset() {
    return labelOffset;
  }

  public void setLabelOffset(int labelOffset) {
    this.labelOffset = labelOffset;
  }

  public EdgeIndexFunction getParallelEdgeIndexFunction() {
    return parallelEdgeIndexFunction;
  }

  public void setParallelEdgeIndexFunction(EdgeIndexFunction parallelEdgeIndexFunction) {
    this.parallelEdgeIndexFunction = parallelEdgeIndexFunction;
    // reset the edge shape Function, as the parallel edge index function
    // is used by it
    this.setEdgeShapeFunction(getEdgeShapeFunction());
  }

  public PickedState getPickedEdgeState() {
    return pickedEdgeState;
  }

  public void setPickedEdgeState(PickedState pickedEdgeState) {
    this.pickedEdgeState = pickedEdgeState;
  }

  public PickedState getPickedNodeState() {
    return pickedNodeState;
  }

  public void setPickedNodeState(PickedState pickedNodeState) {
    this.pickedNodeState = pickedNodeState;
  }

  public CellRendererPane getRendererPane() {
    return rendererPane;
  }

  public void setRendererPane(CellRendererPane rendererPane) {
    this.rendererPane = rendererPane;
  }

  public JComponent getScreenDevice() {
    return screenDevice;
  }

  public void setScreenDevice(JComponent screenDevice) {
    this.screenDevice = screenDevice;
    screenDevice.add(rendererPane);
  }

  public Function getNodeFontFunction() {
    return nodeFontFunction;
  }

  public void setNodeFontFunction(Function nodeFontFunction) {
    this.nodeFontFunction = nodeFontFunction;
  }

  public Function getNodeIconFunction() {
    return nodeIconFunction;
  }

  public void setNodeIconFunction(Function nodeIconFunction) {
    this.nodeIconFunction = nodeIconFunction;
  }

  public Predicate getNodeIncludePredicate() {
    return nodeIncludePredicate;
  }

  public void setNodeIncludePredicate(Predicate nodeIncludePredicate) {
    this.nodeIncludePredicate = nodeIncludePredicate;
  }

  public NodeLabelRenderer getNodeLabelRenderer() {
    return nodeLabelRenderer;
  }

  public void setNodeLabelRenderer(NodeLabelRenderer nodeLabelRenderer) {
    this.nodeLabelRenderer = nodeLabelRenderer;
  }

  public Function getNodeFillPaintFunction() {
    return nodeFillPaintFunction;
  }

  public void setNodeFillPaintFunction(Function nodeFillPaintFunction) {
    this.nodeFillPaintFunction = nodeFillPaintFunction;
  }

  public Function getNodeDrawPaintFunction() {
    return nodeDrawPaintFunction;
  }

  public void setNodeDrawPaintFunction(Function nodeDrawPaintFunction) {
    this.nodeDrawPaintFunction = nodeDrawPaintFunction;
  }

  public Function getNodeLabelFunction() {
    return nodeLabelFunction;
  }

  public void setNodeLabelFunction(Function nodeLabelFunction) {
    this.nodeLabelFunction = nodeLabelFunction;
  }

  public void setNodeLabelDrawPaintFunction(Function nodeLabelDrawPaintFunction) {
    this.nodeLabelDrawPaintFunction = nodeLabelDrawPaintFunction;
  }

  public Function getNodeLabelDrawPaintFunction() {
    return nodeLabelDrawPaintFunction;
  }

  public NetworkElementAccessor getPickSupport() {
    return pickSupport;
  }

  public void setPickSupport(NetworkElementAccessor pickSupport) {
    this.pickSupport = pickSupport;
  }

  public MultiLayerTransformer getMultiLayerTransformer() {
    return multiLayerTransformer;
  }

  public void setMultiLayerTransformer(MultiLayerTransformer basicTransformer) {
    this.multiLayerTransformer = basicTransformer;
  }

  public Function getArrowDrawPaintFunction() {
    return arrowDrawPaintFunction;
  }

  public Function getArrowFillPaintFunction() {
    return arrowFillPaintFunction;
  }

  public void setArrowDrawPaintFunction(Function arrowDrawPaintFunction) {
    this.arrowDrawPaintFunction = arrowDrawPaintFunction;
  }

  public void setArrowFillPaintFunction(Function arrowFillPaintFunction) {
    this.arrowFillPaintFunction = arrowFillPaintFunction;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy