Corrections in the context menu builder:

- Stop using reflection for the creation of context menu builders. It's error prone.
- MultipleDockerContainers was not constructed because it was abstract.
- Do not reconstruct the selectionList for each builder.
- Unused variables.

Signed-off-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>
This commit is contained in:
Konstantina Chremmou 2023-03-31 11:03:25 +01:00
parent 6838b76132
commit b543fbeedb

View File

@ -30,17 +30,15 @@
using System;
using System.Collections.Generic;
using XenAPI;
using System.Windows.Forms;
using XenAdmin.Model;
using XenAdmin.Core;
using System.Reflection;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using XenAdmin.Core;
using XenAdmin.Dialogs;
using XenAdmin.Model;
using XenAdmin.Plugins;
using XenAPI;
namespace XenAdmin.Commands
@ -56,22 +54,41 @@ namespace XenAdmin.Commands
static ContextMenuBuilder()
{
List<Builder> list = new List<Builder>();
foreach (Type type in Assembly.GetCallingAssembly().GetTypes())
var list = new List<Builder>
{
if (typeof(Builder).IsAssignableFrom(type) && !type.IsAbstract)
{
try
{
list.Add((Builder)Activator.CreateInstance(type));
}
catch (MissingMethodException)
{
new MixedPoolsAndStandaloneHosts(),
new MultiplePools(),
new MultipleDifferentXenObjectTypes(),
new MultipleSRs(),
new SingleVDI(),
new MultipleVDI(),
new SingleNetwork(),
new DisconnectedHosts(),
new MixedVMsAndTemplates(),
new MultipleAliveHosts(),
new SingleAliveHostInPool(),
new SingleAliveStandaloneHost(),
new MultipleHostsSomeDeadSomeAlive(),
new DeadHosts(),
new SinglePool(),
new SingleSnapshot(),
new SingleTemplate(),
new SingleVmAppliance(),
new MultipleVmAppliance(),
new SingleVM(),
new SingleSR(),
new SingleFolder(),
new MultipleTemplates(),
new MultipleSnapshots(),
new MultipleVMsInPool(),
new MultipleVMsOverMultiplePools(),
new MultipleFolders(),
new SingleTag(),
new MultipleTags(),
new SingleDockerContainer(),
new MultipleDockerContainers()
};
}
}
}
Builders = new ReadOnlyCollection<Builder>(list);
}
@ -82,11 +99,8 @@ namespace XenAdmin.Commands
/// <param name="mainWindow">The main window command interface. This can be found on mainwindow.</param>
public ContextMenuBuilder(PluginManager pluginManager, IMainWindow mainWindow)
{
Util.ThrowIfParameterNull(pluginManager, "pluginManager");
Util.ThrowIfParameterNull(pluginManager, "mainWindow");
_pluginManager = pluginManager;
_mainWindow = mainWindow;
_pluginManager = pluginManager ?? throw new ArgumentNullException(nameof(pluginManager));
_mainWindow = mainWindow ?? throw new ArgumentNullException(nameof(mainWindow));
}
/// <summary>
@ -106,7 +120,7 @@ namespace XenAdmin.Commands
/// <returns>The context menu items.</returns>
public ToolStripItem[] Build(SelectedItem selection)
{
return Build(new SelectedItem[] { selection });
return Build(new[] { selection });
}
/// <summary>
@ -116,12 +130,10 @@ namespace XenAdmin.Commands
/// <returns>The context menu items.</returns>
public ToolStripItem[] Build(IEnumerable<SelectedItem> selection)
{
Util.ThrowIfParameterNull(selection, "selection");
var selectionList = new SelectedItemCollection(selection ?? throw new ArgumentNullException(nameof(selection)));
foreach (Builder builder in Builders)
{
SelectedItemCollection selectionList = new SelectedItemCollection(selection);
if (builder.IsValid(selectionList))
{
ContextMenuItemCollection items = new ContextMenuItemCollection(_mainWindow, _pluginManager);
@ -134,7 +146,7 @@ namespace XenAdmin.Commands
}
}
return new ToolStripItem[0];
return Array.Empty<ToolStripItem>();
}
[Conditional("DEBUG")]
@ -163,14 +175,14 @@ namespace XenAdmin.Commands
string.Join("\n", usedKeys.Select(kvp => $"{kvp.Key} => {string.Join(", ", kvp.Value)}")));
}
#region Nested Classes
private abstract class Builder
{
public abstract void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items);
public abstract bool IsValid(SelectedItemCollection selection);
}
#region MixedPoolsAndStandaloneHosts class
private class MixedPoolsAndStandaloneHosts : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -210,10 +222,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultiplePools class
private class MultiplePools : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -242,10 +250,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleDifferentTypes
private class MultipleDifferentXenObjectTypes : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -288,10 +292,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleSRs
private class MultipleSRs : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -309,10 +309,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleVDI
private class SingleVDI : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -331,10 +327,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleVDI
private class MultipleVDI : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -351,10 +343,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleNetwork
private class SingleNetwork : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -371,10 +359,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region DisconnectedHosts
private class DisconnectedHosts : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -416,10 +400,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MixedVMsSnapshotsTemplates class
private class MixedVMsAndTemplates : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -461,10 +441,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleAliveHosts class
private class MultipleAliveHosts : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -500,16 +476,10 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleAliveHostInPool class
private class SingleAliveHostInPool : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
{
Host host = (Host)selection[0].XenObject;
items.AddIfEnabled(new NewVMCommand(mainWindow, selection));
items.AddIfEnabled(new NewSRCommand(mainWindow, selection));
items.AddIfEnabled(new ImportCommand(mainWindow, selection));
@ -557,16 +527,10 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleAliveStandaloneHost class
private class SingleAliveStandaloneHost : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
{
Host host = (Host)selection[0].XenObject;
items.AddIfEnabled(new NewVMCommand(mainWindow, selection));
items.AddIfEnabled(new NewSRCommand(mainWindow, selection));
items.AddIfEnabled(new ImportCommand(mainWindow, selection));
@ -613,10 +577,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleHostsSomeDeadSomeAlive class
private class MultipleHostsSomeDeadSomeAlive : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -649,10 +609,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region DeadHosts class
private class DeadHosts : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -687,10 +643,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SinglePool class
private class SinglePool : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -743,10 +695,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleSnapshot class
private class SingleSnapshot : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -772,10 +720,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleTemplate class
private class SingleTemplate : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -811,10 +755,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleVmAppliance class
private class SingleVmAppliance : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -838,10 +778,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region Multiple VMAppliance class
private class MultipleVmAppliance : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -856,10 +792,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleVM class
private class SingleVM : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -918,10 +850,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleSR class
private class SingleSR : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -947,10 +875,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleFolder class
private class SingleFolder : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -968,10 +892,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleVMs class
private abstract class MultipleVMs : Builder
{
public override bool IsValid(SelectedItemCollection selection)
@ -994,10 +914,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleTemplates class
private class MultipleTemplates : Builder
{
public override bool IsValid(SelectedItemCollection selection)
@ -1027,10 +943,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleSnapshots class
private class MultipleSnapshots : Builder
{
public override bool IsValid(SelectedItemCollection selection)
@ -1059,10 +971,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleVMsInPool class
private class MultipleVMsInPool : MultipleVMs
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -1120,11 +1028,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleVMsOverMultiplePools class
private class MultipleVMsOverMultiplePools : MultipleVMs
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -1155,10 +1058,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleFolders class
private class MultipleFolders : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -1174,10 +1073,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleTag class
private class SingleTag : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -1197,10 +1092,6 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleTags class
private class MultipleTags : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
@ -1219,16 +1110,10 @@ namespace XenAdmin.Commands
}
}
#endregion
#region SingleDockerContainer class
private class SingleDockerContainer : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
{
DockerContainer vm = (DockerContainer)selection[0].XenObject;
items.AddIfEnabled(new StartDockerContainerCommand(mainWindow, selection));
items.AddIfEnabled(new StopDockerContainerCommand(mainWindow, selection));
items.AddIfEnabled(new PauseDockerContainerCommand(mainWindow, selection));
@ -1248,11 +1133,7 @@ namespace XenAdmin.Commands
}
}
#endregion
#region MultipleDockerContainers class
private abstract class MultipleDockerContainers : Builder
private class MultipleDockerContainers : Builder
{
public override void Build(IMainWindow mainWindow, SelectedItemCollection selection, ContextMenuItemCollection items)
{