OSDN Git Service

AppliStation-GUI,リソースファイルの画像の破損を修正
[applistation/AppliStation.git] / AppliStation / PackageListView.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using System.Collections.Generic;\r
5 using System.ComponentModel;\r
6 using NaGet.Packages;\r
7 using NaGet.Packages.Install;\r
8 \r
9 namespace AppliStation\r
10 {\r
11         /// <summary>\r
12         /// PackageListViewの表示フィルタ\r
13         /// </summary>\r
14         [Flags]\r
15         public enum PackageListViewPkgTypeFilter\r
16         {\r
17                 None = 0,\r
18                 NotInstalled = 1,\r
19                 InstalledAS = 2,\r
20                 InstalledSys = 4,\r
21                 All = NotInstalled | InstalledAS | InstalledSys\r
22         }\r
23         \r
24         public class PackageListView : ListView\r
25         {\r
26 \r
27                 PackageListsManager pkgListMan = null;\r
28                 \r
29                 PackageListViewPkgTypeFilter filteringType = PackageListViewPkgTypeFilter.All;\r
30                 \r
31                 string filteringKeyword = null;\r
32                 \r
33                 [Category("Action")]\r
34                 public event EventHandler FilterUpdated;\r
35                 \r
36                 public PackageListView()\r
37                 {\r
38                 }\r
39                 \r
40                 /// <summary>\r
41                 /// パッケージリストのデータを設定・取得する。\r
42                 /// </summary>\r
43                 [Category("Behavior")]\r
44                 [DefaultValue(null)]\r
45                 public PackageListsManager Data\r
46                 {\r
47                         get {   return this.pkgListMan; }\r
48                         set {\r
49                                 this.pkgListMan = value;\r
50                                 \r
51                                 if (this.Visible) {\r
52                                         UpdateItems();\r
53                                 }\r
54                         }\r
55                 }\r
56                 \r
57                 /// <summary>\r
58                 /// 表示パッケージ種類フィルタを設定・取得する。\r
59                 /// </summary>\r
60                 [Category("Behavior")]\r
61                 [DefaultValue(PackageListViewPkgTypeFilter.All)]\r
62                 public PackageListViewPkgTypeFilter FilteringType {\r
63                         get { return filteringType; }\r
64                         set {\r
65                                 if (filteringType != value) {\r
66                                         filteringType = value;\r
67                                         \r
68                                         if (this.Visible) {\r
69                                                 UpdateItems();\r
70                                         }\r
71                                         \r
72                                         OnFilterUpdated(new EventArgs());\r
73                                 }\r
74                         }\r
75                 }\r
76                 \r
77                 /// <summary>\r
78                 /// 表示の際のフィルタリングキーワード\r
79                 /// </summary>\r
80                 [Category("Behavior")]\r
81                 [DefaultValue(typeof(string), null)]\r
82                 public string FilteringKeyword {\r
83                         get { return filteringKeyword; }\r
84                         set {\r
85                                 if (filteringKeyword != value) {\r
86                                         filteringKeyword = value;\r
87                                         \r
88                                         if (this.Visible) {\r
89                                                 UpdateItems();\r
90                                         }\r
91                                         \r
92                                         OnFilterUpdated(new EventArgs());\r
93                                 }\r
94                         }\r
95                 }\r
96                                 \r
97                 /// <summary>\r
98                 /// 選択されたパッケージを1つ返す\r
99                 /// </summary>\r
100                 public Package SelectedPackage {\r
101                         get {\r
102                                 if (SelectedItems.Count > 0) {\r
103                                         return (Package) (SelectedItems[0].Tag);\r
104                                 } else {\r
105                                         return null;\r
106                                 }\r
107                         }\r
108                 }\r
109                 \r
110                 /// <summary>\r
111                 /// 選択されたパッケージをイテレータとして返す\r
112                 /// </summary>\r
113                 public IEnumerable<Package> SelectedPackages {\r
114                         get {\r
115                                 foreach (ListViewItem item in SelectedItems) {\r
116                                         yield return (Package) item.Tag;\r
117                                 }\r
118                         }\r
119                 }\r
120                 \r
121                 #region データ領域作成メソッド群\r
122                 \r
123                 private ListViewItem createListViewItemForPackage(Package pkg, ImageCategory imageKey)\r
124                 {\r
125                         string[] labels = new string[this.Columns.Count];\r
126                         foreach (ColumnHeader header in this.Columns) {\r
127                                 string tag = header.Tag as string;\r
128                                 if (tag == "Name")                              labels[header.Index] = pkg.Name;\r
129                                 if (tag == "Version")                   labels[header.Index] = pkg.Version;\r
130                                 if (tag == "Summary")                   labels[header.Index] = pkg.Summary;\r
131                                 if (tag == "PackageListName")   labels[header.Index] = pkg.PackageListName;\r
132                         }\r
133                         \r
134                         ListViewItem item = new ListViewItem(labels);\r
135                         item.Tag = pkg;\r
136                         item.ToolTipText = pkg.Summary;\r
137                         item.ImageKey = ImageCategory_ToString(imageKey);\r
138                         \r
139                         return item;\r
140                 }\r
141                 \r
142                 public void UpdateItems()\r
143                 {\r
144                         string ImageKeyAvailable = ImageCategory_ToString(ImageCategory.Available);\r
145                         string ImageKeyAvailableNew = ImageCategory_ToString(ImageCategory.AvailableNew);\r
146                         string ImageKeyInstalled = ImageCategory_ToString(ImageCategory.Installed);\r
147                         string ImageKeySystem = ImageCategory_ToString(ImageCategory.System);\r
148                         string keyword = filteringKeyword ?? string.Empty;\r
149                         \r
150                         this.BeginUpdate();\r
151                         this.Items.Clear();\r
152                         \r
153                         // ソフトリストの中身のパッケージ\r
154                         if ((filteringType & PackageListViewPkgTypeFilter.NotInstalled) != 0) {\r
155                                 foreach (Package pkg in pkgListMan.AvailablePkgList.Search(keyword)) {\r
156                                         if (Installation.GetPreferInstallerIndex(pkg) >= 0) { // インストール可能\r
157                                                 bool isInstalledAS = (pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name) != null);\r
158                                                 bool isInstalledSys = (pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name) != null);\r
159                                                 \r
160                                                 // インストールされていて、インストール済みソフトを表示しないならば、次のループへ\r
161                                                 if ((((filteringType & PackageListViewPkgTypeFilter.InstalledAS) == 0) && isInstalledAS) ||\r
162                                                     (((filteringType & PackageListViewPkgTypeFilter.InstalledSys) == 0) && isInstalledSys)) {\r
163                                                         continue;\r
164                                                 }\r
165                                                 \r
166                                                 ImageCategory cat = (isInstalledAS || isInstalledSys)? ImageCategory.Available : ImageCategory.AvailableNew;\r
167                                                 Items.Add(createListViewItemForPackage(pkg, cat));\r
168                                         }\r
169                                 }\r
170                         }\r
171                         \r
172                         // AppliStationにインストール済みのパッケージ\r
173                         if ((filteringType & PackageListViewPkgTypeFilter.InstalledAS) != 0) {\r
174                                 foreach (Package pkg in pkgListMan.InstalledPkgList.Search(keyword)) {\r
175                                         Items.Add(createListViewItemForPackage(pkg, ImageCategory.Installed));\r
176                                 }\r
177                         }\r
178                         \r
179                         // PCにインストール済みのパッケージ\r
180                         if ((filteringType & PackageListViewPkgTypeFilter.InstalledSys) != 0) {\r
181                                 foreach (Package pkg in pkgListMan.SystemInstalledPkgList.Search(keyword)) {\r
182                                         Items.Add(createListViewItemForPackage(pkg, ImageCategory.System));\r
183                                 }\r
184                         }\r
185                         \r
186                         this.EndUpdate();\r
187                         \r
188                         this.Refresh();\r
189                 }\r
190                 \r
191                 protected enum ImageCategory {\r
192                         Available,\r
193                         AvailableNew,\r
194                         Installed,\r
195                         System,\r
196                 };\r
197                 \r
198                 protected static string ImageCategory_ToString(ImageCategory cat)\r
199                 {\r
200                         switch (cat) {\r
201                         case ImageCategory.Available:           return "available";\r
202                         case ImageCategory.AvailableNew:        return "available-new";\r
203                         case ImageCategory.Installed:           return "installed";\r
204                         case ImageCategory.System:                      return "sys";\r
205                         default:                                                        return string.Empty;\r
206                         }\r
207                 }\r
208                 \r
209                 #endregion\r
210                 \r
211                 protected virtual void OnFilterUpdated(EventArgs e)\r
212                 {\r
213                         if (FilterUpdated != null) {\r
214                                 FilterUpdated(this, e);\r
215                         }\r
216                 }\r
217                 \r
218                 #region Sort関連\r
219                 \r
220                 AppliStation.Util.ListViewItemSortComparer sortComparer;\r
221                 \r
222                 protected override void OnColumnClick(ColumnClickEventArgs e)\r
223                 {\r
224                         if (this.HeaderStyle == ColumnHeaderStyle.Clickable) {\r
225                                 onColumnClick(e);\r
226                         }\r
227                         \r
228                         base.OnColumnClick(e);\r
229                 }\r
230                 \r
231                 private void onColumnClick(ColumnClickEventArgs e)\r
232                 {\r
233                         SortOrder order = SortOrder.None;\r
234                         \r
235                         if (sortComparer == null) {\r
236                                 order = SortOrder.Ascending;\r
237                                 sortComparer = new AppliStation.Util.ListViewItemSortComparer(e.Column, order);\r
238                                 ListViewItemSorter = sortComparer;\r
239                         } else {\r
240                                 if (sortComparer.Column == e.Column) {\r
241                                         order = (sortComparer.Order == SortOrder.Ascending)? SortOrder.Descending : SortOrder.Ascending;\r
242                                         \r
243                                         sortComparer.Order = order;\r
244                                 } else {\r
245                                         order = sortComparer.Order;\r
246                                         sortComparer.Column = e.Column;\r
247                                 }\r
248                                 \r
249                                 Sort();\r
250                         }\r
251                         AppliStation.Util.NativeMethods.ColumnHeader_SetSortState(this, e.Column, order);\r
252                         \r
253                         // ソート対象列の色付け\r
254                         try {\r
255                                 // SendMessage(hWnd, LVM_SETSELECTEDCOLUMN, column, NULL);\r
256                                 AppliStation.Util.NativeMethods.SendMessage(this.Handle, 0x1000+140, new IntPtr(e.Column), IntPtr.Zero);\r
257                         } catch (Exception) {   \r
258                         }\r
259                 }\r
260                 \r
261                 #endregion\r
262         }\r
263 }\r