/// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handled the click event of the Properties context menu.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event
        /// data.</param>
        /// ------------------------------------------------------------------------------------
        private void propertiesClick(object sender, EventArgs e)
        {
            TreeNode node = m_treeArchives.SelectedNode;

            if (node != null && node.Tag is ScrDraft)
            {
                ScrDraft draft            = node.Tag as ScrDraft;
                DraftPropertiesDialog dlg = new DraftPropertiesDialog();
                dlg.SetDialogInfo(draft);
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    node.Text = GetSavedVersionLabel(draft);
                }
            }
        }
 /// ------------------------------------------------------------------------------------
 /// <summary>
 /// Add archive to the database
 /// </summary>
 /// <param name="description">description for the archive.</param>
 /// <param name="booksToArchive">list of book HVOs to add to the archive.</param>
 /// ------------------------------------------------------------------------------------
 private void AddArchive(string description, List <int> booksToArchive)
 {
     using (new SuppressSubTasks(m_cache))
     {
         string sSavePoint;
         m_cache.DatabaseAccessor.SetSavePointOrBeginTrans(out sSavePoint);
         try
         {
             ScrDraft savedVersion = (ScrDraft)m_scr.CreateSavedVersion(description,
                                                                        booksToArchive.ToArray());
             m_cache.DatabaseAccessor.CommitTrans();
         }
         catch
         {
             m_cache.DatabaseAccessor.RollbackSavePoint(sSavePoint);
             throw;
         }
     }
 }
        public void DeleteBook()
        {
            DummySavedVersionsDialog dlg = new DummySavedVersionsDialog(m_cache);
            TreeView tree = dlg.ArchiveTree;

            // Delete the first book from the last archive (Philemon)
            TreeNode archiveNode = null;

            for (int i = 0; i < tree.Nodes.Count; i++)
            {
                if (tree.Nodes[i].Text.IndexOf("My Archive 2") != -1)
                {
                    archiveNode = tree.Nodes[i];
                    break;
                }
            }
            tree.SelectedNode = archiveNode.Nodes[0];
            dlg.SimulateDelete();

            // check the node counts
            Assert.AreEqual(3, tree.Nodes.Count);
            for (int i = 0; i < tree.Nodes.Count; i++)
            {
                if (tree.Nodes[i].Text.IndexOf("My Archive 0") != -1)
                {
                    Assert.AreEqual(1, tree.Nodes[i].Nodes.Count);
                }
                else if (tree.Nodes[i].Text.IndexOf("My Archive 1") != -1)
                {
                    Assert.AreEqual(2, tree.Nodes[i].Nodes.Count);
                }
                else if (tree.Nodes[i].Text.IndexOf("My Archive 2") != -1)
                {
                    Assert.AreEqual(2, tree.Nodes[i].Nodes.Count);
                }
                else
                {
                    Assert.Fail("Seems we have an extra archive");
                }
            }

            // Check the remaining book names for the archive that was deleted from.
            for (int i = 0; i < 2; i++)
            {
                string bookLabel = archiveNode.Nodes[i].Text;
                Assert.AreEqual(m_bookScrRange[i + 1], bookLabel);
            }

            // Check the books in the database to make sure the database is correct.
            Assert.AreEqual(3, m_scr.ArchivedDraftsOC.Count);
            ScrDraft archive = (ScrDraft)archiveNode.Tag;

            Assert.AreEqual(2, archive.BooksOS.Count);
            List <int> cannonicalBookNums = new List <int>(new int[] { 59, 65 });

            for (int iArchivedBook = 0; iArchivedBook < archive.BooksOS.Count; iArchivedBook++)
            {
                ScrBook book = (ScrBook)archive.BooksOS[iArchivedBook];
                Assert.AreEqual(cannonicalBookNums[iArchivedBook], book.CanonicalNum);
                Assert.AreEqual(m_bookNames[iArchivedBook + 1], book.BestUIName);
            }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handles the Click event of the m_btnCopyToCurr control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event
        /// data.</param>
        /// ------------------------------------------------------------------------------------
        private void m_btnCopyToCurr_Click(object sender, EventArgs e)
        {
            // If nothing or a complete archive is selected, do nothing.
            TreeNode node = m_treeArchives.SelectedNode;

            if (node == null || !(node.Tag is ScrBook))
            {
                return;
            }

            ScrBook            savedBook        = node.Tag as ScrBook;
            ScrBook            originalBook     = (ScrBook)m_scr.FindBook(savedBook.CanonicalNum);
            TreeNode           parentNode       = node.Parent;
            ScrDraft           archive          = parentNode.Tag as ScrDraft;
            OverwriteType      typeOfOverwrite  = OverwriteType.FullNoDataLoss;
            List <IScrSection> sectionsToRemove = null;

            if (originalBook != null)
            {
                string     sDetails;
                List <int> missingBtWs;
                typeOfOverwrite = originalBook.DetermineOverwritability(savedBook, out sDetails,
                                                                        out sectionsToRemove, out missingBtWs);
                if (typeOfOverwrite == OverwriteType.DataLoss)
                {
                    // There will be data loss if the user overwrites so we don't allow them
                    // to continue.
                    ImportedBooks.ReportDataLoss(originalBook, ScrDraftType.SavedVersion, this, sDetails);
                    return;
                }

                if (missingBtWs != null && !ImportedBooks.ConfirmBtOverwrite(originalBook,
                                                                             ScrDraftType.SavedVersion, sectionsToRemove, missingBtWs, this))
                {
                    // The user might lose back translation(s) if they proceed and they decided
                    // against it.
                    return;
                }
            }

            string stUndo;
            string stRedo;

            TeResourceHelper.MakeUndoRedoLabels("kstidOverwriteCurrentWithSaved", out stUndo, out stRedo);
            using (new WaitCursor(this))
                using (UndoTaskHelper helper = new UndoTaskHelper(m_cache.MainCacheAccessor, null, stUndo, stRedo, true))
                {
                    try
                    {
                        if (typeOfOverwrite == OverwriteType.Partial)
                        {
                            // Perform an automerge of the original book and the saved version.
                            using (BookMerger merger = new BookMerger(m_cache, m_styleSheet, savedBook))
                            {
                                using (ProgressDialogWithTask progress = new ProgressDialogWithTask(this))
                                {
                                    progress.Title = DlgResources.ResourceString("kstidOverwriteCaption");
                                    progress.RunTask(true, new BackgroundTaskInvoker(merger.DoPartialOverwrite), sectionsToRemove);
                                }
                                if (!merger.AutoMerged)
                                {
                                    throw new Exception("Partial Overwrite was not successful.");
                                }
                            }
                        }
                        else
                        {
                            if (originalBook != null)
                            {
                                if (m_cache.ActionHandlerAccessor != null)
                                {
                                    // When Undoing, we need to first resurrect the deleted book, then
                                    // put it back in the book filter...so we need a RIFF in the sequence
                                    // BEFORE the delete.
                                    ReplaceInFilterFixer fixer1 = new ReplaceInFilterFixer(originalBook.Hvo, 0, m_cache);
                                    m_cache.ActionHandlerAccessor.AddAction(fixer1);
                                }
                                originalBook.DeleteUnderlyingObject();
                            }
                            int hvoNew = (m_scr as Scripture).CopyBookToCurrent(savedBook);
                            ReplaceInFilterFixer fixer = new ReplaceInFilterFixer(0, hvoNew, m_cache);
                            fixer.Redo(false);
                            if (m_cache.ActionHandlerAccessor != null)
                            {
                                m_cache.ActionHandlerAccessor.AddAction(fixer);
                            }
                        }
                    }
                    catch
                    {
                        helper.EndUndoTask = false;
                        throw;
                    }
                }
        }
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Handle the Delete button.  Either delete an archive or a book within the archive
        /// depending on the selected item.
        /// </summary>
        /// ------------------------------------------------------------------------------------
        protected void m_btnDelete_Click(object sender, System.EventArgs e)
        {
            using (new WaitCursor(this))
            {
                // Figure out what is selected (book or archive)
                TreeNode node = m_treeArchives.SelectedNode;
                if (node == null)
                {
                    return;
                }

                // If the tree node is a book and it is the only book in the archive then
                // switch the node to the parent (archive) node to delete it instead.
                if (node.Parent != null && node.Parent.Nodes.Count == 1)
                {
                    node = node.Parent;
                }

                string stUndo;
                string stRedo;
                // If the tree node is a book, then delete the book from the archive.
                if (node.Tag is ScrBook)
                {
                    ScrBook  book       = node.Tag as ScrBook;
                    TreeNode parentNode = node.Parent;
                    ScrDraft archive    = parentNode.Tag as ScrDraft;
                    if (archive.Protected)
                    {
                        MessageBox.Show(this, TeResourceHelper.GetResourceString("kstidCannotDeleteBookFromProtectedVersion"),
                                        TeResourceHelper.GetResourceString("kstidProtectedVersionCaption"), MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                        return;
                    }

                    // Delete the book from the saved version
                    TeResourceHelper.MakeUndoRedoLabels("kstidUndoDeleteArchiveBook", out stUndo,
                                                        out stRedo);
                    using (new UndoTaskHelper(m_cache.MainCacheAccessor, null, stUndo, stRedo, true))
                    {
                        archive.BooksOS.Remove(book);
                    }

                    // Delete the node from the tree
                    parentNode.Nodes.Remove(node);
                }

                // If the tree node is an archive then delete the entire archive
                else if (node.Tag is ScrDraft)
                {
                    ScrDraft archive = node.Tag as ScrDraft;
                    if (archive.Protected)
                    {
                        MessageBox.Show(this, TeResourceHelper.GetResourceString("kstidCannotDeleteProtectedVersion"),
                                        TeResourceHelper.GetResourceString("kstidProtectedVersionCaption"), MessageBoxButtons.OK,
                                        MessageBoxIcon.Information);
                        return;
                    }

                    // Delete the ScrDraft object from the scripture
                    TeResourceHelper.MakeUndoRedoLabels("kstidUndoDeleteArchive", out stUndo,
                                                        out stRedo);
                    using (new UndoTaskHelper(m_cache.MainCacheAccessor, null, stUndo, stRedo, true))
                    {
                        m_scr.ArchivedDraftsOC.Remove(archive);
                    }

                    // Delete the archive node from the tree
                    m_treeArchives.Nodes.Remove(node);
                }

                // If the last thing was deleted from the tree, disable the action buttons.
                if (m_treeArchives.Nodes.Count == 0)
                {
                    m_btnDelete.Enabled     = false;
                    m_btnCopyToCurr.Enabled = false;
                    m_btnDiff.Enabled       = false;
                }
            }
        }