CP-36392: Refactored so that GetHashCode() does not reference mutable fields.

Also, removed some unused GridRow class members.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2022-04-05 13:35:08 +01:00
parent c28bb4b34c
commit b3bc93f1f6
5 changed files with 46 additions and 121 deletions

View File

@ -31,7 +31,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace XenAdmin.Controls.CustomGridView
@ -41,16 +40,9 @@ namespace XenAdmin.Controls.CustomGridView
public GridHeaderItem DefaultSortColumn;
public List<String> Columns = new List<String>();
public GridHeaderRow(int rowheight, Color backColor, Pen borderPen)
: base(rowheight, backColor, borderPen)
public GridHeaderRow()
: base("1")
{
OpaqueRef = "1";
}
public GridHeaderRow(int rowheight)
: base(rowheight)
{
OpaqueRef = "1";
}
public int GetColumnWidth(string col)

View File

@ -38,38 +38,29 @@ namespace XenAdmin.Controls.CustomGridView
{
public class GridRow : IComparable<GridRow>
{
public Dictionary<string, GridItemBase> Items = new Dictionary<string, GridItemBase>();
public List<GridRow> Rows = new List<GridRow>();
public Dictionary<string, GridItemBase> Items { get; }= new Dictionary<string, GridItemBase>();
public List<GridRow> Rows { get; } = new List<GridRow>();
public static Image ExpandedImage = Images.StaticImages.expanded_triangle;
public static Image ShrunkenImage = Images.StaticImages.contracted_triangle;
private Image Image => Expanded
? Images.StaticImages.expanded_triangle
: Images.StaticImages.contracted_triangle;
public string OpaqueRef;
public object Tag;
public string OpaqueRef { get; }
public object Tag { get; set; }
public int Priority = -1; // -1 => dont care, 0 => highest
public int Priority { get; set; } = -1; // -1 => don't care, 0 => highest
public readonly Color BackColor;
public Pen BorderPen;
public RowState State { get; set; } = RowState.Expanded;
protected RowState state = RowState.Expanded;
public int RowHeight { get; set; }
private readonly int rowHeight;
public GridRow ParentRow { get; set; }
private GridRow parentrow;
private GridView gridview;
public GridRow(int rowHeight)
: this(rowHeight, SystemColors.Window, null)
public GridRow(string opaqueRef = null)
{
}
public GridRow(int rowHeight, Color BackColor, Pen Borderpen)
{
this.rowHeight = rowHeight;
this.BackColor = BackColor;
this.BorderPen = Borderpen;
this.Tag = null;
OpaqueRef = opaqueRef;
}
// Set the row's gridview and add the row to the gridview's row list
@ -90,17 +81,6 @@ namespace XenAdmin.Controls.CustomGridView
}
}
public GridRow ParentRow
{
get
{
return parentrow;
}
set
{
parentrow = value;
}
}
public bool Expanded
{
@ -130,12 +110,6 @@ namespace XenAdmin.Controls.CustomGridView
}
}
public RowState State
{
get { return state; }
set { state = value; }
}
// We return the path as a string like "foo::bar::baz". It would be more theoretically
// correct to return a List<string> but then we would have to define comparison functions
// and hash functions for it. This has a very small chance of clashes, and is much simpler.
@ -224,14 +198,6 @@ namespace XenAdmin.Controls.CustomGridView
row.ParentRow = this;
}
public int RowHeight
{
get
{
return rowHeight;
}
}
public int RowAndChildrenHeight
{
get
@ -263,16 +229,11 @@ namespace XenAdmin.Controls.CustomGridView
if (HasLeftExpander)
{
// paint background
using (Brush brush = new SolidBrush(BackColor))
{
using (Brush brush = new SolidBrush(SystemColors.Window))
e.Graphics.FillRectangle(brush, e.Rectangle);
}
if (BorderPen != null)
e.Graphics.DrawRectangle(BorderPen, e.Rectangle);
Image im = Expanded ? ExpandedImage : ShrunkenImage;
e.Graphics.DrawImage(im, e.Rectangle.X + im.Width, e.Rectangle.Y + im.Height, im.Width, im.Height);
e.Graphics.DrawImage(Image, e.Rectangle.X + Image.Width,
e.Rectangle.Y + Image.Height, Image.Width, Image.Height);
}
// paint this row
@ -393,8 +354,7 @@ namespace XenAdmin.Controls.CustomGridView
{
if (HasLeftExpander)
{
Image image = Expanded ? ExpandedImage : ShrunkenImage;
Size s = new Size(image.Width * 3, image.Height * 3);
Size s = new Size(Image.Width * 3, Image.Height * 3);
Rectangle r = new Rectangle(new Point(), s);
if (r.Contains(point))
@ -519,11 +479,9 @@ namespace XenAdmin.Controls.CustomGridView
internal void OnMouseMove(Point point)
{
if (HasLeftExpander)
{
Image image = Expanded ? ExpandedImage : ShrunkenImage;
Rectangle r = new Rectangle(image.Width, image.Height, image.Width, image.Height);
Rectangle r = new Rectangle(Image.Width, Image.Height, Image.Width, Image.Height);
Cursor = r.Contains(point) ? Cursors.Hand : Cursors.Default;
}
@ -658,24 +616,6 @@ namespace XenAdmin.Controls.CustomGridView
}
}
internal void SaveExpandedState(List<String> state)
{
if (!Expanded)
state.Add(OpaqueRef);
foreach (GridRow row in Rows)
row.SaveExpandedState(state);
}
internal void RestoreExpandedState(List<string> expandedState)
{
if (Rows.Count > 0 && expandedState.Contains(OpaqueRef))
Expanded = false;
foreach (GridRow row in Rows)
row.RestoreExpandedState(expandedState);
}
public override bool Equals(object obj)
{
if (!(obj is GridRow row))
@ -685,7 +625,7 @@ namespace XenAdmin.Controls.CustomGridView
public override int GetHashCode()
{
return (OpaqueRef != null ? OpaqueRef.GetHashCode() : 0);
return OpaqueRef != null ? OpaqueRef.GetHashCode() : 0;
}
}

View File

@ -845,9 +845,7 @@ namespace XenAdmin.Controls.CustomGridView
}
}
public virtual void StartDragDrop()
public void StartDragDrop()
{
GridRowCollection rows = new GridRowCollection();
foreach (GridRow row in RowsAndChildren)
@ -859,7 +857,7 @@ namespace XenAdmin.Controls.CustomGridView
DoDragDrop(rows, DragDropEffects.Move);
}
public virtual bool IsDraggableRow(GridRow row)
protected virtual bool IsDraggableRow(GridRow row)
{
return true;
}

