/* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Apache License, Version 2.0. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Apache License, Version 2.0, please send an email to * vspython@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Apache License, Version 2.0. * * You must not remove this notice, or any other, from this software. * * ***************************************************************************/ using System; using System.Diagnostics; using System.Runtime.InteropServices; using Microsoft.VisualStudio; using Microsoft.VisualStudio.Shell.Interop; namespace Microsoft.VisualStudio.Project.Automation { [ComVisible(true)] public class OAProjectItem : EnvDTE.ProjectItem { #region fields private HierarchyNode node; private OAProject project; #endregion #region properties public HierarchyNode Node { get { return this.node; } } /// /// Returns the automation project /// protected OAProject Project { get { return this.project; } } #endregion #region ctors public OAProjectItem(OAProject project, HierarchyNode node) { this.node = node; this.project = project; } #endregion #region EnvDTE.ProjectItem /// /// Gets the requested Extender if it is available for this object /// /// The name of the extender. /// The extender object. public virtual object get_Extender(string extenderName) { return null; } /// /// Gets an object that can be accessed by name at run time. /// public virtual object Object { get { return this.node.Object; } } /// /// Gets the Document associated with the item, if one exists. /// public virtual EnvDTE.Document Document { get { return null; } } /// /// Gets the number of files associated with a ProjectItem. /// public virtual short FileCount { get { return (short)1; } } /// /// Gets a collection of all properties that pertain to the object. /// public virtual EnvDTE.Properties Properties { get { if (this.node.NodeProperties == null) { return null; } return new OAProperties(this.node.NodeProperties); } } /// /// Gets the FileCodeModel object for the project item. /// public virtual EnvDTE.FileCodeModel FileCodeModel { get { return null; } } /// /// Gets a ProjectItems for the object. /// public virtual EnvDTE.ProjectItems ProjectItems { get { return null; } } /// /// Gets a GUID string indicating the kind or type of the object. /// public virtual string Kind { get { Guid guid; ErrorHandler.ThrowOnFailure(this.node.GetGuidProperty((int)__VSHPROPID.VSHPROPID_TypeGuid, out guid)); return guid.ToString("B"); } } /// /// Saves the project item. /// /// The name with which to save the project or project item. /// Implemented by subclasses. public virtual void Save(string fileName) { throw new NotImplementedException(); } /// /// Gets the top-level extensibility object. /// public virtual EnvDTE.DTE DTE { get { return (EnvDTE.DTE)this.project.DTE; } } /// /// Gets the ProjectItems collection containing the ProjectItem object supporting this property. /// public virtual EnvDTE.ProjectItems Collection { get { // Get the parent node HierarchyNode parentNode = this.node.Parent; Debug.Assert(parentNode != null, "Failed to get the parent node"); // Get the ProjectItems object for the parent node if (parentNode is ProjectNode) { // The root node for the project return ((OAProject)parentNode.GetAutomationObject()).ProjectItems; } else { // Not supported. Override this method in derived classes to return appropriate collection object throw new InvalidOperationException(); } } } /// /// Gets a list of available Extenders for the object. /// public virtual object ExtenderNames { get { return null; } } /// /// Gets the ConfigurationManager object for this ProjectItem. /// /// We do not support config management based per item. public virtual EnvDTE.ConfigurationManager ConfigurationManager { get { return null; } } /// /// Gets the project hosting the ProjectItem. /// public virtual EnvDTE.Project ContainingProject { get { return this.project; } } /// /// Gets or sets a value indicating whether or not the object has been modified since last being saved or opened. /// public virtual bool Saved { get { return !this.IsDirty; } set { throw new NotImplementedException(); } } /// /// Gets the Extender category ID (CATID) for the object. /// public virtual string ExtenderCATID { get { return null; } } /// /// If the project item is the root of a subproject, then the SubProject property returns the Project object for the subproject. /// public virtual EnvDTE.Project SubProject { get { return null; } } /// /// Microsoft public Use Only. Checks if the document associated to this item is dirty. /// public virtual bool IsDirty { get { return false; } set { } } /// /// Gets or sets the name of the object. /// public virtual string Name { get { return this.node.Caption; } set { CheckProjectIsValid(); using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { UIThread.Instance.RunSync(() => this.node.SetEditLabel(value)); } } } protected void CheckProjectIsValid() { VsUtilities.CheckNotNull(this.node); VsUtilities.CheckNotNull(this.node.ProjectMgr); VsUtilities.CheckNotNull(this.node.ProjectMgr.Site); if (this.node.ProjectMgr.IsClosed) { throw new InvalidOperationException(); } } /// /// Removes the project item from hierarchy. /// public virtual void Remove() { CheckProjectIsValid(); using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { UIThread.Instance.RunSync(() => this.node.Remove(false)); } } /// /// Removes the item from its project and its storage. /// public virtual void Delete() { CheckProjectIsValid(); using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { UIThread.Instance.RunSync(() => this.node.Remove(true)); } } /// /// Saves the project item. /// /// The file name with which to save the solution, project, or project item. If the file exists, it is overwritten. /// true if save was successful /// This method is implemented on subclasses. public virtual bool SaveAs(string newFileName) { throw new NotImplementedException(); } /// /// Gets a value indicating whether the project item is open in a particular view type. /// /// A Constants.vsViewKind* indicating the type of view to check./param> /// A Boolean value indicating true if the project is open in the given view type; false if not. public virtual bool get_IsOpen(string viewKind) { throw new NotImplementedException(); } /// /// Gets the full path and names of the files associated with a project item. /// /// The index of the item /// The full path of the associated item /// Is thrown if index is not one public virtual string get_FileNames(short index) { // This method should really only be called with 1 as the parameter, but // there used to be a bug in VB/C# that would work with 0. To avoid breaking // existing automation they are still accepting 0. To be compatible with them // we accept it as well. if (index < 0) { throw new ArgumentOutOfRangeException("index"); } return this.node.Url; } /// /// Expands the view of Solution Explorer to show project items. /// public virtual void ExpandView() { CheckProjectIsValid(); using (AutomationScope scope = new AutomationScope(this.Node.ProjectMgr.Site)) { UIThread.Instance.RunSync(() => { node.ExpandItem(EXPANDFLAGS.EXPF_ExpandFolder); }); } } /// /// Opens the project item in the specified view. Not implemented because this abstract class dont know what to open /// /// Specifies the view kind in which to open the item /// Window object public virtual EnvDTE.Window Open(string ViewKind) { throw new NotImplementedException(); } #endregion } }