Java Data Sources > Java Data Source Adapters > Using IlsGraphDS2IlvGrapherAdapter > Managing New Types of Nodes
 
Managing New Types of Nodes
In this example, we are going to subclass nodes to create a class for colored nodes. Colored nodes will be used and updated according to the lockerStatus node property defined in the view specification (see the code sample in “Managing Row Properties”.). Any class of node objects must be derived from the class IlvGraphic, which is the base class for graphic objects in the Rogue Wave JViews library.
import ilog.views.*;
import ilog.views.awt.*;
import ilog.views.graphic.*;
 
 
public class ColourNode extends IlvReliefLabel {
 
private static final int DEFAULT_THICKNESS = 2;
private static final int DEFAULT_WIDTH = 7000;
private static final int DEFAULT_HEIGHT = 1450;
 
private static final Color DEFAULT_BACKGROUND = Color.white;
private static final Color DEFAULT_FOREGROUND = Color.black;
private static final Color DEFAULT_OTHERLOCKED = Color.red;
private static final Color DEFAULT_OWNLOCKED = Color.green;
 
static final int LOCKER_NONE = 0;
static final int LOCKER_ISANOTHER = 1;
static final int LOCKER_ISME = 2;
 
protected int _lockerStatus;
 
public NetworkGraphNode(float x, float y, String label, int lockerStatus) {
super(new IlvPoint(x, y), label, DEFAULT_THICKNESS);
resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setBackground(DEFAULT_BACKGROUND);
setForeground(DEFAULT_FOREGROUND);
setLockerStatus(lockerStatus);
}
 
public void setLockerStatus(int lockerStatus) {
if (_lockerStatus == lockerStatus) {
return;
}
_lockerStatus = lockerStatus;
switch (_lockerStatus) {
case LOCKER_NONE:
setBackground(DEFAULT_BACKGROUND);
break;
case LOCKER_ISME:
setBackground(DEFAULT_OWNLOCKED);
break;
case LOCKER_ISANOTHER:
setBackground(DEFAULT_OTHERLOCKED);
break;
}
reDraw();
}
 
}
In the next code extract, the ColorNode class is based on the Rogue Wave JViews class IlvReliefLabel because we want to display the node label also. The background color of a node changes according to the lockerStatus property (method setLockerStatus). To change the default node class associated with the representation object of the view, we must therefore also derive the graph adapter. To do this, we need to redefine the methods createNode and updateNode of the adapter.
public class NetworkGraphAdapter extends IlsGraphDS2IlvGrapherAdapter {
 
protected IlvGraphic createNode(Map properties) {
Object label = properties != null ? properties.get("label") : null;
Long x = (properties != null) ? (Long) properties.get("x") : null;
Long y = (properties != null) ? (Long) properties.get("y") : null;
Long status = (properties != null) ? (Long)
properties.get("lockerStatus") : null;
NetworkGraphNode graphic = new ColorNode(x == null ? 0 : x.floatValue(),
y == null ? 0 : y.floatValue(),
label == null ? "<unknown>"
: label.toString(),
status == null ? 0 : status.intValue());
if (_mgrView != null) {
_mgrView.fitTransformerToContent();
}
return graphic;
}
 
protected void updateNode(IlvGraphic node, Map properties) {
super.updateNode(node, properties);
NetworkGraphNode n = (NetworkGraphNode) node;
Long status = (properties != null) ? (Long)
properties.get("lockerStatus") : null;
if (status != null)
n.setLockerStatus(status.intValue());
}
 
}
Now, the adapter associates a ColorNode object with each node representation object (IlsRpNode) in the IlvGrapher instance. Each time a node representation object is created, the method createNode builds and returns a ColorNode instance. Likewise, each time a node is updated, the method updateNode updates the ColorNode instance to reflect the changes in the representation object.

Version 5.8
Copyright © 2014, Rogue Wave Software, Inc. All Rights Reserved.