From 489544e3721e70308ec506b28a068761f48c63a8 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Tue, 23 Feb 2021 23:40:31 -0500 Subject: [PATCH] separate Application contexts (added/removed to Application) and other contexts (e.g. Universal Editor's EditorContext) --- MBS.Framework/Application.cs | 66 ++++++++++++++++++++++++++++++++++-- MBS.Framework/Context.cs | 26 ++++++++++---- 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/MBS.Framework/Application.cs b/MBS.Framework/Application.cs index 1082a7b..eee8db4 100644 --- a/MBS.Framework/Application.cs +++ b/MBS.Framework/Application.cs @@ -25,10 +25,69 @@ namespace MBS.Framework { public class Application { + public class ApplicationContextCollection + : Context.ContextCollection + { + protected override void ClearItems() + { + for (int i = 0; i < this.Count; i++) + { + Instance.RemoveContext(this[i]); + } + base.ClearItems(); + } + protected override void InsertItem(int index, Context item) + { + base.InsertItem(index, item); + Instance.AddContext(item); + } + protected override void RemoveItem(int index) + { + Instance.RemoveContext(this[index]); + base.RemoveItem(index); + } + } + public static Application Instance { get; set; } = null; + protected virtual Command FindCommandInternal(string commandID) + { + return null; + } + public Command FindCommand(string commandID) + { + Command cmd = FindCommandInternal(commandID); + if (cmd != null) return cmd; + + cmd = Commands[commandID]; + if (cmd != null) return cmd; + return null; + } + + protected virtual Context FindContextInternal(Guid contextID) + { + return null; + } + public Context FindContext(Guid contextID) + { + Context ctx = FindContextInternal(contextID); + if (ctx != null) return ctx; + + ctx = Contexts[contextID]; + if (ctx != null) return ctx; + return null; + } + public Guid ID { get; set; } = Guid.Empty; + protected virtual void EnableDisableCommandInternal(Command command, bool enable) + { + } + internal void _EnableDisableCommand(Command command, bool enable) + { + EnableDisableCommandInternal(command, enable); + } + private string _UniqueName = null; public string UniqueName { @@ -86,6 +145,7 @@ namespace MBS.Framework } return false; } + public void ExecuteCommand(string id, KeyValuePair[] namedParameters = null) { Command cmd = Commands[id]; @@ -154,7 +214,7 @@ namespace MBS.Framework /// Gets a collection of objects representing system, application, user, and custom contexts for settings and other items. /// /// A collection of objects representing cpublic for settings and other items. - public Context.ContextCollection Contexts { get; } = new Context.ContextCollection(); + public ApplicationContextCollection Contexts { get; } = new ApplicationContextCollection(); public ContextChangedEventHandler ContextAdded; protected virtual void OnContextAdded(ContextChangedEventArgs e) @@ -171,7 +231,7 @@ namespace MBS.Framework /// /// Handles updating the menus, toolbars, keyboard shortcuts, and other UI elements associated with the application . /// - internal void AddContext(Context ctx) + private void AddContext(Context ctx) { OnContextAdded(new ContextChangedEventArgs(ctx)); } @@ -179,7 +239,7 @@ namespace MBS.Framework /// /// Handles updating the menus, toolbars, keyboard shortcuts, and other UI elements associated with the application . /// - internal void RemoveContext(Context ctx) + private void RemoveContext(Context ctx) { OnContextRemoved(new ContextChangedEventArgs(ctx)); } diff --git a/MBS.Framework/Context.cs b/MBS.Framework/Context.cs index 188843e..2701b58 100644 --- a/MBS.Framework/Context.cs +++ b/MBS.Framework/Context.cs @@ -28,22 +28,36 @@ namespace MBS.Framework public class ContextCollection : System.Collections.ObjectModel.Collection { + private Dictionary _ItemsByID = new Dictionary(); + + public bool Contains(Guid contextID) + { + return _ItemsByID.ContainsKey(contextID); + } + + public Context this[Guid id] + { + get + { + if (_ItemsByID.ContainsKey(id)) + return _ItemsByID[id]; + return null; + } + } + protected override void ClearItems() { - for (int i = 0; i < this.Count; i++) - { - Application.Instance.RemoveContext(this[i]); - } base.ClearItems(); + _ItemsByID.Clear(); } protected override void InsertItem(int index, Context item) { base.InsertItem(index, item); - Application.Instance.AddContext(item); + _ItemsByID[item.ID] = item; } protected override void RemoveItem(int index) { - Application.Instance.RemoveContext(this[index]); + _ItemsByID.Remove(this[index].ID); base.RemoveItem(index); } }