private void CreateStickyNote(DropdownMenuAction a)
        {
            Vector2 pos = a.eventInfo.localMousePosition;

            var     stickyNote = ScriptableObject.CreateInstance <MathStickyNote>();
            Vector2 localPos   = VisualElementExtensions.ChangeCoordinatesTo(this, contentViewContainer, pos);

            stickyNote.title    = "New Title";
            stickyNote.contents = "Type something here";
            stickyNote.position = new Rect(localPos, new Vector2(200, 180));

            m_SimpleGraphViewWindow.AddStickyNote(stickyNote);

            var simpleStickyNote = new SimpleStickyNote();

            simpleStickyNote.model    = stickyNote;
            simpleStickyNote.userData = stickyNote;

            AddElement(simpleStickyNote);
        }
        public void Reload(IEnumerable <MathNode> nodesToReload, IEnumerable <MathPlacemat> placemats, IEnumerable <MathStickyNote> stickies, Dictionary <string, string> oldToNewIdMapping = null)
        {
            string oldId;
            var    nodes          = new Dictionary <MathNode, GraphElement>();
            var    oldIdToNewNode = new Dictionary <string, ISelectable>();

            var newToOldIdMapping = new Dictionary <string, string>();

            if (oldToNewIdMapping != null)
            {
                foreach (var oldIdKV in oldToNewIdMapping)
                {
                    newToOldIdMapping[oldIdKV.Value] = oldIdKV.Key;
                }
            }

            // Create the nodes.
            foreach (MathNode mathNode in nodesToReload)
            {
                GraphElement node = m_SimpleGraphViewWindow.CreateNode(mathNode);

                if (node is Group)
                {
                    node.name = "SimpleGroup";
                }
                else if (node is Scope)
                {
                    node.name = "SimpleScope";
                }
                else
                {
                    node.name = "SimpleNode";
                }

                if (node == null)
                {
                    Debug.LogError("Could not create node " + mathNode);
                    continue;
                }

                nodes[mathNode] = node;

                if (mathNode.groupNode == null)
                {
                    if (newToOldIdMapping.TryGetValue(mathNode.nodeID.ToString(), out oldId))
                    {
                        oldIdToNewNode.Add(oldId, node);
                    }
                }

                AddElement(node);
            }

            // Assign scopes
            foreach (MathNode mathNode in nodesToReload)
            {
                if (mathNode.groupNode == null)
                {
                    continue;
                }

                Scope graphScope = nodes[mathNode.groupNode] as Scope;

                graphScope.AddElement(nodes[mathNode]);
            }

            // Add to stacks
            foreach (MathNode mathNode in nodesToReload)
            {
                MathStackNode stack = mathNode as MathStackNode;

                if (stack == null)
                {
                    continue;
                }

                StackNode graphStackNode = nodes[stack] as StackNode;

                for (int i = 0; i < stack.nodeCount; ++i)
                {
                    MathNode stackMember = stack.GetNode(i);
                    if (stackMember == null)
                    {
                        Debug.LogWarning("null stack member! Item " + i + " of stack " + stack.name + " is null. Possibly a leftover from bad previous manips.");
                    }

                    graphStackNode.AddElement(nodes[stackMember]);
                }
            }

            // Connect the presenters.
            foreach (var mathNode in nodesToReload)
            {
                if (mathNode is MathOperator)
                {
                    MathOperator mathOperator = mathNode as MathOperator;

                    if (!nodes.ContainsKey(mathNode))
                    {
                        Debug.LogError("No element found for " + mathNode);
                        continue;
                    }

                    var graphNode = nodes[mathNode] as Node;

                    if (mathOperator.left != null && nodes.ContainsKey(mathOperator.left))
                    {
                        var outputPort = (nodes[mathOperator.left] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[0] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathOperator.left.nodeID + "_edge";
                        AddElement(edge);
                    }
                    else if (mathOperator.left != null)
                    {
                        //add.m_Left = null;
                        Debug.LogWarning("Invalid left operand for operator " + mathOperator + " , " + mathOperator.left);
                    }

                    if (mathOperator.right != null && nodes.ContainsKey(mathOperator.right))
                    {
                        var outputPort = (nodes[mathOperator.right] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[1] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathOperator.right.nodeID + "_edge";
                        AddElement(edge);
                    }
                    else if (mathOperator.right != null)
                    {
                        Debug.LogWarning("Invalid right operand for operator " + mathOperator + " , " + mathOperator.right);
                    }
                }
                else if (mathNode is MathFunction)
                {
                    MathFunction mathFunction = mathNode as MathFunction;

                    if (!nodes.ContainsKey(mathNode))
                    {
                        Debug.LogError("No element found for " + mathNode);
                        continue;
                    }

                    var graphNode = nodes[mathNode] as Node;

                    for (int i = 0; i < mathFunction.parameterCount; ++i)
                    {
                        MathNode param = mathFunction.GetParameter(i);

                        if (param != null && nodes.ContainsKey(param))
                        {
                            var outputPort = (nodes[param] as Node).outputContainer[0] as Port;
                            var inputPort  = graphNode.inputContainer[i] as Port;

                            Edge edge = inputPort.ConnectTo(outputPort);
                            edge.viewDataKey = param.nodeID + "_edge";
                            AddElement(edge);
                        }
                        else if (param != null)
                        {
                            Debug.LogWarning("Invalid parameter for function" + mathFunction + " , " +
                                             param);
                        }
                    }
                }
                else if (mathNode is MathResult)
                {
                    MathResult mathResult = mathNode as MathResult;
                    var        graphNode  = nodes[mathNode] as Node;

                    if (mathResult.root != null)
                    {
                        var outputPort = (nodes[mathResult.root] as Node).outputContainer[0] as Port;
                        var inputPort  = graphNode.inputContainer[0] as Port;

                        Edge edge = inputPort.ConnectTo(outputPort);
                        edge.viewDataKey = mathResult.root.nodeID + "_edge";
                        AddElement(edge);
                    }
                }
            }

            foreach (var matModel in placemats.OrderBy(p => p.zOrder))
            {
                var newPlacemat = placematContainer.CreatePlacemat <SimplePlacemat>(matModel.position, matModel.zOrder, matModel.title);
                newPlacemat.userData    = matModel;
                newPlacemat.viewDataKey = matModel.identification;
                newPlacemat.Model       = matModel;

                if (newToOldIdMapping.TryGetValue(matModel.identification, out oldId))
                {
                    oldIdToNewNode.Add(oldId, newPlacemat);
                }
            }

            if (stickies != null)
            {
                var existingStickies = this.Query <SimpleStickyNote>().ToList();

                foreach (var sticky in existingStickies.Where(t => !stickies.Contains(t.model)))
                {
                    RemoveElement(sticky);
                }

                foreach (var stickyModel in stickies.Except(existingStickies.Select(t => t.model)))
                {
                    var newSticky = new SimpleStickyNote();
                    newSticky.model    = stickyModel;
                    newSticky.userData = stickyModel;
                    AddElement(newSticky);

                    if (newToOldIdMapping.TryGetValue(stickyModel.id, out oldId))
                    {
                        oldIdToNewNode.Add(oldId, newSticky);
                    }
                }
            }

            // Now that all graph elements have been created, init the collapsed elements of each placemat.
            foreach (var p in this.Query <SimplePlacemat>().ToList())
            {
                p.InitCollapsedElementsFromModel();
            }

            // Make sure collapsed edges are hidden.
            placematContainer.HideCollapsedEdges();

            UpdateSelection(oldIdToNewNode);

            RebuildBlackboard();
        }