SIL.FieldWorks.IText.InterlinDocForAnalysis.ApproveAllSuggestedAnalyses C# (CSharp) Method

ApproveAllSuggestedAnalyses() public method

Approve all the suggested analyses in this text. See LT-4312.
public ApproveAllSuggestedAnalyses ( Command cmd ) : void
cmd Command The command object from the selection event.
return void
		public void ApproveAllSuggestedAnalyses(Command cmd)
		{   // Go through the entire text looking for suggested analyses that can be approved.
			// remember where the focus box or ip is
			// might be on an analysis, labels or translation text
			var helper = SelectionHelper.Create(RootBox.Site); // only helps restore translation and note line selections
			AnalysisOccurrence focusedWf = SelectedOccurrence; // need to restore focus box if selected

			// find the very first analysis
			ISegment firstRealSeg = null;
			IAnalysis firstRealOcc = null;
			int occInd = 0;
			foreach (IStPara p in RootStText.ParagraphsOS)
			{
				var para = (IStTxtPara) p;
				foreach (ISegment seg in para.SegmentsOS)
				{
					firstRealSeg = seg;
					occInd = 0;
					foreach(IAnalysis an in seg.AnalysesRS)
					{
						if (an.HasWordform && an.IsValidObject)
						{
							firstRealOcc = an;
							break;
						}
						occInd++;
					}
					if (firstRealOcc != null) break;
				}
				if (firstRealOcc != null) break;
			}
			// Set it as the current segment and recurse
			if (firstRealOcc == null)
				return; // punctuation only or nothing to analyze
			AnalysisOccurrence ao = null;
			if (focusedWf != null && focusedWf.Analysis == firstRealOcc)
				ao = new AnalysisOccurrence(focusedWf.Segment, focusedWf.Index);
			else
				ao = new AnalysisOccurrence(firstRealSeg, occInd);
			TriggerAnalysisSelected(ao, true, true, false);
			var navigator = new SegmentServices.StTextAnnotationNavigator(ao);

			// This needs to be outside the block for the UOW, since what we are suppressing
			// happens at the completion of the UOW.
			SuppressResettingGuesses(
				() =>
				{
					// Needs to include GetRealAnalysis, since it might create a new one.
					UndoableUnitOfWorkHelper.Do(cmd.UndoText, cmd.RedoText, Cache.ActionHandlerAccessor,
					() =>
					{
						var nav = new SegmentServices.StTextAnnotationNavigator(SelectedOccurrence);
						AnalysisOccurrence lastOccurrence;
						var analyses = navigator.GetAnalysisOccurrencesAdvancingInStText().ToList();
						foreach (var occ in analyses)
						{   // This could be punctuation or any kind of analysis.
							IAnalysis occAn = occ.Analysis; // averts “Access to the modified closure” warning in resharper
							if (occAn is IWfiAnalysis || occAn is IWfiWordform)
							{   // this is an analysis or a wordform
								int hvo = m_vc.GetGuess(occAn);
								if (occAn.Hvo != hvo)
								{   // this is a guess, so approve it
									// 1) A second occurence of a word that has had a lexicon entry or sense created for it.
									// 2) A parser result - not sure which gets picked if multiple.
									// #2 May take a while to "percolate" through to become a "guess".
									var guess = Cache.ServiceLocator.ObjectRepository.GetObject(hvo);
									if (guess != null && guess is IAnalysis)
										occ.Segment.AnalysesRS[occ.Index] = (IAnalysis) guess;
									else
									{
										occ.Segment.AnalysesRS[occ.Index] = occAn.Wordform.AnalysesOC.FirstOrDefault();
									}
								}
							/*	else if (occAn.HasWordform && occAn.Wordform.ParserCount > 0)
								{   // this doesn't seem to be needed (and may not be correct) - always caught above
									bool isHumanNoOpinion = occAn.Wordform.HumanNoOpinionParses.Cast<IWfiWordform>().Any(wf => wf.Hvo == occAn.Hvo);
									if (isHumanNoOpinion)
									{
										occ.Segment.AnalysesRS[occ.Index] = occAn.Wordform.AnalysesOC.FirstOrDefault();
									}
								} */
							}
						}
					});
				}
			);
			// MoveFocusBoxIntoPlace();
			if (focusedWf != null)
				SelectOccurrence(focusedWf);
			else if (helper != null)
				helper.SetSelection(true, true);
			Update();
		}
	}
InterlinDocForAnalysis