From f44ff1e01587a4f763434b932ab148de8900debf Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 10 Apr 2007 11:58:04 +0000 Subject: [PATCH] AddExitingProjectToSolution, AddNewProjectToSolution, AddExistingItemsToProject: set initial directory based on the node that was clicked. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2477 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/Gui/Dialogs/NewProjectDialog.cs | 9 ++++++ .../Commands/FolderNodeCommands.cs | 2 ++ .../Commands/SolutionNodeCommands.cs | 30 +++++++++++++++++++ .../Src/Services/FileUtility/FileUtility.cs | 26 ++++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/NewProjectDialog.cs b/src/Main/Base/Project/Src/Gui/Dialogs/NewProjectDialog.cs index b4f6b68771..52af68c91b 100644 --- a/src/Main/Base/Project/Src/Gui/Dialogs/NewProjectDialog.cs +++ b/src/Main/Base/Project/Src/Gui/Dialogs/NewProjectDialog.cs @@ -34,6 +34,15 @@ namespace ICSharpCode.SharpDevelop.Project.Dialogs protected bool createNewSolution; + public string InitialProjectLocationDirectory { + get { + return ((TextBox)ControlDictionary["locationTextBox"]).Text; + } + set { + ((TextBox)ControlDictionary["locationTextBox"]).Text = value; + } + } + public NewProjectDialog(bool createNewSolution) { StandardHeader.SetHeaders(); 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 757ec14f60..ce099cecf9 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 @@ -177,6 +177,7 @@ namespace ICSharpCode.SharpDevelop.Project.Commands fdiag.AddExtension = true; string[] fileFilters = (string[])(AddInTree.GetTreeNode("/SharpDevelop/Workbench/FileFilter").BuildChildItems(this)).ToArray(typeof(string)); + fdiag.InitialDirectory = node.Directory; fdiag.FilterIndex = GetFileFilterIndex(node.Project, fileFilters); fdiag.Filter = String.Join("|", fileFilters); fdiag.Multiselect = true; @@ -230,6 +231,7 @@ namespace ICSharpCode.SharpDevelop.Project.Commands if (res == 2) { return; } + // only continue for res==0 (Copy) } bool replaceAll = false; foreach (KeyValuePair pair in fileNames) { diff --git a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/SolutionNodeCommands.cs b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/SolutionNodeCommands.cs index 34a9b6d3b3..0398687dc2 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/SolutionNodeCommands.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ProjectBrowser/Commands/SolutionNodeCommands.cs @@ -22,6 +22,9 @@ namespace ICSharpCode.SharpDevelop.Project.Commands ISolutionFolderNode solutionFolderNode = node as ISolutionFolderNode; if (node != null) { using (NewProjectDialog npdlg = new NewProjectDialog(false)) { + npdlg.InitialProjectLocationDirectory = GetInitialDirectorySuggestion(solutionFolderNode); + + // show the dialog to request project type and name if (npdlg.ShowDialog(ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm) == DialogResult.OK) { if (npdlg.NewProjectLocation.Length == 0) { MessageService.ShowError("No project has been created, there is nothing to add."); @@ -33,6 +36,32 @@ namespace ICSharpCode.SharpDevelop.Project.Commands } } } + + internal static string GetInitialDirectorySuggestion(ISolutionFolderNode solutionFolderNode) + { + // Detect the correct folder to place the new project in: + int projectCount = 0; + string initialDirectory = null; + foreach (ISolutionFolder folderEntry in solutionFolderNode.Container.Folders) { + IProject project = folderEntry as IProject; + if (project != null) { + if (projectCount == 0) + initialDirectory = project.Directory; + else + initialDirectory = FileUtility.GetCommonBaseDirectory(initialDirectory, project.Directory); + projectCount++; + } + } + if (initialDirectory != null) { + if (projectCount == 1) { + return FileUtility.GetAbsolutePath(initialDirectory, ".."); + } else { + return initialDirectory; + } + } else { + return solutionFolderNode.Solution.Directory; + } + } } public class AddExitingProjectToSolution : AbstractMenuCommand @@ -62,6 +91,7 @@ namespace ICSharpCode.SharpDevelop.Project.Commands fdiag.Filter = ProjectService.GetAllProjectsFilter(this); fdiag.Multiselect = true; fdiag.CheckFileExists = true; + fdiag.InitialDirectory = AddNewProjectToSolution.GetInitialDirectorySuggestion(solutionFolderNode); if (fdiag.ShowDialog(ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm) == DialogResult.OK) { foreach (string fileName in fdiag.FileNames) { AddProject(solutionFolderNode, fileName); diff --git a/src/Main/Core/Project/Src/Services/FileUtility/FileUtility.cs b/src/Main/Core/Project/Src/Services/FileUtility/FileUtility.cs index ec46d1ab7c..00f0738d55 100644 --- a/src/Main/Core/Project/Src/Services/FileUtility/FileUtility.cs +++ b/src/Main/Core/Project/Src/Services/FileUtility/FileUtility.cs @@ -108,6 +108,32 @@ namespace ICSharpCode.Core return path.IndexOf(':') >= 2; } + public static string GetCommonBaseDirectory(string dir1, string dir2) + { + if (dir1 == null || dir2 == null) return null; + if (IsUrl(dir1) || IsUrl(dir2)) return null; + + dir1 = Path.GetFullPath(dir1); + dir2 = Path.GetFullPath(dir2); + + string[] aPath = dir1.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + string[] bPath = dir2.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + StringBuilder result = new StringBuilder(); + int indx = 0; + for(; indx < Math.Min(bPath.Length, aPath.Length); ++indx) { + if (bPath[indx].Equals(aPath[indx], StringComparison.OrdinalIgnoreCase)) { + if (result.Length > 0) result.Append(Path.DirectorySeparatorChar); + result.Append(aPath[indx]); + } else { + break; + } + } + if (indx == 0) + return null; + else + return result.ToString(); + } + /// /// Converts a given absolute path and a given base path to a path that leads /// from the base path to the absoulte path. (as a relative path)