Ejemplo n.º 1
0
        protected IEnumerable <FileProjectItem> AddExistingItems()
        {
            DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;

            if (node == null)
            {
                return(null);
            }
            node.Expanding();
            node.Expand();

            List <FileProjectItem> addedItems = new List <FileProjectItem>();

            using (OpenFileDialog fdiag = new OpenFileDialog()) {
                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;
                fdiag.CheckFileExists  = true;
                fdiag.Title            = StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles}");

                if (fdiag.ShowDialog(ICSharpCode.SharpDevelop.Gui.WorkbenchSingleton.MainForm) == DialogResult.OK)
                {
                    List <KeyValuePair <string, string> > fileNames = new List <KeyValuePair <string, string> >(fdiag.FileNames.Length);
                    foreach (string fileName in fdiag.FileNames)
                    {
                        fileNames.Add(new KeyValuePair <string, string>(fileName, ""));
                    }
                    bool addedDependentFiles = false;
                    foreach (string fileName in fdiag.FileNames)
                    {
                        foreach (string additionalFile in FindAdditionalFiles(fileName))
                        {
                            if (!fileNames.Exists(delegate(KeyValuePair <string, string> pair) {
                                return(FileUtility.IsEqualFileName(pair.Key, additionalFile));
                            }))
                            {
                                addedDependentFiles = true;
                                fileNames.Add(new KeyValuePair <string, string>(additionalFile, Path.GetFileName(fileName)));
                            }
                        }
                    }



                    string copiedFileName = Path.Combine(node.Directory, Path.GetFileName(fileNames[0].Key));
                    if (!FileUtility.IsEqualFileName(fileNames[0].Key, copiedFileName))
                    {
                        int res = MessageService.ShowCustomDialog(
                            fdiag.Title, "${res:ProjectComponent.ContextMenu.AddExistingFiles.Question}",
                            0, 2,
                            "${res:ProjectComponent.ContextMenu.AddExistingFiles.Copy}",
                            "${res:ProjectComponent.ContextMenu.AddExistingFiles.Link}",
                            "${res:Global.CancelButtonText}");
                        if (res == 1)
                        {
                            // Link
                            foreach (KeyValuePair <string, string> pair in fileNames)
                            {
                                string          fileName        = pair.Key;
                                string          relFileName     = FileUtility.GetRelativePath(node.Project.Directory, fileName);
                                FileNode        fileNode        = new FileNode(fileName, FileNodeStatus.InProject);
                                FileProjectItem fileProjectItem = new FileProjectItem(node.Project, node.Project.GetDefaultItemType(fileName), relFileName);
                                fileProjectItem.SetEvaluatedMetadata("Link", Path.Combine(node.RelativePath, Path.GetFileName(fileName)));
                                fileProjectItem.DependentUpon = pair.Value;
                                addedItems.Add(fileProjectItem);
                                fileNode.ProjectItem = fileProjectItem;
                                fileNode.InsertSorted(node);
                                ProjectService.AddProjectItem(node.Project, fileProjectItem);
                            }
                            node.Project.Save();
                            if (addedDependentFiles)
                            {
                                node.RecreateSubNodes();
                            }
                            return(addedItems.AsReadOnly());
                        }
                        if (res == 2)
                        {
                            // Cancel
                            return(addedItems.AsReadOnly());
                        }
                        // only continue for res==0 (Copy)
                    }
                    bool replaceAll = false;
                    foreach (KeyValuePair <string, string> pair in fileNames)
                    {
                        copiedFileName = Path.Combine(node.Directory, Path.GetFileName(pair.Key));
                        if (!replaceAll && File.Exists(copiedFileName) && !FileUtility.IsEqualFileName(pair.Key, copiedFileName))
                        {
                            ReplaceExistingFile res = ShowReplaceExistingFileDialog(fdiag.Title, Path.GetFileName(pair.Key), true);
                            if (res == ReplaceExistingFile.YesToAll)
                            {
                                replaceAll = true;
                            }
                            else if (res == ReplaceExistingFile.No)
                            {
                                continue;
                            }
                            else if (res == ReplaceExistingFile.Cancel)
                            {
                                break;
                            }
                        }
                        FileProjectItem item = CopyFile(pair.Key, node, true);
                        if (item != null)
                        {
                            addedItems.Add(item);
                            item.DependentUpon = pair.Value;
                        }
                    }
                    node.Project.Save();
                    if (addedDependentFiles)
                    {
                        node.RecreateSubNodes();
                    }
                }
            }

            return(addedItems.AsReadOnly());
        }