View File

@ -1280,7 +1280,7 @@ namespace XenAdmin.Controls.XenSearch
internal class ExtraComboEntry
{
public DatePropertyQuery.PropertyQueryType type;
public readonly DatePropertyQuery.PropertyQueryType type;
public ExtraComboEntry(DatePropertyQuery.PropertyQueryType type)
{
this.type = type;
@ -1327,7 +1327,7 @@ namespace XenAdmin.Controls.XenSearch
public override int GetHashCode()
{
return (int) type;
return (int)type;
}
}

View File

@ -181,7 +181,7 @@ namespace XenAdmin.Controls.XenSearch
private void SetupHeaderRow()
{
GridHeaderRow = new GridHeaderRow(ROW_HEIGHT);
GridHeaderRow = new GridHeaderRow {RowHeight = ROW_HEIGHT};
foreach (string s in DEFAULT_COLUMNS)
ShowColumn(s);
@ -500,7 +500,7 @@ namespace XenAdmin.Controls.XenSearch
private void listUpdateManager_Update()
{
GridRow root = new GridRow(-1);
GridRow root = new GridRow {RowHeight = -1};
RowGroupAcceptor acceptor = new RowGroupAcceptor(root);
CollectionAcceptor ca = new CollectionAcceptor();
@ -565,7 +565,7 @@ namespace XenAdmin.Controls.XenSearch
private void AddNoResultsRow()
{
GridRow row = new GridRow(ROW_HEIGHT);
GridRow row = new GridRow {RowHeight = ROW_HEIGHT};
GridStringItem resultsItem = new GridStringItem(Messages.OVERVIEW_NO_RESULTS, HorizontalAlignment.Left, VerticalAlignment.Middle, false, false, TextBrush, Program.DefaultFont, 6);
row.AddItem("name", resultsItem);
AddRow(row);
@ -618,16 +618,6 @@ namespace XenAdmin.Controls.XenSearch
return new GridVerticalArrayItem(items.ToArray(), false);
}
private static GridRow NewGroupRow(string opaqueref, object tag, int rowHeight, int priority)
{
GridRow row = new GridRow(rowHeight);
row.OpaqueRef = opaqueref;
row.Tag = tag;
row.Expanded = true;
row.Priority = priority;
return row;
}
private static GridHorizontalArrayItem NewNameItem(GridItemBase iconItem, GridItemBase dataItem, int iconWidth, int indent)
{
GridTreeExpanderItem expander = new GridTreeExpanderItem();
@ -639,11 +629,16 @@ namespace XenAdmin.Controls.XenSearch
private static GridRow CreateRow(Grouping grouping, Object o, int indent)
{
IXenObject ixmo = o as IXenObject;
if (ixmo != null)
if (o is IXenObject ixmo)
{
bool isFolderRow = (o is Folder);
GridRow _row = NewGroupRow(ixmo.opaque_ref, ixmo, isFolderRow ? FOLDER_ROW_HEIGHT : ROW_HEIGHT, 0);
bool isFolderRow = o is Folder;
var _row = new GridRow(ixmo.opaque_ref)
{
Tag = ixmo,
Expanded = true,
Priority = 0,
RowHeight = isFolderRow ? FOLDER_ROW_HEIGHT : ROW_HEIGHT
};
foreach (ColumnNames column in Enum.GetValues(typeof(ColumnNames)))
{
@ -686,15 +681,16 @@ namespace XenAdmin.Controls.XenSearch
if (grouping == null)
return null;
GridRow row = NewGroupRow(String.Format("{0}: {1}", grouping.GroupingName, o), null, ROW_HEIGHT, 0);
GridRow row = new GridRow($"{grouping.GroupingName}: {o}")
{
Expanded = true,
Priority = 0,
RowHeight = ROW_HEIGHT
};
GridImageItem statusItem = new GridImageItem(
grouping.GroupingName,
new ImageDelegate(delegate()
{
return Images.GetImage16For(grouping.GetGroupIcon(o));
}),
() => Images.GetImage16For(grouping.GetGroupIcon(o)),
HorizontalAlignment.Left, VerticalAlignment.Top, true);
GridVerticalArrayItem nameItem = NewDoubleRowItem(grouping, o);
@ -704,10 +700,9 @@ namespace XenAdmin.Controls.XenSearch
return row;
}
public override bool IsDraggableRow(GridRow row)
protected override bool IsDraggableRow(GridRow row)
{
IXenObject o = row.Tag as IXenObject;
return o != null;
return row.Tag is IXenObject;
}
private void MetricsUpdated(object o, EventArgs e)