Ejemplo n.º 1
0
        //TreeView Selected Item
        private void tvxpath_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            if (tvxpath.SelectedItem == null)
            {
                return;
            }
            TreeViewItem selected = ((TreeViewItem)tvxpath.SelectedItem);

            selected.IsExpanded = true;
            if (selected.Tag != null)
            {
                ViewerNode selectedNode = (ViewerNode)((TreeViewItem)tvxpath.SelectedItem).Tag;
                highlightFragment(selectedNode);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(xmlin.Text);
                XmlNodeList  node = doc.SelectNodes("//@" + selectedNode.Name);
                DocumentLine line = xmlin.Document.GetLineByOffset(xmlin.CaretOffset);
                foreach (XmlNode xn in node)
                {
                    if (xn.Value.Contains(selectedNode.Value) && (xn.Name.Contains(selectedNode.Name) && selectedNode.LineInfo.LineNumber == line.LineNumber))
                    {
                        string mynode = GetXPathToNode(xn);
                        txtmessage.Text = mynode;
                        Clipboard.SetDataObject(mynode, true);
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
 void tvxpath_SelectionChanged(ViewerNode obj)
 {
     if (obj == null)
     {
         return;
     }
     highlightFragment(obj);
 }
Ejemplo n.º 3
0
        //Load TreeView
        private void displayNavigator(XPathNodeIterator xpi)
        {
            if ((xpi != null) && (xpi.Count > 0))
            {
                for (bool hasNext = xpi.MoveNext(); hasNext; hasNext = xpi.MoveNext())
                {
                    // IXmlLineInfo lineInfo = xpi.Current as IXmlLineInfo;
                    switch (xpi.Current.NodeType)
                    {
                    case XPathNodeType.Text:
                    {
                        TreeViewItem node = new TreeViewItem();
                        node.Header     = xpi.Current.Value;
                        node.Foreground = Brushes.Brown;
                        node.ToolTip    = "(Nodeset/Text)";
                        tvxpath.Items.Add(node);
                        break;
                    }

                    case XPathNodeType.Attribute:
                    {
                        TreeViewItem node = new TreeViewItem();
                        node.Header     = "@" + xpi.Current.Name + ": " + xpi.Current.Value;
                        node.Foreground = Brushes.Brown;
                        node.ToolTip    = "(Nodeset/Attribute)";
                        node.Tag        = xpi.Current.Clone();
                        tvxpath.Items.Add(node);
                        break;
                    }

                    case XPathNodeType.Element:
                    {
                        var          document = new XSDocument(xpi.Current.OuterXml);
                        ViewerNode   vNode    = new ViewerNode(document);
                        TreeViewItem tvi      = TreeViewHelper.BuildTreeView(vNode);
                        tvxpath.Items.Add(tvi);
                        break;
                    }
                    }
                    if (string.IsNullOrEmpty(xmlin.Text))
                    {
                        xmlin.Text = xpi.Current.OuterXml;
                    }
                    else
                    {
                        xmlin.Text = xmlin.Text + "\r\n" + xpi.Current.OuterXml;
                    }
                }
            }
            else
            {
                tvxpath.Items.Add("Nothing found.");
                xmlin.Text = "";
            }
        }
Ejemplo n.º 4
0
 private ViewerNode(XSDocument doc, XmlNode originalNode,
                    ViewerNode parent, int occurrenceIndex = -1)
 {
     _document = doc;
     // ChildNodes = new List<ViewerNode>();
     Attributes      = new List <ViewerNode>();
     NodeType        = NodeType.Unknown;
     _originalNode   = originalNode;
     _parent         = parent;
     OccurrenceIndex = occurrenceIndex;
     Build();
 }
Ejemplo n.º 5
0
        public void SelectNodeBasedOnCursor(TextLocation loc)
        {
            ItemCollection items = tvxpath.Items;

            Debug.Assert(items.Count <= 1);
            if (items.Count != 1)
            {
                return;
            }

            LazyTreeViewItem root = (LazyTreeViewItem)tvxpath.Items[0];
            IEnumerable <LazyTreeViewItem> allChilds = root.AsDepthFirstEnumerable(
                x =>
            {
                x.Expand();
                return(x.Items.Cast <LazyTreeViewItem>());
            }
                );

            TreeViewItem match = null;

            foreach (LazyTreeViewItem child in allChilds)
            {
                if (child.Tag != null)
                {
                    ViewerNode   node     = (ViewerNode)child.Tag;
                    IXmlLineInfo lineInfo = node.LineInfo;
                    if (lineInfo != null)
                    {
                        if (lineInfo.LineNumber == loc.Line && lineInfo.LinePosition <= loc.Column)
                        {
                            //last one counts
                            match = child;
                        }
                        if (lineInfo.LineNumber > loc.Line || (lineInfo.LineNumber == loc.Line && lineInfo.LinePosition > loc.Column))
                        {
                            break;
                        }
                    }
                }
            }
            if (match != null)
            {
                tvxpath.SelectedItemChanged -= tvxpath_SelectedItemChanged;
                match.IsSelected             = true;
                match.BringIntoView();
                tvxpath.SelectedItemChanged += tvxpath_SelectedItemChanged;
            }
        }
Ejemplo n.º 6
0
        private void highlightFragment(ViewerNode selectedNode)
        {
            if (xmlin == null)
            {
                return;
            }

            //show fragment in fragment view
            if (selectedNode == null)
            {
                //todo
            }
            else
            {
                StringBuilder     sb       = new StringBuilder();
                XmlWriterSettings settings = new XmlWriterSettings
                {
                    ConformanceLevel = ConformanceLevel.Auto,
                    Indent           = true
                };

                using (XmlWriter w = XmlWriter.Create(sb, settings))
                {
                    selectedNode.OriginalNode.WriteTo(w);
                }
            }

            //select node in editor
            if (selectedNode != null)
            {
                IXmlLineInfo lineInfo = selectedNode.LineInfo;
                if (lineInfo != null)
                {
                    var offset = xmlin.Document.GetOffset(lineInfo.LineNumber, lineInfo.LinePosition);
                    xmlin.Select(offset, selectedNode.Name.Length);
                    Debug.Assert(xmlin.SelectedText == selectedNode.Name);
                    xmlin.ScrollTo(lineInfo.LineNumber, lineInfo.LinePosition);
                }
            }
        }
Ejemplo n.º 7
0
        protected void Execute(MainWindow ef)
        {
            try
            {
                TreeViewItem selected = ((TreeViewItem)ef.tvxpath.SelectedItem);

                if (selected.Tag != null)
                {
                    ViewerNode  selectedNode = (ViewerNode)selected.Tag;
                    XmlDocument xmldoc       = selectedNode.OriginalNode.OwnerDocument;
                    XmlNode     node         = selectedNode.OriginalNode;

                    Debug.Assert(xmldoc != null, "Checked in CanExecute");
                    Debug.Assert(node.ParentNode != null, "Checked in CanExecute");

                    Execute(ef, selected, selectedNode, xmldoc, node, node.ParentNode);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(Application.Current.MainWindow, "Error: " + e.Message, "Error", MessageBoxButton.OK,
                                MessageBoxImage.Error);
            }
        }
Ejemplo n.º 8
0
        protected bool CanExecute(MainWindow ef)
        {
            if (ef.xmlin == null)
            {
                return(false);
            }

            TreeViewItem selected = ((TreeViewItem)ef.tvxpath.SelectedItem);

            if (selected == null)
            {
                return(false);
            }
            if (selected.Tag == null)
            {
                return(false);
            }

            ViewerNode selectedNode = (ViewerNode)selected.Tag;
            XmlNode    node         = selectedNode.OriginalNode;

            if (node == null)
            {
                return(false);
            }
            if (node.OwnerDocument == null)
            {
                return(false);
            }
            if (node.ParentNode == null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 9
0
 protected abstract void Execute(MainWindow ef, TreeViewItem treeViewItem, ViewerNode selectedNode, XmlDocument xmldoc, XmlNode node, XmlNode parentNode);
Ejemplo n.º 10
0
        public static TreeViewItem BuildTreeView(ViewerNode vNode, Func <TreeViewItem, bool> filter)
        {
            if (vNode.NodeType != NodeType.Unknown)
            {
                LazyTreeViewItem node = null;
                if (vNode.NodeType == NodeType.Attribute)
                {
                    if (vNode.AttributeType != AttributeType.None)
                    {
                        return(null);
                    }
                    node = new LazyTreeViewItem();

                    StackPanel pnl = new StackPanel {
                        Orientation = Orientation.Horizontal
                    };
                    pnl.Children.Add(new TextBlock {
                        Text = "@" + vNode.LocalName, Foreground = Brushes.Gray
                    });
                    if ((vNode.ChildNodes.Count == 0) && !string.IsNullOrEmpty(vNode.Value))
                    {
                        pnl.Children.Add(new TextBlock {
                            Text = ": " + vNode.Value, Foreground = Brushes.Green
                        });
                    }
                    node.Header = pnl;

                    node.Foreground = Brushes.Brown;
                    node.ToolTip    = vNode.ToDetailsString();
                }
                else if (vNode.NodeType == NodeType.Element)
                {
                    node = new LazyTreeViewItem();

                    StackPanel pnl = new StackPanel {
                        Orientation = Orientation.Horizontal
                    };
                    if (string.IsNullOrEmpty(vNode.TypeName))
                    {
                        pnl.Children.Add(new TextBlock {
                            Text = vNode.LocalName, Foreground = Brushes.White
                        });
                    }
                    else
                    {
                        pnl.Children.Add(new TextBlock {
                            Text = vNode.LocalName + " [" + vNode.TypeName + "]", Foreground = Brushes.White
                        });
                    }
                    if ((vNode.ChildNodes.Count == 0) && !string.IsNullOrEmpty(vNode.Value))
                    {
                        pnl.Children.Add(new TextBlock {
                            Text = ": " + vNode.Value.Trim(), Foreground = Brushes.Green
                        });
                    }

                    node.Header  = pnl;
                    node.ToolTip = vNode.ToDetailsString();
                }
                node.Tag = vNode;

                if (vNode.Attributes.Count > 0 || vNode.ChildNodes.Count > 0)
                {
                    //to make item expandable
                    node.Items.Add(new LazyTreeViewItem());
                }

                node.ExpandAction += (thisNode) =>
                {
                    //check if child is DUMMY and expand...
                    if (thisNode.Items.Count == 1 && ((LazyTreeViewItem)thisNode.Items[0]).IsStillLazy)
                    {
                        thisNode.Items.Clear();
                        foreach (ViewerNode t in vNode.Attributes)
                        {
                            TreeViewItem tvi = BuildTreeView(t, filter);
                            if (tvi != null)
                            {
                                thisNode.Items.Add(tvi);
                            }
                        }
                        foreach (ViewerNode t in vNode.ChildNodes)
                        {
                            TreeViewItem tvi = BuildTreeView(t, filter);
                            if (tvi != null)
                            {
                                thisNode.Items.Add(tvi);
                            }
                        }
                    }
                };

                if (filter != null)
                {
                    if (!filter(node))
                    {
                        return(node);
                    }
                    else
                    {
                        return(null);
                    }
                }
                return(node);
            }
            return(null);
        }
Ejemplo n.º 11
0
 public static TreeViewItem BuildTreeView(ViewerNode vNode)
 {
     return(BuildTreeView(vNode, null));
 }