Ejemplo n.º 2
0
        protected IEnumerable <FileProjectItem> AddExistingItems()
        {
            DirectoryNode node = ProjectBrowserPad.Instance.ProjectBrowserControl.SelectedDirectoryNode;

            if (node == null)
            {
                return(null);
            }
            //node.Expanding();
            node.ExpandSubtree();

            List <FileProjectItem> addedItems = new List <FileProjectItem>();

            //using (OpenFileDialog fdiag  = new OpenFileDialog()) {
            string[] files = null;
            Caliburn.Micro.Execute.OnUIThread(delegate {
                Microsoft.Win32.OpenFileDialog fileDialog = new Microsoft.Win32.OpenFileDialog {
                    Title            = StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles}"),
                    CheckFileExists  = true,
                    RestoreDirectory = true,
                    InitialDirectory = node.Directory,
                    AddExtension     = true,
                    FilterIndex      = GetFileFilterIndex(node.Project, ProjectService.GetFileFilters()),
                    Filter           = String.Join("|", ProjectService.GetFileFilters()),
                    Multiselect      = true
                };

                if (fileDialog.ShowDialog() == true)
                {
                    files = fileDialog.FileNames;
                }
            });

            //if (fdiag.ShowDialog(SD.WinForms.MainWin32Window) == DialogResult.OK)
            {
                List <KeyValuePair <string, string> > fileNames = new List <KeyValuePair <string, string> >(files.Length);
                foreach (string fileName in files)
                {
                    fileNames.Add(new KeyValuePair <string, string>(fileName, ""));
                }
                bool addedDependentFiles = false;
                foreach (string fileName in files)
                {
                    foreach (string additionalFile in FindAdditionalFiles(fileName))
                    {
                        if (!fileNames.Exists(delegate(KeyValuePair <string, string> pair) {
                            return(FileUtility.IsEqualFileName(pair.Key, additionalFile));
                        }))
                        {
                            addedDependentFiles = true;
                            fileNames.Add(new KeyValuePair <string, string>(additionalFile, Path.GetFileName(fileName)));
                        }
                    }
                }



                string copiedFileName = Path.Combine(node.Directory, Path.GetFileName(fileNames[0].Key));
                if (!FileUtility.IsEqualFileName(fileNames[0].Key, copiedFileName))
                {
                    int res = MessageService.ShowCustomDialog(
                        StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles}"),
                        "${res:ProjectComponent.ContextMenu.AddExistingFiles.Question}",
                        0, 2,
                        "${res:ProjectComponent.ContextMenu.AddExistingFiles.Copy}",
                        "${res:ProjectComponent.ContextMenu.AddExistingFiles.Link}",
                        "${res:Global.CancelButtonText}");
                    if (res == 1)
                    {
                        // Link
                        foreach (KeyValuePair <string, string> pair in fileNames)
                        {
                            string          fileName        = pair.Key;
                            string          relFileName     = FileUtility.GetRelativePath(node.Project.Directory, fileName);
                            FileNode        fileNode        = new FileNode(fileName, FileNodeStatus.InProject);
                            FileProjectItem fileProjectItem = new FileProjectItem(node.Project, node.Project.GetDefaultItemType(fileName), relFileName);
                            fileProjectItem.SetEvaluatedMetadata("Link", Path.Combine(node.RelativePath, Path.GetFileName(fileName)));
                            fileProjectItem.DependentUpon = pair.Value;
                            addedItems.Add(fileProjectItem);
                            fileNode.ProjectItem = fileProjectItem;
                            fileNode.InsertSorted(node);
                            ProjectService.AddProjectItem(node.Project, fileProjectItem);
                        }
                        node.Project.Save();
                        if (addedDependentFiles)
                        {
                            node.RecreateSubNodes();
                        }
                        return(addedItems.AsReadOnly());
                    }
                    if (res == 2)
                    {
                        // Cancel
                        return(addedItems.AsReadOnly());
                    }
                    // only continue for res==0 (Copy)
                }
                bool replaceAll = false;
                foreach (KeyValuePair <string, string> pair in fileNames)
                {
                    copiedFileName = Path.Combine(node.Directory, Path.GetFileName(pair.Key));
                    if (!replaceAll && File.Exists(copiedFileName) && !FileUtility.IsEqualFileName(pair.Key, copiedFileName))
                    {
                        ReplaceExistingFile res = ShowReplaceExistingFileDialog(
                            StringParser.Parse("${res:ProjectComponent.ContextMenu.AddExistingFiles}"),
                            Path.GetFileName(pair.Key), true);
                        if (res == ReplaceExistingFile.YesToAll)
                        {
                            replaceAll = true;
                        }
                        else if (res == ReplaceExistingFile.No)
                        {
                            continue;
                        }
                        else if (res == ReplaceExistingFile.Cancel)
                        {
                            break;
                        }
                    }
                    FileProjectItem item = CopyFile(pair.Key, node, true);
                    if (item != null)
                    {
                        addedItems.Add(item);
                        item.DependentUpon = pair.Value;
                    }
                }
                node.Project.Save();
                if (addedDependentFiles)
                {
                    node.RecreateSubNodes();
                }
            }
            //}

            return(addedItems.AsReadOnly());
        }