#region License
#endregion
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace MiniSqlQuery.Core
{
public class CommandControlBuilder
{
[DebuggerNonUserCode]
public static void CommandItemClick(object sender, EventArgs e)
{
var item = sender as ToolStripItem;
if (item != null)
{
var cmd = item.Tag as ICommand;
if (cmd != null)
{
cmd.Execute();
}
}
}
public static LinkLabel CreateLinkLabel<TCommand>() where TCommand : ICommand, new()
{
var linkLabel = new LinkLabel();
var cmd = CommandManager.GetCommandInstance<TCommand>();
linkLabel.AutoSize = true;
linkLabel.Name = cmd.GetType().Name + "LinkLabel";
linkLabel.TabStop = true;
linkLabel.Text = cmd.Name.Replace("&", string.Empty);
linkLabel.Tag = cmd;
linkLabel.Padding = new Padding(4);
linkLabel.LinkClicked += LinkLabelLinkClicked;
return linkLabel;
}
public static ToolStripButton CreateToolStripButton<TCommand>() where TCommand : ICommand, new()
{
var button = new ToolStripButton();
var cmd = CommandManager.GetCommandInstance<TCommand>();
button.DisplayStyle = ToolStripItemDisplayStyle.Image;
button.Image = cmd.SmallImage;
button.ImageTransparentColor = Color.Magenta;
button.Name = cmd.GetType().Name + "ToolStripButton";
button.Tag = cmd;
button.Text = cmd.Name;
button.Click += CommandItemClick;
return button;
}
public static ToolStripMenuItem CreateToolStripMenuItem<TCommand>() where TCommand : ICommand, new()
{
var menuItem = new ToolStripMenuItem();
var cmd = CommandManager.GetCommandInstance<TCommand>();
menuItem.Name = cmd.GetType().Name + "ToolStripMenuItem";
menuItem.Text = cmd.Name;
menuItem.Tag = cmd;
menuItem.ShortcutKeys = cmd.ShortcutKeys;
menuItem.Image = cmd.SmallImage;
menuItem.Click += CommandItemClick;
cmd.Host = menuItem;
return menuItem;
}
public static ToolStripSeparator CreateToolStripMenuItemSeparator()
{
return new ToolStripSeparator();
}
public static void MonitorMenuItemsOpeningForEnabling(ToolStrip menuStrip)
{
if (menuStrip is ContextMenuStrip || menuStrip is MenuStrip)
{
foreach (ToolStripItem item in menuStrip.Items)
{
var topLevelMenu = item as ToolStripMenuItem;
if (topLevelMenu != null)
{
topLevelMenu.DropDownOpening += TopLevelMenuDropDownOpening;
topLevelMenu.DropDownClosed += TopLevelMenuDropDownClosed;
}
}
}
}
[DebuggerNonUserCode]
public static void TopLevelMenuDropDownOpening(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
foreach (ToolStripItem item in menuItem.DropDownItems)
{
var cmd = item.Tag as ICommand;
if (cmd != null)
{
item.Enabled = cmd.Enabled;
}
}
}
}
private static void LinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var linkLabel = sender as Control;
if (linkLabel != null)
{
var cmd = linkLabel.Tag as ICommand;
if (cmd != null)
{
cmd.Execute();
}
}
}
[DebuggerNonUserCode]
private static void TopLevelMenuDropDownClosed(object sender, EventArgs e)
{
var menuItem = sender as ToolStripMenuItem;
if (menuItem != null)
{
foreach (ToolStripItem item in menuItem.DropDownItems)
{
var cmd = item.Tag as ICommand;
if (cmd != null)
{
item.Enabled = true;
}
}
}
}
}
}
|