From ca8804629791fe207501f4c9808bff1f70134e28 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 3 Apr 2013 01:16:42 +0200 Subject: [PATCH] Hide NewFileDialog behind IUIService. --- .../Project/Configuration/AssemblyInfo.cs | 1 + .../Project/ICSharpCode.SharpDevelop.csproj | 1 + src/Main/Base/Project/Services/IUIService.cs | 20 +++++++++ .../Base/Project/Src/Commands/FileCommands.cs | 4 +- .../Project/Src/Gui/Dialogs/NewFileDialog.cs | 24 ++++++----- .../Commands/FolderNodeCommands.cs | 28 ++++--------- .../Base/Project/Templates/FileTemplate.cs | 16 +++++++- .../Project/Templates/FileTemplateResult.cs | 41 +++++++++++++++++++ .../Project/Templates/ITemplateService.cs | 6 +++ src/Main/SharpDevelop/Services/UIService.cs | 29 +++++++++++++ 10 files changed, 134 insertions(+), 36 deletions(-) create mode 100644 src/Main/Base/Project/Templates/FileTemplateResult.cs diff --git a/src/Main/Base/Project/Configuration/AssemblyInfo.cs b/src/Main/Base/Project/Configuration/AssemblyInfo.cs index eece7d0f38..5c893ace55 100644 --- a/src/Main/Base/Project/Configuration/AssemblyInfo.cs +++ b/src/Main/Base/Project/Configuration/AssemblyInfo.cs @@ -25,3 +25,4 @@ using System.Windows.Markup; [assembly: AssemblyCulture("")] [assembly: InternalsVisibleTo("SharpDevelop,PublicKey=002400000480000094000000060200000024000052534131000400000100010063d5db5957250f41969c79e88cbd8806165ca7d96d468a9d60f11704a8b0698684b5acc16fc82e6cede459e1d6ed7384b044c47a67d68bae1e08182473168e2ad92c6fabe32b9217ea59d05bb9a101318aeec9f767991d2ae8d987b60c591b6020d2816c395db7f3045a1c77c2b074c508c2b4f25dcd969688da94ebc83f5f9b")] +[assembly: InternalsVisibleTo("ICSharpCode.SharpDevelop.Tests,PublicKey=002400000480000094000000060200000024000052534131000400000100010063d5db5957250f41969c79e88cbd8806165ca7d96d468a9d60f11704a8b0698684b5acc16fc82e6cede459e1d6ed7384b044c47a67d68bae1e08182473168e2ad92c6fabe32b9217ea59d05bb9a101318aeec9f767991d2ae8d987b60c591b6020d2816c395db7f3045a1c77c2b074c508c2b4f25dcd969688da94ebc83f5f9b")] diff --git a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj index adfd0daa71..a47230b68e 100644 --- a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj +++ b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj @@ -237,6 +237,7 @@ + diff --git a/src/Main/Base/Project/Services/IUIService.cs b/src/Main/Base/Project/Services/IUIService.cs index 93f9354db8..e9a4fc436f 100644 --- a/src/Main/Base/Project/Services/IUIService.cs +++ b/src/Main/Base/Project/Services/IUIService.cs @@ -2,7 +2,10 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.Collections.Generic; +using ICSharpCode.Core; using ICSharpCode.SharpDevelop.Project; +using ICSharpCode.SharpDevelop.Templates; namespace ICSharpCode.SharpDevelop { @@ -13,6 +16,23 @@ namespace ICSharpCode.SharpDevelop /// public interface IUIService { + /// + /// Shows the 'Edit Solution Configurations' dialog. + /// void ShowSolutionConfigurationEditorDialog(ISolution solution); + + /// + /// Shows the 'New File' dialog. + /// + /// The parent project to which the new file should be added. + /// May be null to create files outside of a project. + /// The target directory to which the new file should be saved. + /// May be null to create an untitled file. + /// The list of templates that are available in the dialog. + /// Pass null to use the default list () + /// + /// Returns the FileTemplateResult; or null if no file was created. + /// + FileTemplateResult ShowNewFileDialog(IProject project, DirectoryName directory, IEnumerable templates = null); } } diff --git a/src/Main/Base/Project/Src/Commands/FileCommands.cs b/src/Main/Base/Project/Src/Commands/FileCommands.cs index 084b4871fa..5654bd3a12 100644 --- a/src/Main/Base/Project/Src/Commands/FileCommands.cs +++ b/src/Main/Base/Project/Src/Commands/FileCommands.cs @@ -41,9 +41,7 @@ namespace ICSharpCode.SharpDevelop.Commands } } - using (NewFileDialog nfd = new NewFileDialog(null)) { - nfd.ShowDialog(SD.WinForms.MainWin32Window); - } + SD.UIService.ShowNewFileDialog(null, null); } } diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs b/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs index 9ca4b88e6e..b32fa00471 100644 --- a/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs +++ b/src/Main/Base/Project/Src/Gui/Dialogs/NewFileDialog.cs @@ -25,14 +25,16 @@ namespace ICSharpCode.SharpDevelop.Gui /// /// This class is for creating a new "empty" file /// - public class NewFileDialog : BaseSharpDevelopForm + internal class NewFileDialog : BaseSharpDevelopForm { ArrayList alltemplates = new ArrayList(); ArrayList categories = new ArrayList(); Hashtable icons = new Hashtable(); bool allowUntitledFiles; - string basePath; + IProject project; + DirectoryName basePath; List> createdFiles = new List>(); + internal FileTemplateOptions options; public List> CreatedFiles { get { @@ -40,9 +42,10 @@ namespace ICSharpCode.SharpDevelop.Gui } } - public NewFileDialog(string basePath) + public NewFileDialog(IProject project, DirectoryName basePath) { StandardHeader.SetHeaders(); + this.project = project; this.basePath = basePath; this.allowUntitledFiles = basePath == null; try { @@ -469,13 +472,12 @@ namespace ICSharpCode.SharpDevelop.Gui } fileName = Path.Combine(basePath, fileName); fileName = FileUtility.NormalizePath(fileName); - IProject project = ProjectService.CurrentProject; if (project != null) { StringParserPropertyContainer.FileCreation["StandardNamespace"] = CustomToolsService.GetDefaultNamespace(project, fileName); } } - FileTemplateOptions options = new FileTemplateOptions(); + options = new FileTemplateOptions(); options.ClassName = GenerateValidClassOrNamespaceName(Path.GetFileNameWithoutExtension(fileName), false); options.FileName = FileName.Create(fileName); options.IsUntitled = allowUntitledFiles; @@ -490,20 +492,20 @@ namespace ICSharpCode.SharpDevelop.Gui StringParserPropertyContainer.FileCreation["ClassName"] = options.ClassName; // when adding a file to a project (but not when creating a standalone file while a project is open): - if (ProjectService.CurrentProject != null && !this.allowUntitledFiles) { - options.Project = ProjectService.CurrentProject; + if (project != null && !this.allowUntitledFiles) { + options.Project = project; // add required assembly references to the project bool changes = false; foreach (ReferenceProjectItem reference in item.Template.RequiredAssemblyReferences) { - IEnumerable refs = ProjectService.CurrentProject.GetItemsOfType(ItemType.Reference); + IEnumerable refs = project.GetItemsOfType(ItemType.Reference); if (!refs.Any(projItem => string.Equals(projItem.Include, reference.Include, StringComparison.OrdinalIgnoreCase))) { - ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(ProjectService.CurrentProject); - ProjectService.AddProjectItem(ProjectService.CurrentProject, projItem); + ReferenceProjectItem projItem = (ReferenceProjectItem)reference.CloneFor(project); + ProjectService.AddProjectItem(project, projItem); changes = true; } } if (changes) { - ProjectService.CurrentProject.Save(); + project.Save(); } } diff --git a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/FolderNodeCommands.cs b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/FolderNodeCommands.cs index 7e576fe810..e25dbdba07 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/FolderNodeCommands.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/FolderNodeCommands.cs @@ -6,10 +6,10 @@ using System.Collections.Generic; using System.Linq; using System.IO; using System.Windows.Forms; - using ICSharpCode.Core; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop.Internal.Templates; +using ICSharpCode.SharpDevelop.Templates; namespace ICSharpCode.SharpDevelop.Project.Commands { @@ -348,27 +348,13 @@ namespace ICSharpCode.SharpDevelop.Project.Commands node.Expand(); node.Expanding(); - List addedItems = new List(); - - using (NewFileDialog nfd = new NewFileDialog(node.Directory)) { - if (nfd.ShowDialog(SD.WinForms.MainWin32Window) == DialogResult.OK) { - bool additionalProperties = false; - foreach (KeyValuePair createdFile in nfd.CreatedFiles) { - FileProjectItem item = node.AddNewFile(createdFile.Key); - addedItems.Add(item); - - if (createdFile.Value.SetProjectItemProperties(item)) { - additionalProperties = true; - } - } - if (additionalProperties) { - node.Project.Save(); - node.RecreateSubNodes(); - } - } + FileTemplateResult result = SD.UIService.ShowNewFileDialog(node.Project, node.Directory); + if (result != null) { + node.RecreateSubNodes(); + return result.NewFiles.Select(node.Project.FindFile).Where(f => f != null).ToArray(); + } else { + return null; } - - return addedItems.AsReadOnly(); } } diff --git a/src/Main/Base/Project/Templates/FileTemplate.cs b/src/Main/Base/Project/Templates/FileTemplate.cs index 5b9a96ac5d..0802ed11a2 100644 --- a/src/Main/Base/Project/Templates/FileTemplate.cs +++ b/src/Main/Base/Project/Templates/FileTemplate.cs @@ -3,6 +3,7 @@ using System; using ICSharpCode.Core; +using ICSharpCode.SharpDevelop.Project; namespace ICSharpCode.SharpDevelop.Templates { @@ -14,7 +15,15 @@ namespace ICSharpCode.SharpDevelop.Templates public abstract string Description { get; } public abstract IImage Icon { get; } - public virtual bool NewFileDialogVisible { get { return true; } } + /// + /// Gets whether this template is available for the specified project. + /// + /// The project to which the new file should be added. + /// Can be null when creating a file outside of a project. + public virtual bool IsVisible(IProject project) + { + return true; + } /// /// Proposes a name for the new file. @@ -31,5 +40,10 @@ namespace ICSharpCode.SharpDevelop.Templates { return null; } + + /// + /// Instanciates the template, writes the new files to disk, and adds them to the project. + /// + public abstract FileTemplateResult Create(FileTemplateOptions options); } } diff --git a/src/Main/Base/Project/Templates/FileTemplateResult.cs b/src/Main/Base/Project/Templates/FileTemplateResult.cs new file mode 100644 index 0000000000..624224ea14 --- /dev/null +++ b/src/Main/Base/Project/Templates/FileTemplateResult.cs @@ -0,0 +1,41 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Collections.Generic; +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop.Project; + +namespace ICSharpCode.SharpDevelop.Templates +{ + /// + /// Description of FileTemplateResult. + /// + public class FileTemplateResult + { + readonly FileTemplateOptions options; + + public FileTemplateResult(FileTemplateOptions options) + { + if (options == null) + throw new ArgumentNullException("options"); + this.options = options; + } + + /// + /// Gets the options used to instanciate the template. + /// + public FileTemplateOptions Options { + get { return options; } + } + + IList newFiles = new List(); + + /// + /// Gets the list of newly created files. + /// + public IList NewFiles { + get { return newFiles; } + } + } +} diff --git a/src/Main/Base/Project/Templates/ITemplateService.cs b/src/Main/Base/Project/Templates/ITemplateService.cs index f6ff9f8006..13d0f0dda6 100644 --- a/src/Main/Base/Project/Templates/ITemplateService.cs +++ b/src/Main/Base/Project/Templates/ITemplateService.cs @@ -11,8 +11,14 @@ namespace ICSharpCode.SharpDevelop.Templates [SDService("SD.Templates")] public interface ITemplateService { + /// + /// Gets the list of file templates that are available in the 'new file' dialog. + /// IEnumerable FileTemplates { get; } + /// + /// Loads a file template (.xft file) from a stream. + /// FileTemplate LoadFileTemplate(Stream stream); } } diff --git a/src/Main/SharpDevelop/Services/UIService.cs b/src/Main/SharpDevelop/Services/UIService.cs index f4b36539f9..9f078875e0 100644 --- a/src/Main/SharpDevelop/Services/UIService.cs +++ b/src/Main/SharpDevelop/Services/UIService.cs @@ -2,7 +2,14 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop.Gui; +using ICSharpCode.SharpDevelop.Internal.Templates; using ICSharpCode.SharpDevelop.Project; +using ICSharpCode.SharpDevelop.Templates; namespace ICSharpCode.SharpDevelop { @@ -19,5 +26,27 @@ namespace ICSharpCode.SharpDevelop } } } + + public FileTemplateResult ShowNewFileDialog(IProject project, DirectoryName directory, IEnumerable templates) + { + using (NewFileDialog nfd = new NewFileDialog(project, directory)) { + if (nfd.ShowDialog(SD.WinForms.MainWin32Window) != DialogResult.OK) + return null; + if (project != null) { + foreach (KeyValuePair createdFile in nfd.CreatedFiles) { + FileName fileName = FileName.Create(createdFile.Key); + ItemType type = project.GetDefaultItemType(fileName); + FileProjectItem newItem = new FileProjectItem(project, type); + newItem.FileName = fileName; + createdFile.Value.SetProjectItemProperties(newItem); + project.Items.Add(newItem); + } + project.Save(); + } + var result = new FileTemplateResult(nfd.options); + result.NewFiles.AddRange(nfd.CreatedFiles.Select(p => FileName.Create(p.Key))); + return result; + } + } } }