#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System.Drawing; using System.Windows.Forms; namespace MiniSqlQuery.Core.Commands { /// /// A basic implementation of the interface. /// Represents a "command", typically a user action such as saving a file or executing a query. /// Inheritors must implement the abstract method . /// public abstract class CommandBase : ICommand { /// /// The host window. /// private IHostWindow _hostWindow; /// /// The command name. /// private string _name; /// /// Initializes a new instance of the class. /// The default value for is true, and is Keys.None. /// /// /// The name of the command. /// protected CommandBase(string name) { _name = name; ShortcutKeys = Keys.None; } /// /// Gets a value indicating whether this is enabled. /// /// true if enabled; otherwise, false (the default is true). public virtual bool Enabled { get { return true; } } public object Host { get; set; } /// /// The name of the command, used in menus and buttons. /// /// The name of the command. public virtual string Name { get { return _name; } protected set { if (_name == value) { return; } _name = value; // if the "host" of this command is a toolstring item, update its Text property. var item = Host as ToolStripItem; if (item != null) { item.Text = _name; } } } /// /// A reference to the application services to allow access to the other components. /// /// A reference to the instance. public IApplicationServices Services { get; set; } /// /// Gets a reference to the application settings. /// /// The application settings. public IApplicationSettings Settings { get; set; } /// /// Gets the menu shortcut keys for this command (e.g. Keys.F5). /// /// The shortcut keys for this command (the default is Keys.None). public virtual Keys ShortcutKeys { get; protected set; } /// /// Gets the shortcut key text to be displayed as help. /// /// The shortcut keys text. public string ShortcutKeysText { get; protected set; } /// /// Gets the "small image" associated with this control (for use on buttons or menu items). /// Use null (or Nothing in Visual Basic) if there is no image. /// /// The small image representing this command (the default is null). public virtual Image SmallImage { get; protected set; } /// /// Attempts to convert the current host windows active form to . /// /// A reference to the active base editor window, otherwise null. protected IEditor ActiveFormAsEditor { get { return Services.HostWindow.ActiveChildForm as IEditor; } } /// /// Attempts to convert the current host windows active form to . /// /// A reference to the active query editor window, otherwise null. protected IQueryEditor ActiveFormAsSqlQueryEditor { get { return Services.HostWindow.ActiveChildForm as IQueryEditor; } } /// /// Gets a reference to the host window. /// /// The host window. protected IHostWindow HostWindow { get { return _hostWindow ?? (_hostWindow = Services.HostWindow); } } /// /// Executes the command based on the current settings (abstract). /// /// /// If a commands value is false, a call to should have no effect /// (and not throw an exception). /// public abstract void Execute(); } }