Skip to content

Commit

Permalink
Better connection between LayoutXML and Results view
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Jul 4, 2022
1 parent 5b8da88 commit 0b5bfe4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 19 deletions.
36 changes: 19 additions & 17 deletions FetchXmlBuilder/DockControls/ResultGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class ResultGrid : WeifenLuo.WinFormsUI.Docking.DockContent
{
private FetchXmlBuilder form;
private QueryInfo queryinfo;
private bool reloaded;

public ResultGrid(FetchXmlBuilder fetchXmlBuilder)
{
Expand Down Expand Up @@ -84,6 +85,7 @@ private void RefreshData()
{
crmGridView1.FilterText = string.Empty;
}
reloaded = true;
crmGridView1.SuspendLayout();
crmGridView1.Refresh();
crmGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
Expand All @@ -93,6 +95,7 @@ private void RefreshData()
.ForEach(c => c.Width = form.settings.Results.MaxColumnWidth);
GetLayoutFromGrid();
crmGridView1.ResumeLayout();
reloaded = false;
}

private void GetLayoutFromGrid()
Expand All @@ -101,25 +104,21 @@ private void GetLayoutFromGrid()
{
return;
}
var layoutxml = new LayoutXML
if (form.dockControlBuilder.LayoutXML == null || form.dockControlBuilder.LayoutXML.EntityMeta != entity)
{
EntityMeta = entity,
EntityName = entity.LogicalName,
Cells = new List<Cell>()
};
foreach (var col in crmGridView1.Columns.Cast<DataGridViewColumn>()
.Where(c => !c.Name.StartsWith("#") && c.Visible && c.Width > 5)
.OrderBy(c => c.DisplayIndex))
{
layoutxml.Cells.Add(new Cell
form.dockControlBuilder.LayoutXML = new LayoutXML
{
Parent = layoutxml,
Attribute = form.dockControlBuilder.GetAttributeNodeFromLayoutName(col.Name),
Name = col.Name,
Width = col.Width
});
EntityMeta = entity,
EntityName = entity.LogicalName,
Cells = new List<Cell>()
};
}
form.dockControlBuilder.LayoutXML = layoutxml;
var columns = crmGridView1.Columns.Cast<DataGridViewColumn>()
.Where(c => !c.Name.StartsWith("#") && c.Visible && c.Width > 5)
.OrderBy(c => c.DisplayIndex)
.ToDictionary(c => c.Name, c => c.Width);
form.dockControlBuilder.LayoutXML.AdjustAllCells(columns);
form.dockControlBuilder.UpdateLayoutXML();
}

private void crmGridView1_RecordDoubleClick(object sender, Rappen.XTB.Helpers.Controls.XRMRecordEventArgs e)
Expand Down Expand Up @@ -252,7 +251,10 @@ private void ctxCopy_Click(object sender, EventArgs e)

private void crmGridView1_LayoutChanged(object sender, DataGridViewColumnEventArgs e)
{
GetLayoutFromGrid();
if (!reloaded)
{
GetLayoutFromGrid();
}
}
}
}
8 changes: 6 additions & 2 deletions FetchXmlBuilder/DockControls/TreeBuilderControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ internal LayoutXML LayoutXML
set
{
layoutxml = value;
fxb.dockControlLayoutXml?.UpdateXML(layoutxml.ToXML());
layoutxml?.Cells?.ToList()?.ForEach(c => (GetCurrentControl() as attributeControl)?.UpdateUIFromCell(c));
}
}

Expand Down Expand Up @@ -392,6 +390,12 @@ internal void UpdateAllNode()
tvFetch.Nodes.OfType<TreeNode>().ToList().ForEach(n => UpdateChildNode(n));
}

internal void UpdateLayoutXML()
{
fxb.dockControlLayoutXml?.UpdateXML(LayoutXML.ToXML());
LayoutXML?.Cells?.ToList()?.ForEach(c => (GetCurrentControl() as attributeControl)?.UpdateUIFromCell(c));
}

#endregion Internal Methods

#region Private Methods
Expand Down
17 changes: 17 additions & 0 deletions FetchXmlBuilder/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,22 @@ internal static string AttributeValue(this XmlNode node, string key)
}
return string.Empty;
}

public static void Move<T>(this List<T> list, T item, int newIndex)
{ // From this tip: https://stackoverflow.com/a/450250/2866704
if (item != null)
{
var oldIndex = list.IndexOf(item);
if (oldIndex > -1 && oldIndex != newIndex)
{
list.RemoveAt(oldIndex);

if (newIndex > oldIndex) newIndex--;
// the actual index could have shifted due to the removal

list.Insert(newIndex, item);
}
}
}
}
}
2 changes: 2 additions & 0 deletions FetchXmlBuilder/Views/Cell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ internal Cell(LayoutXML layoutxml, XmlNode cell)
}
}

public override string ToString() => Name;

public string ToXML()
{
return $"<cell name='{Name}' width='{Width}' />";
Expand Down
35 changes: 35 additions & 0 deletions FetchXmlBuilder/Views/LayoutXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ public class LayoutXML
public string EntityName;
public EntityMetadata EntityMeta;
public List<Cell> Cells;
private FetchXmlBuilder fxb;

public LayoutXML()
{ }

public LayoutXML(string layoutxml, TreeNode entity, FetchXmlBuilder fxb)
{
this.fxb = fxb;
if (!string.IsNullOrEmpty(layoutxml))
{
var doc = new XmlDocument();
Expand All @@ -44,17 +46,50 @@ public LayoutXML(string layoutxml, TreeNode entity, FetchXmlBuilder fxb)
AdjustAllCells(attributes);
}

public override string ToString() => $"{EntityName} {Cells.Where(c => c.Width > 0).Count()}/{Cells.Count} cells";

public void AdjustAllCells(IEnumerable<TreeNode> attributes)
{
if (Cells == null)
{
Cells = new List<Cell>();
}
// Add missing Cells
attributes.Where(a => GetCell(a) == null).ToList().ForEach(a => AddCell(a));
// Update Cells that missing the Attribute
Cells.Where(c => c.Attribute == null).ToList().ForEach(c => c.Attribute = attributes.FirstOrDefault(a => c.Name == a.GetAttributeLayoutName()));
// Remove unused Cells
Cells.Where(c => c.Attribute == null).ToList().ForEach(c => Cells.Remove(c));
}

internal void AdjustAllCells(Dictionary<string, int> namewidths)
{
if (Cells == null)
{
Cells = new List<Cell>();
}
// Add these missing
namewidths.Where(n => Cells
.FirstOrDefault(c => c.Name == n.Key) == null)
.ToList().ForEach(nw => Cells.Add(new Cell
{
Parent = this,
Name = nw.Key,
Width = nw.Value,
Attribute = fxb.dockControlBuilder.GetAttributeNodeFromLayoutName(nw.Key)
}));
// Remove not using cells
// Cells.Where(c => !namewidths.Any(nw => nw.Key == c.Name)).ToList().ForEach(c => Cells.Remove(c));
// Update the Widths
Cells.ToList().ForEach(c => c.Width = namewidths.ContainsKey(c.Name) ? namewidths[c.Name] : 0);
int index = 0;
foreach (var nw in namewidths)
{
var cell = Cells.FirstOrDefault(c => c.Name == nw.Key);
Cells.Move(cell, index++);
}
}

public string ToXML()
{
var result = $@"<grid name='resultset' object='{EntityMeta.ObjectTypeCode}' jump='{EntityMeta.PrimaryNameAttribute}' select='1' icon='1' preview='1'>
Expand Down

0 comments on commit 0b5bfe4

Please sign in to comment.