JGoLabeledLink and JGoSubGraph

I have the following definition of JGoDocument. When i display it on screen the Labels of links lo1o2 and ln2o1 are visible correctly but labels of link ln1n2 are missing (are not visible at all). I see when JGoLabeledLink is associated with JGoSubGraph then its label are not drawed on sceen. Is it some bug or am i do somethink wrong?
JGoDocument doc = new JGoDocument();
JGoTextNode n1 = new JGoTextNode(“n1”);
JGoTextNode n2 = new JGoTextNode(“n2”);
JGoTextNode o1 = new JGoTextNode(“o1”);
JGoTextNode o2 = new JGoTextNode(“o2”);


JGoLabeledLink ln1n2 = new JGoLabeledLink(n1.getRightPort(),n2.getLeftPort());
ln1n2.setFromLabel(new JGoLinkLabel(“n1s”));
ln1n2.setToLabel(new JGoLinkLabel(“n1e”));

JGoLabeledLink lo1o2 = new JGoLabeledLink(o1.getRightPort(),o2.getLeftPort());
lo1o2.setFromLabel(new JGoLinkLabel(“o1e”));
lo1o2.setToLabel(new JGoLinkLabel(“o2s”));

JGoLabeledLink ln2o1 = new JGoLabeledLink(n2.getRightPort(),o1.getLeftPort());
ln2o1.setFromLabel(new JGoLinkLabel(“n2s”));
ln2o1.setToLabel(new JGoLinkLabel(“o1e”));

JGoSubGraph su1 = new JGoSubGraph(“N”);
su1.addObjectAtTail(n1);
su1.addObjectAtTail(n2);
su1.addObjectAtTail(ln1n2); &nb sp;


doc.addObjectAtTail(su1);
doc.addObjectAtTail(o1);
doc.addObjectAtTail(o2);
doc.addObjectAtTail(ln2o1);
doc.addObjectAtTail(lo1o2);


JGoNetwork net = new JGoNetwork(su1);
JGoNetworkNode nn = net.findNode(su1.getHandle());
if (nn != null)
net.deleteNode(nn); &nbsp ;
nn = net.findNode(su1.getLabel());
if (nn != null)
net.deleteNode(nn);


JGoLayeredDigraphAutoLayout layout = new JGoLayeredDigraphAutoLayout(doc,net);
layout.setDirectionOption(JGoLayeredDigraphAutoLayout.LD_DIR ECTION_RIGHT);
layout.setLayerSpacing(24);
layout.setColumnSpacing(8); &nb sp;
layout.performLayout();


JGoNetwork network=new JGoNetwork(doc);
layout = new JGoLayeredDigraphAutoLayout(doc,network);
layout.setDirectionOption(JGoLayeredDigraphAutoLayout.LD_DIR ECTION_RIGHT);
layout.setLayerSpacing(24);
layout.setColumnSpacing(8);
layout.performLayout();

