public ControlSet Execute(DependencyObject sourceControl) { Common.AddToLog("executing selector: " + this.Query); SourceControl = sourceControl; ControlSet superSet = XamlQuery.All(sourceControl); ControlSet matchedControls = new ControlSet(); if (SimpleSelectors.Count == 1) //if only one simple-selector, then execute it and return the result { return(SimpleSelectors[0].Execute(superSet)); } else if (SimpleSelectors.Count > 1) //if more than one simple-selectors, then apply combinators while executing { //apply each combinator to result of simple-selector that precedes it ControlSet currentSet = new ControlSet(); currentSet.AddRange(superSet); for (int index = 0; index < SimpleSelectors.Count; index++) { if (index > 0) { Common.AddToLog("**Executing Combinator: " + Combinators[index - 1].Token.Content); currentSet = Combinators[index - 1].Execute(currentSet); Common.AddToLog("**Combinator-Result: " + currentSet.Count + " controls"); if (Combinators[index - 1].Type == CombinatorType.Adjacent) { //for adjacent combinator // get first-set by executing simple-selector before + with result of combinator // get second-set by executing simple-selector after + with result of combinator // combine first and second sets as result of combinator and its simple-selectors ControlSet firstSet = SimpleSelectors[index - 1].Execute(currentSet); ControlSet secondSet = SimpleSelectors[index].Execute(currentSet); currentSet.Clear(); currentSet.AddRange(firstSet); currentSet.AddRange(secondSet); Common.AddToLog("**First-Set-Result: " + firstSet.Count + " controls. " + string.Join(",", (from c in firstSet select c.GetType().ToString()).ToArray())); Common.AddToLog("**Second-Set-Result: " + secondSet.Count + " controls. " + string.Join(",", (from c in secondSet select c.GetType().ToString()).ToArray())); Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls"); continue; } } Common.AddToLog("**Executing Simple-Selector: " + SimpleSelectors[index].Query); currentSet = SimpleSelectors[index].Execute(currentSet); Common.AddToLog("**Simple-Selector-Result: " + currentSet.Count + " controls"); } matchedControls.AddRange(currentSet); } return(matchedControls); }
public ControlSet Execute(ControlSet superSet) { Common.AddToLog("executing combinator: " + this.Type); ControlSet result = new ControlSet(); switch (this.Type) { case CombinatorType.Descendant: //get descendants (children in visual-tree) of all controls in superset foreach (DependencyObject control in superSet) { ControlSet controlSet = XamlQuery.All(control); foreach (DependencyObject c in controlSet) { if (!result.Contains(c)) { result.Add(c); } } } break; case CombinatorType.Child: //get children of all Panel controls in superset foreach (DependencyObject control in superSet) { if (control is Panel) { result.AddRange(from child in ((Panel)control).Children select(DependencyObject) child); } } break; case CombinatorType.Adjacent: //get children of all parent Panels of all controls in superset //if a parent is not a Panel, then it is ignored foreach (DependencyObject control in superSet) { if (control is FrameworkElement) { DependencyObject parent = ((FrameworkElement)control).Parent; if (parent is Panel) { result.AddRange(from child in ((Panel)parent).Children select(DependencyObject) child); } } } //include original controls also in result result.AddRange(superSet); Common.AddToLog("adj-superset-added"); break; } //remove duplicate-controls ControlSet uniqueControls = new ControlSet(); foreach (DependencyObject c in result) { if (!uniqueControls.Contains(c)) { uniqueControls.Add(c); } } return(uniqueControls); }