Ejemplo n.º 1
0
    private ToolStripMenuItem getRightClickItem_Delete()
    {
        return(new ToolStripMenuItem("Delete", Icons.GetBitmap("menu.delete", 16), delegate(object sender, EventArgs e) {
            //get the currently selected item
            object selectedObj = p_Tree.SelectedNode.Tag;

            #region Delete project
            if (selectedObj is Project)
            {
                Project project = (Project)selectedObj;

                //prompt if the user wants to delete the project
                if (MessageBox.Show(
                        "Are you sure you want to delete the project \"" + project.ProjectName + "\"?",
                        "Are you sure?",
                        MessageBoxButtons.YesNoCancel,
                        MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return;
                }

                //prompt the user if they want to delete the physical project as well.
                DialogResult result = MessageBox.Show(
                    "Do you want to delete the physical project files?\nWarning! This CANNOT be undone!",
                    "Question",
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question);
                if (result == DialogResult.Cancel)
                {
                    return;
                }
                bool deletePhysical = result == DialogResult.Yes;

                //delete the physical files if requested
                if (deletePhysical)
                {
                    string dir = new FileInfo(project.Filename).Directory.FullName;
                    if (Directory.Exists(dir))
                    {
                        Directory.Delete(dir, true);
                    }
                }

                //trigger remove event of the entire project directory
                triggerRemoveDirectory(project.Root);

                //delete the project from the solution
                project.Solution.RemoveProject(project);

                //remove the project from the tree
                p_Tree.SelectedNode.Remove();
                return;
            }
            #endregion

            //get the entity that needs to be deleted
            ProjectEntity entity = (ProjectEntity)selectedObj;

            //prompt the user if they want to delete the entity
            string promptMessage = entity is ProjectDirectory ?
                                   "Are you sure you want to delete the directory \"" + entity.FullName + "\" and all of it's contents?\nThis cannot be undone." :
                                   "Are you sure you want to perminently delete the file \"" + entity.FullName + "\"?";
            if (MessageBox.Show(promptMessage, "Are you sure?",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
            {
                return;
            }

            //trigger the events for deletion
            if (entity is ProjectFile && FileDelete != null)
            {
                FileDelete(this, (ProjectFile)entity);
            }
            if (entity is ProjectDirectory)
            {
                triggerRemoveDirectory((ProjectDirectory)entity);
            }

            //delete the entry
            ProjectDirectory parent = entity.Parent;
            entity.Delete();

            //update the tree
            TreeNode node = findNodeByTag((parent.IsRoot ? (object)parent.Project : (object)parent));
            object st = PushTreeState();
            IndexDirectory(parent, node);
            PopTreeState(st);
        }));
    }