Object rootsArray[] = null;
if (model.roots != null) {
rootsArray = new Object[model.roots.length];
for (int i = 0; i < model.roots.length; i++) {
Object node = model.roots[i];
JGraphHierarchyNode internalNode = (JGraphHierarchyNode) model.getVertexMapping()
.get(node);
rootsArray[i] = internalNode;
}
}
model.dfs(new JGraphFacade.CellVisitor() {
public void visit(Object parent, Object cell,
Object connectingEdge, int layer, int seen) {
// Check if the cell is in it's own ancestor list, if so
// invert the connecting edge and reverse the target/source
// relationship to that edge in the parent and the cell
if (((JGraphHierarchyNode)cell).isAncestor((JGraphHierarchyNode)parent)) {
((JGraphHierarchyEdge) connectingEdge).invert();
((JGraphHierarchyNode) parent).connectsAsSource
.remove(connectingEdge);
((JGraphHierarchyNode) parent).connectsAsTarget
.add(connectingEdge);
((JGraphHierarchyNode) cell).connectsAsTarget
.remove(connectingEdge);
((JGraphHierarchyNode) cell).connectsAsSource
.add(connectingEdge);
}
seenNodes.add(cell);
unseenNodes.remove(cell);
}
}, rootsArray, true, null);
Set possibleNewRoots = null;
if (unseenNodes.size() > 0) {
possibleNewRoots = new HashSet(unseenNodes);
}
// If there are any nodes that should be nodes that the dfs can miss
// these need to be processed with the dfs and the roots assigned
// correctly to form a correct internal model
Set seenNodesCopy = new HashSet(seenNodes);
// Pick a random cell and dfs from it
model.dfs(new JGraphFacade.CellVisitor() {
public void visit(Object parent, Object cell,
Object connectingEdge, int layer, int seen) {
// Check if the cell is in it's own ancestor list, if so
// invert the connecting edge and reverse the target/source
// relationship to that edge in the parent and the cell
if (((JGraphHierarchyNode)cell).isAncestor((JGraphHierarchyNode)parent)) {
((JGraphHierarchyEdge) connectingEdge).invert();
((JGraphHierarchyNode) parent).connectsAsSource
.remove(connectingEdge);
((JGraphHierarchyNode) parent).connectsAsTarget
.add(connectingEdge);
((JGraphHierarchyNode) cell).connectsAsTarget
.remove(connectingEdge);
((JGraphHierarchyNode) cell).connectsAsSource
.add(connectingEdge);
}
seenNodes.add(cell);
unseenNodes.remove(cell);
}
}, unseenNodes.toArray(), true, seenNodesCopy);
if (possibleNewRoots != null && possibleNewRoots.size() > 0) {
Iterator iter = possibleNewRoots.iterator();
List roots = facade.getRoots();
while (iter.hasNext()) {
JGraphHierarchyNode node = (JGraphHierarchyNode)iter.next();
Object realNode = node.cell;
int numIncomingEdges = facade.getIncomingEdges(realNode,
null, true, false).size();
if (numIncomingEdges == 0) {
roots.add(realNode);