// Repair any references of the clone
source.maintainReferences(ModelObject.RESOLVE_GLOBAL_REFS | ModelObject.RESOLVE_LOCAL_REFS);
// We will copy to our target process
ProcessDrawing drawing = (ProcessDrawing) workspaceView.drawing();
ProcessItem target = drawing.getProcess();
// Make sure the references names of the target are up to date
target.maintainReferences(ModelObject.SYNC_LOCAL_REFNAMES | ModelObject.SYNC_GLOBAL_REFNAMES);
// We need to make the names in the source process unique with respect to our target process
NamedObjectCollectionUtil.createUniqueNames(source.getNodeList(), target.getNodeList());
NamedObjectCollectionUtil.createUniqueNames(source.getTextElementList(), target.getTextElementList());
NamedObjectCollectionUtil.createUniqueNames(source.getDataLinkList(), target.getDataLinkList());
NamedObjectCollectionUtil.createUniqueNames(source.getControlLinkList(), target.getControlLinkList());
// Update the reference names according to the changed object names
source.maintainReferences(ModelObject.SYNC_LOCAL_REFNAMES);
Figure firstFigure = null;
// Add the nodes to the target process
for (Iterator it = source.getNodes(); it.hasNext();)
{
Node node = (Node) it.next();
target.addNode(node);
// Rebuild the references after adding the object to the target
node.maintainReferences(ModelObject.SYNC_GLOBAL_REFNAMES);
NodeFigure nodeFigure = drawing.createNodeFigure(node);
if (nodeFigure == null)
{
target.removeNode(node);
continue;
}
if (firstFigure == null)
firstFigure = nodeFigure;
workspaceView.add(nodeFigure);
workspaceView.addToSelection(nodeFigure);
// Position the new node within the process drawing so that it doesn't hide an existing figure
ModelerUtil.preventOverlap(drawing, nodeFigure);
}
// Add the text elements to the target process
for (Iterator it = source.getTextElements(); it.hasNext();)
{
TextElement textElement = (TextElement) it.next();
target.addTextElement(textElement);
// Rebuild the references after adding the object to the target
textElement.maintainReferences(ModelObject.SYNC_GLOBAL_REFNAMES);
TextElementFigure textElementFigure = drawing.createTextElementFigure(textElement);
if (textElementFigure == null)
{
target.removeTextElement(textElement);
continue;
}
if (firstFigure == null)
firstFigure = textElementFigure;
workspaceView.add(textElementFigure);
workspaceView.addToSelection(textElementFigure);
// Position the new element within the process drawing so that it doesn't hide an existing figure
ModelerUtil.preventOverlap(drawing, textElementFigure);
}
// Add the control links
for (Iterator it = source.getControlLinks(); it.hasNext();)
{
ControlLink link = (ControlLink) it.next();
target.addControlLink(link);
// Determine the reference names after adding the object to the target
link.maintainReferences(ModelObject.SYNC_LOCAL_REFNAMES);
FlowConnection flowConnection = drawing.createFlowConnection(link);
if (flowConnection == null)
{
target.removeControlLink(link);
continue;
}
workspaceView.add(flowConnection);
workspaceView.addToSelection(flowConnection);
}
// Add the data links
for (Iterator it = source.getDataLinks(); it.hasNext();)
{
DataLink link = (DataLink) it.next();
target.addDataLink(link);
// Determine the reference names after adding the object to the target
link.maintainReferences(ModelObject.SYNC_LOCAL_REFNAMES);
ParamConnection paramConnection = drawing.createParamConnection(link);
if (paramConnection == null)
{
target.removeDataLink(link);
continue;
}