OSDN Git Service

AppliStation-GUI,ソート後に対象列を削除すると異常終了する不具合を修正
authorttp <ttp@users.sourceforge.jp>
Mon, 23 Nov 2009 07:51:54 +0000 (16:51 +0900)
committerttp <ttp@users.sourceforge.jp>
Mon, 23 Nov 2009 07:51:54 +0000 (16:51 +0900)
AppliStation/AppliStation.Util/ListViewItemSortComparer.cs
AppliStation/PackageListView.cs

index 475d46c..277b84f 100644 (file)
@@ -13,44 +13,66 @@ namespace AppliStation.Util
        /// </summary>\r
        class ListViewItemSortComparer : System.Collections.IComparer\r
        {\r
+               SortOrder order = SortOrder.Ascending;\r
+               \r
+               ColumnHeader column = null;\r
+               \r
+               /// <summary>\r
+               /// コンストラクタ\r
+               /// </summary>\r
+               public ListViewItemSortComparer()\r
+               {\r
+               }\r
+               \r
                /// <summary>\r
                /// ソートの方向。昇順か?降順か?\r
                /// </summary>\r
-               public SortOrder Order = SortOrder.Ascending;\r
+               public SortOrder Order {\r
+                       get { return order; }\r
+                       set { order = value; }\r
+               }\r
                \r
                /// <summary>\r
-               /// ソート対象のコラムインデックス\r
+               /// 選択しているコラムを取得・設定する。\r
+               /// nullを設定するとソートが解除される。\r
                /// </summary>\r
-               public int Column = -1;\r
+               public ColumnHeader Column {\r
+                       get { return column; }\r
+                       set {\r
+                               column = value;\r
+                               \r
+                               if (value == null || value.Index < 0) {\r
+                                       order = SortOrder.None;\r
+                               }\r
+                       }\r
+               }\r
                \r
                /// <summary>\r
-               /// コンストラクタ\r
+               /// 選択しているコラムのインデックスを返す。選択されていないときは-1を返す。\r
                /// </summary>\r
-               /// <param name="index">ソート対象のコラムインデックス</param>\r
-               /// <param name="order">ソートの方向。昇順か?降順か?</param>\r
-               public ListViewItemSortComparer(int index, SortOrder order)\r
-               {\r
-                       this.Column = index;\r
-                       this.Order = order;\r
+               public int ColumnIndex {\r
+                       get {\r
+                               return (column != null)? column.Index : -1;\r
+                       }\r
                }\r
                \r
                public int Compare(object x, object y)\r
                {\r
-                       ListViewItem itemx = (ListViewItem) x;\r
-                       ListViewItem itemy = (ListViewItem) y;\r
+                       int columnIndex = this.ColumnIndex;\r
+                       int result = 0;\r
                        \r
-                       int result = -1;\r
-                       result = string.Compare(itemx.SubItems[Column].Text,\r
-                                               itemy.SubItems[Column].Text);\r
-                       \r
-                       switch (Order) {\r
-                               case SortOrder.Descending:\r
-                                       result =  -result;\r
-                                       break;\r
-                               case SortOrder.None:\r
-                                       result = 0;\r
-                                       break;\r
+                       if (columnIndex >= 0 && order != SortOrder.None) {\r
+                               ListViewItem itemx = (ListViewItem) x;\r
+                               ListViewItem itemy = (ListViewItem) y;\r
+                               \r
+                               result = string.Compare(itemx.SubItems[columnIndex].Text,\r
+                                                       itemy.SubItems[columnIndex].Text);\r
+                               \r
+                               if (order == SortOrder.Descending) {\r
+                                       result = -result;\r
+                               }\r
                        }\r
+                       \r
                        return result;\r
                }\r
        }\r
index daf2a5a..5c22ea3 100644 (file)
@@ -30,11 +30,14 @@ namespace AppliStation
                \r
                string filteringKeyword = null;\r
                \r
+               AppliStation.Util.ListViewItemSortComparer sortComparer;\r
+               \r
                [Category("Action")]\r
                public event EventHandler FilterUpdated;\r
                \r
                public PackageListView()\r
                {\r
+                       ListViewItemSorter = sortComparer = new AppliStation.Util.ListViewItemSortComparer();\r
                }\r
                \r
                /// <summary>\r
@@ -217,47 +220,40 @@ namespace AppliStation
                \r
                #region Sort関連\r
                \r
-               AppliStation.Util.ListViewItemSortComparer sortComparer;\r
-               \r
-               protected override void OnColumnClick(ColumnClickEventArgs e)\r
-               {\r
-                       if (this.HeaderStyle == ColumnHeaderStyle.Clickable) {\r
-                               onColumnClick(e);\r
-                       }\r
-                       \r
-                       base.OnColumnClick(e);\r
-               }\r
-               \r
-               private void onColumnClick(ColumnClickEventArgs e)\r
+               protected void sortColumn(ColumnHeader column)\r
                {\r
+                       int columnIndex = column.Index;\r
                        SortOrder order = SortOrder.None;\r
                        \r
-                       if (sortComparer == null) {\r
-                               order = SortOrder.Ascending;\r
-                               sortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
-                               ListViewItemSorter = sortComparer;\r
-                       } else {\r
-                               if (sortComparer.Column == e.Column) {\r
-                                       order = (sortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
+                       if (sortComparer.ColumnIndex == columnIndex) {\r
+                               order = (sortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
                                        \r
-                                       sortComparer.Order = order;\r
-                               } else {\r
-                                       order = sortComparer.Order;\r
-                                       sortComparer.Column = e.Column;\r
-                               }\r
-                               \r
-                               Sort();\r
+                               sortComparer.Order = order;\r
+                       } else {\r
+                               order = SortOrder.Ascending;\r
+                               sortComparer.Column = column;\r
                        }\r
-                       AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(this, e.Column, order);\r
+                       Sort();\r
+                       \r
+                       AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(this, columnIndex, order);\r
                        \r
                        // ソート対象列の色付け\r
                        try {\r
-                               // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
-                               AppliStation.Util.NativeMethods.SendMessage(this.Handle, 0x1000+140, new IntPtr(e.Column), IntPtr.Zero);\r
+                               // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, columnIndex, NULL);\r
+                               AppliStation.Util.NativeMethods.SendMessage(this.Handle, 0x1000+140, new IntPtr(columnIndex), IntPtr.Zero);\r
                        } catch (Exception) {   \r
                        }\r
                }\r
                \r
+               protected override void OnColumnClick(ColumnClickEventArgs e)\r
+               {\r
+                       if (this.HeaderStyle == ColumnHeaderStyle.Clickable) {\r
+                               sortColumn(this.Columns[e.Column]);\r
+                       }\r
+                       \r
+                       base.OnColumnClick(e);\r
+               }\r
+               \r
                #endregion\r
        }\r
 }\r