#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; using System.Collections.Specialized; using System.Diagnostics; using System.Linq; using System.Windows.Forms; using MiniSqlQuery.Commands; using MiniSqlQuery.Core; using MiniSqlQuery.Core.Commands; namespace MiniSqlQuery.PlugIns { /// The core application plug in. public class CoreApplicationPlugIn : PluginLoaderBase { /// The _timer. private Timer _timer; /// Initializes a new instance of the class. public CoreApplicationPlugIn() : base("迷你SQL查询核心", "用于设置迷你SQL查询核心功能的插件。", 1) { } /// Iinitialize the plug in. public override void InitializePlugIn() { IApplicationServices services = Services; IHostWindow hostWindow = services.HostWindow; services.RegisterSingletonComponent("CompletionProvider"); services.RegisterEditor(new FileEditorDescriptor("Default text editor", "default-editor")); services.RegisterEditor(new FileEditorDescriptor("SQL Editor", "sql-editor", "sql")); services.RegisterEditor(new FileEditorDescriptor("C# Editor", "cs-editor", "cs")); services.RegisterEditor(new FileEditorDescriptor("VB/VB.NET Editor", "vb-editor", "vb")); services.RegisterEditor(new FileEditorDescriptor("XML Editor", "xml-editor", "xml")); services.RegisterEditor(new FileEditorDescriptor("HTML Editor", "htm-editor", "htm", "html")); services.RegisterEditor(new FileEditorDescriptor("Text Editor", "txt-editor", "txt")); services.RegisterComponent("NewFileForm"); services.RegisterComponent("OptionsForm"); services.RegisterSingletonComponent("MostRecentFilesService"); services.RegisterConfigurationObject(); ToolStripMenuItem fileMenu = hostWindow.GetMenuItem("File"); ToolStripMenuItem editMenu = hostWindow.GetMenuItem("edit"); ToolStripMenuItem queryMenu = hostWindow.GetMenuItem("query"); ToolStripMenuItem helpMenu = hostWindow.GetMenuItem("help"); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); fileMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); queryMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); // editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); editMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); // get the new item and make in invisible - the shortcut key is still available etc ;-) ToolStripItem item = editMenu.DropDownItems["SetLeftPasteAroundSelectionCommandToolStripMenuItem"]; item.Visible = false; item = editMenu.DropDownItems["SetRightPasteAroundSelectionCommandToolStripMenuItem"]; item.Visible = false; helpMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); helpMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); helpMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); helpMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItemSeparator()); helpMenu.DropDownItems.Add(CommandControlBuilder.CreateToolStripMenuItem()); CommandControlBuilder.MonitorMenuItemsOpeningForEnabling(hostWindow.Instance.MainMenuStrip); // toolstrip hostWindow.AddToolStripCommand(0); hostWindow.AddToolStripCommand(1); hostWindow.AddToolStripCommand(2); hostWindow.AddToolStripSeperator(3); hostWindow.AddToolStripCommand(4); hostWindow.AddToolStripCommand(5); hostWindow.AddToolStripSeperator(6); hostWindow.AddToolStripSeperator(null); hostWindow.AddToolStripCommand(null); hostWindow.AddPluginCommand(); ConfigureMostRecentFileList(services); // watch tool strip enabled properties // by simply iterating each one every second or so we avoid the need to track via events _timer = new Timer(); _timer.Interval = 1000; _timer.Tick += TimerTick; _timer.Enabled = true; } protected void ConfigureMostRecentFileList(IApplicationServices services) { // get the files out of the settings and register them var mostRecentFilesService = services.Resolve(); if (services.Settings.MostRecentFiles != null) { foreach (string mostRecentFile in services.Settings.MostRecentFiles) { mostRecentFilesService.Filenames.Add(mostRecentFile); } } // watch for changes mostRecentFilesService.MostRecentFilesChanged += mostRecentFilesService_MostRecentFilesChanged; // need to manually call the update - only required on first load ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); ((OpenRecentFileCommand)CommandManager.GetCommandInstance()).UpdateName(); } /// The unload plug in. public override void UnloadPlugIn() { if (_timer != null) { _timer.Dispose(); } } /// Called frequently to run through all the commands on the toolstrip and check their enabled state. /// Marked as "DebuggerNonUserCode" because it can get in the way of a debug sesssion. /// The sender. /// The instance containing the event data. [DebuggerNonUserCode] private void TimerTick(object sender, EventArgs e) { try { _timer.Enabled = false; foreach (ToolStripItem item in Services.HostWindow.ToolStrip.Items) { ICommand cmd = item.Tag as ICommand; if (cmd != null) { item.Enabled = cmd.Enabled; } } } finally { _timer.Enabled = true; } } static void mostRecentFilesService_MostRecentFilesChanged(object sender, MostRecentFilesChangedEventArgs e) { var services = ApplicationServices.Instance; var mostRecentFilesService = services.Resolve(); if (services.Settings.MostRecentFiles == null) { services.Settings.MostRecentFiles = new StringCollection(); } else { services.Settings.MostRecentFiles.Clear(); } services.Settings.MostRecentFiles.AddRange(mostRecentFilesService.Filenames.ToArray()); } } }