protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { List <MergeBlock> blocks; using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); byte[] data = null; TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat); tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat); blocks = MergeBlock.GetMergeBlocksFlattened(tx); } // select the selected block in the TextControl.Web foreach (MergeBlock block in blocks) { if (block.Name == TreeView1.SelectedValue) { TextControl1.Selection = new TXTextControl.Web.Selection(block.StartMarker.Start, block.Length); break; } } }
/// <summary> /// Gathers merge blocks in current document. /// </summary> /// <returns>Merge blocks in current document.</returns> public static List <MergeBlock> GetMergeBlocksFlattened(ServerTextControl textControl) { ValidateMergeBlockNesting(textControl); var result = new List <MergeBlock>(); // Get a list of all block start and end markers ordered by their start position: var blockMarkers = GetBlockMarkersOrdered(textControl); // Gather blocks: var startMarkers = new Stack <TXTextControl.DocumentTarget>(); foreach (var marker in blockMarkers) { if (marker.TargetName.StartsWith(MergeBlock.BlockStartPrefix, StringComparison.OrdinalIgnoreCase)) { // Start marker: startMarkers.Push(marker); } else if (marker.TargetName.StartsWith(MergeBlock.BlockEndPrefix, StringComparison.OrdinalIgnoreCase)) { // End marker: TXTextControl.DocumentTarget startMarker = startMarkers.Pop(); // Get currently open start marker var blockNew = new MergeBlock(startMarker, marker); result.Add(blockNew); } } result.Sort(MergeBlock.SortPosition); return(result); }
// add the merge fields to the nodes private void fillMergeFields(MergeBlock block, TreeNode node) { foreach (MergeField field in block.Fields) { TreeNode tnField = new TreeNode(field.Name); tnField.ImageUrl = "images/field.png"; node.ChildNodes.Add(tnField); } }
/// <summary> /// Constructor. /// </summary> public MergeBlock(List <DocumentTarget> blockMarkers, ref int i, ServerTextControl textControl, MergeBlock parent) { if (!blockMarkers[i].TargetName.StartsWith(BlockStartPrefix)) { throw new Exception( "Invalid merge block structure. Block start marker doesn't start with the block start prefix (Marker name: \"" + blockMarkers[i].TargetName + "\")"); } _fields = new List <MergeField>(); _children = new List <MergeBlock>(); _parent = parent; StartMarker = blockMarkers[i++]; // Gather child blocks if any while ((i < blockMarkers.Count) && blockMarkers[i].TargetName.StartsWith(BlockStartPrefix)) { _children.Add(new MergeBlock(blockMarkers, ref i, textControl, this)); ++i; } if (i >= blockMarkers.Count) { // Not possible throw new Exception("Invalid merge block structure."); } // End marker. string endMarkerName = blockMarkers[i].TargetName.Substring(BlockEndPrefix.Length); if (endMarkerName != Name) { throw new Exception( "Invalid merge block structure. End marker name is not the same as the start marker name. (End marker name: \"" + blockMarkers[i].TargetName + "\")"); } EndMarker = blockMarkers[i]; // Get fields inside this block but not in child blocks GetFields(textControl); }
// read all blocks from the current document and fill the TreeView private void updateNavigationPanel() { List <MergeBlock> blocks; try { using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); byte[] data = null; TextControl1.SaveText(out data, TXTextControl.Web.BinaryStreamType.InternalUnicodeFormat); tx.Load(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat); blocks = MergeBlock.GetMergeBlocks(MergeBlock.GetBlockMarkersOrdered(tx), tx); } TreeView1.Nodes.Clear(); fillTreeView(blocks); TreeView1.ExpandAll(); } catch { } }
private static bool IsInside(ApplicationField field, MergeBlock block) { int nFldEnd = field.Start + field.Length; return((block.StartMarker.Start <= field.Start) && (block.EndMarker.Start >= nFldEnd)); }