public void ReplaceAll() { if (_FFR_tb_ReplaceWhat.Text.Length == 0) { return; } MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_ReplaceWhat.Text, _FFR_tb_ReplaceWith.Text, false, _FFR_cb_XmlValuesReplace.Checked, _FFR_cob_AttNameReplace.Text, _FFR_cb_NodeNamesReplace.Checked, _FFR_cb_CommentariesReplace.Checked, false, _FFR_cb_CaseReplace.Checked, _FFR_cb_ExactReplace.Checked, _FFR_cb_SelectedNodeReplace.Checked); List <MissionSearchResult> list = new List <MissionSearchResult>(); int result = Mission.Current.ReplaceAll(list, msc); Log.Add("Replaced '" + msc.Input + "' with '" + _FFR_tb_ReplaceWith.Text + "' " + result.ToString() + " time(s)."); if (result > 0) { Program.FormSearchResultsInstance.SetDataSearchResult(msc, list); Program.FormSearchResultsInstance.ShowIfNotEmpty(); SystemSounds.Asterisk.Play(); } else { SystemSounds.Beep.Play(); } }
/// <summary> Set subtitle from a Mission Search Command class </summary> public void SetDataSearchResult(MissionSearchCommand missionSearchCommandInstance, List<MissionSearchResult> list) { string text = (missionSearchCommandInstance.Replacement == null ? "Looked for \"" + missionSearchCommandInstance.Input + "\" in " : "Replaced \"" + missionSearchCommandInstance.Input + "\" with \"" + missionSearchCommandInstance.Replacement + "\" in ") + (missionSearchCommandInstance.XmlAttName ? "[Xml attribute name], " : "") + (missionSearchCommandInstance.XmlAttValue ? "[Xml attribute value" + (string.IsNullOrWhiteSpace(missionSearchCommandInstance.AttName) ? "" : " (in att name \"" + missionSearchCommandInstance.AttName + "\")") + "], " : "") + (missionSearchCommandInstance.NodeNames ? "[Mission node name], " : "") + (missionSearchCommandInstance.Commentaries ? "[Commentary], " : "") + (missionSearchCommandInstance.StatementText ? "[Displayed text], " : "") + (missionSearchCommandInstance.MatchCase ? "Matching case and " : "") + (missionSearchCommandInstance.MatchExact ? "Matching value exactly and " : "") + (missionSearchCommandInstance.OnlyInCurrentNode ? "Only in selected node and " : ""); if (text.Substring(text.Length - 2, 2) == ", ") text = text.Substring(0, text.Length - 2); if (text.Substring(text.Length - 5, 5) == " and ") text = text.Substring(0, text.Length - 5); _FSR_ss_Main_l_Main.Text = text; Text = missionSearchCommandInstance.Replacement == null ? "Find Results: Total " + _FSR_lv_Main.Rows.Count.ToString() + " matches" : "Replace Results: Total " + _FSR_lv_Main.Rows.Count.ToString() + " items"; _FSR_ss_Main_tsb_Update.Visible = false; SetListContents(list); UpdateState(); CurrentOwner = Program.FormFindReplaceInstance; }
/// <summary> Set subtitle from a Mission Search Command class </summary> public void SetDataSearchResult(MissionSearchCommand missionSearchCommandInstance, List <MissionSearchResult> list) { string text = (missionSearchCommandInstance.Replacement == null ? "Looked for \"" + missionSearchCommandInstance.Input + "\" in " : "Replaced \"" + missionSearchCommandInstance.Input + "\" with \"" + missionSearchCommandInstance.Replacement + "\" in ") + (missionSearchCommandInstance.XmlAttName ? "[Xml attribute name], " : "") + (missionSearchCommandInstance.XmlAttValue ? "[Xml attribute value" + (string.IsNullOrWhiteSpace(missionSearchCommandInstance.AttName) ? "" : " (in att name \"" + missionSearchCommandInstance.AttName + "\")") + "], " : "") + (missionSearchCommandInstance.NodeNames ? "[Mission node name], " : "") + (missionSearchCommandInstance.Commentaries ? "[Commentary], " : "") + (missionSearchCommandInstance.StatementText ? "[Displayed text], " : "") + (missionSearchCommandInstance.MatchCase ? "Matching case and " : "") + (missionSearchCommandInstance.MatchExact ? "Matching value exactly and " : "") + (missionSearchCommandInstance.OnlyInCurrentNode ? "Only in selected node and " : ""); if (text.Substring(text.Length - 2, 2) == ", ") { text = text.Substring(0, text.Length - 2); } if (text.Substring(text.Length - 5, 5) == " and ") { text = text.Substring(0, text.Length - 5); } _FSR_ss_Main_l_Main.Text = text; Text = missionSearchCommandInstance.Replacement == null ? "Find Results: Total " + _FSR_lv_Main.Rows.Count.ToString() + " matches" : "Replace Results: Total " + _FSR_lv_Main.Rows.Count.ToString() + " items"; _FSR_ss_Main_tsb_Update.Visible = false; SetListContents(list); UpdateState(); CurrentOwner = Program.FormFindReplaceInstance; }
public void FindAll() { if (_FFR_tb_Input.Text.Length == 0) return; EnsureChecked(); Program.FormSearchResultsInstance.Hide(); MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_Input.Text, null, _FFR_cb_XmlNames.Checked, _FFR_cb_XmlValuesFind.Checked, _FFR_cob_AttNameFind.Text, _FFR_cb_NodeNamesFind.Checked, _FFR_cb_CommentariesFind.Checked, _FFR_cb_DisplayedText.Checked, _FFR_cb_CaseFind.Checked, _FFR_cb_ExactFind.Checked, _FFR_cb_SelectedNodeFind.Checked); List<MissionSearchResult> result = Mission.Current.FindAll(msc); Program.FormSearchResultsInstance.SetDataSearchResult(msc, result); Program.FormSearchResultsInstance.ShowIfNotEmpty(true); }
public void FindAll() { if (_FFR_tb_Input.Text.Length == 0) { return; } EnsureChecked(); Program.FormSearchResultsInstance.Hide(); MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_Input.Text, null, _FFR_cb_XmlNames.Checked, _FFR_cb_XmlValuesFind.Checked, _FFR_cob_AttNameFind.Text, _FFR_cb_NodeNamesFind.Checked, _FFR_cb_CommentariesFind.Checked, _FFR_cb_DisplayedText.Checked, _FFR_cb_CaseFind.Checked, _FFR_cb_ExactFind.Checked, _FFR_cb_SelectedNodeFind.Checked); List <MissionSearchResult> result = Mission.Current.FindAll(msc); Program.FormSearchResultsInstance.SetDataSearchResult(msc, result); Program.FormSearchResultsInstance.ShowIfNotEmpty(true); }
public void ReplaceAll() { if (_FFR_tb_ReplaceWhat.Text.Length == 0) return; MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_ReplaceWhat.Text, _FFR_tb_ReplaceWith.Text, false, _FFR_cb_XmlValuesReplace.Checked, _FFR_cob_AttNameReplace.Text, _FFR_cb_NodeNamesReplace.Checked, _FFR_cb_CommentariesReplace.Checked, false, _FFR_cb_CaseReplace.Checked, _FFR_cb_ExactReplace.Checked, _FFR_cb_SelectedNodeReplace.Checked); List<MissionSearchResult> list = new List<MissionSearchResult>(); int result = Mission.Current.ReplaceAll(list, msc); Log.Add("Replaced '" + msc.Input + "' with '" + _FFR_tb_ReplaceWith.Text + "' " + result.ToString() + " time(s)."); if (result > 0) { Program.FormSearchResultsInstance.SetDataSearchResult(msc, list); Program.FormSearchResultsInstance.ShowIfNotEmpty(); SystemSounds.Asterisk.Play(); } else SystemSounds.Beep.Play(); }
public void ReplacePrevious() { if (_FFR_tb_ReplaceWhat.Text.Length == 0) { return; } MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_ReplaceWhat.Text, _FFR_tb_ReplaceWith.Text, false, _FFR_cb_XmlValuesReplace.Checked, _FFR_cob_AttNameReplace.Text, _FFR_cb_NodeNamesReplace.Checked, _FFR_cb_CommentariesReplace.Checked, false, _FFR_cb_CaseReplace.Checked, _FFR_cb_ExactReplace.Checked, _FFR_cb_SelectedNodeReplace.Checked); if (!Mission.Current.Find_DoesCurrentSelectionMatch(msc)) { ReplaceFindPrevious(); } int result = Mission.Current.ReplaceCurrent(msc); Log.Add("Replaced '" + msc.Input + "' with '" + _FFR_tb_ReplaceWith.Text + "' " + result.ToString() + " time(s)."); if (result == 0) { SystemSounds.Beep.Play(); } }
/// <summary> /// Recursively look for matches in nodes that are child to the current node. /// Also used to look for first only (Find Next and Find Previous. /// Returns if we found first in firstOnly mode or reached last node. /// Called from FindAll and recursively calls itself. /// </summary> private bool FindAll_RecursivelyFind(TreeNode node, ref int curNode, List<MissionSearchResult> list, MissionSearchCommand msc, bool forward, bool firstOnly, ref int limitNode, ref int limitStatement) { MissionNode mNode = (MissionNode)node.Tag; curNode += forward ? 1 : -1; int curStatement = forward ? 0 : mNode.Conditions.Count + mNode.Actions.Count + 1; for (int i = 0; i < node.Nodes.Count; i++) if (FindAll_RecursivelyFind(node.Nodes[forward ? i : node.Nodes.Count - 1 - i], ref curNode, list, msc, forward, firstOnly, ref limitNode, ref limitStatement)) return true; //Skip node in we are only looking in the current node and this isnt current node if (msc.OnlyInCurrentNode && !TreeViewNodes.NodeIsInsideNode(node, TreeViewNodes.SelectedNode)) return false; if (forward) { //Check if node matches our search criteria if (Find_TryAdd(list, Find_CheckNodeForMatch(node, mNode, curNode, msc), firstOnly, ref limitNode, ref limitStatement)) return true; //Then we start looking through statements for (int i = 0; i < mNode.Conditions.Count; i++) if (Find_TryAdd(list, Find_CheckStatementForMatch(node, mNode.Conditions[forward ? i : mNode.Conditions.Count - 1 - i], curNode, forward ? ++curStatement : --curStatement, msc), firstOnly, ref limitNode, ref limitStatement)) return true; for (int i = 0; i < mNode.Actions.Count; i++) if (Find_TryAdd(list, Find_CheckStatementForMatch(node, mNode.Actions[forward ? i : mNode.Actions.Count - 1 - i], curNode, forward ? ++curStatement : --curStatement, msc), firstOnly, ref limitNode, ref limitStatement)) return true; } else { //Then we start looking through statements for (int i = 0; i < mNode.Actions.Count; i++) if (Find_TryAdd(list, Find_CheckStatementForMatch(node, mNode.Actions[forward ? i : mNode.Actions.Count - 1 - i], curNode, forward ? ++curStatement : --curStatement, msc), firstOnly, ref limitNode, ref limitStatement)) return true; for (int i = 0; i < mNode.Conditions.Count; i++) if (Find_TryAdd(list, Find_CheckStatementForMatch(node, mNode.Conditions[forward ? i : mNode.Conditions.Count - 1 - i], curNode, forward ? ++curStatement : --curStatement, msc), firstOnly, ref limitNode, ref limitStatement)) return true; //Check if node matches our search criteria if (Find_TryAdd(list, Find_CheckNodeForMatch(node, mNode, curNode, msc), firstOnly, ref limitNode, ref limitStatement)) return true; } return false; }
/// <summary> /// Replace according to search command in the specified string. Returns processed string. /// </summary> public string Replace_ReplaceInString(string text1, MissionSearchCommand msc) { if (msc.MatchExact) text1 = msc.Replacement; else if (msc.MatchCase) text1 = text1.Replace(msc.Input, msc.Replacement); else text1 = Helper.StringReplaceEx(text1, msc.Input, msc.Replacement); return text1; }
/// <summary> /// Replace according to search command in the specified statement. Returns how many replacements were done. /// </summary> public int Replace_InStatement(TreeNode node, MissionStatement statement, int curNode, int curStatement, List<MissionSearchResult> list, MissionSearchCommand msc) { int replacements = 0; //Replace in statement if (statement!=null) { //We are interested in comments only if we are especially looking for them if (statement.Kind == MissionStatementKind.Commentary) { if (msc.Commentaries) if (Find_CheckStringForMatch(statement.Body, msc)) { statement.Body = Replace_ReplaceInString(statement.Body, msc); replacements++; } } else if (msc.XmlAttValue) //Look for xml attribute values foreach (KeyValuePair<string, string> kvp in statement.GetAttributes()) if ((string.IsNullOrEmpty(msc.AttName) || kvp.Key == msc.AttName) && Find_CheckStringForMatch(kvp.Value, msc)) { statement.SetAttribute(kvp.Key, Replace_ReplaceInString(kvp.Value, msc)); replacements++; } statement.Update(); if (replacements>0) list.Add(new MissionSearchResult(curNode, curStatement, statement.Text, node, statement)); } return replacements; }
/// <summary> /// Replace according to search command in the specified node (name). Returns how many replacements were done. /// </summary> public int Replace_InNode(TreeNode node, int curNode, List<MissionSearchResult> list, MissionSearchCommand msc) { int replacements = 0; //Replace in node name if ((msc.NodeNames || msc.Commentaries) && node != null) { MissionNode mNode = (MissionNode)node.Tag; //We are interested in event/start/folder nodes... if ((msc.NodeNames && (node.Tag is MissionNode_Event || node.Tag is MissionNode_Folder || node.Tag is MissionNode_Start)) || msc.Commentaries && node.Tag is MissionNode_Comment) if (Find_CheckStringForMatch(mNode.Name, msc)) { mNode.Name = Replace_ReplaceInString(mNode.Name, msc); replacements++; } node.Text = mNode.Name; if (replacements>0) list.Add(new MissionSearchResult(curNode, 0, mNode.Name, node, null)); } return replacements; }
/// <summary> /// Replace in currently selected node and statement /// </summary> public int ReplaceCurrent(MissionSearchCommand msc) { int replacements = 0; replacements += Replace_InNode(TreeViewNodes.SelectedNode, 0, new List<MissionSearchResult>(), msc); replacements += TreeViewStatements.SelectedNode == null || !(TreeViewStatements.SelectedNode.Tag is MissionStatement) ? 0 : Replace_InStatement(TreeViewNodes.SelectedNode, (MissionStatement)TreeViewStatements.SelectedNode.Tag, 0, 0, new List<MissionSearchResult>(), msc); OutputMissionNodeContentsToStatementTree(); RegisterChange("Replaced '" + msc.Input + "' with '" + msc.Replacement + "' " + replacements.ToString() + " time(s)."); return replacements; }
/// <summary> /// Check for match, taking search terms in account /// </summary> private bool Find_CheckStringForMatch(string text1, MissionSearchCommand msc) { string text2 = msc.Input; if (!msc.MatchCase) { text1 = text1.ToLower(); text2 = text2.ToLower(); } if (msc.MatchExact) return text1 == text2; else return text1.Contains(text2); }
/// <summary> /// Find all items matching the search command's criteria and return the list of matching items. /// Also used to look for the first after the current (Find Next and Find Previous). /// </summary> /// <remarks> /// In "firstOnly" mode, we actually search twice, because we always start from the first node and statement, /// but we must actually find first that happens after the current one. /// Therefore, we start from the first, and when we reach the current, we flag it as "limitNode" and "limitStatement", /// and only since then does the first found node cause a return. /// And then if we reach the flagged node for the second time, we also return (with zero results) /// </remarks> public List<MissionSearchResult> FindAll(MissionSearchCommand msc, bool forward = true, bool firstOnly = false) { List<MissionSearchResult> result = new List<MissionSearchResult>(); int curNode, limitNode = -1, limitStatement = -1; for (int j = 0; j < 1 + (firstOnly ? 1 : 0); j++)//do twice if looking for first (see remarks) { curNode = forward ? 0 : GetNodeCount() + 1; bool found = false; for (int i = 0; i < TreeViewNodes.Nodes.Count; i++) { if (FindAll_RecursivelyFind(TreeViewNodes.Nodes[forward ? i : TreeViewNodes.Nodes.Count - 1 - i], ref curNode, result, msc, forward, firstOnly, ref limitNode, ref limitStatement)) { found = true; break; } } if (found) break; } return result; }
public void ReplacePrevious() { if (_FFR_tb_ReplaceWhat.Text.Length == 0) return; MissionSearchCommand msc = new MissionSearchCommand(_FFR_tb_ReplaceWhat.Text, _FFR_tb_ReplaceWith.Text, false, _FFR_cb_XmlValuesReplace.Checked, _FFR_cob_AttNameReplace.Text, _FFR_cb_NodeNamesReplace.Checked, _FFR_cb_CommentariesReplace.Checked, false, _FFR_cb_CaseReplace.Checked, _FFR_cb_ExactReplace.Checked, _FFR_cb_SelectedNodeReplace.Checked); if (!Mission.Current.Find_DoesCurrentSelectionMatch(msc)) ReplaceFindPrevious(); int result = Mission.Current.ReplaceCurrent(msc); Log.Add("Replaced '" + msc.Input + "' with '" + _FFR_tb_ReplaceWith.Text + "' " + result.ToString() + " time(s)."); if (result == 0) SystemSounds.Beep.Play(); }
/// <summary> /// Check if the mission node matches the search criteria, return search result if it does /// </summary> private MissionSearchResult Find_CheckNodeForMatch(TreeNode node, MissionNode mNode, int curNode, MissionSearchCommand msc) { bool statisfies = false; //We are interested in event/start/folder nodes... if (msc.NodeNames && (node.Tag is MissionNode_Event || node.Tag is MissionNode_Folder || node.Tag is MissionNode_Start)) statisfies = statisfies | Find_CheckStringForMatch(mNode.Name, msc); //... or commentaries if (msc.Commentaries && node.Tag is MissionNode_Comment) statisfies = statisfies | Find_CheckStringForMatch(mNode.Name, msc); if (statisfies) return new MissionSearchResult(curNode, 0, mNode.Name, node, null); return new MissionSearchResult(curNode, 0, null, node, null); }
/// <summary> /// Check if mission statement matches the search criteria, return search result if it does /// </summary> private MissionSearchResult Find_CheckStatementForMatch(TreeNode node, MissionStatement statement, int curNode, int curStatement, MissionSearchCommand msc) { //We are interested in comments only if we are especially looking for them if (statement.Kind == MissionStatementKind.Commentary) { if (msc.Commentaries) if (Find_CheckStringForMatch(statement.Body, msc)) return new MissionSearchResult(curNode, curStatement, statement.Body, node, statement); return new MissionSearchResult(curNode, curStatement, null, node, statement); } bool statisfies = false; //Look for xml attribute names if (msc.XmlAttName) foreach (KeyValuePair<string, string> kvp in statement.GetAttributes()) statisfies = statisfies | Find_CheckStringForMatch(kvp.Key, msc); //Look for xml attribute values if (msc.XmlAttValue) foreach (KeyValuePair<string, string> kvp in statement.GetAttributes()) statisfies = statisfies | ((string.IsNullOrEmpty(msc.AttName) || kvp.Key == msc.AttName) && Find_CheckStringForMatch(kvp.Value, msc)); //Look for statement text if (msc.StatementText) statisfies = statisfies | Find_CheckStringForMatch(statement.Text, msc); if (statisfies) return new MissionSearchResult(curNode, curStatement, statement.Text, node, statement); return new MissionSearchResult(curNode, curStatement, null, node, statement); }
/// <summary> /// Checks if current selection (node and/or statement) matches the search criteria /// </summary> public bool Find_DoesCurrentSelectionMatch(MissionSearchCommand msc) { if (Find_CheckNodeForMatch(TreeViewNodes.SelectedNode, (MissionNode)TreeViewNodes.SelectedNode.Tag, -1, msc).Valid) return true; if (TreeViewStatements.SelectedNode == null || !(TreeViewStatements.SelectedNode.Tag is MissionStatement)) return false; return Find_CheckStatementForMatch(TreeViewNodes.SelectedNode, (MissionStatement)TreeViewStatements.SelectedNode.Tag, -1, -1, msc).Valid; }
/// <summary> /// Replace according to specified command in all nodes that are child to the current node. /// Called from ReplaceAll and recursively calls itself. /// </summary> private int ReplaceAll_RecursiveReplace(TreeNode node, ref int curNode, List<MissionSearchResult> list, MissionSearchCommand msc) { int replacements = 0; curNode++; int curStatement = 0; for (int i = 0; i < node.Nodes.Count; i++) replacements += ReplaceAll_RecursiveReplace(node.Nodes[i], ref curNode, list, msc); //Skip node in we are only looking in the current node and this isnt current node if (msc.OnlyInCurrentNode && !TreeViewNodes.NodeIsInsideNode(node, TreeViewNodes.SelectedNode)) return replacements; MissionNode mNode = (MissionNode)node.Tag; //Check if node matches our search criteria (before statements if going forward) replacements += Replace_InNode(node, curNode, list, msc); //Then we start looking through statements for (int i = 0; i < mNode.Conditions.Count; i++) replacements += Replace_InStatement(node, mNode.Conditions[i], curNode, ++curStatement, list, msc); for (int i = 0; i < mNode.Actions.Count; i++) replacements += Replace_InStatement(node, mNode.Actions[i], curNode, ++curStatement, list, msc); return replacements; }
/// <summary> /// Replace according to specified command in all mission nodes. Returns number of replacements made. /// </summary> public int ReplaceAll(List<MissionSearchResult> list, MissionSearchCommand msc) { BeginUpdate(); int replacements = 0; int curNode = 0; for (int i = 0; i < TreeViewNodes.Nodes.Count; i++) replacements += ReplaceAll_RecursiveReplace(TreeViewNodes.Nodes[i], ref curNode, list, msc); OutputMissionNodeContentsToStatementTree(); RegisterChange("Replaced '" + msc.Input + "' with '" + msc.Replacement + "' " + replacements.ToString() + " time(s)."); EndUpdate(); return replacements; }