Exemplo n.º 1
0
        void SyncNodeInfos(Document oldDocument, Document newDocument)
        {
            var oldFilePath = oldDocument.FilePath;
            var filePath    = newDocument.FilePath;
            var oldMap      = GetNodeDefinitions(oldDocument).ToDictionary(d => d.Identity);

            foreach (var newDef in GetNodeDefinitions(newDocument))
            {
                var oldDef = oldMap.ValueOrDefault(newDef.Identity);
                if (oldDef == newDef)
                {
                    continue;
                }

                var(oldName, oldCategory, oldVersion) = GetNodeInfoKeys(oldDef ?? newDef);
                var(name, category, version)          = GetNodeInfoKeys(newDef);

                var nodeInfo    = FNodeInfoFactory.CreateNodeInfo(oldName, oldCategory, oldVersion, oldFilePath, beginUpdate: true);
                var oldNodeInfo = nodeInfo;

                // Check if document was moved or the definition renamed.
                if (name != oldName || category != oldCategory || version != oldVersion || filePath != oldFilePath)
                {
                    if (FNodeInfoFactory.ContainsKey(name, category, version, filePath))
                    {
                        // A node info already exists (document already existed). Switch to the existing one (otherwise we'd see double entries).
                        nodeInfo = FNodeInfoFactory.CreateNodeInfo(name, category, version, filePath, beginUpdate: false);
                    }
                    else
                    {
                        // Update the existing node info.
                        FNodeInfoFactory.UpdateNodeInfo(nodeInfo, name, category, version, filePath);
                    }

                    // Point all existing nodes to the updated node info
                    foreach (var node in FHDEHost.RootNode.AsDepthFirstEnumerable())
                    {
                        if (node.NodeInfo == oldNodeInfo)
                        {
                            var patchMessage = new PatchMessage(node.Parent.NodeInfo.Filename);
                            var nodeMessage  = patchMessage.AddNode(node.ID);
                            nodeMessage.CreateMe   = true;
                            nodeMessage.SystemName = nodeInfo.Systemname;
                            nodeMessage.Filename   = nodeInfo.Filename;
                            FHDEHost.SendXMLSnippet(patchMessage.Filename, patchMessage.ToString(), true);
                        }
                    }
                }

                if (nodeInfo == oldNodeInfo)
                {
                    UpdateNodeInfo(newDef, nodeInfo);
                }
                else
                {
                    FNodeInfoFactory.DestroyNodeInfo(oldNodeInfo);
                }
            }

            var newMap = GetNodeDefinitions(newDocument).ToDictionary(d => d.Identity);

            foreach (var oldDef in GetNodeDefinitions(oldDocument))
            {
                if (newMap.ContainsKey(oldDef.Identity))
                {
                    continue;
                }

                var(name, category, version) = GetNodeInfoKeys(oldDef);
                var nodeInfo = FNodeInfoFactory.CreateNodeInfo(name, category, version, filePath, beginUpdate: false);
                if (nodeInfo.Factory == this)
                {
                    FNodeInfoFactory.DestroyNodeInfo(nodeInfo);
                }
            }

            if (filePath != oldFilePath && File.Exists(oldFilePath))
            {
                // The document was saved under a new name but old document still exists on disk.
                // Scan the old document again so we have double entries in the node browser as we'd have after a restart.
                foreach (var info in LoadNodeInfos(oldFilePath))
                {
                    Touch(info);
                }
            }
        }