//****************************************************************** /// <summary> /// Loads and displays the indicated parse file (sFileName). The /// Modified property is set to false after the file is loaded. /// </summary> public void LoadParseFile(string sFileName) { //************************************************************** // Validate the parameters. if ((sFileName == null) || (sFileName == "")) { string sMessage = "Invalid argument: " + "ParseListViewer.LoadParseFile() requires " + "a file name that is not null or blank."; throw new Exception(sMessage); } //************************************************************** // Clear the linked ParseTreeViewer. UpdateParseTreeViewer(null); //************************************************************** // Clear the undo information. ClearUndoInformation(); //************************************************************** // Clear the list of items in the ListView. moListView.Items.Clear(); //************************************************************** // Create a ParseReader to read from the indicated parse file. StreamReader oStreamReader = new StreamReader(sFileName); ParseReader oParseReader = new ParseReader(oStreamReader); //************************************************************** // For each parse loaded from the parse file, create a new parse // item and append it to the ListView. while (oParseReader.Read()) { ParseListViewerItem oParseItem = new ParseListViewerItem(); oParseItem.Text = oParseReader.ParseName; oParseItem.ParseTreeRoot = oParseReader.ParseTreeRoot; moListView.Items.Add(oParseItem); } //************************************************************** // Close the ParseReader. oParseReader.Close(); //************************************************************** // Select the first item in the ListView. Make sure it is // scrolled into view and has focus within the ListView. if (moListView.Items.Count > 0) { ListViewItem oItem = moListView.Items[0]; oItem.Selected = true; oItem.EnsureVisible(); oItem.Focused = true; } //************************************************************** // Set the Modified property to false. Modified = false; //************************************************************** // Raise the SelectionChanged event. OnSelectionChanged(new EventArgs()); //************************************************************** // Raise the ListChanged event. OnListChanged(new EventArgs()); }
//****************************************************************** /// <summary> /// The timer is fired once after the form is loaded, and this event /// handler performs the tree-transfer processing. It uses the /// collection of transfer rules to process the parse trees in the /// input file, and it writes the modified parse trees to the output /// file. /// </summary> private void moTimer_Tick(object oSender,EventArgs oArgs) { ParseReader oParseReader = null; ParseWriter oParseWriter = null; try { //********************************************************** // Stop the timer (so it only fires once after the form is // loaded). moTimer.Stop(); //********************************************************** // Initialize the information displayed on the form. Text = "Processing Parse File..."; moInputFileTextBox.Text = InputFileName; moOutputFileTextBox.Text = OutputFileName; moRuleCountTextBox.Text = TransferRules.Count.ToString(); moParseCountTextBox.Text = "0"; moInputFileTextBox.Select( moInputFileTextBox.Text.Length,0); moOutputFileTextBox.Select( moOutputFileTextBox.Text.Length,0); moRuleCountTextBox.Select( moRuleCountTextBox.Text.Length,0); moParseCountTextBox.Select( moParseCountTextBox.Text.Length,0); moParseCountTextBox.Focus(); moOKButton.Enabled = false; moCancelButton.Enabled = true; //********************************************************** // Make sure the form is visible. Show(); Application.DoEvents(); //********************************************************** // Open the input and output files. StreamReader oStreamReader = new StreamReader(InputFileName); oParseReader = new ParseReader(oStreamReader); StreamWriter oStreamWriter = new StreamWriter(OutputFileName); oParseWriter = new ParseWriter(oStreamWriter); //********************************************************** // Read and process each parse tree from the input file. int iCount = 0; ProcessingWasCanceled = false; while ((oParseReader.Read()) && (! ProcessingWasCanceled)) { //****************************************************** // Create a TreeTransfer object and set its // ParseTreeRoot to the parse tree that was read from // the input file. TreeTransfer oTransfer = new TreeTransfer(); oTransfer.ParseTreeRoot = oParseReader.ParseTreeRoot; //****************************************************** // Apply each rule in the TransferRules collection. foreach (TransferRule oRule in TransferRules) { oTransfer.FindPatternRoot = oRule.FindPatternRoot; oTransfer.ReplacePatternRoot = oRule.ReplacePatternRoot; //************************************************** // Apply the rule to each matching branch. oTransfer.CurrentParseTreeNode = null; while (oTransfer.FindNextMatchingBranch()) { oTransfer.ReplaceCurrentMatchingBranch(); } } //****************************************************** // Write the modified parse tree to the output file. oParseWriter.ParseTreeRoot = oTransfer.ParseTreeRoot; oParseWriter.Write(); //****************************************************** // Update the information displayed on the form. ++iCount; moParseCountTextBox.Text = iCount.ToString(); moParseCountTextBox.Select( moParseCountTextBox.Text.Length,0); //****************************************************** // Call DoEvents() so the form can update the display // and check if the user clicked the Cancel button. Application.DoEvents(); } //********************************************************** // Close the input and output files. oParseReader.Close(); oParseReader = null; oParseWriter.Close(); oParseWriter = null; //********************************************************** // Update the final information displayed on the form. if (ProcessingWasCanceled) { Text = "Processing Parse File... Canceled"; } else { Text = "Processing Parse File... Finished"; } moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); //********************************************************** // If the Cancel button was clicked, set DialogResult to // DialogResult.Cancel, which will close the form. // // If the Cancel button was not clicked, but the // CloseWhenFinished property is true, set DialogResult to // DialogResult.OK, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } else { if (CloseWhenFinished) { DialogResult = DialogResult.OK; } } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the output file is closed (even if an exception // was thrown). if (oParseWriter != null) { oParseWriter.Close(); oParseWriter = null; } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the OK button is enabled (even if an exception // was thrown). moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // If the Cancel button was clicked (or an exception was // thrown), make sure DialogResult is set to // DialogResult.Cancel, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } } catch (Exception oException) { ShowException(oException); } }