//****************************************************************** /// <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> /// Makes sure a parse is selected when the input focus leaves this /// control. /// </summary> private void ParseListViewer_Leave(object oSender,EventArgs oArgs) { try { //********************************************************** // If the list is empty (and not read-only), add a new parse // to the list and select it. if (moListView.Items.Count == 0) { if (! ReadOnly) { //************************************************** // Set the Modified property to true. Modified = true; //************************************************** // Clear the undo information. ClearUndoInformation(); //************************************************** // Create a new parse item and append it to the // list. ParseListViewerItem oParseItem = new ParseListViewerItem(); oParseItem.Text = "(parse)"; oParseItem.ParseTreeRoot = new SyntaxNode(); moListView.Items.Add(oParseItem); //************************************************** // Select the item. oParseItem.Selected = true; //************************************************** // Raise the ListChanged event. OnListChanged(new EventArgs()); } } //********************************************************** // If the list is not empty, one of the items should be the // SelectedParse. if (moListView.Items.Count > 0) { Debug.Assert(SelectedParse != null); Debug.Assert(moListView.Items.Contains(SelectedParse)); } //********************************************************** // Make sure the SelectedParse item is selected. if (SelectedParse != null) { if (SelectedParse != FirstSelectedItem()) { ClearSelection(); SelectedParse.Selected = true; } } //********************************************************** // Make sure the SelectedParse item is scrolled into view // and has focus within the ListView. if (SelectedParse != null) { SelectedParse.EnsureVisible(); SelectedParse.Focused = true; } } catch (Exception oException) { ShowException(oException); } }
//****************************************************************** /// <summary> /// Uses the .ParseTreeRoot property of the given oSelectedParse /// object to populate the linked ParseTreeViewer. The SelectedParse /// property is set to the given oSelectedParse object. /// </summary> private void UpdateParseTreeViewer( ParseListViewerItem oSelectedParse) { //************************************************************** // Use the given oSelectedParse to initialize a TreeTransfer // object, which will be used to populate the parse-tree viewer. // // If oSelectedParse is null, a blank node will be used to // populate the parse-tree viewer. // // If oSelectedParse is not null, its .ParseTreeRoot tree will // be used to populate the parse-tree viewer. TreeTransfer oTreeTransfer = new TreeTransfer(); if (oSelectedParse == null) { oTreeTransfer.ParseTreeRoot = new SyntaxNode(); oTreeTransfer.FindPatternRoot = null; oTreeTransfer.ReplacePatternRoot = null; } else { oTreeTransfer.ParseTreeRoot = oSelectedParse.ParseTreeRoot; oTreeTransfer.FindPatternRoot = null; oTreeTransfer.ReplacePatternRoot = null; } //************************************************************** // Temporarily set the SelectedParse property to null, because // we do not want the LinkedParseTreeViewer TreeChanged event to // update the SelectedParse (and set the Modified property to // true) when we populate the parse-tree viewer. moSelectedParse = null; //************************************************************** // Populate the linked parse-tree viewer. if (LinkedParseTreeViewer != null) { LinkedParseTreeViewer.PopulateTree(oTreeTransfer); } //************************************************************** // Now set the SelectedParse property to the given // oSelectedParse object, because we want the TreeChanged events // to update the SelectedParse (and set the Modified property to // true) when the user edits the parse tree. moSelectedParse = oSelectedParse; }
//****************************************************************** /// <summary> /// Inserts the given parses (oParseTrees) into the ListView, /// starting at the indicated index (iFirstIndex). The inserted /// parses are selected. /// </summary> private void InsertParses(int iFirstIndex, TransferRuleCollection oParseTrees) { Debug.Assert(iFirstIndex >= 0); Debug.Assert(oParseTrees != null); Debug.Assert(oParseTrees.Count > 0); //************************************************************** // Clear the selection. ClearSelection(); //************************************************************** // Insert the given parses starting at the indicated index. int iIndex = iFirstIndex; foreach (TransferRule oRule in oParseTrees) { //********************************************************** // Create the item to display in the ListView. (Get the // item's displayed text from the TransferRule object's // .RuleName property and the parse tree from its // .FindPatternRoot property.) ParseListViewerItem oParseItem = new ParseListViewerItem(); oParseItem.Text = oRule.RuleName; oParseItem.ParseTreeRoot = oRule.FindPatternRoot; //********************************************************** // Insert or append the item in the ListView. if (iIndex < moListView.Items.Count) { moListView.Items.Insert(iIndex,oParseItem); } else { moListView.Items.Add(oParseItem); } //********************************************************** // Select the item and make sure it is scrolled into view. oParseItem.Selected = true; oParseItem.EnsureVisible(); //********************************************************** // Increment the index. ++iIndex; } //************************************************************** // Make sure the first selected item is scrolled into view and // has focus within the ListView. ParseListViewerItem oFirstSelectedItem = FirstSelectedItem(); if (oFirstSelectedItem != null) { oFirstSelectedItem.EnsureVisible(); oFirstSelectedItem.Focused = true; } }