Example #1
0
        /// <summary>
        /// Performs initial verifications on the new caption before anything is changed. This includes
        /// making sure that it's a valid file name and that the user wants to change the extension.
        /// </summary>
        /// <param name="newCaption">The new caption value.</param>
        /// <param name="newPath">The new absolute path.</param>
        /// <returns>
        /// true if processing should continue after the method returns; false if the caller should return S_OK.
        /// Note that on errors an exception is thrown.
        /// </returns>
        protected virtual bool VerifyCaption(string newCaption, string newPath)
        {
            if (!this.CaptionEditable)
            {
                Tracer.Fail("SetCaption should not be called when the caption is not editable.");
                return(false);
            }

            // If the caption is exactly the same, then there's no need to do anything.
            bool differInCaseOnly;

            if (PackageUtility.FileStringEquals(this.Caption, newCaption, out differInCaseOnly) && !differInCaseOnly)
            {
                return(false);
            }

            // Make sure the new caption is a valid file name.
            if (!PackageUtility.IsValidFileOrFolderName(newCaption))
            {
                throw new InvalidOperationException(SconceStrings.ErrorInvalidFileOrFolderName);
            }

            // If the old and the new caption differ in just case, then we won't do any
            // file moving since the file system is case-insensitive. We do want to allow
            // users to change the case on their captions, though.
            if (differInCaseOnly)
            {
                this.OnPropertyChanged(__VSHPROPID.VSHPROPID_Caption);
                return(false);
            }

            // Make sure the file doesn't already exist in the hierarchy or the file system.
            this.VerifyCaptionDoesNotExist(newCaption, newPath);

            // We don't want to do anything if we don't own the document.
            if (this.DocumentCookie != DocumentInfo.NullCookie && this.DocumentCookie != this.Hierarchy.RootNode.DocumentCookie)
            {
                DocumentInfo docInfo = Context.RunningDocumentTable.FindByCookie(this.DocumentCookie);
                if (docInfo.VisualStudioHierarhcy == null || !PackageUtility.IsSameComObject(docInfo.VisualStudioHierarhcy, (Hierarchy)this.Hierarchy))
                {
                    return(false);
                }
            }

            return(true);
        }