xenadmin/XenAdmin/Wizards/GenericPages/NewVMGroupVMsPage.cs

341 lines
12 KiB
C#
Raw Normal View History

/* 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.ComponentModel;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using XenAdmin.Actions;
using XenAdmin.Commands;
using XenAdmin.Controls;
using XenAdmin.Core;
using XenAdmin.SettingsPanels;
using XenAPI;
namespace XenAdmin.Wizards.GenericPages
{
// This design for this class is in NewVMGroupVMsPageBase: see notes there.
public class NewVMGroupVMsPage<T> : NewVMGroupVMsPageBase where T : XenObject<T>
{
private Pool _pool;
public Pool Pool
{
get { return _pool; }
set
{
_pool = value;
if (_pool != null)
label1.Text = string.Format(Helpers.IsPool(_pool.Connection) ? Messages.VMS_IN_POOL : Messages.VMS_IN_SERVER,
_pool.Name.Ellipsise(60));
label2.Text = VMGroup<T>.ChooseVMsPage_Rubric;
dataGridView1.Columns["ColumnCurrentGroup"].HeaderText = VMGroup<T>.ChooseVMsPage_CurrentGroup;
}
}
public NewVMGroupVMsPage()
{
SelectedVMs = new List<VM>();
dataGridView1.SortCompare += dataGridView1_SortCompare;
}
public override string Text
{
get { return VMGroup<T>.ChooseVMsPage_Text; }
}
public override string SubText
{
get { return labelCounterVMs.Text; }
}
public override string HelpID
{
get { return VMGroup<T>.ChooseVMsPage_HelpID; }
}
public override Image Image
{
get { return Properties.Resources._000_VM_h32bit_16; }
}
public override string PageTitle
{
get { return VMGroup<T>.ChooseVMsPage_Title; }
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
internal List<VM> SelectedVMs { get; set; }
public string GroupName { private get; set; }
public List<XenRef<VM>> SelectedVMsRefs
{
get { return SelectedVMs.ConvertAll<XenRef<VM>>(converter); }
}
private static XenRef<VM> converter(VM vm)
{
return new XenRef<VM>(vm.opaque_ref);
}
protected override void searchTextBox1_TextChanged(object sender, System.EventArgs e)
{
if (Pool != null)
{
var sortedColumn = dataGridView1.SortedColumn;
var sortOrder = dataGridView1.SortOrder;
dataGridView1.Rows.Clear();
foreach (var vm in Pool.Connection.Cache.VMs)
{
CP-16922: Implement GUI for enabling PVS read caching (#1179) * CP-16922: UI changes to PvsPage to replace the single configure button with a separate enable and disable one, per the design doc * CP-16922: Intermediate checkin - command + dialog implemented, but PVS page has some layout problems. * CP-16922: Implement GUI for enabling PVS read caching and marking/unmarking VMs for prepopulation Fix the broken layout by reverting MainWindow.resx (which shouldn't have changed) back to the previous commit. Also removed duplicate properties button code. * CP-16922: PVS Page changes, to properly display all VMs (not just ones with proxy), only populating proxy columns if there is a proxy set. * CP-16922: Use a CommandButton for the enable button, connected to the same command as the VM main menu button, and using the table selection as its selection manager. * CP-16922: Delete the old enable button click behaviour (because it now has a proper command), was causing the dialog to show up twice * CP-16922: Properly list VMs by hiding those that should be hidden (in addition to non-VMs, also hide the __gui__ prefix and others covered by Show function). Also fixes a bug in NewVMGroupVMsPage discovered when fixing this - it would show VMs that should be hidden if you used the search bar. * CP-16922: Use ButtonText on the Command to set the text on the PVS enable button * CP-16922: Add the disable PVS read-caching command, and its associated proxy destroy action. Both based off enable equivalents (main difference is no dialog for now). Not used yet but will be in main window menu + disable button * CP-16922: Add disable PVS read-caching to the main window menu (under enable), with the disable command attached to it * CP-16922: Add support for disabling PVS read-caching for selected hosts via the PVS tab page by converting the disable button into a CommandButton in the same way as the enable button, using its own command + selection manager (because Selection Managers only bind to one control) * CP-16922: Design - adding the "read caching enabled" column from the design, so that we display both whether caching is enabled and whether the VM is currently cached. If it's disabled all other values are missing. * CP-16922: Add the PVS site column per the design to show the site a VM's PVS Proxy is on if it has a proxy * CP-16922: Add an event handler for VMs changing so that newly provisioned VMs immediately show up in the PVS tab page * CP-16922: Support for properly remembering the selection when the VM list is refreshed. A new method returns the list of VMs for the selected rows, and we can then check if the VM for the row being added is in this list. If there was no previous selection then we still select the first row by default. * CP-16922: Remove prepopulate, which is being removed from the design. The next XAPI version will remove it, so just pass False as prepopulate for now, and remove the option from the dialog and PVS tab page. * CP-16922: Improve UI appearance for a missing Storage Repository. Per the design, if there is no SR show the message None (memory only) instead of an empty cell * CP-16922: Improved sorting behaviour. All columns (except VM name, which always uses it) use the VM tag as a sorting fallback (since they all have duplicates). New default sort which sorts by caching enabled first (yes before no), and then VM tag as a tiebreaker. Support for remembering the current sort on refresh, ie when a value changes. * CP-16922: Refactoring to use a VM extension method to get the PVSProxy (or null if none exists) for a VM. This was used in the PVS Page + commands which are now much simpler. * CP-16922: Move where the SelectionChanged event handler is set when refreshing the PVS table, so that the command buttons are updated correctly. * CP-16922: Improvements to PVS read-caching dialog. Set accept/cancel buttons properly + adjust tab values, so focus acts as expected * CP-16922: Remove the images from the menu for Enable/Disable PVS read-caching, most commands in the menu don't have one * CP-16922: Sorting out the strings for the actions + ParallelAction calls, and moving them into the resx. Also reverting wrong MainWindow.resx changes. * CAR-2184: XAPI changes. I've made the minimal possible changes to PvsPage to ensure it compiles, but it's had a lot of feature work (CP-16922) that supersedes this, these small changes should keep that merge painless. * CAR-2184 XAPI: Reverse the unrelated changes, only keep the PVS ones. * CP-16922: Merge latest XAPI changes, notably changes to Pvs_site.create * CP-16922: Change tab show/hide logic for PVS tab, show it even if there are no PVS sites. * CP-16922: Implement GUI for enabling PVS read caching Design changes - remove the storage repo column and replace it with status, which is populated with a friendly version of the pvs_proxy_status enum. Also populate site list using site.name_label Signed-off-by: Callum McIntyre <callumiandavid.mcintyre@citrix.com> * CP-16922: Implement GUI for enabling PVS read caching Adjusting the CanExecute of Enable PVS read-caching command to require at least one PVS site to be created. Since the PVS tab can show up without any sites, we need this because we can't create a proxy without a site. * CP-16922: Code review changes. Tidied up messages, and ensured actions run async properly, and without exiting before their task. * CP-16922: Code review changes. Update dialog resx, wider combobox on dialog, update action descriptions before setting RelatedTask, add friendly message body for PVS error notifications.
2016-09-22 16:08:15 +02:00
if (searchTextBox1.Matches(vm.Name) && vm.is_a_real_vm && vm.Show(Properties.Settings.Default.ShowHiddenVMs))
dataGridView1.Rows.Add(new VMDataGridViewRow(SelectedVMs.Contains(vm), vm));
}
if (sortOrder != SortOrder.None && sortedColumn != null)
dataGridView1.Sort(sortedColumn, (sortOrder == SortOrder.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending);
UpdateCounterLabelAndButtons();
}
}
private void UpdateCounterLabelAndButtons()
{
labelCounterVMs.Text = SelectedVMs.Count == 1 ? Messages.ONE_VM_SELECTED : string.Format(Messages.MOREONE_VM_SELECTED, SelectedVMs.Count);
buttonClearAll.Enabled = SelectedVMs.Count > 0;
buttonSelectAll.Enabled = SelectedVMs.Count < dataGridView1.RowCount;
}
// How to sort the VMs in the grid view: CA-69817
void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
e.Handled = false;
bool sortByName = false;
if (e.Column == ColumnCheckBox)
{
// Sort by check mark first, then by name
int checked1 = (bool)e.CellValue1 ? 1 : 0;
int checked2 = (bool)e.CellValue2 ? 1 : 0;
if (checked1 != checked2)
{
e.SortResult = checked2 - checked1;
e.Handled = true;
}
else
sortByName = true;
}
if (e.Column == ColumnName || sortByName)
{
// In order to sort by name, we actually use the built-in VM sort.
// This ensures we use the correct name sorting, and gives a consistent tie-breaker.
VM vm1 = ((VMDataGridViewRow)dataGridView1.Rows[e.RowIndex1]).Vm;
VM vm2 = ((VMDataGridViewRow)dataGridView1.Rows[e.RowIndex2]).Vm;
e.SortResult = vm1.CompareTo(vm2);
e.Handled = true;
}
// For any other column, we fall off the end of this function with e.Handled still false,
// and let the framework handle it.
}
private class VMDataGridViewRow : DataGridViewRow
{
private DataGridViewCheckBoxCell _selectedCell = new DataGridViewCheckBoxCell();
private DataGridViewTextBoxCell _nameCell = new DataGridViewTextBoxCell();
private DataGridViewTextBoxCell _descriptionCell = new DataGridViewTextBoxCell();
private DataGridViewTextBoxCell _currentGroupCell = new DataGridViewTextBoxCell();
public readonly VM Vm;
public VMDataGridViewRow(bool selected, VM vm)
{
Vm = vm;
Cells.Add(_selectedCell);
Cells.Add(_nameCell);
Cells.Add(_descriptionCell);
Cells.Add(_currentGroupCell);
Refresh(selected);
}
void Refresh(bool selected)
{
_selectedCell.Value = selected;
_nameCell.Value = Vm.Name;
_descriptionCell.Value = Vm.Description;
T group = Vm.Connection.Resolve(VMGroup<T>.VmToGroup(Vm));
_currentGroupCell.Value = group == null ? Messages.NONE : group.Name;
}
}
public override void PageLoaded(PageLoadedDirection direction)
{
base.PageLoaded(direction);
RefreshTab(null);
}
private T _group = null;
private void RefreshTab(T group)
{
_group = group;
if (Pool != null)
{
dataGridView1.Rows.Clear();
foreach (var vm in Pool.Connection.Cache.VMs)
{
int index = 0;
if (vm.is_a_real_vm && vm.Show(Properties.Settings.Default.ShowHiddenVMs))
{
bool selected = group != null && VMGroup<T>.GroupToVMs(group).Contains(new XenRef<VM>(vm.opaque_ref));
index = dataGridView1.Rows.Add(new VMDataGridViewRow(selected, vm));
if (SelectedVMs.Contains(vm))
dataGridView1.Rows[index].Cells[0].Value = true;
else if (selected && !SelectedVMs.Contains(vm))
SelectedVMs.Add(vm);
}
}
dataGridView1.Sort(ColumnCheckBox, ListSortDirection.Ascending);
dataGridView1.AutoResizeColumns();
}
UpdateCounterLabelAndButtons();
}
protected override void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex >= 0)
{
dataGridView1.Rows[e.RowIndex].Cells[0].Value = !(bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
}
}
protected override void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex != 0 || e.RowIndex < 0 && e.RowIndex >= dataGridView1.RowCount)
return;
var row = dataGridView1.Rows[e.RowIndex] as VMDataGridViewRow;
if (row != null)
{
if ((bool)row.Cells[0].Value)
{
if (!SelectedVMs.Contains(row.Vm))
SelectedVMs.Add(row.Vm);
}
else
SelectedVMs.Remove(row.Vm);
}
UpdateCounterLabelAndButtons();
}
protected override void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
if ((e.Row.State & DataGridViewElementStates.Selected) != 0)
e.Row.Selected = false;
}
public override void PageLeave(PageLoadedDirection direction, ref bool cancel)
{
if (direction == PageLoadedDirection.Back)
return;
if (!userAcceptsWarning())
cancel = true;
base.PageLeave(direction, ref cancel);
}
private bool userAcceptsWarning()
{
string groupName = (_group != null) ? _group.Name : GroupName;
return AssignGroupToolStripMenuItem<T>.AssignGroupToVMCommand.ChangesOK(SelectedVMs, _group, groupName);
}
protected override void buttonClearAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells[0].Value = false;
}
}
protected override void buttonSelectAll_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow dataGridViewRow in dataGridView1.Rows)
{
dataGridViewRow.Cells[0].Value = true;
}
}
#region IEditPage implementation
public override AsyncAction SaveSettings()
{
return VMGroup<T>.AssignVMsToGroupAction(_clone, SelectedVMsRefs, true);
}
private T _clone;
public override void SetXenObjects(IXenObject orig, IXenObject clone)
{
_clone = (T)clone;
Pool = Helpers.GetPoolOfOne(_clone.Connection);
RefreshTab(_clone);
}
public override bool ValidToSave
{
get { return userAcceptsWarning(); }
}
public override void ShowLocalValidationMessages()
{
}
public override void Cleanup()
{
dataGridView1.Rows.Clear();
}
public override bool HasChanged
{
get
{
return (!Helpers.CompareLists(SelectedVMsRefs, VMGroup<T>.GroupToVMs(_clone)));
}
}
#endregion
}
}