Subclassing IlvGeneralNode to add CSS parameters and change the node shape

Article ID: 2250
Last updated: 29 May, 2018
Article ID: 2250
Last updated: 29 May, 2018
Revision: 3
Views: 566
Posted: 01 Mar, 2011
by Dean J.
Updated: 29 May, 2018
by Gargani A.

Question

How to subclass IlvGeneralNode to add CSS parameters and change the node shape?

Answer

This sample shows an SDM graph with two nodes and a link. The shape of each node is set to an IlvGraphicSet containing an IlvLabel on top and an IlvIcon below. This is done using MyNode, a subclass of IlvGeneralNode. The fields for the text and the icon are set through the CSS file.

Note that an icon file has to have 24-bit color depth in order for Java to know which colors to use.

To run the sample, unzip the file sdm_subclass.zip, then compile "MyNode.java" and "Main.java", and finally run "Main".

MyNode.java

import ilog.views.sdm.graphic.*;
import ilog.views.graphic.*;
import ilog.views.*;
import java.awt.*;
public class MyNode extends IlvGeneralNode {
private String iconKey;
private String textKey;
public void setIconKey(String inIconKey)
{
iconKey = inIconKey;
if(textKey != null)
mySetShape();
}
public void setTextKey(String inTextKey)
{
textKey = inTextKey;
if(iconKey != null)
mySetShape();
}
private void mySetShape()
{
IlvGraphicSet graphicSet = new IlvGraphicSet();
graphicSet.addObject(
new IlvIcon(iconKey, new IlvRect(0, 0, 32, 32)), false);
graphicSet.addObject(
new IlvLabel(new IlvPoint(16, -8), textKey), false);
setShapeHeight(graphicSet.boundingBox(null).heightFloor());
setShapeWidth(graphicSet.boundingBox(null).widthFloor());
setShape(graphicSet);
}
}

Main.java

import ilog.views.*;
import ilog.views.interactor.*;
import ilog.views.sdm.*;
import ilog.views.swing.*;
import java.io.*;
import javax.swing.*;
public class Main
{
public static void main(String args[]) throws IOException, IlvSDMException
{
// Create a window
JFrame window = new JFrame("Main");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(20, 20, 400, 300);
// Add an SDM engine with a select interactor to the window
IlvSDMEngine engine = new IlvSDMEngine();
IlvManagerView view = new IlvManagerView(engine.getGrapher());
view.setInteractor(new IlvSelectInteractor());
window.getContentPane().add(new IlvJScrollManagerView(view));
// Load the style sheet and the XML file
engine.setStyleSheets(new String[] { "file:simple.css" });
engine.setXMLFile("file:simple.xml");
// Show the window
window.show();
}
}

simple.css

node {
class : MyNode;
iconKey : "icon.png";
textKey : @label;
}
link {
class : ilog.views.sdm.graphic.IlvGeneralLink;
lineWidth : 4;
minimumLineWidth : 2;
maximumLineWidth : 8;
oriented : true;
foreground : "blue";
}

simple.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE SDM>
<SDM>
<node id="node1">
<property name="label">Node 1</property>
<property name="x">50</property>
<property name="y">50</property>
</node>
<node id="node2">
<property name="label">Node 2</property>
<property name="x">150</property>
<property name="y">50</property>
</node>
<link islink="true" id="link1" from="node1" to="node2">
<property name="type">IN</property>
</link>
</SDM>

Note: In order to run this sample with JViews 8.7 and later, you must call the ilog.views.util.IlvProductUtil.DeploymentLicenseRequired method with the appropriate argument. See the General information > Deployment licenses > Declaring the use of IBM ILOG JViews services section in the documentation for more information.

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2250
Last updated: 29 May, 2018
Revision: 3
Views: 566
Posted: 01 Mar, 2011 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Attached files


Others in this category