1. Mathias Kluba
  2. SOSNet

Commits

gaut...@x61  committed 5c5dc74

* some abstraction / infrastructure to refresh / sort / persist sorting order of any list views
* making sorting persistent in FormMain for process list

  • Participants
  • Parent commits 9c71dc3
  • Branches default

Comments (0)

Files changed (1)

File FormMain.cs

View file
             if(!string.IsNullOrEmpty(this.textBoxSearchProcesses.Text))
                 result = this.controller.FilterProcesses(this.textBoxSearchProcesses.Text);
 
-            this.listViewProcesses.BeginUpdate();
-            this.listViewProcesses.Items.Clear();
-            this.listViewProcesses.Items.AddRange(result);
-            this.listViewProcesses.EndUpdate();
+
+            updateListViewItemsWithNewItems(result, this.listViewProcesses);
+        }
+
+        private void updateListViewItemsWithNewItems(ListViewItem[] result, ListView listViewToRefreshWithNewItems)
+        {
+            listViewToRefreshWithNewItems.BeginUpdate();
+            listViewToRefreshWithNewItems.Items.Clear();
+            listViewToRefreshWithNewItems.Items.AddRange(result);
+            performSort(listViewToRefreshWithNewItems);
+            listViewToRefreshWithNewItems.EndUpdate();
         }
 
         private void textBoxSearchProcesses_TextChanged(object sender, EventArgs e)
 
             var ascending = (bool)clickedCol.Tag;
 
-            // Get the number of items in the list.
+            persistSort(listview, e.Column, ascending);
+            
+            performSort(listview);
+        }
+
+
+            Dictionary<ListView, KeyValuePair<int,bool>> columnSortByListViewMap = new Dictionary<ListView, KeyValuePair<int, bool>>();
+
+        private void persistSort(ListView listViewToPersistSortOrder, int columnIndex, bool isAscending)
+        {
+            var sortingDetails = new KeyValuePair<int,bool>(columnIndex, isAscending);
+                columnSortByListViewMap[listViewToPersistSortOrder] = sortingDetails;
+        }
+
+            private void getSort(ListView listViewToRetriveSortInfoFor, out int columnIndex, out bool isAscending)
+            {
+                if(columnSortByListViewMap.ContainsKey(listViewToRetriveSortInfoFor))
+                {
+                    var sortingdetails = columnSortByListViewMap[listViewToRetriveSortInfoFor];
+                    columnIndex = sortingdetails.Key;
+                    isAscending = sortingdetails.Value;
+                }
+                else
+                {
+                    columnIndex = 0;
+                    isAscending = true;
+                }
+            }
+
+        private void performSort(ListView listview/*, ColumnClickEventArgs e, bool ascending*/)
+        {
+						// Get the number of items in the list.
+            int columnIndexToSortOver;
+            bool isAscending;
+            getSort(listview, out columnIndexToSortOver, out isAscending);
             int numItems = listview.Items.Count;
 
             // Turn off display while data is repoplulated.
             ArrayList SortArray = new ArrayList();
             for (int i = 0; i < numItems; i++)
             {
-                SortArray.Add(new SortWrapper(listview.Items[i], e.Column));
+                SortArray.Add(new SortWrapper(listview.Items[i],columnIndexToSortOver));
             }
 
             // Sort the elements in the ArrayList using a new instance of the SortComparer
             // and the IComparer implementation to use for comparing elements. Note that
             // the IComparer implementation (SortComparer) requires the sort
             // direction for its constructor; true if ascending, othwise false.
-            SortArray.Sort(0, SortArray.Count, new SortWrapper.SortComparer(ascending));
+            SortArray.Sort(0, SortArray.Count, new SortWrapper.SortComparer(isAscending));
 
             // Clear the list, and repopulate with the sorted items.
             listview.Items.Clear();
             listview.EndUpdate();
         }
     }
-}
+}