using System; using System.Collections.Generic; namespace MBS.Framework { public class Command : ISupportsExtraData { public class CommandCollection : System.Collections.ObjectModel.Collection { public Command this[string ID] { get { foreach (Command command in this) { if (command.ID == ID) return command; } return null; } } } public Command() { } public Command(string id, string title, CommandItem[] items = null) { ID = id; Title = title; if (items != null) { for (int i = 0; i < items.Length; i++) { Items.Add(items[i]); } } } /// /// Determines whether this command displays as checked. /// public bool Checked { get; set; } = false; /// /// The ID of the command, used to reference it in . /// public string ID { get; set; } = String.Empty; /// /// The title of the command (including mnemonic prefix, if applicable). /// public string Title { get; set; } = String.Empty; private string mvarDefaultCommandID = String.Empty; public string DefaultCommandID { get { return mvarDefaultCommandID; } set { mvarDefaultCommandID = value; } } /// /// A that represents a predefined, platform-themed command. /// public StockType StockType { get; set; } = StockType.None; private string mvarImageFileName = String.Empty; /// /// The file name of the image to be displayed on the command. /// public string ImageFileName { get { return mvarImageFileName; } set { mvarImageFileName = value; } } /// /// The child s that are contained within this . /// public CommandItem.CommandItemCollection Items { get; } = new CommandItem.CommandItemCollection(); /// /// The event that is fired when the command is executed. /// public event EventHandler Executed; /// /// Determines whether this is enabled in all s and s /// that reference it. /// /// true if visible; otherwise, false. private bool _Enabled = true; public bool Enabled { get { return _Enabled; } set { _Enabled = value; Application.Instance._EnableDisableCommand(this, value); } } /// /// Determines whether this is visible in all s and s /// that reference it. /// /// true if visible; otherwise, false. public bool Visible { get; set; } /// /// Executes this . /// [Obsolete("Please use Application.ExecuteCommand. Command.Execute does not always work and will be removed in a future release.")] public void Execute() { if (Executed != null) Executed(this, EventArgs.Empty); } public override string ToString() { return String.Format("{0} [{1}]", ID, Title); } private Dictionary _extraData = new Dictionary(); public T GetExtraData(string key, T defaultValue = default(T)) { if (_extraData.ContainsKey(key)) { if (_extraData[key] is T) { return (T)_extraData[key]; } } return defaultValue; } public void SetExtraData(string key, T value) { _extraData[key] = value; } public object GetExtraData(string key, object defaultValue = null) { if (_extraData.ContainsKey(key)) return _extraData[key]; return defaultValue; } public void SetExtraData(string key, object value) { _extraData[key] = value; } } }