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

edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer Maven / Gradle / Ivy

/*
 * Copyright (c) 2005, 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.
 *
 * Created on Apr 14, 2005
 */

package edu.uci.ics.jung.visualization.renderers;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Rectangle;
import java.io.Serializable;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

/**
 * DefaultEdgeLabelRenderer is similar to the cell renderers used by the JTable and JTree jfc
 * classes.
 *
 * @author Tom Nelson
 */
@SuppressWarnings("serial")
public class DefaultEdgeLabelRenderer extends JLabel implements EdgeLabelRenderer, Serializable {

  protected static Border noFocusBorder = new EmptyBorder(0, 0, 0, 0);

  protected Color pickedEdgeLabelColor = Color.black;
  protected boolean rotateEdgeLabels;

  public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor) {
    this(pickedEdgeLabelColor, true);
  }

  /**
   * Creates an instance with the specified properties.
   *
   * @param pickedEdgeLabelColor the color to use for rendering the labels of picked edges
   * @param rotateEdgeLabels whether the
   */
  public DefaultEdgeLabelRenderer(Color pickedEdgeLabelColor, boolean rotateEdgeLabels) {
    super();
    this.pickedEdgeLabelColor = pickedEdgeLabelColor;
    this.rotateEdgeLabels = rotateEdgeLabels;
    setOpaque(true);
    setBorder(noFocusBorder);
  }

  /** @return Returns the rotateEdgeLabels. */
  public boolean isRotateEdgeLabels() {
    return rotateEdgeLabels;
  }
  /** @param rotateEdgeLabels The rotateEdgeLabels to set. */
  public void setRotateEdgeLabels(boolean rotateEdgeLabels) {
    this.rotateEdgeLabels = rotateEdgeLabels;
  }
  /**
   * Overrides JComponent.setForeground to assign the unselected-foreground color to
   * the specified color.
   *
   * @param c set the foreground color to this value
   */
  @Override
  public void setForeground(Color c) {
    super.setForeground(c);
  }

  /**
   * Overrides JComponent.setBackground to assign the unselected-background color to
   * the specified color.
   *
   * @param c set the background color to this value
   */
  @Override
  public void setBackground(Color c) {
    super.setBackground(c);
  }

  /**
   * Notification from the UIManager that the look and feel has changed. Replaces the
   * current UI object with the latest version from the UIManager.
   *
   * @see JComponent#updateUI
   */
  @Override
  public void updateUI() {
    super.updateUI();
    setForeground(null);
    setBackground(null);
  }

  /**
   * Returns the default label renderer for an Edge
   *
   * @param vv the VisualizationViewer to render on
   * @param value the value to assign to the label for Edge
   * @param edge the Edge
   * @return the default label renderer
   */
  public  Component getEdgeLabelRendererComponent(
      JComponent vv, Object value, Font font, boolean isSelected, E edge) {

    super.setForeground(vv.getForeground());
    if (isSelected) {
      setForeground(pickedEdgeLabelColor);
    }
    super.setBackground(vv.getBackground());

    if (font != null) {
      setFont(font);
    } else {
      setFont(vv.getFont());
    }
    setIcon(null);
    setBorder(noFocusBorder);
    setValue(value);
    return this;
  }

  /*
   * Implementation Note
   * The following methods are overridden as a performance measure to
   * prune code-paths that are often called in the case of renders
   * but which we know are unnecessary.  Great care should be taken
   * when writing your own renderer to weigh the benefits and
   * drawbacks of overriding methods like these.
   */

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public boolean isOpaque() {
    Color back = getBackground();
    Component p = getParent();
    if (p != null) {
      p = p.getParent();
    }
    boolean colorMatch =
        (back != null) && (p != null) && back.equals(p.getBackground()) && p.isOpaque();
    return !colorMatch && super.isOpaque();
  }

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public void validate() {}

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public void revalidate() {}

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public void repaint(long tm, int x, int y, int width, int height) {}

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public void repaint(Rectangle r) {}

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    // Strings get interned...
    if (propertyName == "text") {
      super.firePropertyChange(propertyName, oldValue, newValue);
    }
  }

  /**
   * Overridden for performance reasons. See the Implementation Note for
   * more information.
   */
  @Override
  public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) {}

  /**
   * Sets the String object for the cell being rendered to value.
   *
   * @param value the string value for this cell; if value is null it sets the text
   *     value to an empty string
   * @see JLabel#setText
   */
  protected void setValue(Object value) {
    setText((value == null) ? "" : value.toString());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy