Search
Tutorial 2: Loading Hierarchical Data

This tutorial shows how to load hierarchical data recursively from XML and create diagram items corresponding to the hierarchy.

1. Right-click the project in Package Explorer and choose New -> Other from the context menu. Create a new XML file called SampleTree.xml and add to it content in the following form:

XML  Copy Code

<?xml version="1.0" encoding="utf-8" ?>
<Project>
    <Activity Name="Activity 1">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
        </Activity>
        <Activity  Name="sub-activity 2">
            <Activity  Name="sub-activity 2-1" />
            <Activity  Name="sub-activity 2-2" />
            <Activity  Name="sub-activity 2-3" />
        </Activity>
        <Activity  Name="sub-activity 3">
            <Activity  Name="sub-activity 3-1" />
            <Activity  Name="sub-activity 3-2" />
        </Activity>
    </Activity>
    <Activity Name="Activity 2">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
            <Activity  Name="sub-activity 1-3" />
        </Activity>
        <Activity  Name="sub-activity 2">
            <Activity  Name="sub-activity 2-1" />
            <Activity  Name="sub-activity 2-2" />
        </Activity>
    </Activity>
    <Activity Name="Activity 3">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
            <Activity  Name="sub-activity 1-3" />
            <Activity  Name="sub-activity 1-4" />
        </Activity>
        <Activity  Name="sub-activity 2" />
        <Activity  Name="sub-activity 3">
            <Activity  Name="sub-activity 3-1" />
            <Activity  Name="sub-activity 3-2" />
        </Activity>
    </Activity>
</Project>

2. Add a windowOpened event handler to the JFrame:

Java  Copy Code

this.addWindowListener(new WindowAdapter()
{
    @Override
    public void windowOpened(WindowEvent e)
    {
        loadTree(diagram, "SampleTree.xml");
    }
});

3. Add the following member to the frame class to hold the default node size.

Java  Copy Code

final Rectangle2D.Float bounds = new Rectangle2D.Float(0,0,30,8);

4. In loadTree, create the tree root node, and call the createChildren method, which builds the tree recursively.

Java  Copy Code

void loadTree(Diagram diagram, String filepath)
{
    // load the tree XML
    Document document = loadXmlFile(filepath);
    Element rootElement = document.getDocumentElement();

    // create diagram elements recursively
    ShapeNode root = diagram.getFactory().createShapeNode(bounds);
    root.setText("Project");
    createChildren(diagram, root, rootElement);
}

5. Define createChildren as follows. It takes as parameters the parent DiagramNode and parent XML element, iterates the child XML elements to build the next level of the hierarchy, and creates corresponding diagram items by calling createShapeNode and createDiagramLink.

Java  Copy Code

void createChildren(Diagram diagram, DiagramNode parentNode, Node parentElement)
{
    NodeList elements = parentElement.getChildNodes();
    for (int i = 0; i < elements.getLength(); i++)
    {
        Node element = elements.item(i);
        if (element.getNodeName().equals("Activity"))
        {
            ShapeNode node = diagram.getFactory().createShapeNode(bounds);
            node.setText(((Element)element).getAttribute("Name"));
            diagram.getFactory().createDiagramLink(parentNode, node);
            createChildren(diagram, node, element);
        }
    }
}

6. Use TreeLayout to arrange the diagram. Add the following code to the end of the loadTree method.

Java  Copy Code

// arrange using TreeLayout
TreeLayout layout = new TreeLayout();
layout.setType(TreeLayoutType.Cascading);
layout.setDirection(TreeLayoutDirection.LeftToRight);
layout.setLinkStyle(TreeLayoutLinkType.Cascading2);
layout.setNodeDistance(3);
layout.setLevelDistance(-8); // let horizontal positions overlap
layout.arrange(diagram);

7. Build and run the project. If everything is fine, you should see this representation of the hierarchy: