Properly Customizing the Visual Behavior of Nodes

Tips & Tricks

Summary

Properly customizing the visual behavior of nodes depends on the correct specialization of class NodeRealizer.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

In yFiles, visual representation of graph elements is handled by so-called "realizer" classes. There are several different NodeRealizer and also several different EdgeRealizer implementations available.

Since yFiles 2.3 GenericNodeRealizer is a new Realizer implementation that can be used to easily customize the visual appearance in a pluggable way.

To customize the default visual representation of nodes, abstract class NodeRealizer offers various setter methods. However, there are cases where subclassing of NodeRealizer becomes necessary. Then, it is important to customize all necessary methods a proper realizer needs. Most importantly, a realizer implementation has to provide its own 'createCopy()' method that supersedes NodeRealizer.createCopy(y.view.NodeRealizer).

The following sample code is a demo application that defines a minimalistic specialization of class ShapeNodeRealizer.
What the sample code does:

  • Creates a small demo where the default node type has customized selection indication behavior. In particular, selecting a node does not darken the node's fill color, only the resize knobs are added around the node's border.

package demo.view;

import java.awt.Color;
import java.awt.EventQueue;

import y.view.Graph2D;
import y.view.NodeRealizer;
import y.view.ShapeNodeRealizer;

public class CustomSelectionColorDemo extends ViewActionDemo {
  Graph2D graph = null;

  public CustomSelectionColorDemo() {
    MyShapeNodeRealizer msnr = new MyShapeNodeRealizer();
    // The node's original fill color(s) should be used when it gets selected.
    // Effectively, this means that, when selected, only the resize knobs are added
    // to the node's representation.
    msnr.useOriginalFillColor(false);
    msnr.setUniformSelectionColor(Color.yellow);

    graph = view.getGraph2D();
    graph.setDefaultNodeRealizer(msnr);
  }

  public class MyShapeNodeRealizer extends ShapeNodeRealizer {
    boolean useOriginalFillColor = true;
    Color uniformSelectionColor = null;

    public Color getUniformSelectionColor() {
      return uniformSelectionColor;
    }
    public void setUniformSelectionColor(Color uniformSelectionColor) {
      this.uniformSelectionColor = uniformSelectionColor;
    }

    public boolean isOriginalFillColor() {
      return useOriginalFillColor;
    }
    public void useOriginalFillColor(boolean value) {
      useOriginalFillColor = value;
    }

    protected Color createSelectionColor(Color color) {
      if (isOriginalFillColor() || uniformSelectionColor == null) {
        return color;
      }
      else {
        return uniformSelectionColor;
      }
    }

    public MyShapeNodeRealizer() {
      super();
    }

    public MyShapeNodeRealizer(NodeRealizer arg) {
      super(arg);
      if (arg instanceof MyShapeNodeRealizer) {
        MyShapeNodeRealizer msnr = (MyShapeNodeRealizer)arg;
        useOriginalFillColor = msnr.isOriginalFillColor();
        uniformSelectionColor = msnr.getUniformSelectionColor();
      }
    }

    // This method is essential.
    // Without it, class MyShapeNodeRealizer would not work properly!
    public NodeRealizer createCopy(NodeRealizer arg) {
      MyShapeNodeRealizer msnr = new MyShapeNodeRealizer(arg);
      msnr.useOriginalFillColor = this.useOriginalFillColor;
      msnr.uniformSelectionColor = this.uniformSelectionColor;

      return msnr;
    }
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        initLnF();
        new CustomSelectionColorDemo().start("Custom Selection Color Demo");
      }
    });
  }
}
To compile and run the demo, copy the attached file to the src/demo/view directory of your yFiles installation.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > Bringing Graph Elements to Life: The Realizer Concept
Applies to:
yFiles for Java 2: 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
custom - NodeRealizer - extending - subclassing - ShapeNodeRealizer - selection - generic - GenericNodeRealizer