I have a GoPalette and GoDiagram and want to allow users to drag items from the Palette onto the diagram. Each node in the palette may be dragged repeatedly, so I need the nodes to be unique in the diagram. I’m also using a custom dragging tool to highlight and validate the target node before allowing nodes to be dropped.
When the drag starts, a node is created immediately within the model, and then when the node is dropped, another one is created. The first node is inserted without any problem. The second node of the same type throws an exception.
I have tried using strings, ints and Guids for the key type. I have also tried overriding the MakeNodeKeyUnique method as well as overriding the InsertNode methods. Nothing seems to work.
My custom GraphModel is defined as follows:
public class MyElement : GraphModelNodeData<Guid> { public MyElement() { Key = Guid.NewGuid(); } public Guid ID { get { return Key; } set { Key = value; } } }
public class MyModel : GraphModel<MyElement, Guid>{ <span style="color: blue;">public</span> MyModel(<span style="color: blue;">object</span> owner) { NodeKeyPath = <span style="color: rgb163, 21, 21;">"ID"</span>; Modifiable = <span style="color: blue;">true</span>; HasUndoManager = <span style="color: blue;">true</span>; }
protected override void InsertNode(MyElement nodedata)
{
Debug.WriteLine(“MyModel.InsertNode({0})”, nodedata.Key);
Guid key = FindKeyForNode(nodedata);
MyElement e = FindNodeByKey(key);
if (e != null)
{
Debug.WriteLine(string.Format(“Found duplicate key: {0}”, key));
nodedata.ID = Guid.NewGuid();
Debug.WriteLine(string.Format("==> creating new key: {0}", nodedata.Key));
}<span style="color: blue;">try</span> { <span style="color: blue;">base</span>.InsertNode(nodedata); } <span style="color: blue;">catch</span> (<span style="color: rgb43, 145, 175;">Exception</span> x) { <span style="color: rgb43, 145, 175;">Debug</span>.WriteLine(<span style="color: blue;">string</span>.Format(<span style="color: rgb163, 21, 21;">"Exception: {0}"</span>, x.Message)); <span style="color: rgb43, 145, 175;">Debug</span>.WriteLine(<span style="color: rgb163, 21, 21;">"Attempting to clone the node..."</span>); <span style="color: blue;">base</span>.InsertNode(nodedata.Clone() <span style="color: blue;">as</span> <span style="color: rgb43, 145, 175;">MyElement</span>); } }
protected override bool MakeNodeKeyUnique(MyElement nodedata)
{
Debug.WriteLine(“MyModel.MakeNodeKeyUnique({0})”, nodedata.Key);
nodedata.ID = Guid.NewGuid();
Debug.WriteLine("==> {0}", nodedata.Key);
return true;
}
}