/// <summary> /// Route control. /// </summary> /// <param name="root">CodeDomProvider.</param> #else /// <summary> /// コンストラクタ。 /// </summary> /// <param name="root">ルートのコントロール。</param> #endif public void CreateDriver(Control root) { //FormとUserControlを全取得 var recursiveCheck = new List <Control>(); var targets = new Dictionary <Type, Control>(); var getFromControlTreeOnly = new List <Type>(); GetAllFormAndUserControl(false, root, targets, getFromControlTreeOnly, recursiveCheck); //ドライバ情報を取得 var driverInfos = new Dictionary <Type, DriverInfo <Control> >(); foreach (var e in targets) { var fileName = $"{_driverTypeNameManager.MakeDriverType(e.Value)}.cs"; var info = CreateDriverInfo(e.Value, fileName); driverInfos[e.Key] = info; } //コード生成 foreach (var e in driverInfos) { var driverName = _driverTypeNameManager.MakeDriverType(e.Value.Target, out var nameSpace); if (string.IsNullOrEmpty(nameSpace)) { nameSpace = DriverCreatorAdapter.SelectedNamespace; } DriverCreatorAdapter.AddCode($"{driverName}.cs", GenerateCode(root, e.Value.Target, nameSpace, driverName, e.Value.Usings, e.Value.Members, getFromControlTreeOnly), e.Value.Target); } }
private void CreateDriverInfoFindFromControlTree( int mdiChildrenIndex, Control root, Control target, DriverInfo <Control> driverInfo, IList <ControlAndDefine> controlAndDefines, IList <Control> mappedControls, IList <string> names, int[] controlTreeIndecies, string fileName) { var next = new List <int>(controlTreeIndecies) { 0 }; for (int i = 0; i < target.Controls.Count; i++) { next[next.Count - 1] = i; var ctrl = target.Controls[i]; //すでにマップされているかチェック if (CollectionUtility.HasReference(mappedControls, ctrl)) { continue; } //コントロールドライバ検索 var driver = DriverCreatorUtils.GetDriverTypeFullName(ctrl, DriverCreatorAdapter.TypeFullNameAndControlDriver); if (!string.IsNullOrEmpty(driver)) { var name = _customNameGenerator.MakeDriverPropName(ctrl, string.Empty, names); var typeName = DriverCreatorUtils.GetTypeName(driver); var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver); var getter = MakeCodeGetFromControls(mdiChildrenIndex, root, ctrl.GetType(), next, out var nogood); DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, getter, ctrl); var code = $"public {typeName} {name} => new {typeName}({getter});"; if (nogood) { code += $" {TodoComment}"; } controlAndDefines.Add(new ControlAndDefine(ctrl, code)); mappedControls.Add(ctrl); if (!driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } } else if (!(ctrl is UserControl)) { //さらに下の階層を検索 CreateDriverInfoFindFromControlTree(mdiChildrenIndex, root, ctrl, driverInfo, controlAndDefines, mappedControls, names, next.ToArray(), fileName); } } }
internal static void PickupChildren(UIElement ctrl) { foreach (UIElement e in GetChildren(ctrl)) { var driver = DriverCreatorUtils.GetDriverTypeFullName(e, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver, DriverCreatorAdapter.TypeFullNameAndWindowDriver, out var searchDescendantUserControls); if (!string.IsNullOrEmpty(driver)) { DriverCreatorAdapter.AddDriverElements(e); } } }
public void CreateItemsControlDriver(string driverName, ItemsControl ctrl) { if (ctrl.Items.Count == 0) { return; } var item = ctrl.ItemContainerGenerator.ContainerFromIndex(0); var info = CreateDriverInfo(item, driverName + ".cs"); DriverCreatorAdapter.AddCode(driverName + ".cs", GenerateCode(null, item, DriverCreatorAdapter.SelectedNamespace, driverName, info.Usings, info.Members, new List <Type>()), info.Target); }
public void CreateControlDriver(UIElement root) { var driverName = root.GetType().Name + "Driver"; var generatorName = driverName + "Generator"; var driverCode = @"using Codeer.Friendly; using Codeer.Friendly.Dynamic; using Codeer.Friendly.Windows; using Codeer.Friendly.Windows.Grasp; using Codeer.TestAssistant.GeneratorToolKit; using RM.Friendly.WPFStandardControls; using System; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace [*namespace] { [ControlDriver(TypeFullName = ""{typefullname}"", Priority = 2)] public class {driverName} : WPFUIElement { public {driverName}(AppVar appVar) : base(appVar) { } } } "; DriverCreatorAdapter.AddCode($"{driverName}.cs", driverCode.Replace("{typefullname}", root.GetType().FullName).Replace("{driverName}", driverName), root); var generatorCode = @"using System; using System.Windows; using Codeer.TestAssistant.GeneratorToolKit; namespace [*namespace] { [CaptureCodeGenerator(""[*namespace.{driverName}]"")] public class {generatorName} : CaptureCodeGeneratorBase { UIElement _element; protected override void Attach() { _element = (UIElement)ControlObject; } protected override void Detach() { } } } "; DriverCreatorAdapter.AddCode($"{generatorName}.cs", generatorCode.Replace("{generatorName}", generatorName).Replace("{driverName}", driverName), root); }
public void GenerateCode(object targetControl, DriverDesignInfo info) { var code = GenerateCodeCore((UIElement)targetControl, info); var fileName = $"{info.ClassName}.cs"; DriverCreatorAdapter.AddCode(fileName, code, targetControl); //行選択でのツリーとの連動用 foreach (var e in info.Properties) { DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, e.Identify, e.Element); } }
private void CreateDriverInfoFindFromControlTree(DependencyObject target, DriverInfo <DependencyObject> driverInfo, List <ControlAndDefine> controlAndDefines, List <DependencyObject> mappedControls, List <string> names, string fileName) { var logical = WPFUtility.GetLogicalTreeDescendants(target, true, true, 0); var visual = WPFUtility.GetVisualTreeDescendants(target, true, true, 0); var logicalForGetter = WPFUtility.GetLogicalTreeDescendants(target, false, false, 0); var visualForGetter = WPFUtility.GetVisualTreeDescendants(target, false, false, 0); foreach (var tree in new[] { logical, visual }) { var cache = new BindingExpressionCache(); foreach (var ctrl in tree) { if (ReferenceEquals(ctrl, target)) { continue; } //すでにマップされているかチェック if (CollectionUtility.HasReference(mappedControls, ctrl)) { continue; } //コントロールドライバ検索 var driver = DriverCreatorUtils.GetDriverTypeFullName(ctrl, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver); if (!string.IsNullOrEmpty(driver)) { var name = _customNameGenerator.MakeDriverPropName(ctrl, string.Empty, names); var typeName = DriverCreatorUtils.GetTypeName(driver); var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver); var getter = MakeCodeGetFromTree("Core.", logicalForGetter, visualForGetter, ctrl, cache, driverInfo.Usings, out var nogood); var code = $"public {typeName} {name} => {getter};"; if (nogood) { code += $" {TodoComment}"; } controlAndDefines.Add(new ControlAndDefine(ctrl, name, code)); mappedControls.Add(ctrl); DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, getter, ctrl); if (!driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } } } } }
public Dictionary <string, MenuAction> GetAction(object target, WindowAnalysisTreeInfo info) { var dic = new Dictionary <string, MenuAction>(); var grid = target as DataGrid; if (grid != null) { dic["Create Grid Column Define to Output."] = () => { AnalyzeWindow.Output.Show(); AnalyzeWindow.Output.WriteLine("public class GridColumns"); AnalyzeWindow.Output.WriteLine("{"); int i = 0; foreach (var e in grid.Columns) { AnalyzeWindow.Output.WriteLine(" const int " + e.Header.ToString().Replace(" ", "") + " = " + i++ + ";"); } AnalyzeWindow.Output.WriteLine("}"); }; dic["Create Grid Column Define to Code View."] = () => { var code = new StringBuilder(); code.AppendLine("namespace " + DriverCreatorAdapter.SelectedNamespace); code.AppendLine("{"); code.AppendLine(" public class GridColumns"); code.AppendLine(" {"); int i = 0; foreach (var e in grid.Columns) { code.AppendLine(" const int " + e.Header.ToString().Replace(" ", "") + " = " + i++ + ";"); } code.AppendLine(" }"); code.AppendLine("}"); DriverCreatorAdapter.AddCode("GridColumns.cs", code.ToString(), target); }; } return(dic); }
public void CreateDriver(DependencyObject root) { //FormとUserControlを全取得 var recursiveCheck = new List <DependencyObject>(); var targets = new Dictionary <Type, DependencyObject>(); //ルートはUserControlでなくても指定されたらUserControlDriverを作れるようにする if (!(root is UserControl)) { targets[root.GetType()] = root; } var getFromControlTreeOnly = new List <Type>(); GetAllWindowAndUserControl(false, root, targets, getFromControlTreeOnly, recursiveCheck); //ドライバ情報を取得 var driverInfos = new Dictionary <Type, DriverInfo <DependencyObject> >(); foreach (var e in targets) { var fileName = $"{_driverTypeNameManager.MakeDriverType(e.Value)}.cs"; var info = CreateDriverInfo(e.Value, fileName); driverInfos[e.Key] = info; } //変換 foreach (var e in driverInfos) { var driverName = _driverTypeNameManager.MakeDriverType(e.Value.Target, out var nameSpace); if (string.IsNullOrEmpty(nameSpace)) { nameSpace = DriverCreatorAdapter.SelectedNamespace; } //コード生成 DriverCreatorAdapter.AddCode($"{driverName}.cs", GenerateCode(root, e.Value.Target, nameSpace, driverName, e.Value.Usings, e.Value.Members, getFromControlTreeOnly), e.Value.Target); } }
private DriverInfo <Control> CreateDriverInfo(Control targetControl, string fileName) { var driverInfo = new DriverInfo <Control>(targetControl); var mappedControls = new List <Control>(); var names = new List <string>(); var ancesters = WindowUtilityInTarget.GetAncesters(targetControl); var controlAndDefines = new List <ControlAndDefine>(); //フィールドから検索 foreach (var field in GetFields(targetControl)) { //たまにフィールドに親を持っているのがいるのではじく if (CollectionUtility.HasReference(ancesters, field.Control)) { continue; } //不正なフィールド名のものは取得できない if (!_dom.IsValidIdentifier(field.Name)) { continue; } //すでにマップされているかチェック if (CollectionUtility.HasReference(mappedControls, field.Control)) { continue; } //コントロールドライバ var driver = DriverCreatorUtils.GetDriverTypeFullName(field.Control, DriverCreatorAdapter.TypeFullNameAndControlDriver); if (!string.IsNullOrEmpty(driver)) { mappedControls.Add(field.Control); var typeName = DriverCreatorUtils.GetTypeName(driver); var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver); var name = _customNameGenerator.MakeDriverPropName(field.Control, field.Name, names); var key = $"Core.Dynamic().{field.Name}"; controlAndDefines.Add(new ControlAndDefine(field.Control, $"public {typeName} {name} => new {typeName}({key});")); DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, field.Control); if (!driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } } //ユーザーコントロールドライバ else if (field.Control is UserControl) { mappedControls.Add(field.Control); var name = _customNameGenerator.MakeDriverPropName(field.Control, field.Name, names); names.Add(name); var typeName = _driverTypeNameManager.MakeDriverType(field.Control, out var nameSpace); if (!string.IsNullOrEmpty(nameSpace) && (nameSpace != DriverCreatorAdapter.SelectedNamespace) && !driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } var key = $"new WindowControl(Core.Dynamic().{field.Name}"; controlAndDefines.Add(new ControlAndDefine(field.Control, $"public {typeName} {name} => new {typeName}({key}));")); DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, field.Control); } } //フィールド上に現れないオブジェクトを検索 CreateDriverInfoFindFromControlTree(-1, targetControl, targetControl, driverInfo, controlAndDefines, mappedControls, names, new int[0], fileName); //Sortのロジックがイマイチわかっていない。念のため try { // タブオーダー順のコントロールリスト取得 var controlList = GetTabOrderChildControls(targetControl); // フィールドをタブオーダーでソート controlAndDefines.Sort((l, r) => controlList.IndexOf(l.Control) - controlList.IndexOf(r.Control)); } catch { } foreach (var e in controlAndDefines) { driverInfo.Members.Add(e.Define); } return(driverInfo); }
private DriverInfo <DependencyObject> CreateDriverInfo(DependencyObject targetControl, string fileName) { var driverInfo = new DriverInfo <DependencyObject>(targetControl); var mappedControls = new List <DependencyObject>(); var names = new List <string>(); var ancesters = WPFUtility.GetVisualTreeAncestor(targetControl); var controlAndDefines = new List <ControlAndDefine>(); //フィールドから検索 foreach (var e in GetFields(targetControl)) { //たまに親を持っているのがいるのではじく if (CollectionUtility.HasReference(ancesters, e.Control)) { continue; } //不正なフィールド名のものは取得できない if (!_dom.IsValidIdentifier(e.Name)) { continue; } //すでにマップされているかチェック if (CollectionUtility.HasReference(mappedControls, e.Control)) { continue; } //コントロールドライバ var driver = DriverCreatorUtils.GetDriverTypeFullName(e.Control, DriverCreatorAdapter.TypeFullNameAndControlDriver, DriverCreatorAdapter.TypeFullNameAndUserControlDriver); if (!string.IsNullOrEmpty(driver)) { mappedControls.Add(e.Control); var name = _customNameGenerator.MakeDriverPropName(e.Control, e.Name, names); var typeName = DriverCreatorUtils.GetTypeName(driver); var nameSpace = DriverCreatorUtils.GetTypeNamespace(driver); var key = $"Core.Dynamic().{e.Name}"; controlAndDefines.Add(new ControlAndDefine(e.Control, name, $"public {typeName} {name} => {key};")); DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, e.Control); if (!driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } } //ユーザーコントロールドライバ else if (((e.Control is UserControl) && (e.Control.GetType() != typeof(UserControl))) || ((e.Control is Page) && (e.Control.GetType() != typeof(Page)))) { mappedControls.Add(e.Control); var name = _customNameGenerator.MakeDriverPropName(e.Control, e.Name, names); var typeName = _driverTypeNameManager.MakeDriverType(e.Control, out var nameSpace); var key = $"Core.Dynamic().{e.Name}"; controlAndDefines.Add(new ControlAndDefine(e.Control, name, $"public {typeName} {name} => {key};")); if (!string.IsNullOrEmpty(nameSpace) && (nameSpace != DriverCreatorAdapter.SelectedNamespace) && !driverInfo.Usings.Contains(nameSpace)) { driverInfo.Usings.Add(nameSpace); } DriverCreatorAdapter.AddCodeLineSelectInfo(fileName, key, e.Control); } } //フィールド上に現れないオブジェクトを検索 CreateDriverInfoFindFromControlTree(targetControl, driverInfo, controlAndDefines, mappedControls, names, fileName); //Sortのロジックがイマイチわかっていない。念のため try { // LogicalTree順のコントロールリスト取得 var controlList = WPFUtility.GetLogicalTreeDescendants(targetControl, true, true, 0); // フィールドをタブオーダーでソート controlAndDefines.Sort((l, r) => controlList.IndexOf(l.Control) - controlList.IndexOf(r.Control)); } catch { } //コンテキストメニュー特別処理 foreach (var e in controlAndDefines) { driverInfo.Members.Add(e.Define); var frameworkElement = e.Control as FrameworkElement; if (frameworkElement != null && frameworkElement.ContextMenu != null) { var core = (frameworkElement is Window || frameworkElement is UserControl || frameworkElement is Page) ? ".Core" : string.Empty; var code = $"public WPFContextMenu {e.Name}ContextMenu => new WPFContextMenu{{Target = {e.Name}{core}.AppVar}};"; driverInfo.Members.Add(code); } } return(driverInfo); }
internal static void CreateControlDriver(UIElement root) { var list = new List <Type>(); var type = root.GetType(); while (type != null) { list.Add(type); type = type.BaseType; if (type == null || type.Namespace.IndexOf(typeof(Window).Namespace) == 0) { break; } } var driverName = string.Empty; var targetType = root.GetType(); if (1 == list.Count) { driverName = list[0].Name + "Driver"; } else { using (var dlg = new TypeSelectForm(list.ToArray())) { if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } driverName = dlg.SelectedType.Name + "Driver"; targetType = dlg.SelectedType; } } var generatorName = driverName + "Generator"; var driverCode = @"using Codeer.Friendly; using Codeer.Friendly.Dynamic; using Codeer.Friendly.Windows; using Codeer.Friendly.Windows.Grasp; using Codeer.TestAssistant.GeneratorToolKit; using RM.Friendly.WPFStandardControls; using System; using System.Windows.Controls; using System.Windows.Controls.Primitives; namespace [*namespace] { [ControlDriver(TypeFullName = ""{typefullname}"", Priority = 2)] public class {driverName} : WPFUIElement { public {driverName}(AppVar appVar) : base(appVar) { } } } "; DriverCreatorAdapter.AddCode($"{driverName}.cs", driverCode.Replace("{typefullname}", targetType.FullName).Replace("{driverName}", driverName), root); var generatorCode = @"using System; using System.Windows; using Codeer.TestAssistant.GeneratorToolKit; namespace [*namespace] { [CaptureCodeGenerator(""[*namespace.{driverName}]"")] public class {generatorName} : CaptureCodeGeneratorBase { UIElement _element; protected override void Attach() { _element = (UIElement)ControlObject; } protected override void Detach() { } } } "; DriverCreatorAdapter.AddCode($"{generatorName}.cs", generatorCode.Replace("{generatorName}", generatorName).Replace("{driverName}", driverName), root); }