private void Fire(HierarchyNode node, Action <IVsExtensibility3, ProjectItem> fire) { // When we are in suspended mode. Do not fire anything if (this._suspended) { return; } // Project has to be opened if (!this._project.IsProjectOpened) { return; } // We don't want to fire events for references here. OAReferences should do the job if (node is ReferenceNode) { return; } if (this._project.GetService(typeof(IVsExtensibility)) is IVsExtensibility3 vsExtensibility) { var obj = node.GetAutomationObject(); if (obj is ProjectItem item) { fire(vsExtensibility, item); } } }
/// <summary> /// Adds a folder to the collection of ProjectItems with the given name. /// /// The kind must be null, empty string, or the string value of vsProjectItemKindPhysicalFolder. /// Virtual folders are not supported by this implementation. /// </summary> /// <param name="name">The name of the new folder to add</param> /// <param name="kind">A string representing a Guid of the folder kind.</param> /// <returns>A ProjectItem representing the newly added folder.</returns> public override ProjectItem AddFolder(string name, string kind) { Project.CheckProjectIsValid(); //Verify name is not null or empty Utilities.ValidateFileName(this.Project.ProjectNode.Site, name); //Verify that kind is null, empty, or a physical folder if (!(string.IsNullOrEmpty(kind) || kind.Equals(EnvDTE.Constants.vsProjectItemKindPhysicalFolder))) { throw new ArgumentException("Parameter specification for AddFolder was not meet", "kind"); } var existingChild = this.NodeWithItems.FindImmediateChildByName(name); if (existingChild != null) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Folder already exists with the name '{0}'", name)); } ProjectNode proj = this.Project.ProjectNode; HierarchyNode newFolder = null; using (AutomationScope scope = new AutomationScope(this.Project.ProjectNode.Site)) { //In the case that we are adding a folder to a folder, we need to build up //the path to the project node. name = Path.Combine(NodeWithItems.FullPathToChildren, name); newFolder = proj.CreateFolderNodes(name); } return(newFolder.GetAutomationObject() as ProjectItem); }
/// <summary> /// Returns an enumeration for items in a collection. /// </summary> /// <returns>An IEnumerator for this object.</returns> public virtual IEnumerator GetEnumerator() { for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { var item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (item != null) { yield return(item); } } }
/// <summary> /// Adds a folder to the collection of ProjectItems with the given name. /// /// The kind must be null, empty string, or the string value of vsProjectItemKindPhysicalFolder. /// Virtual folders are not supported by this implementation. /// </summary> /// <param name="name">The name of the new folder to add</param> /// <param name="kind">A string representing a Guid of the folder kind.</param> /// <returns>A ProjectItem representing the newly added folder.</returns> public override ProjectItem AddFolder(string name, string kind) { return(UIThread.DoOnUIThread(delegate() { if (this.Project == null || this.Project.Project == null || this.Project.Project.Site == null || this.Project.Project.IsClosed) { throw new InvalidOperationException(); } //Verify name is not null or empty Utilities.ValidateFileName(this.Project.Project.Site, name); //Verify that kind is null, empty, or a physical folder if (!(string.IsNullOrEmpty(kind) || kind.Equals(EnvDTE.Constants.vsProjectItemKindPhysicalFolder))) { throw new ArgumentException("Parameter specification for AddFolder was not meet", "kind"); } for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { if (child.Caption.Equals(name, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Folder already exists with the name '{0}'", name)); } } ProjectNode proj = this.Project.Project; IVsExtensibility3 extensibility = this.Project.Project.Site.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; if (extensibility == null) { throw new InvalidOperationException(); } HierarchyNode newFolder = null; extensibility.EnterAutomationFunction(); //In the case that we are adding a folder to a folder, we need to build up //the path to the project node. name = Path.Combine(this.NodeWithItems.VirtualNodeName, name); try { newFolder = proj.CreateFolderNodes(name); } finally { extensibility.ExitAutomationFunction(); } return newFolder.GetAutomationObject() as ProjectItem; })); }
/// <summary> /// Retrives a list of items associated with the current node. /// </summary> /// <returns>A List of project items</returns> protected IList <EnvDTE.ProjectItem> GetListOfProjectItems() { List <EnvDTE.ProjectItem> list = new List <EnvDTE.ProjectItem>(); for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { EnvDTE.ProjectItem item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (null != item) { list.Add(item); } } return(list); }
/// <summary> /// Retrives a list of items associated with the current node. /// </summary> /// <returns>A List of project items</returns> protected virtual IList <EnvDTE.ProjectItem> GetListOfProjectItems() { return(UIThread.DoOnUIThread(delegate() { List <EnvDTE.ProjectItem> list = new List <EnvDTE.ProjectItem>(); for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { EnvDTE.ProjectItem item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (null != item) { list.Add(item); } } return list; })); }
/// <summary> /// Retrives a list of items associated with the current node. /// </summary> /// <returns>A List of project items</returns> protected IList <EnvDTE.ProjectItem> GetListOfProjectItems() { return(ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); List <EnvDTE.ProjectItem> list = new List <EnvDTE.ProjectItem>(); for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { EnvDTE.ProjectItem item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (null != item) { list.Add(item); } } return list; })); }
/// <summary> /// Get Project Item from index /// </summary> /// <param name="index">Either index by number (1-based) or by name can be used to get the item</param> /// <returns>Project Item. ArgumentException if invalid index is specified</returns> public virtual EnvDTE.ProjectItem Item(object index) { // Changed from MPFProj: throws ArgumentException instead of returning null (http://mpfproj10.codeplex.com/workitem/9158) if (index is int) { int realIndex = (int)index - 1; if (realIndex >= 0) { for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { if (child.IsNonMemberItem) { continue; } var item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (item != null) { if (realIndex == 0) { return(item); } realIndex -= 1; } } } } else if (index is string) { string name = (string)index; for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { if (child.IsNonMemberItem) { continue; } var item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (item != null && String.Compare(item.Name, name, StringComparison.OrdinalIgnoreCase) == 0) { return(item); } } } throw new ArgumentException("Failed to find item: " + index); }
/// <summary> /// Retrives a list of items associated with the current node. /// </summary> /// <returns>A List of project items</returns> public IList <EnvDTE.ProjectItem> GetListOfProjectItems() { return(UIThread.DoOnUIThread(delegate() { List <EnvDTE.ProjectItem> list = new List <EnvDTE.ProjectItem>(); for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { if (!(child is ReferenceContainerNode)) // skip 'references' node when navigating over project items, to behave more like C#/VB { EnvDTE.ProjectItem item = child.GetAutomationObject() as EnvDTE.ProjectItem; if (null != item) { list.Add(item); } } } return list; })); }
/// <summary> /// Adds a folder to the collection of ProjectItems with the given name. /// /// The kind must be null, empty string, or the string value of vsProjectItemKindPhysicalFolder. /// Virtual folders are not supported by this implementation. /// </summary> /// <param name="name">The name of the new folder to add</param> /// <param name="kind">A string representing a Guid of the folder kind.</param> /// <returns>A ProjectItem representing the newly added folder.</returns> public override ProjectItem AddFolder(string name, string kind) { CheckProjectIsValid(); return(ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); //Verify name is not null or empty Utilities.ValidateFileName(this.Project.Project.Site, name); //Verify that kind is null, empty, or a physical folder if (!(String.IsNullOrEmpty(kind) || kind.Equals(EnvDTE.Constants.vsProjectItemKindPhysicalFolder))) { throw new ArgumentException("Parameter specification for AddFolder was not meet", "kind"); } for (HierarchyNode child = this.NodeWithItems.FirstChild; child != null; child = child.NextSibling) { if (string.Compare(child.Caption, name, StringComparison.OrdinalIgnoreCase) == 0) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Folder already exists with the name '{0}'", name)); } } ProjectNode proj = this.Project.Project; HierarchyNode newFolder = null; using (AutomationScope scope = new AutomationScope(this.Project.Project.Site)) { //In the case that we are adding a folder to a folder, we need to build up //the path to the project node. name = Path.Combine(this.NodeWithItems.VirtualNodeName, name); newFolder = proj.CreateFolderNodes(name); } return newFolder.GetAutomationObject() as ProjectItem; })); }
public virtual VSADDRESULT RunWizard(HierarchyNode parentNode, string itemName, string wizardToRun, IntPtr dlgOwner) { Debug.Assert(!String.IsNullOrEmpty(itemName), "The Add item dialog was passing in a null or empty item to be added to the hierrachy."); Debug.Assert(!String.IsNullOrEmpty(this.ProjectFolder), "The Project Folder is not specified for this project."); if (parentNode == null) { throw new ArgumentNullException("parentNode"); } if (String.IsNullOrEmpty(itemName)) { throw new ArgumentNullException("itemName"); } // We just validate for length, since we assume other validation has been performed by the dlgOwner. if (this.ProjectFolder.Length + itemName.Length + 1 > NativeMethods.MAX_PATH) { string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.PathTooLong, CultureInfo.CurrentUICulture), itemName); if (!Utilities.IsInAutomationFunction(this.Site)) { string title = null; OLEMSGICON icon = OLEMSGICON.OLEMSGICON_CRITICAL; OLEMSGBUTTON buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK; OLEMSGDEFBUTTON defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST; VsShellUtilities.ShowMessageBox(this.Site, title, errorMessage, icon, buttons, defaultButton); return VSADDRESULT.ADDRESULT_Failure; } else { throw new InvalidOperationException(errorMessage); } } // Build up the ContextParams safearray // [0] = Wizard type guid (bstr) // [1] = Project name (bstr) // [2] = ProjectItems collection (bstr) // [3] = Local Directory (bstr) // [4] = Filename the user typed (bstr) // [5] = Product install Directory (bstr) // [6] = Run silent (bool) object[] contextParams = new object[7]; contextParams[0] = EnvDTE.Constants.vsWizardAddItem; contextParams[1] = this.Caption; object automationObject = parentNode.GetAutomationObject(); if (automationObject is EnvDTE.Project) { EnvDTE.Project project = (EnvDTE.Project)automationObject; contextParams[2] = project.ProjectItems; } else { // This would normally be a folder unless it is an item with subitems EnvDTE.ProjectItem item = (EnvDTE.ProjectItem)automationObject; contextParams[2] = item.ProjectItems; } contextParams[3] = this.ProjectFolder; contextParams[4] = itemName; object objInstallationDir = null; IVsShell shell = (IVsShell)this.GetService(typeof(IVsShell)); ErrorHandler.ThrowOnFailure(shell.GetProperty((int)__VSSPROPID.VSSPROPID_InstallDirectory, out objInstallationDir)); string installDir = (string)objInstallationDir; // append a '\' to the install dir to mimic what the shell does (though it doesn't add one to destination dir) if (!installDir.EndsWith("\\", StringComparison.Ordinal)) { installDir += "\\"; } contextParams[5] = installDir; contextParams[6] = true; IVsExtensibility3 ivsExtensibility = this.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; Debug.Assert(ivsExtensibility != null, "Failed to get IVsExtensibility3 service"); if (ivsExtensibility == null) { return VSADDRESULT.ADDRESULT_Failure; } // Determine if we have the trust to run this wizard. IVsDetermineWizardTrust wizardTrust = this.GetService(typeof(SVsDetermineWizardTrust)) as IVsDetermineWizardTrust; if (wizardTrust != null) { Guid guidProjectAdding = Guid.Empty; ErrorHandler.ThrowOnFailure(wizardTrust.OnWizardInitiated(wizardToRun, ref guidProjectAdding)); } int wizResultAsInt; try { Array contextParamsAsArray = contextParams; int result = ivsExtensibility.RunWizardFile(wizardToRun, (int)dlgOwner, ref contextParamsAsArray, out wizResultAsInt); if (!ErrorHandler.Succeeded(result) && result != VSConstants.OLE_E_PROMPTSAVECANCELLED) { ErrorHandler.ThrowOnFailure(result); } } finally { if (wizardTrust != null) { ErrorHandler.ThrowOnFailure(wizardTrust.OnWizardCompleted()); } } EnvDTE.wizardResult wizardResult = (EnvDTE.wizardResult)wizResultAsInt; switch (wizardResult) { default: return VSADDRESULT.ADDRESULT_Cancel; case wizardResult.wizardResultSuccess: return VSADDRESULT.ADDRESULT_Success; case wizardResult.wizardResultFailure: return VSADDRESULT.ADDRESULT_Failure; } }
private void Fire(HierarchyNode node, Action<IVsExtensibility3, ProjectItem> fire) { // When we are in suspended mode. Do not fire anything if (this._suspended) { return; } // Project has to be opened if (!this._project.IsProjectOpened) { return; } // We don't want to fire events for references here. OAReferences should do the job if (node is ReferenceNode) { return; } IVsExtensibility3 vsExtensibility = this._project.GetService(typeof(IVsExtensibility)) as IVsExtensibility3; if (vsExtensibility != null) { object obj = node.GetAutomationObject(); ProjectItem item = obj as ProjectItem; if (item != null) { fire(vsExtensibility, item); } } }