Example #1
0
        /// <summary>
        /// Does the actual work of changing the caption after all of the verifications have been done
        /// that it's Ok to move the file.
        /// </summary>
        /// <param name="newCaption">The new caption.</param>
        /// <param name="newPath">The new absolute path.</param>
        public override void MoveNodeOnCaptionChange(string newCaption, string newPath)
        {
            string oldPath       = this.AbsolutePath;
            bool   expandNewNode = this.Expanded;
            bool   selected      = this.Selected;

            // Make sure the environment says we can start the rename.
            if (!this.Hierarchy.AttachedProject.Tracker.CanRenameDirectory(oldPath, newPath))
            {
                return;
            }

            // Create the new folder before moving any of the children. Getting the parent is tricky
            // because on recursion this.Parent will point to the same parent and not be remapped
            // to the new location yet. Therefore, we have to use this mechanism to get the right parent.
            FolderNode newParent = (this.Parent.NewNodeOnRename == null ? this.Parent : this.Parent.NewNodeOnRename);
            FolderNode newNode   = this.Hierarchy.CreateAndAddFolder(newParent, newCaption);

            this.NewNodeOnRename = newNode;

            // Iterate through the children and change their hierarchy/file locations.
            // Do it on a cloned collection, however, since the collection will be
            // changing from underneath us. Note that the directory will automatically
            // be created when the first child is moved.
            ArrayList clone = new ArrayList(this.Children);

            foreach (Node child in clone)
            {
                string newChildPath = Path.Combine(newPath, child.Caption);
                child.MoveNodeOnCaptionChange(child.Caption, newChildPath);
            }

            // Move the rest of the files in the old directory to the new one.
            PackageUtility.XMove(oldPath, newPath);

            // Delete the old directory (it should be empty).
            DirectoryInfo oldDir = new DirectoryInfo(oldPath);

            if (oldDir.Exists && oldDir.GetFileSystemInfos().Length == 0)
            {
                oldDir.Delete(true);
            }

            // Remove us from the hierarchy.
            this.RemoveFromProject();

            // Expand and select the new node if we were expanded.
            if (expandNewNode)
            {
                newNode.Expand();
            }

            if (selected)
            {
                newNode.Select();
            }

            // Tell the environment that we're done renaming the document.
            this.Hierarchy.AttachedProject.Tracker.OnDirectoryRenamed(oldPath, newPath);

            // Update the property browser.
            IVsUIShell vsUIShell = (IVsUIShell)this.Hierarchy.ServiceProvider.GetServiceOrThrow(typeof(SVsUIShell), typeof(IVsUIShell), classType, "MoveNodeOnCaptionChange");

            vsUIShell.RefreshPropertyBrowser(0);
        }