xenadmin/XenAdmin/Commands/Controls/PoolRemoveServerToolStripMenuItem.cs
Konstantina Chremmou adab54a9c5 CA-293375: Fixed inconsistencies in the items related to removing servers from a pool
- Add item "Remove From Pool" to the main Server menu; it should be enabled
if the selected servers are removable and disabled at all other times.
The item should be present in the server's right click menu only if it is enabled.
- Replaced the above item in the Pool menu with the dropdown "Remove Server"
where the user can select the server to remove from the list of removable
servers.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
2019-08-15 11:33:01 +01:00

107 lines
4.0 KiB
C#

/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using XenAdmin.Core;
namespace XenAdmin.Commands
{
internal class PoolRemoveServerToolStripMenuItem : CommandToolStripMenuItem
{
public PoolRemoveServerToolStripMenuItem()
: base(new PoolRemoveServerCommand(), false)
{
base.DropDownItems.Add(new ToolStripMenuItem());
}
public PoolRemoveServerToolStripMenuItem(IMainWindow mainWindow, IEnumerable<SelectedItem> selection, bool inContextMenu)
: base(new PoolRemoveServerCommand(mainWindow, selection), inContextMenu)
{
base.DropDownItems.Add(new ToolStripMenuItem());
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new Command Command => base.Command;
protected override void OnDropDownOpening(EventArgs e)
{
DropDownItems.Clear();
var selection = Command.GetSelection();
var connection = selection.GetConnectionOfAllItems();
var pool = Helpers.GetPool(connection);
if (pool == null)
return;
var hosts = connection.Cache.Hosts;
foreach (var host in hosts)
{
if (host.resident_VMs == null || host.resident_VMs.Count >= 2 || !host.IsLive())
continue;
var cmd = new RemoveHostFromPoolCommand(Command.MainWindowCommandInterface, host);
DropDownItems.Add(new CommandToolStripMenuItem(cmd, host.Name(), Images.GetImage16For(host)));
}
}
private class PoolRemoveServerCommand : Command
{
public PoolRemoveServerCommand()
{
}
public PoolRemoveServerCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection)
: base(mainWindow, selection)
{
}
public override string MenuText => Messages.REMOVE_SERVER_MENU_ITEM;
protected override bool CanExecuteCore(SelectedItemCollection selection)
{
var connection = selection.GetConnectionOfAllItems();
var pool = Helpers.GetPool(connection);
if (pool == null)
return false;
return connection.Cache.Hosts.Any(host => host.resident_VMs != null && host.resident_VMs.Count < 2 && host.IsLive());
}
}
}
}