From 915c81151ff627f7da5fee589d6fe497b88eec30 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Fri, 23 Feb 2018 12:51:16 +0100 Subject: [PATCH] instantiators dictionary no longer static but local to Interface --- CrowIDE/src/CrowIDE.cs | 4 +-- CrowIDE/src/ImlVisualEditor.cs | 4 +-- src/CompilerServices/CompilerServices.cs | 2 +- src/GraphicObjects/Docker.cs | 10 +++--- src/GraphicObjects/TemplatedControl.cs | 4 +-- src/GraphicObjects/TemplatedGroup.cs | 2 +- src/Instantiator.cs | 42 ++++++++++++++---------- src/Interface.cs | 26 +++++++-------- src/ItemTemplate.cs | 12 +++---- 9 files changed, 57 insertions(+), 49 deletions(-) diff --git a/CrowIDE/src/CrowIDE.cs b/CrowIDE/src/CrowIDE.cs index 905468a9..2e0f2901 100644 --- a/CrowIDE/src/CrowIDE.cs +++ b/CrowIDE/src/CrowIDE.cs @@ -65,7 +65,7 @@ namespace CrowIDE } void openFileDialog () { - AddWidget (instFileDlg.CreateInstance(CurrentInterface)).DataSource = this; + AddWidget (instFileDlg.CreateInstance()).DataSource = this; } void openOptionsDialog(){} void newFile() {} @@ -96,7 +96,7 @@ namespace CrowIDE base.OnLoad (e); instFileDlg = Instantiator.CreateFromImlFragment - (""); + (CurrentInterface, ""); initCommands (); diff --git a/CrowIDE/src/ImlVisualEditor.cs b/CrowIDE/src/ImlVisualEditor.cs index fe116f78..cf799cc7 100644 --- a/CrowIDE/src/ImlVisualEditor.cs +++ b/CrowIDE/src/ImlVisualEditor.cs @@ -144,7 +144,7 @@ namespace CrowIDE Instantiator iTmp; try { - iTmp = Instantiator.CreateFromImlFragment (imlSource); + iTmp = Instantiator.CreateFromImlFragment (imlVE, imlSource); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine (ex.ToString()); return; @@ -172,7 +172,7 @@ namespace CrowIDE try { imlVE.ClearInterface (); if (itor != null) - imlVE.AddWidget(itor.CreateInstance(imlVE)); + imlVE.AddWidget(itor.CreateInstance()); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine (ex.ToString()); diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index 1e7fab8e..eb8f2ecf 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -87,7 +87,7 @@ namespace Crow.IML internal static EventInfo eiLogicalParentChanged = typeof(GraphicObject).GetEvent("LogicalParentChanged"); internal static MethodInfo miIFaceLoad = typeof(Interface).GetMethod ("Load", BindingFlags.Instance | BindingFlags.Public); - internal static MethodInfo miGetITemp = typeof(Interface).GetMethod ("GetItemTemplate"); + internal static MethodInfo miGetITemp = typeof(Interface).GetMethod ("GetItemTemplate", BindingFlags.Instance | BindingFlags.Public); internal static MethodInfo miAddITemp = typeof(Dictionary).GetMethod ("set_Item", new Type[] { typeof(string), typeof(ItemTemplate) }); internal static MethodInfo miGetITempFromDic = typeof(Dictionary).GetMethod ("get_Item", new Type[] { typeof(string) }); diff --git a/src/GraphicObjects/Docker.cs b/src/GraphicObjects/Docker.cs index c52c094f..d34e5ba7 100644 --- a/src/GraphicObjects/Docker.cs +++ b/src/GraphicObjects/Docker.cs @@ -35,12 +35,12 @@ namespace Crow static Instantiator instStack, instSplit, instSpacer; #region CTOR static Docker () { - instStack = Instantiator.CreateFromImlFragment(@""); - instSplit = Instantiator.CreateFromImlFragment(@""); - instSpacer = Instantiator.CreateFromImlFragment(@""); } public Docker () : base () { + instStack = Instantiator.CreateFromImlFragment(CurrentInterface, @""); + instSplit = Instantiator.CreateFromImlFragment(CurrentInterface, @""); + instSpacer = Instantiator.CreateFromImlFragment(CurrentInterface, @""); } #endregion @@ -196,7 +196,7 @@ namespace Crow return; lock (CurrentInterface.UpdateMutex) { - Splitter splitter = instSplit.CreateInstance (CurrentInterface); + Splitter splitter = instSplit.CreateInstance (); dw.Resizable = false; dw.Left = dw.Top = 0; @@ -204,7 +204,7 @@ namespace Crow Rectangle r; if (mainStack == null) { - mainStack = instStack.CreateInstance (CurrentInterface); + mainStack = instStack.CreateInstance (); this.AddChild (mainStack); this.putWidgetOnBottom (mainStack); r = ClientRectangle; diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs index 34e4ce07..e0e77fc0 100644 --- a/src/GraphicObjects/TemplatedControl.cs +++ b/src/GraphicObjects/TemplatedControl.cs @@ -133,9 +133,9 @@ namespace Crow this.ClearTemplateBinding(); if (template == null) { - if (!Interface.DefaultTemplates.ContainsKey (this.GetType ().FullName)) + if (!CurrentInterface.DefaultTemplates.ContainsKey (this.GetType ().FullName)) throw new Exception (string.Format ("No default template found for '{0}'", this.GetType ().FullName)); - this.SetChild (CurrentInterface.Load (Interface.DefaultTemplates[this.GetType ().FullName])); + this.SetChild (CurrentInterface.Load (CurrentInterface.DefaultTemplates[this.GetType ().FullName])); }else this.SetChild (template); } diff --git a/src/GraphicObjects/TemplatedGroup.cs b/src/GraphicObjects/TemplatedGroup.cs index a6f1b151..cf378dbd 100644 --- a/src/GraphicObjects/TemplatedGroup.cs +++ b/src/GraphicObjects/TemplatedGroup.cs @@ -428,7 +428,7 @@ namespace Crow } lock (CurrentInterface.LayoutMutex) { - g = iTemp.CreateInstance(CurrentInterface); + g = iTemp.CreateInstance(); page.AddChild (g); // if (isPaged) g.LogicalParent = this; diff --git a/src/Instantiator.cs b/src/Instantiator.cs index 14e33bf0..2481206a 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -70,6 +70,7 @@ namespace Crow.IML public Type RootType; InstanciatorInvoker loader; + protected Interface iface; internal string sourcePath; @@ -77,14 +78,15 @@ namespace Crow.IML /// /// Initializes a new instance of the Instantiator class. /// - public Instantiator (string path) : this (Interface.GetStreamFromPath(path), path) { + public Instantiator (Interface _iface, string path) : this (_iface, Interface.GetStreamFromPath(path), path) { } /// /// Initializes a new instance of the Instantiator class. /// - public Instantiator (Stream stream, string srcPath = null) + public Instantiator (Interface _iface, Stream stream, string srcPath = null) { + iface = _iface; sourcePath = srcPath; #if DEBUG_LOAD Stopwatch loadingTime = new Stopwatch (); @@ -109,12 +111,14 @@ namespace Crow.IML /// Initializes a new instance of the Instantiator class with an already openned xml reader /// positionned on the start tag inside the itemTemplate /// - public Instantiator (XmlReader itr){ + public Instantiator (Interface _iface, XmlReader itr){ + iface = _iface; parseIML (itr); } //TODO:check if still used - public Instantiator (Type _root, InstanciatorInvoker _loader) + public Instantiator (Interface _iface, Type _root, InstanciatorInvoker _loader) { + iface = _iface; RootType = _root; loader = _loader; } @@ -123,11 +127,11 @@ namespace Crow.IML /// /// A new instantiator /// IML string - public static Instantiator CreateFromImlFragment (string fragment) + public static Instantiator CreateFromImlFragment (Interface _iface, string fragment) { try { using (Stream s = new MemoryStream (Encoding.UTF8.GetBytes (fragment))) { - return new Instantiator (s); + return new Instantiator (_iface, s); } } catch (Exception ex) { throw new Exception ("Error loading IML fragment:\n" + fragment + "\n", ex); @@ -141,7 +145,7 @@ namespace Crow.IML /// /// The new graphic object instance /// The interface to bind to - public GraphicObject CreateInstance(Interface iface){ + public GraphicObject CreateInstance(){ return loader (iface) as GraphicObject; } /// @@ -150,7 +154,7 @@ namespace Crow.IML /// /// The new T instance /// The interface to bind to - public T CreateInstance(Interface iface){ + public T CreateInstance(){ return (T)loader (iface); } List dsValueChangedDynMeths = new List(); @@ -242,16 +246,16 @@ namespace Crow.IML if (string.IsNullOrEmpty (path)) { itemTmpID += Guid.NewGuid ().ToString (); - Interface.Instantiators [itemTmpID] = - new ItemTemplate (new MemoryStream (Encoding.UTF8.GetBytes (reader.ReadInnerXml ())), dataTest, dataType, datas); + iface.Instantiators [itemTmpID] = + new ItemTemplate (iface, new MemoryStream (Encoding.UTF8.GetBytes (reader.ReadInnerXml ())), dataTest, dataType, datas); } else { if (!reader.IsEmptyElement) throw new Exception ("ItemTemplate with Path attribute set may not include sub nodes"); itemTmpID += path+dataType+datas; - if (!Interface.Instantiators.ContainsKey (itemTmpID)) - Interface.Instantiators [itemTmpID] = - new ItemTemplate (Interface.GetStreamFromPath (path), dataTest, dataType, datas); + if (!iface.Instantiators.ContainsKey (itemTmpID)) + iface.Instantiators [itemTmpID] = + new ItemTemplate (iface, Interface.GetStreamFromPath (path), dataTest, dataType, datas); } return new string [] { dataType, itemTmpID, datas, dataTest }; } @@ -299,7 +303,7 @@ namespace Crow.IML //try to load ItemTemplate(s) from ItemTemplate attribute of TemplatedGroup if (!string.IsNullOrEmpty (itemTemplatePath)) { //check if it is already loaded in cache as a single itemTemplate instantiator - if (Interface.Instantiators.ContainsKey (itemTemplatePath)) { + if (iface.Instantiators.ContainsKey (itemTemplatePath)) { itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "" }); } else { using (Stream stream = Interface.GetStreamFromPath (itemTemplatePath)) { @@ -312,8 +316,8 @@ namespace Crow.IML if (itr.NodeType == XmlNodeType.Element) { if (itr.Name != "ItemTemplate") { //the file contains a single template to use as default - Interface.Instantiators [itemTemplatePath] = - new ItemTemplate (itr); + iface.Instantiators [itemTemplatePath] = + new ItemTemplate (iface, itr); itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "", "TypeOf" }); break;//we should be at the end of the file } @@ -334,8 +338,12 @@ namespace Crow.IML foreach (string [] iTempId in itemTemplateIds) { ctx.il.Emit (OpCodes.Ldloc_0);//load TempControl ref ctx.il.Emit (OpCodes.Ldfld, CompilerServices.fldItemTemplates);//load ItemTemplates dic field + + //prepare argument to add itemTemplate to templated group dic of ItemTemplates ctx.il.Emit (OpCodes.Ldstr, iTempId [0]);//load key - ctx.il.Emit (OpCodes.Ldstr, iTempId [1]);//load value + //load itemTemplate + ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface + ctx.il.Emit (OpCodes.Ldstr, iTempId [1]);//load path ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miGetITemp); ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miAddITemp); diff --git a/src/Interface.cs b/src/Interface.cs index 3d2389b2..ee95eeb5 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -89,7 +89,6 @@ namespace Crow } loadCursors (); - findAvailableTemplates (); FontRenderingOptions = new FontOptions (); FontRenderingOptions.Antialias = Antialias.Subpixel; @@ -101,6 +100,7 @@ namespace Crow CurrentInterface = this; CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.InvariantCulture; loadStyling (); + findAvailableTemplates (); initTooltip (); initContextMenus (); } @@ -210,7 +210,7 @@ namespace Crow /// each IML and fragments (such as inline Templates) are compiled as a Dynamic Method stored here /// on the first instance creation of a IML item. /// - public static Dictionary Instantiators = new Dictionary(); + public Dictionary Instantiators = new Dictionary(); public List CrowThreads = new List();//used to monitor thread finished public DragDropEventArgs DragAndDropOperation = null; @@ -278,16 +278,16 @@ namespace Crow /// Resource ID must be 'fullClassName.template' (not case sensitive) /// Those found in application assembly have priority to the default Crow's one /// - public static Dictionary DefaultTemplates = new Dictionary(); + public Dictionary DefaultTemplates = new Dictionary(); /// Finds available default templates at startup - static void findAvailableTemplates(){ + void findAvailableTemplates(){ searchTemplatesOnDisk ("./"); string defTemplatePath = System.IO.Path.Combine (CrowConfigRoot, "defaultTemplates"); searchTemplatesOnDisk (defTemplatePath); searchTemplatesIn (Assembly.GetEntryAssembly ()); searchTemplatesIn (Assembly.GetExecutingAssembly ()); } - static void searchTemplatesOnDisk (string templatePath){ + void searchTemplatesOnDisk (string templatePath){ if (!Directory.Exists (templatePath)) return; foreach (string f in Directory.GetFiles(templatePath, "*.template",SearchOption.AllDirectories)) { @@ -297,7 +297,7 @@ namespace Crow DefaultTemplates [clsName] = f; } } - static void searchTemplatesIn(Assembly assembly){ + void searchTemplatesIn(Assembly assembly){ if (assembly == null) return; foreach (string resId in assembly @@ -344,7 +344,7 @@ namespace Crow /// a valid IML string public GraphicObject LoadIMLFragment (string imlFragment) { lock (UpdateMutex) { - GraphicObject tmp = Instantiator.CreateFromImlFragment (imlFragment).CreateInstance(this); + GraphicObject tmp = Instantiator.CreateFromImlFragment (this, imlFragment).CreateInstance(); AddWidget (tmp); return tmp; } @@ -370,7 +370,7 @@ namespace Crow public GraphicObject Load (string path) { //try { - return GetInstantiator (path).CreateInstance (this); + return GetInstantiator (path).CreateInstance (); //} catch (Exception ex) { // throw new Exception ("Error loading <" + path + ">:", ex); //} @@ -380,22 +380,22 @@ namespace Crow /// /// new Instantiator /// path of the iml file to load - public static Instantiator GetInstantiator(string path){ + public Instantiator GetInstantiator(string path){ if (!Instantiators.ContainsKey(path)) - Instantiators [path] = new Instantiator(path); + Instantiators [path] = new Instantiator(this, path); return Instantiators [path]; } /// Item templates are derived from instantiator, this function /// try to fetch the requested one in the cache or create it. /// They have additional properties for recursivity and /// custom display per item type - public static ItemTemplate GetItemTemplate(string path){ + public ItemTemplate GetItemTemplate(string path){ if (!Instantiators.ContainsKey(path)) - Instantiators [path] = new ItemTemplate(path); + Instantiators [path] = new ItemTemplate(this, path); return Instantiators [path] as ItemTemplate; } //TODO: .Net xml serialisation is no longer used, it has been replaced with instantiators - public static void Save (string file, T graphicObject) + public void Save (string file, T graphicObject) { XmlSerializerNamespaces xn = new XmlSerializerNamespaces (); xn.Add ("", ""); diff --git a/src/ItemTemplate.cs b/src/ItemTemplate.cs index 5ede1d94..7eb9d752 100644 --- a/src/ItemTemplate.cs +++ b/src/ItemTemplate.cs @@ -61,8 +61,8 @@ namespace Crow /// IML file to parse /// type this item will be choosen for, or member of the data item /// for hierarchical data, method to call for children fetching - public ItemTemplate(string path, string _dataTest = "TypeOf", string _dataType = null, string _fetchDataMethod = null) - : base(path) { + public ItemTemplate(Interface _iface, string path, string _dataTest = "TypeOf", string _dataType = null, string _fetchDataMethod = null) + : base(_iface, path) { strDataType = _dataType; fetchMethodName = _fetchDataMethod; dataTest = _dataTest; @@ -74,8 +74,8 @@ namespace Crow /// IML fragment to parse /// type this item will be choosen for, or member of the data item /// for hierarchical data, method to call for children fetching - public ItemTemplate (Stream ImlFragment, string _dataTest, string _dataType, string _fetchDataMethod) - :base(ImlFragment) + public ItemTemplate (Interface _iface, Stream ImlFragment, string _dataTest, string _dataType, string _fetchDataMethod) + :base(_iface, ImlFragment) { strDataType = _dataType; fetchMethodName = _fetchDataMethod; @@ -87,8 +87,8 @@ namespace Crow /// XML reader positionned before or at the root node /// type this item will be choosen for, or member of the data item /// for hierarchical data, method to call for children fetching - public ItemTemplate (XmlReader reader, string _dataTest = "TypeOf" , string _dataType = null, string _fetchDataMethod = null) - :base(reader) + public ItemTemplate (Interface _iface, XmlReader reader, string _dataTest = "TypeOf" , string _dataType = null, string _fetchDataMethod = null) + :base(_iface, reader) { strDataType = _dataType; fetchMethodName = _fetchDataMethod; -- 2.47.3