public void DeleteObjects() { CheckDisposed(); m_fdoCache.BeginUndoTask("make texts", "undo make texts"); // One text with 4 paras SIL.FieldWorks.FDO.Ling.Text text1 = new Text(); m_fdoCache.LangProject.TextsOC.Add(text1); StText body1 = new StText(); text1.ContentsOA = body1; StTxtPara para1A = new StTxtPara(); body1.ParagraphsOS.Append(para1A); StTxtPara para1B = new StTxtPara(); body1.ParagraphsOS.Append(para1B); StTxtPara para1C = new StTxtPara(); body1.ParagraphsOS.Append(para1C); StTxtPara para1D = new StTxtPara(); body1.ParagraphsOS.Append(para1D); para1A.Contents.Text = "Contents of para1A"; para1B.Contents.Text = "Para 1B stuff"; para1C.Contents.Text = "Rubbish in 1C"; para1D.Contents.Text = "Things in 1D"; // Second text, 2 paras SIL.FieldWorks.FDO.Ling.Text text2 = new Text(); m_fdoCache.LangProject.TextsOC.Add(text2); StText body2 = new StText(); text2.ContentsOA = body2; StTxtPara para2A = new StTxtPara(); body2.ParagraphsOS.Append(para2A); StTxtPara para2B = new StTxtPara(); body2.ParagraphsOS.Append(para2B); para2A.Contents.Text = "2A contents"; para2B.Contents.Text = "2B stuff"; m_fdoCache.EndUndoTask(); Set <int> idsToDelete = new Set <int>(); idsToDelete.Add(para1A.Hvo); idsToDelete.Add(para1B.Hvo); idsToDelete.Add(para1D.Hvo); idsToDelete.Add(para2A.Hvo); m_fdoCache.BeginUndoTask("delete paras", "undo delete paras"); CmObject.DeleteObjects(idsToDelete, m_fdoCache); m_fdoCache.EndUndoTask(); Assert.AreEqual(1, body1.ParagraphsOS.Count, "paras were deleted1"); Assert.AreEqual(1, body2.ParagraphsOS.Count, "paras were deleted2"); m_fdoCache.Undo(); m_fdoCache.VwCacheDaAccessor.ClearAllData(); // as in Refresh. Assert.AreEqual(4, body1.ParagraphsOS.Count, "paras were restored1"); Assert.AreEqual(2, body2.ParagraphsOS.Count, "paras were restored1"); Assert.AreEqual("Contents of para1A", para1A.Contents.Text, "para 1A contents restored"); Assert.AreEqual("Things in 1D", para1D.Contents.Text, "para 1D contents restored"); Assert.AreEqual("2A contents", para2A.Contents.Text, "para 2A contents restored"); m_fdoCache.Redo(); m_fdoCache.VwCacheDaAccessor.ClearAllData(); // as in Refresh. Assert.AreEqual(1, body1.ParagraphsOS.Count, "paras were deleted1"); Assert.AreEqual(1, body2.ParagraphsOS.Count, "paras were deleted2"); m_fdoCache.Undo(); m_fdoCache.VwCacheDaAccessor.ClearAllData(); // as in Refresh. Assert.AreEqual(4, body1.ParagraphsOS.Count, "paras were restored1"); Assert.AreEqual(2, body2.ParagraphsOS.Count, "paras were restored1"); Assert.AreEqual("Contents of para1A", para1A.Contents.Text, "para 1A contents restored"); Assert.AreEqual("Things in 1D", para1D.Contents.Text, "para 1D contents restored"); Assert.AreEqual("2A contents", para2A.Contents.Text, "para 2A contents restored"); m_fdoCache.Undo(); // cleans up the last of what we added. }
/// ------------------------------------------------------------------------------------ /// <summary> /// Creates a new StFootnote owned by the given book created from the given string /// representation (Created from GetTextRepresentation()) /// </summary> /// <param name="owner">The object that owns the sequence of footnotes into which the /// new footnote is to be inserted</param> /// <param name="flid">The field id of the property in which the footnotes are owned /// </param> /// <param name="sTextRepOfFootnote">The given string representation of a footnote /// </param> /// <param name="footnoteIndex">0-based index where the footnote will be inserted</param> /// <param name="footnoteMarkerStyleName">style name for footnote markers</param> /// <returns>An StFootnote with the properties set to the properties in the /// given string representation</returns> /// ------------------------------------------------------------------------------------ public static StFootnote CreateFromStringRep(CmObject owner, int flid, string sTextRepOfFootnote, int footnoteIndex, string footnoteMarkerStyleName) { StFootnote createdFootnote = new StFootnote(owner, flid, footnoteIndex); // create an XML reader to read in the string representation System.IO.StringReader reader = new System.IO.StringReader(sTextRepOfFootnote); XmlDocument doc = new XmlDocument(); try { doc.Load(reader); } catch (XmlException) { throw new ArgumentException("Unrecognized XML format for footnote."); } XmlNodeList tagList = doc.SelectNodes("FN"); foreach (XmlNode bla in tagList[0].ChildNodes) { // Footnote marker if (bla.Name == "M") { ITsPropsBldr propBlr = TsPropsBldrClass.Create(); propBlr.SetStrPropValue((int)FwTextPropType.ktptNamedStyle, footnoteMarkerStyleName); ITsStrBldr tss = TsStrBldrClass.Create(); tss.Replace(0, 0, bla.InnerText, propBlr.GetTextProps()); createdFootnote.FootnoteMarker.UnderlyingTsString = tss.GetString(); } // Display footnote marker else if (bla.Name == "ShowMarker") { createdFootnote.DisplayFootnoteMarker = true; } // display footnote scripture reference else if (bla.Name == "ShowReference") { createdFootnote.DisplayFootnoteReference = true; } // start of a paragraph else if (bla.Name == "P") { StTxtPara newPara = new StTxtPara(); createdFootnote.ParagraphsOS.Append(newPara); ITsIncStrBldr paraBldr = TsIncStrBldrClass.Create(); CmTranslation trans = null; //ITsStrBldr paraBldr = TsStrBldrClass.Create(); foreach (XmlNode paraTextNode in bla.ChildNodes) { if (paraTextNode.Name == "PS") { // paragraph style ITsPropsBldr propBldr = TsPropsBldrClass.Create(); if (!String.IsNullOrEmpty(paraTextNode.InnerText)) { propBldr.SetStrPropValue((int)FwTextPropType.ktptNamedStyle, paraTextNode.InnerText); } else { Debug.Fail("Attempting to create a footnote paragraph with no paragraph style specified!"); } newPara.StyleRules = propBldr.GetTextProps(); } else if (paraTextNode.Name == "RUN") { CreateRunFromStringRep(owner, paraBldr, paraTextNode); paraBldr.Append(paraTextNode.InnerText); } else if (paraTextNode.Name == "TRANS") { if (trans == null) { trans = (CmTranslation)newPara.GetOrCreateBT(); } // Determine which writing system where the string run(s) will be added. string iculocale = paraTextNode.Attributes.GetNamedItem("WS").Value; if (iculocale == null || iculocale == string.Empty) { throw new ArgumentException( "Unknown ICU locale encountered: " + iculocale); } int transWS = owner.Cache.LanguageEncodings.GetWsFromIcuLocale(iculocale); Debug.Assert(transWS != 0, "Unable to find ws from ICU Locale"); // Build a TsString from the run(s) description. ITsIncStrBldr strBldr = TsIncStrBldrClass.Create(); foreach (XmlNode transTextNode in paraTextNode.ChildNodes) { if (transTextNode.Name != "RUN") { throw new ArgumentException("Unexpected translation element '" + transTextNode.Name + "' encountered for ws '" + iculocale + "'"); } CreateRunFromStringRep(owner, strBldr, transTextNode); strBldr.Append(transTextNode.InnerText); } trans.Translation.SetAlternative(strBldr.GetString(), transWS); } } newPara.Contents.UnderlyingTsString = paraBldr.GetString(); } } owner.Cache.PropChanged(null, PropChangeType.kpctNotifyAll, owner.Hvo, flid, footnoteIndex, 1, 0); return(createdFootnote); }
int MakeRealObject(ITsString tssTyped) { // Figure whether owning atomic or owning collection or owning sequence. Throw if none. IFwMetaDataCache mdc = m_fdoCache.MetaDataCacheAccessor; FieldType iType = m_fdoCache.GetFieldType(m_flidEmptyProp); iType &= FieldType.kcptVirtualMask; ISilDataAccess sdaReal = m_fdoCache.MainCacheAccessor; // Make a new object of the specified class in the specified property. int ord = 0; switch (iType) { default: throw new Exception("ghost string property must be owning object property"); case FieldType.kcptOwningAtom: ord = -2; break; case FieldType.kcptOwningCollection: ord = -1; break; case FieldType.kcptOwningSequence: // ord = 0 set above (inserting the first and only object at position 0). break; } string sClassRaw = mdc.GetClassName((uint)m_clidDst); string sClass = m_mediator.StringTbl.GetString(sClassRaw, "ClassNames"); string sUndo = String.Format(DetailControlsStrings.ksUndoCreate0, sClass); string sRedo = String.Format(DetailControlsStrings.ksRedoCreate0, sClass); sdaReal.BeginUndoTask(sUndo, sRedo); int hvoNewObj = sdaReal.MakeNewObject((int)m_clidDst, m_hvoObj, m_flidEmptyProp, ord); // Set its property m_flidStringProp to tssTyped. If it is multilingual, choose based on ghostWs. FieldType iTypeString = m_fdoCache.GetFieldType(m_flidStringProp); iTypeString &= FieldType.kcptVirtualMask; switch (iTypeString) { default: throw new Exception("ghost property must store strings!"); case FieldType.kcptMultiString: case FieldType.kcptMultiBigString: case FieldType.kcptMultiUnicode: case FieldType.kcptMultiBigUnicode: sdaReal.SetMultiStringAlt(hvoNewObj, m_flidStringProp, m_wsToCreate, tssTyped); break; case FieldType.kcptString: case FieldType.kcptBigString: sdaReal.SetString(hvoNewObj, m_flidStringProp, tssTyped); break; } string ghostInitMethod = XmlUtils.GetOptionalAttributeValue(m_nodeObjProp, "ghostInitMethod"); if (ghostInitMethod != null) { ICmObject obj = CmObject.CreateFromDBObject(m_fdoCache, hvoNewObj); Type objType = obj.GetType(); System.Reflection.MethodInfo mi = objType.GetMethod(ghostInitMethod); mi.Invoke(obj, null); } // Issue PropChanged for the addition of the new object. (could destroy this). sdaReal.PropChanged(null, (int)PropChangeType.kpctNotifyAll, m_hvoObj, m_flidEmptyProp, 0, 1, 0); sdaReal.EndUndoTask(); return(hvoNewObj); }