Draw a PolylineConnection between existing Nodes on a FigureCanvas

233 views Asked by At

I am having list of nodes to be drawn.Here Node means RectangleFigure. So, all these nodes are plotted first on canvas(FigureCanvas) Then I have a map maintained having dependency relations between nodes drawn earlier. In the map, keys are the nodes and values are nothing but a list of nodes maintained.

e.g. There are 3 nodes, [ClassA, ClassB, InterfaceC]

I have them drawn on a canvas. And my map is like below: [ClassA=[ClassB], ClassB=[InterfaceC]] So, it means ClassA extends ClassB and ClassB implements InterfaceC.

Now, I want to draw PolylineConnection in between the nodes drawn already ,using the map maintained. So, how can I proceed for this?

Any pointers are really appreciated, Thanks in advance!

2

There are 2 answers

0
Sheetal Bhatewara On BEST ANSWER

Tried ways to solve the problem, and it worked. So, adding answer for the own question.

Steps are as below:- 1. Consider the example in the question itself. There are 3 nodes, [ClassA, ClassB, InterfaceC] and we have a map of relations in these nodes as [ClassA=[ClassB], ClassB=[InterfaceC]] means ClassA extends ClassB and ClassB implements InterfaceC.

  1. Take one key at a time from the map, and fetch that node as a source of PolylineConnection.
  2. Then for the same source(key in map) fetch node for values one-by-one as target node.
  3. Now as we have both the source node and target node, we can draw connection in between using Anchors

The logic for fetching node is as below:-

  1. Get all the children drawn on the canvas in one List, if the children is of type RectangleFigure(node) then comparing the label given to it we can fetch the node required.

Code snippet for more clarification

private RectangleFigure fetchNode(String node_label) {
        RectangleFigure emptyNode = new RectangleFigure();
        // get the list of nodes drawn on canvas
        @SuppressWarnings("rawtypes")
        List childrens = panel.getChildren();  //private IFigure panel;

        for (int count = 0; count < childrens.size(); count++) {
            if (childrens.get(count) instanceof RectangleFigure) {
                RectangleFigure node = (RectangleFigure) childrens.get(count);
                @SuppressWarnings("rawtypes")
                List node_children = node.getChildren();
                for (int count2 = 0; count2 < node_children.size(); count2++) {
                    if (node_children.get(count2) instanceof Label) {
                        Label lbl = (Label) node_children.get(count2);
                        if (lbl.getText().equals(node_label)) {
                            return node;
                        }
                    }
                }
            }
        }

        return emptyNode;
    }
3
vainolo On

There are many examples as part of the draw2d project. Check the details here: http://nyssen.blogspot.de/2010/12/draw2d-examples-hidden-treasure.html