public void DoEditReference()
{
if (!CurrentDocumentControllerIsReady)
{
return;
}
var currentDatabase = currentDocumentController.GetDatabase();
if (currentDatabase == null)
{
ShowNoDatabaseMessage();
return;
}
var selectionManager = new SelectionManager(currentDocumentController);
if (selectionManager.IsRange)
{
Debug.WriteLine("Has Range");
}
if (selectionManager.FieldMatches.Count != 0)
{
Debug.WriteLine(String.Format("Has {0} field(s)", selectionManager.FieldMatches.Count));
}
if (!selectionManager.IsSingleCitation)
{
return;
}
var fieldMatch = selectionManager.FieldMatches[0];
var field = fieldMatch.Field;
var inlineCitation = currentDocumentController.CiteProc.CreateInlineCitationFromFieldJSON(field);
var entryAndPagePairs = Helper.ExtractSources(inlineCitation, currentDatabase);
var expectedItemCount = inlineCitation.CitationItems.Length;
var foundItemCount = entryAndPagePairs.Count;
if (foundItemCount != expectedItemCount)
{
var missingItemCount = expectedItemCount - foundItemCount;
var message = expectedItemCount == 1
? "The reference"
: foundItemCount == 0
? "None of references"
: missingItemCount == 1
? "One of the references"
: missingItemCount + " references";
message += " could not be found in the current database\r\nIf you continue and make changes, these will be lost.\r\n\r\nAre you sure you want to continue?";
if (MessageBox.Show(message, "Edit Items Missing References Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
{
return;
}
}
var editReferencesForm = new AddReferencesForm();
editReferencesForm.Reset(currentDatabase);
editReferencesForm.SetSelectedReferences(entryAndPagePairs);
var result = editReferencesForm.ShowDialog();
if (result == DialogResult.Cancel)
{
return;
}
entryAndPagePairs = editReferencesForm.GetSelectedReferences();
if (entryAndPagePairs.Count == 0)
{
return;
}
currentDocumentController.EditCitation(field, entryAndPagePairs);
}