public void GenerateGroupsFromColumn(Collumns col, Boolean reversed = false) {
if (col == null) return;
this.BeginInvoke(new MethodInvoker(() => this._IIListView.RemoveAllGroups()));
this.Groups.Clear();
if (col.CollumnType == typeof(String)) {
if (this.IsTraditionalNameGrouping) {
var groups = this.Items.ToArray().GroupBy(k => k.DisplayName.ToUpperInvariant().First(), e => e).OrderBy(o => o.Key);
var i = reversed ? groups.Count() - 1 : 0;
foreach (var group in groups) {
var groupItems = group.Select(s => s).ToArray();
groupItems.ToList().ForEach(c => c.GroupIndex = this.Groups.Count);
this.Groups.Add(new ListViewGroupEx() { Items = groupItems, Index = reversed ? i-- : i++, Header = $"{group.Key.ToString()} ({groupItems.Count()})" });
}
} else {
var i = reversed ? 3 : 0;
Action<String, String, Boolean> addNameGroup = (String char1, String char2, Boolean isOthers) => {
var testgrn = new ListViewGroupEx();
if (isOthers) {
testgrn.Items =
this.Items.Where(
w =>
(w.DisplayName.ToUpperInvariant().First() < Char.Parse("A") ||
w.DisplayName.ToUpperInvariant().First() > Char.Parse("Z")) && (w.DisplayName.ToUpperInvariant().First() < Char.Parse("0") || w.DisplayName.ToUpperInvariant().First() > Char.Parse("9"))).ToArray();
} else {
testgrn.Items =
this.Items.Where(
w =>
w.DisplayName.ToUpperInvariant().First() >= Char.Parse(char1) &&
w.DisplayName.ToUpperInvariant().First() <= Char.Parse(char2)).ToArray();
}
testgrn.Header = isOthers
? char1 + $" ({testgrn.Items.Count()})"
: char1 + " - " + char2 + $" ({testgrn.Items.Count()})";
testgrn.Index = reversed ? i-- : i++;
this.Groups.Add(testgrn);
};
addNameGroup("0", "9", false);
addNameGroup("A", "H", false);
addNameGroup("I", "P", false);
addNameGroup("Q", "Z", false);
addNameGroup("Others", String.Empty, true);
}
} else if (col.CollumnType == typeof(Int64)) {
var j = reversed ? 7 : 0;
//TODO: Upgrade next to use an Action<>
var uspec = new ListViewGroupEx();
uspec.Items = this.Items.Where(w => w.IsFolder).ToArray();
uspec.Header = $"Unspecified ({uspec.Items.Count()})";
uspec.Index = reversed ? j-- : j++;
this.Groups.Add(uspec);
var testgrn = new ListViewGroupEx();
testgrn.Items =
this.Items.Where(w => Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) == 0 && !w.IsFolder)
.ToArray();
testgrn.Header = $"Empty ({testgrn.Items.Count()})";
testgrn.Index = reversed ? j-- : j++;
this.Groups.Add(testgrn);
var testgr = new ListViewGroupEx();
testgr.Items =
this.Items.Where(
w =>
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 0 &&
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) <= 10 * 1024).ToArray();
testgr.Header = $"Very Small ({testgr.Items.Count()})";
testgr.Index = reversed ? j-- : j++;
this.Groups.Add(testgr);
var testgr2 = new ListViewGroupEx();
testgr2.Items =
this.Items.Where(
w =>
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 10 * 1024 &&
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) <= 100 * 1024).ToArray();
testgr2.Header = $"Small ({testgr2.Items.Count()})";
testgr2.Index = reversed ? j-- : j++;
this.Groups.Add(testgr2);
var testgr3 = new ListViewGroupEx();
testgr3.Items =
this.Items.Where(
w =>
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 100 * 1024 &&
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) <= 1 * 1024 * 1024).ToArray();
testgr3.Header = $"Medium ({testgr3.Items.Count()})";
testgr3.Index = reversed ? j-- : j++;
this.Groups.Add(testgr3);
var testgr4 = new ListViewGroupEx();
testgr4.Items =
this.Items.Where(
w =>
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 1 * 1024 * 1024 &&
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) <= 16 * 1024 * 1024).ToArray();
testgr4.Header = $"Big ({testgr4.Items.Count()})";
testgr4.Index = reversed ? j-- : j++;
this.Groups.Add(testgr4);
var testgr5 = new ListViewGroupEx();
testgr5.Items =
this.Items.Where(
w =>
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 16 * 1024 * 1024 &&
Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) <= 128 * 1024 * 1024).ToArray();
testgr5.Header = $"Huge ({testgr5.Items.Count()})";
testgr5.Index = reversed ? j-- : j++;
this.Groups.Add(testgr5);
var testgr6 = new ListViewGroupEx();
testgr6.Items =
this.Items.Where(w => Convert.ToInt64(w.GetPropertyValue(col.pkey, typeof(Int64)).Value) > 128 * 1024 * 1024)
.ToArray();
testgr6.Header = $"Gigantic ({testgr6.Items.Count()})";
testgr6.Index = reversed ? j-- : j++;
this.Groups.Add(testgr6);
} else if (col.CollumnType == typeof(PerceivedType)) {
var groups = this.Items.GroupBy(k => k.GetPropertyValue(col.pkey, typeof(String)).Value, e => e).OrderBy(o => o.Key);
var i = reversed ? groups.Count() - 1 : 0;
foreach (var group in groups.ToArray()) {
var groupItems = group.Select(s => s).ToArray();
this.Groups.Add(new ListViewGroupEx() { Items = groupItems, Index = reversed ? i-- : i++, Header = $"{((PerceivedType)group.Key).ToString()} ({groupItems.Count()})" });
}
} else {
var groups = this.Items.GroupBy(k => k.GetPropertyValue(col.pkey, typeof(String)).Value, e => e).OrderBy(o => o.Key);
var i = reversed ? groups.Count() - 1 : 0;
foreach (var group in groups.ToArray()) {
var groupItems = group.Select(s => s).ToArray();
this.Groups.Add(new ListViewGroupEx() { Items = groupItems, Index = reversed ? i-- : i++, Header = $"{group.Key.ToString()} ({groupItems.Count()})" });
}
}
if (reversed) this.Groups.Reverse();
this.Invoke(new MethodInvoker(() => {
this._IIListView.SetItemCount(this.Items.Count, 0x2);
}));
foreach (var group in this.Groups.ToArray()) {
group.Items.ToList().ForEach(e => e.GroupIndex = group.Index);
var nativeGroup = group.ToNativeListViewGroup();
var insertedPosition = -1;
this.BeginInvoke(new MethodInvoker(() => {
this._IIListView.InsertGroup(-1, nativeGroup, out insertedPosition);
}));
}
this.LastGroupCollumn = col;
this.LastGroupOrder = reversed ? SortOrder.Descending : SortOrder.Ascending;
this.SetSortIcon(this.Collumns.IndexOf(col), this.LastGroupOrder);
}