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

edu.uci.ics.jung.visualization.renderers.BasicRenderer 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.renderers;

import com.google.common.graph.Network;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationModel;
import edu.uci.ics.jung.visualization.VisualizationServer;
import edu.uci.ics.jung.visualization.spatial.Spatial;
import java.util.ConcurrentModificationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The default implementation of the Renderer used by the VisualizationViewer. Default Node and Edge
 * Renderers are supplied, or the user may set custom values. The Node and Edge renderers are used
 * in the renderNode and renderEdge methods, which are called in the render loop of the
 * VisualizationViewer.
 *
 * @author Tom Nelson
 */
public class BasicRenderer implements Renderer {

  private static final Logger log = LoggerFactory.getLogger(BasicRenderer.class);
  protected Node nodeRenderer = new BasicNodeRenderer();
  protected NodeLabel nodeLabelRenderer = new BasicNodeLabelRenderer();
  protected Renderer.Edge edgeRenderer = new BasicEdgeRenderer();
  protected Renderer.EdgeLabel edgeLabelRenderer = new BasicEdgeLabelRenderer();

  public void render(
      RenderContext renderContext,
      VisualizationModel visualizationModel,
      Spatial nodeSpatial,
      Spatial edgeSpatial) {
    if (nodeSpatial == null) {
      render(renderContext, visualizationModel);
      return;
    }
    Iterable visibleNodes = null;
    Iterable visibleEdges = null;

    try {
      visibleNodes =
          nodeSpatial.getVisibleElements(
              ((VisualizationServer) renderContext.getScreenDevice()).viewOnLayout());

      if (edgeSpatial != null) {
        visibleEdges =
            edgeSpatial.getVisibleElements(
                ((VisualizationServer) renderContext.getScreenDevice()).viewOnLayout());
      } else {
        visibleEdges = visualizationModel.getNetwork().edges();
      }
    } catch (ConcurrentModificationException ex) {
      // skip rendering until graph node index is stable,
      // this can happen if the layout relax thread is changing locations while the
      // visualization is rendering
      log.info("got {} so returning", ex);
      return;
    }

    try {
      Network network = visualizationModel.getNetwork();
      // paint all the edges
      log.trace("the visibleEdges are {}", visibleEdges);
      for (E e : visibleEdges) {
        if (network.edges().contains(e)) {
          renderEdge(renderContext, visualizationModel, e);
          renderEdgeLabel(renderContext, visualizationModel, e);
        }
      }
    } catch (ConcurrentModificationException cme) {
      renderContext.getScreenDevice().repaint();
    }

    // paint all the nodes
    try {
      log.trace("the visibleNodes are {}", visibleNodes);

      for (N v : visibleNodes) {
        renderNode(renderContext, visualizationModel, v);
        renderNodeLabel(renderContext, visualizationModel, v);
      }
    } catch (ConcurrentModificationException cme) {
      renderContext.getScreenDevice().repaint();
    }
  }

  @Override
  public void render(
      RenderContext renderContext, VisualizationModel visualizationModel) {
    Network network = visualizationModel.getNetwork();
    // paint all the edges
    try {
      for (E e : network.edges()) {
        renderEdge(renderContext, visualizationModel, e);
        renderEdgeLabel(renderContext, visualizationModel, e);
      }
    } catch (ConcurrentModificationException cme) {
      renderContext.getScreenDevice().repaint();
    }

    // paint all the nodes
    try {
      for (N v : network.nodes()) {
        renderNode(renderContext, visualizationModel, v);
        renderNodeLabel(renderContext, visualizationModel, v);
      }
    } catch (ConcurrentModificationException cme) {
      renderContext.getScreenDevice().repaint();
    }
  }

  public void renderNode(
      RenderContext renderContext, VisualizationModel visualizationModel, N v) {
    nodeRenderer.paintNode(renderContext, visualizationModel, v);
  }

  public void renderNodeLabel(
      RenderContext renderContext, VisualizationModel visualizationModel, N v) {
    nodeLabelRenderer.labelNode(
        renderContext, visualizationModel, v, renderContext.getNodeLabelFunction().apply(v));
  }

  public void renderEdge(
      RenderContext renderContext, VisualizationModel visualizationModel, E e) {
    edgeRenderer.paintEdge(renderContext, visualizationModel, e);
  }

  public void renderEdgeLabel(
      RenderContext renderContext, VisualizationModel visualizationModel, E e) {
    edgeLabelRenderer.labelEdge(
        renderContext, visualizationModel, e, renderContext.getEdgeLabelFunction().apply(e));
  }

  public void setNodeRenderer(Node r) {
    this.nodeRenderer = r;
  }

  public void setEdgeRenderer(Renderer.Edge r) {
    this.edgeRenderer = r;
  }

  /** @return the edgeLabelRenderer */
  public Renderer.EdgeLabel getEdgeLabelRenderer() {
    return edgeLabelRenderer;
  }

  /** @param edgeLabelRenderer the edgeLabelRenderer to set */
  public void setEdgeLabelRenderer(Renderer.EdgeLabel edgeLabelRenderer) {
    this.edgeLabelRenderer = edgeLabelRenderer;
  }

  /** @return the nodeLabelRenderer */
  public NodeLabel getNodeLabelRenderer() {
    return nodeLabelRenderer;
  }

  /** @param nodeLabelRenderer the nodeLabelRenderer to set */
  public void setNodeLabelRenderer(NodeLabel nodeLabelRenderer) {
    this.nodeLabelRenderer = nodeLabelRenderer;
  }

  /** @return the edgeRenderer */
  public Renderer.Edge getEdgeRenderer() {
    return edgeRenderer;
  }

  /** @return the nodeRenderer */
  public Node getNodeRenderer() {
    return nodeRenderer;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy