// TODO: // Given a dictionary of oldText Keys, fill in the corresponding newText Values using user input public static void AddNewTextInput(AutoFillDocument.Form1 form) { // TODO: Should this go here or in Form1.AddForm? // clear the table layout before adding in new forms form.ClearTableLayout(); form.AddForm(textTransformMap); }
// TODO: // Find all fields to replace in the document and populate dictionary with oldText as the Keys and newText as the Values public static void ParseDocument(AutoFillDocument.Form1 form, Word._Document oDoc) { // get a set of fields to replace in the document HashSet <String> words = GetTextFields(oDoc.Content.Text, '{', '}'); // use ToDictionary(), not ToDictionary<String, String>() to overload 2 lambda functions. // create dictionary from hashset where the keys are the hashset's elements. // use dictionary in String.Replace. textTransformMap = words.ToDictionary(Key => Key, Value => ""); // Populate the table with oldText fields and newText textbox inputs AddNewTextInput(form); }
// TODO: // given a dictionary, replace all occurences of Keys i.e. oldText with corresponding Values i.e. newText // return the modified text after replacements are done public static String ReplaceText(AutoFillDocument.Form1 form, String text) { String bodyText = text; // TODO: Reduce time complexity of string replacement // String.replace complexity is O(n) where n is size of the String // calling String.replace on each field in the Dictionary will take O(n*m) where m is size of the Dictionary foreach (KeyValuePair <String, String> textMap in textTransformMap) { bodyText = bodyText.Replace(textMap.Key, textMap.Value); } return(bodyText); }
// find children newTextInput forms in form1 public static void FindNewTextInput(AutoFillDocument.Form1 form) { System.Windows.Forms.Control[] controls; for (int i = 0; i < textTransformMap.Count; ++i) { // TODO: use alternative with better time complexity // Assuming Controls.Find takes linear time O(c) where c is the number of children in form // this function takes O(c*k) where k is the number of dictionary elements // Controls.Find searches by name controls = form.Controls.Find("newTextInput" + i, true); if (controls.Length > 0) { Console.Out.WriteLine(String.Format("{0}: {1}", controls[0].Tag.ToString(), controls[0].Text)); textTransformMap[controls[0].Tag.ToString()] = controls[0].Text; } } }
// find table layout form within form1 // use the table layout to get its children forms public static void FindTableLayout(AutoFillDocument.Form1 form) { System.Windows.Forms.Control[] controls = form.Controls.Find("tableLayoutPanel1", true); }
public static void FillNewTextInput(AutoFillDocument.Form1 form) { FindNewTextInput(form); }