private void btnAdd_Click(object sender, EventArgs e) { // Get the checked twfics; List <int> twfics = m_rbv.CheckedItems; if (twfics == null || twfics.Count == 0) { // do nothing. return; } List <int> twficSegments = StTxtPara.TwficSegments(m_cache, twfics); Set <int> uniqueSegments = new Set <int>(twficSegments); FdoObjectSet <CmBaseAnnotation> cbaSegments = new FdoObjectSet <CmBaseAnnotation>(m_cache, uniqueSegments.ToArray(), true); int insertIndex = m_owningSense.ExamplesOS.Count; // by default, insert at the end. int sourceIndex = -1; if (m_les != null) { // we were given a LexExampleSentence, so set our insertion index after the given one. List <int> examples = new List <int>(m_owningSense.ExamplesOS.HvoArray); sourceIndex = examples.IndexOf(m_les.Hvo); insertIndex = sourceIndex + 1; } // Load all the annotations for these twfics. int tagSegFF = StTxtPara.SegmentFreeformAnnotationsFlid(m_cache); Set <int> allAnalWsIds = new Set <int>(m_cache.LangProject.AnalysisWssRC.HvoArray); StTxtPara.LoadSegmentFreeformAnnotationData(m_cache, uniqueSegments, allAnalWsIds); bool fBoolModifiedExisting = false; List <ILexExampleSentence> newExamples = new List <ILexExampleSentence>(); // keep track of how many new objects we created. ILexExampleSentence newLexExample = null; // delay prop changes until all the new examples are added. using (new IgnorePropChanged(m_cache, PropChangedHandling.SuppressView)) { foreach (CmBaseAnnotation cbaSegment in cbaSegments) { if (newExamples.Count == 0 && m_les != null && m_les.Example.BestVernacularAlternative.Text == "***" && (m_les.TranslationsOC == null || m_les.TranslationsOC.Count == 0) && m_les.Reference.Length == 0) { // we were given an empty LexExampleSentence, so use this one for our first new Example. newLexExample = m_les; fBoolModifiedExisting = true; } else { // create a new example sentence. newLexExample = m_owningSense.ExamplesOS.InsertAt(new LexExampleSentence(), insertIndex + newExamples.Count); newExamples.Add(newLexExample); } // copy the segment string into the new LexExampleSentence // Enhance: bold the relevant twfic(s). newLexExample.Example.SetVernacularDefaultWritingSystem(StTxtPara.TssSubstring(cbaSegment).Text); int segDefn_literalTranslation = m_cache.GetIdFromGuid(LangProject.kguidAnnLiteralTranslation); int segDefn_freeTranslation = m_cache.GetIdFromGuid(LangProject.kguidAnnFreeTranslation); int hvoTransType_literalTranslation = m_cache.GetIdFromGuid(LangProject.kguidTranLiteralTranslation); int hvoTransType_freeTranslation = m_cache.GetIdFromGuid(LangProject.kguidTranFreeTranslation); // copy the translation information List <ICmTranslation> newTranslations = new List <ICmTranslation>(); foreach (int freeFormAnnotationId in m_cache.GetVectorProperty(cbaSegment.Hvo, tagSegFF, true)) { int hvoAnnType = m_cache.MainCacheAccessor.get_ObjectProp(freeFormAnnotationId, (int)CmAnnotation.CmAnnotationTags.kflidAnnotationType); // map annotation type to translation type. int hvoTranslationType = 0; if (hvoAnnType == segDefn_literalTranslation) { hvoTranslationType = hvoTransType_literalTranslation; } else if (hvoAnnType == segDefn_freeTranslation) { hvoTranslationType = hvoTransType_freeTranslation; } else { continue; // skip unsupported translation type. } ICmTranslation newTranslation = newLexExample.TranslationsOC.Add(new CmTranslation()); newTranslations.Add(newTranslation); newTranslation.TypeRAHvo = hvoTranslationType; foreach (int analWs in allAnalWsIds) { ITsString tssComment = m_cache.GetMultiStringAlt(freeFormAnnotationId, (int)CmAnnotation.CmAnnotationTags.kflidComment, analWs); if (tssComment.Length > 0) { newTranslation.Translation.SetAlternative(tssComment, analWs); } } } // copy the reference. // Enhance: get the ws from the 'Reference' column spec? // Enhance: AnnotationRefHandler can also m_cache the reference directly to the segment. int iTwfic = twficSegments.IndexOf(cbaSegment.Hvo); newLexExample.Reference.UnderlyingTsString = m_cache.GetTsStringProperty(twfics[iTwfic], m_virtFlidReference); // Need to correctly add Translations (or ghost if none). } } if (fBoolModifiedExisting) { m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, m_les.Hvo, (int)LexExampleSentence.LexExampleSentenceTags.kflidExample, 0, 1, 1); m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, m_les.Hvo, (int)LexExampleSentence.LexExampleSentenceTags.kflidReference, 0, 1, 1); //m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, m_les.Hvo, // (int)LexExampleSentence.LexExampleSentenceTags.kflidTranslations, 0, 1, 1); //m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, m_owningSense.Hvo, // (int)LexSense.LexSenseTags.kflidExamples, sourceIndex, 1, 1); } m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, m_owningSense.Hvo, (int)LexSense.LexSenseTags.kflidExamples, insertIndex, newExamples.Count, 0); }