private XsdElement[] getChildNames(IXsdNode node) { IList <XsdElement> result = new List <XsdElement>(); getChildNames(node, result); return(result.ToArray()); }
private void getChildNames(IXsdNode node, ICollection <XsdElement> result) { var kids = node.GetChildren(); foreach (IXsdNode xsdNode in kids) { if (xsdNode is XsdElement) { result.Add((XsdElement)xsdNode); } else if (xsdNode is XsdAllSequenceChoice) { XsdAllSequenceChoice list = (XsdAllSequenceChoice)xsdNode; getChildNames(list, result); } } if ((node as IXsdHasType)?.TypeTarget != null) { IXsdHasType n = (IXsdHasType)node; getChildNames(n.TypeTarget, result); } if ((node as IXsdHasRef)?.RefTarget != null) { IXsdHasRef n = (IXsdHasRef)node; getChildNames(n.RefTarget, result); } }
private ICollection <XsdAttribute> getAttributeNames(IXsdNode node) { List <XsdAttribute> result = new List <XsdAttribute>(); if (node is IXsdHasAttribute) { result.AddRange(((IXsdHasAttribute)node).GetAttributes()); } IXsdNode typeTarget = (node as IXsdHasType)?.TypeTarget; if (typeTarget is IXsdHasAttribute) { //don't call me recursively to prevent endless loops result.AddRange(((IXsdHasAttribute)typeTarget).GetAttributes()); } IXsdNode refTarget = (node as IXsdHasRef)?.RefTarget; if (refTarget is IXsdHasAttribute) { //don't call me recursively to prevent endless loops result.AddRange(((IXsdHasAttribute)refTarget).GetAttributes()); } foreach (XsdExtension kid in node.GetChildren().OfType <XsdExtension>()) { result.AddRange(getAttributeNames(kid)); } return(result); }
public void HistoryBack() { if (_history.Count > 1) { _history.Pop(); HistoryItem item = _history.Pop(); Schema = item.parser; IXsdNode node = item.node; Root = node; } else { MessageBox.Show(Application.Current.MainWindow, "No item in history", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } }
public void SetDescription(IXsdNode o) { if (_editor == null) { return; } _editor._lstXsdSelectedProperties.Items.Clear(); _editor._lstXsdSelectedProperties.Items.Add(string.Format("{0}", o.GetType().Name)); PropertyInfo[] props = o.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in props) { object value = propertyInfo.GetValue(o, null); if (value != null) { if (value is ICollection) { foreach (object item in (IEnumerable)value) { _editor._lstXsdSelectedProperties.Items.Add(string.Format("{0}: {1}", propertyInfo.Name, item)); } } else { _editor._lstXsdSelectedProperties.Items.Add(string.Format("{0}: {1}", propertyInfo.Name, value)); } } } //TODO is this neccesary? FragmentView will prettyprint nonetheless... XmlWriterSettings settings = new XmlWriterSettings { CheckCharacters = true, OmitXmlDeclaration = true, Indent = true, CloseOutput = true, }; StringWriter sw; using (sw = new StringWriter()) using (XmlWriter w = XmlWriter.Create(sw, settings)) { o.XmlNode.WriteTo(w); } _editor.SetFragmentText(sw.ToString()); }
protected static void addMouseEvents(XsdVisualizer.PaintOptions po, FrameworkElement pnl, IXsdNode node) { pnl.MouseDown += (sender, args) => { if (args.ClickCount == 2) { IXsdNode xsdNode = node; if (xsdNode is IXsdHasRef) { IXsdHasRef refer = (IXsdHasRef)xsdNode; if (refer.RefTarget != null) { po.Visualizer.SetRoot(refer.RefTarget); return; } } po.Visualizer.SetRoot(xsdNode); } args.Handled = true; }; pnl.MouseMove += (sender, args) => { po.Visualizer.SetDescription(node); args.Handled = true; }; ContextMenu mainMenu = new ContextMenu(); mainMenu.Items.Add(new MenuItem { Header = node.ToString(), IsEnabled = false }); MenuItem item1 = new MenuItem { Header = "Hide" }; item1.Click += (e, args) => { po.Visualizer.IgnoreNodes.Add(node); po.Visualizer.Refresh(); }; mainMenu.Items.Add(item1); pnl.ContextMenu = mainMenu; }
private void addMouseEvents(XsdVisualizer.PaintOptions po, UIElement pnl, IXsdNode node) { pnl.MouseDown += (sender, args) => { if (args.ClickCount == 2) { if (TypeTarget != null) { po.Visualizer.SetRoot(TypeTarget); } } args.Handled = true; }; pnl.MouseMove += (sender, args) => { po.Visualizer.SetDescription(node); args.Handled = true; }; }
public void SetRoot(IXsdNode node) { Root = node; }
private bool checkAttributeCompletion() { if (_schemaParser == null) { return(false); } int offset = _editor.CaretOffset; if (XParser.IsInsideElementDeclaration(_editor.Text, offset - 1)) { string element = XParser.GetElementAtCursorFuzzy(_editor.Text, offset - 1); IList <IXsdNode> all = _schemaParser.GetAllNodes(); IXsdNode node = getNodeWithName(all, element); if (node != null) { ICollection <XsdAttribute> attrs = getAttributeNames(node); if (attrs != null && attrs.Count > 0) { IList <ICompletionData> data = new List <ICompletionData>(); foreach (XsdAttribute attr in attrs) { string desc = null; if (attr.Annotation != null && attr.Annotation.Count > 0) { StringBuilder description = new StringBuilder(); foreach (string ann in attr.Annotation) { description.AppendLine(ann); } desc = description.ToString(); } data.Add(new ActionCompletionData(attr.Name, desc, (area, segment, args) => { int idxOfSpace = area.Document.Text.IndexOf(' ', offset - 1); int idxOfElementClose = area.Document.Text.IndexOf('>', offset - 1); int insertAt = Math.Min(idxOfSpace, idxOfElementClose); if (insertAt < 0) { insertAt = offset; } else { if (idxOfElementClose == insertAt) { area.Document.Insert(insertAt, " "); } insertAt = insertAt + 1; } area.Document.Insert(insertAt, attr.Name + "=\""); _editor.CaretOffset = insertAt + attr.Name.Length + 2; })); } showCompletion(data); return(true); } } } return(false); }
private bool completeBasedOnTextEntered(TextCompositionEventArgs e) { try { switch (e.Text) { case ">": { //auto-insert closing element int offset = _editor.CaretOffset; string s = XParser.GetElementAtCursor(_editor.Text, offset - 1); if (!string.IsNullOrWhiteSpace(s) && "!--" != s) { if (!XParser.IsClosingElement(_editor.Text, offset - 1, s)) { string endElement = "</" + s + ">"; var rightOfCursor = _editor.Text.Substring(offset, Math.Max(0, Math.Min(endElement.Length + 50, _editor.Text.Length) - offset - 1)).TrimStart(); if (!rightOfCursor.StartsWith(endElement)) { _editor.TextArea.Document.Insert(offset, endElement); _editor.CaretOffset = offset; return(true); } } } break; } case "/": { //insert name of closing element int offset = _editor.CaretOffset; if (offset > 1 && _editor.Text[offset - 2] == '<') { //expand to closing tag string s = XParser.GetParentElementAtCursor(_editor.Text, offset - 1); if (!string.IsNullOrEmpty(s)) { showCompletion(new List <ICompletionData> { new MyCompletionData(s + ">") }); return(true); } } if (_editor.Text.Length > offset + 2 && _editor.Text[offset] == '>') { //remove closing tag if exist string s = XParser.GetElementAtCursor(_editor.Text, offset - 1); if (!string.IsNullOrWhiteSpace(s)) { //search closing end tag. Element must be empty (whitespace allowed) //"<hallo> </hallo>" --> enter '/' --> "<hallo/> " string expectedEndTag = "</" + s + ">"; for (int i = offset + 1; i < _editor.Text.Length - expectedEndTag.Length + 1; i++) { if (!char.IsWhiteSpace(_editor.Text[i])) { if (_editor.Text.Substring(i, expectedEndTag.Length) == expectedEndTag) { //remove already existing endTag _editor.Document.Remove(i, expectedEndTag.Length); } break; } } } } break; } case "!": { int offset = _editor.CaretOffset; if (offset > 1 && _editor.Text[offset - 2] == '<') { _editor.TextArea.Document.Insert(offset, "-- "); _editor.CaretOffset = offset + 2; return(true); } break; } case "\"": { //auto-insert closing apostroph int offset = _editor.CaretOffset; int countApostroph = 0; for (int i = offset; i >= 0; i--) { char charAtCursor = _editor.Text[i]; if (charAtCursor == '\"') { countApostroph++; } else if (charAtCursor == '<') { break; } } bool oddLeft = (countApostroph % 2 == 1); for (int i = offset; i < _editor.Text.Length; i++) { char charAtCursor = _editor.Text[i]; if (charAtCursor == '\"') { countApostroph++; } else if (charAtCursor == '>') { break; } } bool oddRight = (countApostroph % 2 == 1); if (oddLeft && oddRight) { _editor.TextArea.Document.Insert(offset, "\""); _editor.CaretOffset = offset; return(true); } break; } case " ": { //when user writes an element/attribute, completion shall pop up on whitespace bool result = checkAttributeCompletion(); if (result) { return(true); } break; } case "<": { if (_schemaParser == null) { return(false); } int offset = _editor.CaretOffset; string parent = XParser.GetParentElementAtCursor(_editor.Text, offset); XsdElement[] names; if (parent == "") { IXsdNode root = _schemaParser.GetVirtualRoot(); names = getChildNames(root); } else { IList <IXsdNode> all = _schemaParser.GetAllNodes(); IXsdNode node = getNodeWithName(all, parent); if (node != null) { names = getChildNames(node); } else { names = null; } } if (names != null && names.Length > 0) { IList <ICompletionData> data = new List <ICompletionData>(); foreach (XsdElement name in names) { if (name.Annotation != null && name.Annotation.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (string ann in name.Annotation) { sb.AppendLine(ann); } data.Add(new MyCompletionData(name.Name, sb.ToString())); } else { data.Add(new MyCompletionData(name.Name)); } } showCompletion(data); return(true); } break; } } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); } return(false); }
public void AddKids(IXsdNode kid) { _kids.Add(kid); }
protected UIElement GetPaintChildren(XsdVisualizer.PaintOptions po, int fontSize) { StackPanel pnlKids = new StackPanel(); pnlKids.Orientation = Orientation.Horizontal; foreach (IXsdNode kid in _kids) { if (po.HideTypes && kid is IXsdIsType) { continue; } if (po.HideImportIncludes && kid is XsdImportInclude) { continue; } bool referenceExpanded = false, typeExpanded = false; IXsdNode toPaint = kid; if (po.ExpandReferences && kid is IXsdHasRef && ((IXsdHasRef)kid).RefTarget != null) { toPaint = ((IXsdHasRef)kid).RefTarget; referenceExpanded = true; } if (po.ExpandTypes && kid is IXsdHasType && ((IXsdHasType)kid).TypeTarget != null) { toPaint = ((IXsdHasType)kid).TypeTarget; typeExpanded = true; } Debug.Assert(!(typeExpanded && referenceExpanded), "node cannot be type and ref"); //shade expanded nodes if (referenceExpanded || typeExpanded) { StackPanel pnlExpanded = new StackPanel { Margin = new Thickness(3) }; Color c = referenceExpanded ? Colors.Blue : /*typeExpanded */ Colors.Green; pnlExpanded.Background = new SolidColorBrush(Color.FromArgb(32, c.R, c.G, c.G)); UIElement title = ((XsdNode)kid).GetPaintTitle(po, fontSize); if (!po.DontPaint.Contains(toPaint)) { var kidComp = toPaint.GetPaintComponent(po, fontSize); if (title != null && kidComp != null) { pnlExpanded.Children.Add(title); pnlExpanded.Children.Add(kidComp); } pnlKids.Children.Add(pnlExpanded); } } else { if (!po.DontPaint.Contains(toPaint)) { var kidComp = toPaint.GetPaintComponent(po, fontSize); if (kidComp != null) { pnlKids.Children.Add(kidComp); } } } } return(pnlKids); }