Tree Graph with a tree nested grapher as a node

Article ID: 2284
Last updated: 29 May, 2018
Article ID: 2284
Last updated: 29 May, 2018
Revision: 3
Views: 580
Posted: 07 Dec, 2004
by Dean J.
Updated: 29 May, 2018
by Gargani A.

Question

How to create tree graph with a tree nested grapher as a node?

Answer

This sample uses two tree layouts, one for the main grapher and one for the subgrapher. The subgrapher tree is a node of the main grapher tree.
To set different layouts to the grapher, IlvDefaultLayoutProvider and IlvGrapherAdapter are used as follows:

IlvTreeLayout layout = new IlvTreeLayout();
IlvTreeLayout sublayout = new IlvTreeLayout();
IlvGrapherAdapter adapterA = new IlvGrapherAdapter(grapher);
IlvGraphModel adapterB = adapterA.getGraphModel(subGrapher);
IlvDefaultLayoutProvider provider = new IlvDefaultLayoutProvider();
provider.setPreferredLayout(adapterA, layout);
provider.setPreferredLayout(adapterB, sublayout);

We could simply apply the same layout recursively but this enables to change the subgrapher layout independently. (See JViews documentation: Graph Layout/Using Advanced Features/Laying Out Nested Graphs):

import ilog.views.*
import ilog.views.interactor.*;
import ilog.views.graphic.*;
import ilog.views.graphlayout.*;
import ilog.views.graphlayout.tree.*;
import java.awt.*;
import javax.swing.*;
public class TreeSubGraph extends JFrame {
	IlvGrapher grapher;
	IlvGrapher subGrapher;
	IlvManagerView view;
	IlvDefaultLayoutProvider provider;
	IlvGrapherAdapter adapterA;
	IlvGraphModel adapterB;
	public TreeSubGraph() {
		super();
	}
	void init() {
		grapher = new IlvGrapher();
		view = new IlvManagerView(grapher);
		view.setAutoFitToContents(true);
		IlvPanInteractor inte = new IlvPanInteractor();
		view.setInteractor(inte);
		setDefaultCloseOperation(3);
		setSize(600, 600);
		getContentPane().add(view);
		setVisible(true);
		IlvPoint Point = new IlvPoint(0.0f, 0.0f);
		IlvFilledLabel RootNode = new IlvFilledLabel(Point, "Root ");
		RootNode.setBackground(new Color(200, 0, 0, 255));
		grapher.addNode(RootNode, 1, false);
		IlvFilledLabel Node1 = new IlvFilledLabel(Point, "Node1");
		Node1.setBackground(new Color(200, 0, 200, 255));
		grapher.addNode(Node1, 1, false);
		IlvPolylineLinkImage LinkRootNode1 = new IlvPolylineLinkImage(Node1, RootNode, 
				false, null);
		grapher.addLink(LinkRootNode1, false);
		IlvFilledLabel Node2 = new IlvFilledLabel(Point, "Node2");
		Node2.setBackground(new Color(200, 0, 200, 255));
		grapher.addNode(Node2, 1, false);
		IlvPolylineLinkImage LinkNode1Node2 = new IlvPolylineLinkImage(Node2, Node1, 
				false, null);
		grapher.addLink(LinkNode1Node2, false);
		subGrapher = new IlvGrapher();
		IlvDefaultManagerFrame frame = new IlvDefaultManagerFrame();
		frame.setBottomMargin(0.f);
		frame.setLeftMargin(0.f);
		frame.setRightMargin(0.f);
		frame.setTopMargin(0.f);
		subGrapher.setFrame(frame);
		grapher.addNode(subGrapher, 1, false);
		IlvFilledLabel node0 = new IlvFilledLabel(Point, "node0");
		node0.setBackground(new Color(0, 0, 200, 255));
		IlvFilledLabel node1 = new IlvFilledLabel(Point, "node1");
		node1.setBackground(new Color(200, 0, 200, 255));
		IlvFilledLabel node2 = new IlvFilledLabel(Point, "node2");
		node2.setBackground(new Color(200, 0, 200, 255));
		IlvFilledLabel node3 = new IlvFilledLabel(Point, "node3");
		node3.setBackground(new Color(200, 0, 200, 255));
		subGrapher.addNode(node0, 1, false);
		subGrapher.addNode(node1, 1, false);
		subGrapher.addNode(node2, 1, false);
		subGrapher.addNode(node3, 1, false);
		IlvPolylineLinkImage Link01 = new IlvPolylineLinkImage(node0, node1, false, 
				null);
		IlvPolylineLinkImage Link02 = new IlvPolylineLinkImage(node0, node2, false, 
				null);
		IlvPolylineLinkImage Link03 = new IlvPolylineLinkImage(node0, node3, false, 
				null);
		subGrapher.addLink(Link01, 1, false);
		subGrapher.addLink(Link02, 1, false);
		subGrapher.addLink(Link03, 1, false);
		IlvPolylineLinkImage LinkAC1 = new IlvPolylineLinkImage(subGrapher, RootNode, 
				false, null);
		grapher.addLink(LinkAC1, false);
		IlvFilledLabel Node3 = new IlvFilledLabel(Point, "Node3");
		Node3.setBackground(new Color(200, 0, 200, 255));
		grapher.addNode(Node3, 1, false);
		IlvPolylineLinkImage LinkAD1 = new IlvPolylineLinkImage(Node3, RootNode, false, 
				null);
		grapher.addLink(LinkAD1, false);
		IlvFilledLabel Node4 = new IlvFilledLabel(Point, "Node4");
		Node4.setBackground(new Color(200, 0, 200, 255));
		grapher.addNode(Node4, 1, false);
		IlvPolylineLinkImage Link34 = new IlvPolylineLinkImage(Node3, Node4, false, 
				null);
		grapher.addLink(Link34, false);
		IlvFilledLabel Node5 = new IlvFilledLabel(Point, "Node5");
		Node5.setBackground(new Color(200, 0, 0, 255));
		grapher.addNode(Node5, 1, false);
		IlvPolylineLinkImage Linknode0Node5 = new IlvPolylineLinkImage(subGrapher, 
				Node5, false, null);
		grapher.addLink(Linknode0Node5, false);
		IlvTreeLayout layout = new IlvTreeLayout();
		IlvTreeLayout sublayout = new IlvTreeLayout();
		IlvGrapherAdapter adapterA = new IlvGrapherAdapter(grapher);
		IlvGraphModel adapterB = adapterA.getGraphModel(subGrapher);
		IlvDefaultLayoutProvider provider = new IlvDefaultLayoutProvider();
		provider.setPreferredLayout(adapterA, layout);
		provider.setPreferredLayout(adapterB, sublayout);
		layout.setRoot(RootNode);
		layout.setLayoutMode(IlvTreeLayout.FREE);
		layout.setGlobalAlignment(IlvTreeLayout.CENTER);
		layout.setFlowDirection(IlvDirection.Left);
		layout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);
		layout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);
		sublayout.setRoot(node0);
		sublayout.setLayoutMode(IlvTreeLayout.TIP_LEAVES_OVER);
		sublayout.setGlobalAlignment(IlvTreeLayout.TIP_OVER);
		sublayout.setFlowDirection(IlvDirection.Bottom);
		sublayout.setGlobalLinkStyle(IlvTreeLayout.ORTHOGONAL_STYLE);
		sublayout.setCoordinatesMode(IlvGraphLayout.MANAGER_COORDINATES);
		sublayout.setIncrementalMode(true);
		try {
			IlvGraphLayout.PerformLayout(adapterA, provider, true, true, true);
		} catch (IlvGraphLayoutException e) {
			System.err.println(e.getMessage());
		}
	}
	public static void main(String[] arg) {	                      
                         
ilog.views.util.IlvProductUtil.DeploymentLicenseRequired(ilog.views.util.IlvProductUtil.Rogue_Wave_JView s_Diagrammer_Deployment);
		TreeSubGraph g2d = new TreeSubGraph();
      		g2d.init();
	}
};

This article was:   Helpful | Not helpful
Report an issue
Article ID: 2284
Last updated: 29 May, 2018
Revision: 3
Views: 580
Posted: 07 Dec, 2004 by Dean J.
Updated: 29 May, 2018 by Gargani A.
Also listed in


Others in this category