The problem is due to the labels of JGoLabeledLinks not being children but siblings of the links. So the labels (just like the Label and Handle of the JGoSubGraph, which you were careful to remove from the JGoNetwork) were also being laid out along with the rest of the nodes. That’s also why the subgraph became so large.
In addition, after the layout, the labels do need to be positioned properly, so we iterate again to call JGoLabeledLink.positionLabels().
public void layoutWithLabeledLinks(JGoObjectSimpleCollection coll) {
if (coll == null) return;
JGoDocument doc = (coll.getObjectAtPos(coll.getFirstObjectPos())).getDocument( );
JGoNetwork net = new JGoNetwork(coll);
if (coll instanceof JGoSubGraph) {
JGoSubGraph sg = (JGoSubGraph)coll;
JGoNetworkNode nn = net.findNode(sg.getHandle());
if (nn != null)
net.deleteNode(nn);
nn = net.findNode(sg.getLabel());
if (nn != null)
net.deleteNode(nn);
}
JGoListPosition pos = coll.getFirstObjectPos();
while (pos != null) {
JGoObject obj = coll.getObjectAtPos(pos);
pos = coll.getNextObjectPosAtTop(pos);
if (obj instanceof JGoLabeledLink) {
JGoLabeledLink ll = (JGoLabeledLink)obj;
JGoNetworkNode nn = net.findNode(ll.getFromLabel());
if (nn != null)
net.deleteNode(nn);
nn = net.findNode(ll.getMidLabel());
if (nn != null)
net.deleteNode(nn);
nn = net.findNode(ll.getToLabel());
if (nn != null)
net.deleteNode(nn);
}
}

JGoLayeredDigraphAutoLayout layout = new JGoLayeredDigraphAutoLayout(doc,net);
layout.setDirectionOption(JGoLayeredDigraphAutoLayout.LD_DIR ECTION_RIGHT);
layout.setLayerSpacing(24);
layout.setColumnSpacing(8);
layout.performLayout();

pos = coll.getFirstObjectPos();
while (pos != null) {
JGoObject obj = coll.getObjectAtPos(pos);
pos = coll.getNextObjectPos(pos);
if (obj instanceof JGoLabeledLink) {
JGoLabeledLink ll = (JGoLabeledLink)obj;
ll.positionLabels();
}
}
}
public void test() {
JGoDocument doc = new JGoDocument();
JGoTextNode n1 = new JGoTextNode(“n1”);
JGoTextNode n2 = new JGoTextNode(“n2”);
JGoTextNode o1 = new JGoTextNode(“o1”);
JGoTextNode o2 = new JGoTextNode(“o2”);
JGoLabeledLink ln1n2 = new JGoLabeledLink(n1.getRightPort(),n2.getLeftPort());
ln1n2.setFromLabel(new JGoLinkLabel(“n1s”));
ln1n2.setToLabel(new JGoLinkLabel(“n1e”));
JGoLabeledLink lo1o2 = new JGoLabeledLink(o1.getRightPort(),o2.getLeftPort());
lo1o2.setFromLabel(new JGoLinkLabel(“o1e”));
lo1o2.setToLabel(new JGoLinkLabel(“o2s”));
JGoLabeledLink ln2o1 = new JGoLabeledLink(n2.getRightPort(),o1.getLeftPort());
ln2o1.setFromLabel(new JGoLinkLabel(“n2s”));
ln2o1.setToLabel(new JGoLinkLabel(“o1e”));
JGoSubGraph su1 = new JGoSubGraph(“N”);
su1.addObjectAtTail(n1);
su1.addObjectAtTail(n2);
su1.addObjectAtTail(ln1n2);
doc.addObjectAtTail(su1);
doc.addObjectAtTail(o1);
doc.addObjectAtTail(o2);
doc.addObjectAtTail(ln2o1);
doc.addObjectAtTail(lo1o2);
layoutWithLabeledLinks(su1);
layoutWithLabeledLinks(doc);
}

I understand the reply but still does not work. I tried your proposed snipplet but the result is still the same.
I will send you the printscreen of the result (I have no idea how to attach the file to forum)

I have the Same Problem. But I do not understand the sulotion. Would you please post the complete code. Thanks a lot

