Properly Customizing the Visual Behavior of Nodes
Applies to: yFiles for Java 2.8, 2.7, 2.6, 2.5, 2.4 print article email article

Type: Tips & Tricks

Properly customizing the visual behavior of nodes depends on the correct specialization of class NodeRealizer.

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.
Note
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");
      }
    });
  }
}
Note
To compile and run the demo, copy the attached file to the src/demo/view directory of your yFiles installation.

Keywords: custom - NodeRealizer - extending - subclassing - ShapeNodeRealizer - selection - generic - GenericNodeRealizer

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2012 yWorks · ALL RIGHTS RESERVED imprint | terms of use | privacy policy | home