From 60bca4d1df9b4067506dec69e42d4bd5dff8e4e9 Mon Sep 17 00:00:00 2001 From: Konstantina Chremmou Date: Thu, 22 Aug 2013 16:48:16 +0100 Subject: [PATCH] MainWindow code tidy: removed methods constructing ToolStripMenuItems as unecessary. Signed-off-by: Konstantina Chremmou --- XenAdmin/Controls/TreeSearchBox.cs | 7 +- XenAdmin/MainWindow.cs | 36 +------- XenAdmin/TabPages/GeneralTabPage.cs | 129 ++++++++++++++-------------- XenAdmin/TabPages/HAPage.cs | 37 ++++---- 4 files changed, 88 insertions(+), 121 deletions(-) diff --git a/XenAdmin/Controls/TreeSearchBox.cs b/XenAdmin/Controls/TreeSearchBox.cs index 8440df0af..90c3c34ef 100644 --- a/XenAdmin/Controls/TreeSearchBox.cs +++ b/XenAdmin/Controls/TreeSearchBox.cs @@ -74,8 +74,11 @@ namespace XenAdmin.Controls private void AddMenuItem(string name, object tag, Image image, bool selected) { - ToolStripMenuItem menuItem = MainWindow.NewToolStripMenuItem(name, image, null); - menuItem.Tag = tag; + var menuItem = new ToolStripMenuItem(name) + { + Image = image, + Tag = tag + }; comboButtonViews.AddItem(menuItem); if (selected) comboButtonViews.SelectedItem = menuItem; diff --git a/XenAdmin/MainWindow.cs b/XenAdmin/MainWindow.cs index 55f618fce..980999fd7 100644 --- a/XenAdmin/MainWindow.cs +++ b/XenAdmin/MainWindow.cs @@ -2366,7 +2366,7 @@ namespace XenAdmin foreach (Form form in GetAuxWindows()) { - ToolStripMenuItem item = NewToolStripMenuItem(form.Text.EscapeAmpersands()); + ToolStripMenuItem item = new ToolStripMenuItem(form.Text.EscapeAmpersands()); item.Tag = form; windowToolStripMenuItem.DropDown.Items.Add(item); } @@ -3668,40 +3668,6 @@ namespace XenAdmin } } - /// - /// Create a new ToolStripMenuItem, setting the appropriate default font, and configuring it with the given text, icon, and click handler. - /// - /// The menu item text. Don't forget that this may need to have its ampersands escaped. - /// May be null, in which case no icon is set. - /// May be null, in which case no click handler is set. - /// The new ToolStripMenuItem - internal static ToolStripMenuItem NewToolStripMenuItem(string text, Image ico, EventHandler clickHandler) - { - ToolStripMenuItem m = new ToolStripMenuItem(text); - m.Font = Program.DefaultFont; - if (ico != null) - m.Image = ico; - if (clickHandler != null) - m.Click += clickHandler; - return m; - } - - /// - /// Equivalent to NewToolStripMenuItem(text, null, null). - /// - internal static ToolStripMenuItem NewToolStripMenuItem(string text) - { - return NewToolStripMenuItem(text, null, null); - } - - /// - /// Equivalent to NewToolStripMenuItem(text, null, clickHandler). - /// - internal static ToolStripMenuItem NewToolStripMenuItem(string text, EventHandler clickHandler) - { - return NewToolStripMenuItem(text, null, clickHandler); - } - internal void FocusTreeView() { treeView.Focus(); diff --git a/XenAdmin/TabPages/GeneralTabPage.cs b/XenAdmin/TabPages/GeneralTabPage.cs index a626b3a19..c3e7cb622 100644 --- a/XenAdmin/TabPages/GeneralTabPage.cs +++ b/XenAdmin/TabPages/GeneralTabPage.cs @@ -499,11 +499,12 @@ namespace XenAdmin.TabPages { PDSection s = pdSectionManagementInterfaces; - ToolStripMenuItem editValue = MainWindow.NewToolStripMenuItem(Messages.EDIT, Properties.Resources.edit_16, delegate(object sender, EventArgs e) - { - NetworkingProperties p = new NetworkingProperties(Host, null); - p.ShowDialog(Program.MainWindow); - }); + ToolStripMenuItem editValue = new ToolStripMenuItem(Messages.EDIT) { Image = Properties.Resources.edit_16 }; + editValue.Click += delegate + { + NetworkingProperties p = new NetworkingProperties(Host, null); + p.ShowDialog(Program.MainWindow); + }; List menuItems = new List(); menuItems.Add(editValue); @@ -556,12 +557,13 @@ namespace XenAdmin.TabPages foreach (CustomField customField in customFields) { - ToolStripMenuItem editValue = MainWindow.NewToolStripMenuItem(Messages.EDIT, Properties.Resources.edit_16, delegate(object sender, EventArgs e) - { - PropertiesDialog dialog = new PropertiesDialog(xenObject); - dialog.SelectPage(dialog.CustomFieldsEditPage); - dialog.ShowDialog(); - }); + ToolStripMenuItem editValue = new ToolStripMenuItem(Messages.EDIT){Image= Properties.Resources.edit_16}; + editValue.Click += delegate + { + PropertiesDialog dialog = new PropertiesDialog(xenObject); + dialog.SelectPage(dialog.CustomFieldsEditPage); + dialog.ShowDialog(); + }; List menuItems = new List(); menuItems.Add(editValue); CustomFieldWrapper cfWrapper = new CustomFieldWrapper(xenObject, customField.Definition); @@ -679,17 +681,18 @@ namespace XenAdmin.TabPages bool detached = !sr.HasPBDs; List menuItems = new List(); - ToolStripMenuItem repair = MainWindow.NewToolStripMenuItem(sr.NeedsUpgrading ? Messages.UPGRADE_SR : Messages.GENERAL_SR_CONTEXT_REPAIR, - Properties.Resources._000_StorageBroken_h32bit_16, - delegate(object sender, EventArgs e) + ToolStripMenuItem repair = new ToolStripMenuItem + { + Text = sr.NeedsUpgrading ? Messages.UPGRADE_SR : Messages.GENERAL_SR_CONTEXT_REPAIR, + Image = Properties.Resources._000_StorageBroken_h32bit_16 + }; + repair.Click += delegate { if (sr.NeedsUpgrading) - { new UpgradeSRCommand(Program.MainWindow.CommandInterface, sr).Execute(); - } else Program.MainWindow.ShowPerConnectionWizard(xenObject.Connection, new RepairSRDialog(sr)); - }); + }; menuItems.Add(repair); if (broken && !detached) @@ -932,19 +935,19 @@ namespace XenAdmin.TabPages if (info.ContainsKey("expiry")) { - ToolStripMenuItem editItem = MainWindow.NewToolStripMenuItem(Messages.LAUNCH_LICENSE_MANAGER, delegate - { - if(LicenseLauncher != null) + ToolStripMenuItem editItem = new ToolStripMenuItem(Messages.LAUNCH_LICENSE_MANAGER); + editItem.Click += delegate { - LicenseLauncher.Parent = Program.MainWindow; - LicenseLauncher.LaunchIfRequired(false, ConnectionsManager.XenConnections); - } - - }); + if (LicenseLauncher != null) + { + LicenseLauncher.Parent = Program.MainWindow; + LicenseLauncher.LaunchIfRequired(false, ConnectionsManager.XenConnections); + } + }; GeneralTabLicenseStatusStringifier ss = new GeneralTabLicenseStatusStringifier(licenseStatus); s.AddEntry(Messages.LICENSE_STATUS, ss.ExpiryStatus, new List(new [] { editItem })); - s.AddEntry(FriendlyName("host.license_params-expiry"), ss.ExpiryDate, new List(new ToolStripMenuItem[] { editItem })); + s.AddEntry(FriendlyName("host.license_params-expiry"), ss.ExpiryDate, new List(new [] { editItem })); info.Remove("expiry"); } @@ -1094,31 +1097,33 @@ namespace XenAdmin.TabPages } else if (!host.enabled) { + var item = new ToolStripMenuItem(Messages.EXIT_MAINTENANCE_MODE); + item.Click += delegate + { + new HostMaintenanceModeCommand(Program.MainWindow.CommandInterface, host, + HostMaintenanceModeCommandParameter.Exit).Execute(); + }; s.AddEntry(FriendlyName("host.enabled"), host.MaintenanceMode ? Messages.HOST_IN_MAINTENANCE_MODE : Messages.DISABLED, - new List(new ToolStripMenuItem[] { - MainWindow.NewToolStripMenuItem(Messages.EXIT_MAINTENANCE_MODE, delegate(object sender, EventArgs e) - { - new HostMaintenanceModeCommand(Program.MainWindow.CommandInterface, host, HostMaintenanceModeCommandParameter.Exit).Execute(); - }) - }), + new List(new[] { item }), Color.Red); } else { + var item = new ToolStripMenuItem(Messages.ENTER_MAINTENANCE_MODE); + item.Click += delegate + { + new HostMaintenanceModeCommand(Program.MainWindow.CommandInterface, host, + HostMaintenanceModeCommandParameter.Enter).Execute(); + }; s.AddEntry(FriendlyName("host.enabled"), Messages.YES, - new List(new ToolStripMenuItem[] { - MainWindow.NewToolStripMenuItem(Messages.ENTER_MAINTENANCE_MODE, delegate(object sender, EventArgs e) - { - new HostMaintenanceModeCommand(Program.MainWindow.CommandInterface, host, HostMaintenanceModeCommandParameter.Enter).Execute(); - }) - })); + new List(new [] {item})); } s.AddEntry(FriendlyName("host.iscsi_iqn"), host.iscsi_iqn, - new List(new ToolStripMenuItem[] { EditMenuItem("GeneralEditPage", "txtIQN") })); + new List(new [] { EditMenuItem("GeneralEditPage", "txtIQN") })); s.AddEntry(FriendlyName("host.log_destination"), host.SysLogDestination ?? Messages.HOST_LOG_DESTINATION_LOCAL, - new List(new ToolStripMenuItem[] { EditMenuItem("LogDestinationEditPage", "localRadioButton") })); + new List(new [] { EditMenuItem("LogDestinationEditPage", "localRadioButton") })); PrettyTimeSpan uptime = host.Uptime; PrettyTimeSpan agentUptime = host.AgentUptime; @@ -1144,12 +1149,13 @@ namespace XenAdmin.TabPages var appl = vm.Connection.Resolve(vm.appliance); if (appl != null) { - ToolStripMenuItem applProperties = MainWindow.NewToolStripMenuItem(Messages.VM_APPLIANCE_PROPERTIES, - (sender, e) => - { - using (PropertiesDialog propertiesDialog = new PropertiesDialog(appl)) - propertiesDialog.ShowDialog(this); - }); + var applProperties = new ToolStripMenuItem(Messages.VM_APPLIANCE_PROPERTIES); + applProperties.Click += + (sender, e) => + { + using (PropertiesDialog propertiesDialog = new PropertiesDialog(appl)) + propertiesDialog.ShowDialog(this); + }; s.AddEntryLink(Messages.VM_APPLIANCE, appl.Name, new List(new[] { applProperties }), () => @@ -1177,13 +1183,14 @@ namespace XenAdmin.TabPages { if (InstallToolsCommand.CanExecute(vm)) { - ToolStripMenuItem installtools = MainWindow.NewToolStripMenuItem( - Messages.INSTALL_XENSERVER_TOOLS_DOTS, delegate(object sender, EventArgs e) + var installtools = new ToolStripMenuItem(Messages.INSTALL_XENSERVER_TOOLS_DOTS); + installtools.Click += delegate { new InstallToolsCommand(Program.MainWindow.CommandInterface, vm).Execute(); - }); + }; s.AddEntryLink(FriendlyName("VM.VirtualizationState"), vm.VirtualisationStatusString, - new List(new ToolStripMenuItem[] { installtools }), new InstallToolsCommand(Program.MainWindow.CommandInterface, vm)); + new List(new [] { installtools }), + new InstallToolsCommand(Program.MainWindow.CommandInterface, vm)); } else { @@ -1369,20 +1376,18 @@ namespace XenAdmin.TabPages private void GenTagRow(PDSection s) { - List toolStrip = new List(new ToolStripMenuItem[] { EditMenuItem("GeneralEditPage", "") }); + List toolStrip = new List(new [] { EditMenuItem("GeneralEditPage", "") }); string[] tags = Tags.GetTags(xenObject); if (tags != null && tags.Length > 0) { - ToolStripMenuItem goToTag = MainWindow.NewToolStripMenuItem(Messages.VIEW_TAG_MENU_OPTION); + ToolStripMenuItem goToTag = new ToolStripMenuItem(Messages.VIEW_TAG_MENU_OPTION); foreach (string tag in tags) { - goToTag.DropDownItems.Add(MainWindow.NewToolStripMenuItem(tag.Ellipsise(30), - delegate(object sender, EventArgs e) - { - Program.MainWindow.SearchForTag(tag); - })); + var item = new ToolStripMenuItem(tag.Ellipsise(30)); + item.Click += delegate { Program.MainWindow.SearchForTag(tag); }; + goToTag.DropDownItems.Add(item); } toolStrip.Insert(0, goToTag); s.AddEntry(Messages.TAGS, TagsString(), toolStrip); @@ -1407,13 +1412,9 @@ namespace XenAdmin.TabPages List menuItems = new List(); if (xenObject.Path != "") { - menuItems.Add( - MainWindow.NewToolStripMenuItem(Messages.VIEW_FOLDER_MENU_OPTION, - delegate(object sender, EventArgs e) - { - Program.MainWindow.SearchForFolder(xenObject.Path); - }) - ); + var item = new ToolStripMenuItem(Messages.VIEW_FOLDER_MENU_OPTION); + item.Click += delegate { Program.MainWindow.SearchForFolder(xenObject.Path); }; + menuItems.Add(item); } menuItems.Add(EditMenuItem("GeneralEditPage", "")); s.AddEntry( diff --git a/XenAdmin/TabPages/HAPage.cs b/XenAdmin/TabPages/HAPage.cs index 85fd1b23e..dfb0484e9 100644 --- a/XenAdmin/TabPages/HAPage.cs +++ b/XenAdmin/TabPages/HAPage.cs @@ -329,10 +329,8 @@ namespace XenAdmin.TabPages if (redBold) { newChild.Items[1].ForeColor = Color.Red; - ToolStripMenuItem editHa = MainWindow.NewToolStripMenuItem(Messages.CONFIGURE_HA_ELLIPSIS, delegate(object sender, EventArgs e) - { - EditHA(pool); - }); + ToolStripMenuItem editHa = new ToolStripMenuItem(Messages.CONFIGURE_HA_ELLIPSIS); + editHa.Click += delegate { EditHA(pool); }; newChild.MenuItems.Add(editHa); newChild.DefaultMenuItem = editHa; } @@ -351,10 +349,8 @@ namespace XenAdmin.TabPages if (redBold) { newChild.Items[1].ForeColor = Color.Red; - ToolStripMenuItem editHa = MainWindow.NewToolStripMenuItem(Messages.CONFIGURE_HA_ELLIPSIS, delegate(object sender, EventArgs e) - { - EditHA(pool); - }); + ToolStripMenuItem editHa = new ToolStripMenuItem(Messages.CONFIGURE_HA_ELLIPSIS); + editHa.Click += delegate { EditHA(pool); }; newChild.MenuItems.Add(editHa); newChild.DefaultMenuItem = editHa; } @@ -575,19 +571,20 @@ namespace XenAdmin.TabPages { menu.Items.Add(item); } - ToolStripMenuItem copyItem = MainWindow.NewToolStripMenuItem(Messages.COPY, Properties.Resources.copy_16, delegate(object sender2, EventArgs eve) - { - try + ToolStripMenuItem copyItem = new ToolStripMenuItem(Messages.COPY) { Image = Properties.Resources.copy_16 }; + copyItem.Click += delegate { - String text = Helpers.ToWindowsLineEndings(e.Item.Tag != null ? e.Item.Tag.ToString() : e.Item.Text); - Clipboard.SetText(text); - } - catch (Exception ex) - { - log.Error("Exception while trying to set clipboard text.", ex); - log.Error(ex, ex); - } - }); + try + { + String text = Helpers.ToWindowsLineEndings(e.Item.Tag != null ? e.Item.Tag.ToString() : e.Item.Text); + Clipboard.SetText(text); + } + catch (Exception ex) + { + log.Error("Exception while trying to set clipboard text.", ex); + log.Error(ex, ex); + } + }; menu.Items.Add(copyItem); menu.Show(this, PointToClient(MousePosition)); }