OK, here’s a modified MinimalApp.java, for Swing. The only changes are the addition of layoutWithLabeledLinks, which handles JGoSubGraphs as well as JGoLabeledLinks, and the modification of init to create a subgraph as well as some nodes and links.
The original code worked with JGo 5.0; the following assumes JGo 5.1.
package com.nwoods.jgo.examples;
import java.applet.;
import java.awt.
;
import java.awt.event.;
import javax.swing.
;
import com.nwoods.jgo.;
import com.nwoods.jgo.layout.
;
/**
* This is a minimal JGo applet/application. Most of the code here
* is standard boilerplate that must be in any applet/application.
*


* This just displays two BasicNodes, of different colors.
* The user can only select nodes, move them, copy them, and link them together.
*/
public class MinimalApp extends JApplet implements Runnable {
public MinimalApp() {
myView = new JGoView();
getContentPane().add(“Center”, myView);
}

public void start() { // Applet starting
new Thread(this).start(); // enable drag-and-drop from separate thread (JDC bug 4225247)
}
public void run() {
myView.initializeDragDropHandling();
}
public static void main(String args[]) {
try {
JFrame mainFrame = new JFrame();
// close the application when the main window closes
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
System.exit(0);
}
});
mainFrame.setTitle(“Minimal JGo Application”);
mainFrame.setSize(400, 300);
MinimalApp app = new MinimalApp();
Container contentPane = mainFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(“Center”, app);
contentPane.validate();
mainFrame.setVisible(true);
app.init(); // do common initialization
app.start();
} catch (Throwable t) {
System.err.println(t);
t.printStackTrace();
System.exit(1);
}
}
public void layoutWithLabeledLinks(JGoObjectSimpleCollection coll) {
if (coll == null) return;
JGoDocument doc = (coll.getObjectAtPos(coll.getFirstObjectPos())).getDocument( );
JGoNetwork net = new JGoNetwork(coll);
// for subgraphs, ignore any Handle or Label
if (coll instanceof JGoSubGraph) {
JGoSubGraph sg = (JGoSubGraph)coll;
net.deleteNode(sg.getHandle());
net.deleteNode(sg.getLabel());
}
// for labeled links, ignore any label
JGoListPosition pos = coll.getFirstObjectPos();
while (pos != null) {
JGoObject obj = coll.getObjectAtPos(pos);
pos = coll.getNextObjectPosAtTop(pos);
if (obj instanceof JGoLabeledLink) {
JGoLabeledLink ll = (JGoLabeledLink)obj;
net.deleteNode(ll.getFromLabel());
net.deleteNode(ll.getMidLabel());
net.deleteNode(ll.getToLabel());
}
}
JGoLayeredDigraphAutoLayout layout = new JGoLayeredDigraphAutoLayout(doc,net);
layout.setDirectionOption(JGoLayeredDigraphAutoLayout.LD_DIR ECTION_RIGHT);
layout.setLayerSpacing(24);
layout.setColumnSpacing(8);
layout.performLayout();
}
public void init() { // App initialization–only here do we do anything JGo specific
// add JGoObjects to the document, not to the view
JGoDocument doc = myView.getDocument();
// create a subgraph, its child nodes, and the child links connecting those child nodes
JGoSubGraph su1 = new JGoSubGraph(“N”);
JGoTextNode n1 = new JGoTextNode(“n1”);
su1.addObjectAtTail(n1);

JGoTextNode n2 = new JGoTextNode(“n2”);
su1.addObjectAtTail(n2);
JGoTextNode n3 = new JGoTextNode(“n3”);
su1.addObjectAtTail(n3);
JGoLabeledLink ln1n2 = new JGoLabeledLink(n1.getRightPort(),n2.getLeftPort());
ln1n2.setFromLabel(new JGoLinkLabel(“2”));
ln1n2.setToLabel(new JGoLinkLabel(“1”));
su1.addObjectAtTail(ln1n2);
JGoLabeledLink ln3n2 = new JGoLabeledLink(n3.getRightPort(),n2.getLeftPort());
ln3n2.setFromLabel(new JGoLinkLabel(“2”));
ln3n2.setToLabel(new JGoLinkLabel(“3”));
su1.addObjectAtTail(ln3n2);
// add subgraph to document
doc.addObjectAtTail(su1);
// create other nodes and add to document
JGoTextNode o1 = new JGoTextNode(“o1”);
doc.addObjectAtTail(o1);

JGoTextNode o2 = new JGoTextNode(“o2”);
doc.addObjectAtTail(o2);
JGoTextNode o3 = new JGoTextNode(“o3”);
doc.addObjectAtTail(o3);
// create links and add to document
JGoLabeledLink lo1o2 = new JGoLabeledLink(o1.getRightPort(),o2.getLeftPort());
lo1o2.setFromLabel(new JGoLinkLabel(“2”));
lo1o2.setToLabel(new JGoLinkLabel(“1”));
doc.addObjectAtTail(lo1o2);
JGoLabeledLink lo1o3 = new JGoLabeledLink(o1.getRightPort(),o3.getLeftPort());
lo1o3.setFromLabel(new JGoLinkLabel(“3”));
lo1o3.setToLabel(new JGoLinkLabel(“1”));
doc.addObjectAtTail(lo1o3);
JGoLabeledLink ln2o1 = new JGoLabeledLink(n2.getRightPort(),o1.getLeftPort());
ln2o1.setFromLabel(new JGoLinkLabel(“N”));
ln2o1.setToLabel(new JGoLinkLabel(“2”));
doc.addObjectAtTail(ln2o1);
// layout subgraphs first
JGoListPosition pos = doc.getFirstObjectPos();
while (pos != null) {
JGoObject obj = doc.getObjectAtPos(pos);
pos = doc.getNextObjectPosAtTop(pos);
if (obj instanceof JGoSubGraph) {
JGoSubGraph sg = (JGoSubGraph)obj;
layoutWithLabeledLinks(sg);
}
}
// then layout whole document
layoutWithLabeledLinks(doc);
}
private JGoView myView;
}