HashMap<Node, UINeoNode> currentNodes = new HashMap<Node, UINeoNode>(
getGround().getChildrenCount());
Enumeration<UINeoNode> en = neoNodesChildren.elements();
while (en.hasMoreElements()) {
UINeoNode node = en.nextElement();
if (!node.isDestroyed()) {
Util.Assert(node.getModel() != null);
currentNodes.put(node.getModel(), node);
}
}
neoNodesChildren.clear();
/*
* Construct Nodes from the Network model
*/
Node[] nodes = getModel().getNodes();
for (Node node : nodes) {
if (getUINode(node) == null) {
UINeoNode nodeUI = currentNodes.get(node);
if (nodeUI == null) {
/*
* Create UI Wrappers here
*/
nodeUI = UINeoNode.createNodeUI(node);
if (newItemPositionX != null && newItemPositionY != null) {
nodeUI.setOffset(newItemPositionX, newItemPositionY);
neoNodesChildren.put(nodeUI.getModel(), nodeUI);
getGround().addChildFancy(nodeUI, false);
} else {
boolean centerAndNotify = !isFirstUpdate;
addUINode(nodeUI, centerAndNotify, false);
if (centerAndNotify) {
nodeUI.showPopupMessage("Node " + node.getName() + " added to Network");
}
}
} else {
neoNodesChildren.put(nodeUI.getModel(), nodeUI);
}
} else {
Util.Assert(false, "Trying to add node which already exists");
}
}
newItemPositionX=null;
newItemPositionY=null;
/*
* Prune existing nodes by deleting them
*/
for (Node node : currentNodes.keySet()) {
// Remove nodes which are no longer referenced by the network model
if (getUINode(node) == null) {
UINeoNode nodeUI = currentNodes.get(node);
nodeUI.showPopupMessage("Node " + nodeUI.getName() + " removed from Network");
nodeUI.destroy();
}
}
/*
* Create projection map
*/
HashSet<Projection> projectionsToAdd = new HashSet<Projection>(
getModel().getProjections().length);
for (Projection projection : getModel().getProjections()) {
projectionsToAdd.add(projection);
}
HashMap<Termination, Projection> projectionMap = new HashMap<Termination, Projection>(
projectionsToAdd.size());
for (Projection projection : projectionsToAdd) {
Util.Assert(!projectionMap.containsKey(projection.getTermination()),
"More than one projection found per termination");
projectionMap.put(projection.getTermination(), projection);
}
/*
* Get UI projections
*/
LinkedList<UIProjection> projectionsToRemove = new LinkedList<UIProjection>();
for (UINeoNode nodeUI : getUINodes()) {
for (UITermination terminationUI : nodeUI.getVisibleTerminations()) {
if (terminationUI.getConnector() != null) {
UIOrigin originUI = terminationUI.getConnector().getOriginUI();
Termination termination = terminationUI.getModel();
Origin origin = originUI.getModel();
Projection projection = projectionMap.get(termination);
if (projection != null && projection.getOrigin() == origin) {
/*
* Projection already exists
*/
projectionsToAdd.remove(projectionMap.get(termination));
} else {
projectionsToRemove.add(terminationUI.getConnector());
}
}
}
}
/*
* Destroy unreferenced projections
*/
for (UIProjection projectionUI : projectionsToRemove) {
UITermination terminationUI = projectionUI.getTermination();
projectionUI.destroy();
if (!isFirstUpdate) {
terminationUI.showPopupMessage("REMOVED Projection to "
+ terminationUI.getNodeParent().getName() + "." + terminationUI.getName());
}
}
/*
* Construct projections
*/
for (Projection projection : projectionsToAdd) {
Origin origin = projection.getOrigin();
Termination term = projection.getTermination();
UINeoNode nodeOrigin = getUINode(origin.getNode());
UINeoNode nodeTerm = getUINode(term.getNode());
if (nodeOrigin != null && nodeTerm != null) {
UIOrigin originUI = nodeOrigin.showOrigin(origin.getName());
UITermination termUI = nodeTerm.showTermination(term.getName());
originUI.connectTo(termUI, false);
if (!isFirstUpdate) {
termUI.showPopupMessage("NEW Projection to " + termUI.getName() + "."
+ getName());