Esempio n. 1
0
		/// <summary>
		/// Ensure that the current project item has the required extensions loaded.
		/// This is only called after verifying that the current project item does
		/// not satisfy the requirements.
		/// </summary>
		/// <param name="projectItem">The <see cref="EnvDTE.ProjectItem"/> to modify</param>
		/// <param name="serviceProvider">The <see cref="IServiceProvider"/> for document tracking.</param>
		/// <param name="extensions">An <see cref="T:ICollection{System.String}"/> of additional required extensions</param>
		public static bool EnsureExtensions(EnvDTE.ProjectItem projectItem, IServiceProvider serviceProvider, ICollection<string> extensions)
		{
			ServiceProvider provider = new ServiceProvider((Microsoft.VisualStudio.OLE.Interop.IServiceProvider)projectItem.DTE);
			// UNDONE: Localize message strings in here
			if ((int)DialogResult.No == VsShellUtilities.ShowMessageBox(
				provider,
				"Additional extensions are required to support the chosen generators. Would you like to load the required extensions now?",
				"ORM Generator Selection",
				OLEMSGICON.OLEMSGICON_QUERY,
				OLEMSGBUTTON.OLEMSGBUTTON_YESNO,
				OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST))
			{
				return false;
			}
			EnvDTE.Document document = projectItem.Document;
			bool secondPass = false;
			bool tryDocument = true;
			string itemPath = null;
			while (tryDocument)
			{
				object documentExtensionManager;
				MethodInfo methodInfo;
				if (null != document &&
					null != (documentExtensionManager = ORMCustomToolUtility.GetDocumentExtension<object>(document, "ORMExtensionManager", itemPath ?? (itemPath = projectItem.get_FileNames(0)), serviceProvider)) &&
					null != (methodInfo = documentExtensionManager.GetType().GetMethod("EnsureExtensions", new Type[] { typeof(string[]) })))
				{
					string[] extensionsArray = new string[extensions.Count];
					extensions.CopyTo(extensionsArray, 0);
					methodInfo.Invoke(documentExtensionManager, new object[] { extensionsArray });
					return true;
				}

				if (secondPass)
				{
					return false;
				}
				tryDocument = false;
				secondPass = true;

				// UNDONE: Localize message strings in here
				if ((int)DialogResult.No == VsShellUtilities.ShowMessageBox(
					provider,
					"The .ORM file must be open in the default designer to add extensions. Would you like to open or reopen the document now?",
					"ORM Generator Selection",
					OLEMSGICON.OLEMSGICON_QUERY,
					OLEMSGBUTTON.OLEMSGBUTTON_YESNO,
					OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST))
				{
					return false;
				}

				if (document != null)
				{
					document.Close(EnvDTE.vsSaveChanges.vsSaveChangesPrompt);
					document = projectItem.Document;
				}
				if (document == null)
				{
					projectItem.Open(Guid.Empty.ToString("B")).Visible = true;
					document = projectItem.Document;
					tryDocument = true;
				}
			}
			return false;
		}