Properly Customizing the Visual Behavior of Nodes
Tips & TricksSummary
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.
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");
}
});
}
}