OSDN Git Service

AppliStation-GUI,タスク進捗ダイアログの自動クローズ機能を追加。
[applistation/AppliStation.git] / AppliStation / AppliStation.Util / ExecutionProgressViewer.cs
1 using System;\r
2 using System.Drawing;\r
3 using System.Windows.Forms;\r
4 using System.Threading;\r
5 using NaGet.SubCommands;\r
6 using NaGet.Net;\r
7 \r
8 namespace AppliStation.Util\r
9 {\r
10         /// <summary>\r
11         /// Description of ExecutionProgressViewer.\r
12         /// </summary>\r
13         public partial class ExecutionProgressViewer : Form\r
14         {\r
15                 private NaGetTaskSet taskSet;\r
16                 \r
17                 private Thread tasksetRunningThread = null;\r
18                 \r
19                 /// <summary>\r
20                 /// 終了時に何を行うかのフラグ\r
21                 /// </summary>\r
22                 public enum ActionOnDoneFlags {\r
23                         None = 0,\r
24                         FlashWindow = 1,\r
25                         AutoCloseOnSuccess = 2,\r
26                 }\r
27                 \r
28                 /// <summary>\r
29                 /// 終了時に何を行うか\r
30                 /// </summary>\r
31                 public ActionOnDoneFlags ActionOnDone = ActionOnDoneFlags.FlashWindow;\r
32                                 \r
33                 public ExecutionProgressViewer()\r
34                 {\r
35                         //\r
36                         // The InitializeComponent() call is required for Windows Forms designer support.\r
37                         //\r
38                         InitializeComponent();\r
39                         \r
40                         this.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);\r
41                         toolTip.SetToolTip(autoCloseCheckBox,\r
42                                            string.Format("エラーがなく正常に終了した場合、{0}秒後に自動的にこのダイアログを閉じます", autoCloseTimer.Interval/1000));\r
43                 }\r
44                                 \r
45                 private void onDownloadEvent(object sender, DownloadEventArgs a)\r
46                 {\r
47                         if (InvokeRequired) {\r
48                                 Invoke(new EventHandler<DownloadEventArgs>(onDownloadEvent), sender, a);\r
49                                 return;\r
50                         }\r
51                         \r
52                         \r
53                         if (a.TaskProgressPercent >= 0) {\r
54                                 progressBarSub.Value = (int) a.TaskProgressPercent;\r
55                                 progressBarSub.Style = ProgressBarStyle.Continuous;\r
56                         } else {\r
57                                 progressBarSub.Style = ProgressBarStyle.Marquee;\r
58                         }\r
59                         \r
60                         switch (a.Type) {\r
61                                 case DownloadEventType.INITED:\r
62                                 case DownloadEventType.CONNECTED:\r
63                                         progressSubLabel.Text = a.TaskMessage;\r
64                                         \r
65                                         progressBarSub.Visible = true;\r
66                                         progressSubLabel.Visible = true;\r
67                                         break;\r
68                                 case DownloadEventType.DOWNLOADING:\r
69                                         progressSubLabel.Text = a.TaskMessage;\r
70                                         break;\r
71                                 case DownloadEventType.COMPLETED:\r
72                                         progressBarSub.Visible = false;\r
73                                         progressSubLabel.Visible = false;\r
74                                         break;\r
75                                 case DownloadEventType.ERROR:\r
76                                         progressBarSub.Visible = false;\r
77                                         progressSubLabel.Visible = false;\r
78                                         \r
79                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
80                                         logBox.AppendText("   [エラー] " + a.TaskMessage + System.Environment.NewLine);\r
81                                         logBox.SelectionColor = logBox.ForeColor;\r
82                                         break;\r
83                         }\r
84                 }\r
85                 \r
86                 #region NaGetTaskSet関連\r
87                 \r
88                 private void onTaskSetRaised(object sender, NaGetTaskSetEventArgs e)\r
89                 {\r
90                         NaGetTaskSet taskSet = (NaGetTaskSet) sender;\r
91                         \r
92                         if (e.TaskProgressPercent >= 0) {\r
93                                 progressBar.Value = (int) e.TaskProgressPercent;\r
94                                 progressBar.Style = ProgressBarStyle.Continuous;\r
95                         } else {\r
96                                 progressBar.Style = ProgressBarStyle.Marquee;\r
97                         }\r
98                         progressLabel.Text = e.TaskMessage ?? string.Empty;\r
99                         \r
100                         cancelButton.Enabled = taskSet.Running && taskSet.Cancelable;\r
101                         \r
102                         switch (e.Type) {\r
103                                 case NaGetTaskSetEventType.COMPLETED:\r
104                                         logBox.AppendText("完了." + System.Environment.NewLine);\r
105                                         if (taskSet.Done) {\r
106                                                 okButton.Enabled = true;\r
107                                                 cancelButton.Enabled = false;\r
108                                                 \r
109                                                 // タスクが完了したらしばらく待って閉じるために、自動クローズタイマーを起動する\r
110                                                 autoCloseTimer.Start();\r
111                                         }\r
112                                         break;\r
113                                 case NaGetTaskSetEventType.STARTED_TASKSET:\r
114                                         subtitleLabel.Text = taskSet.TaskSetNames[taskSet.CurrentTaskSetIndex];\r
115                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
116                                         break;\r
117                                 case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
118                                         if (progressBarSub.Visible) progressBarSub.Hide();\r
119                                         if (progressSubLabel.Visible) progressSubLabel.Hide();\r
120                                         \r
121                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:ノーマル\r
122                                         \r
123                                         logBox.AppendText(string.Format(" ... 完了. [{0}%]", (int) e.TaskProgressPercent));\r
124                                         logBox.AppendText(System.Environment.NewLine);\r
125                                         break;\r
126                                 case NaGetTaskSetEventType.INFO:\r
127                                         logBox.AppendText("  " + e.TaskMessage + System.Environment.NewLine);\r
128                                         break;\r
129                                 case NaGetTaskSetEventType.ERROR:\r
130                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
131                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
132                                         logBox.SelectionColor = logBox.ForeColor;\r
133                                         \r
134                                         NativeMethods.ProgressBar_SetState(progressBar, 2); // VistaProgress色:エラー\r
135                                         \r
136                                         autoCloseCheckBox.Enabled = false;\r
137                                         okButton.Enabled = true;\r
138                                         cancelButton.Enabled = false;\r
139                                         break;\r
140                                 case NaGetTaskSetEventType.CANCELED:\r
141                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
142                                         logBox.AppendText(e.TaskMessage + System.Environment.NewLine);\r
143                                         logBox.SelectionColor = logBox.ForeColor;\r
144                                         \r
145                                         NativeMethods.ProgressBar_SetState(progressBar, 1); // VistaProgress色:中断\r
146                                         \r
147                                         autoCloseCheckBox.Enabled = false;\r
148                                         okButton.Enabled = true;\r
149                                         cancelButton.Enabled = false;\r
150                                         break;\r
151                                 case NaGetTaskSetEventType.WARNING:\r
152                                         logBox.SelectionColor = System.Drawing.Color.Red;\r
153                                         logBox.AppendText("  [エラー] " + e.TaskMessage + System.Environment.NewLine);\r
154                                         logBox.SelectionColor = logBox.ForeColor;\r
155                                         \r
156                                         autoCloseCheckBox.Enabled = false;\r
157                                         break;\r
158                         }\r
159                         \r
160                         if (taskSet.Done) {\r
161                                 if ((ActionOnDone & ActionOnDoneFlags.FlashWindow) != 0) {\r
162                                         NativeMethods.Form_FlashWindow(this,\r
163                                                                NativeMethods.FlashFlag.All | NativeMethods.FlashFlag.TimerNoFG,\r
164                                                                uint.MaxValue, 0);\r
165                                 }\r
166                                 if (okButton.Enabled && (ActionOnDone & ActionOnDoneFlags.AutoCloseOnSuccess) != 0) {\r
167                                         this.DialogResult = DialogResult.OK;\r
168                                         Close();\r
169                                         Dispose();\r
170                                 }\r
171                         }\r
172                 }\r
173                 \r
174                 private NaGetTaskQueryResult onTaskQueryRaised(object sender, NaGetTaskQueryArgs e)\r
175                 {\r
176                         MessageBoxButtons buttons = MessageBoxButtons.OKCancel;\r
177                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
178                                 buttons = MessageBoxButtons.AbortRetryIgnore;\r
179                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
180                                 buttons = MessageBoxButtons.RetryCancel;\r
181                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
182                                 buttons = MessageBoxButtons.OK;\r
183                         }\r
184                         \r
185                         DialogResult result = MessageBox.Show(e.Message, this.Text, buttons);\r
186                         \r
187                         switch (result) {\r
188                                 case DialogResult.OK:\r
189                                 case DialogResult.Ignore:\r
190                                         return NaGetTaskQueryResult.CONTINUE;\r
191                                 case DialogResult.Cancel:\r
192                                 case DialogResult.Abort:\r
193                                         return NaGetTaskQueryResult.CANCEL;\r
194                                 case DialogResult.Retry:\r
195                                         return NaGetTaskQueryResult.RETRY;\r
196                                 default:\r
197                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
198                         }\r
199                 }\r
200                 \r
201                 public void SetTaskSet(NaGetTaskSet taskSet)\r
202                 {\r
203                         this.taskSet = taskSet;\r
204                         \r
205                         taskSet.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {\r
206                                 if (InvokeRequired) {\r
207                                         Invoke(new EventHandler<NaGetTaskSetEventArgs>(onTaskSetRaised), taskSet, e);\r
208                                 } else {\r
209                                         onTaskSetRaised(taskSet, e);\r
210                                 }\r
211                         };\r
212                         \r
213                         System.Reflection.PropertyInfo fDownloader = taskSet.GetType().GetProperty("Downloader");\r
214                         if (fDownloader != null && (fDownloader.PropertyType == typeof(Downloader))) {\r
215                                 ((Downloader) fDownloader.GetValue(taskSet, null)).DownloadEventRaised += onDownloadEvent;\r
216                         }\r
217                         \r
218                         taskSet.TaskQueryRaised += onTaskQueryRaised;\r
219                 }\r
220 \r
221                 public void StartTaskSet()\r
222                 {\r
223                         tasksetRunningThread = new Thread(taskSet.Run);\r
224                         tasksetRunningThread.Start();\r
225                 }\r
226                 \r
227                 #endregion\r
228                 \r
229                 void OkButtonClick(object sender, EventArgs e)\r
230                 {\r
231                         if (taskSet == null || taskSet.Done) {\r
232                                 this.Close();\r
233                                 this.Dispose();\r
234                         }\r
235                 }\r
236                 \r
237                 void CancelButtonClick(object sender, EventArgs e)\r
238                 {\r
239                         if (InvokeRequired) {\r
240                                 Invoke(new EventHandler(CancelButtonClickConcrete), sender, e);\r
241                         } else {\r
242                                 CancelButtonClickConcrete(sender,e);\r
243                         }\r
244                 }\r
245                 \r
246                 void CancelButtonClickConcrete(object sender, EventArgs e)\r
247                 {\r
248                         autoCloseCheckBox.Enabled = false;\r
249                         if (taskSet != null && taskSet.Running && taskSet.Cancelable) {\r
250                                 cancelButton.Enabled = false;\r
251                                 \r
252                                 NativeMethods.ProgressBar_SetState(progressBar, 3); // VistaProgress色:中断\r
253                                 \r
254                                 taskSet.Cancel();\r
255                         }\r
256                 }\r
257                 \r
258                 void ExecutionProgressViewerShown(object sender, EventArgs e)\r
259                 {\r
260                         this.BringToFront();\r
261                 }\r
262                 \r
263                 void ExecutionProgressViewerFormClosed(object sender, FormClosedEventArgs e)\r
264                 {\r
265                         autoCloseTimer.Enabled = false;\r
266                 }\r
267                 \r
268                 void AutoCloseTimerTick(object sender, EventArgs e)\r
269                 {\r
270                         autoCloseTimer.Stop();\r
271                         \r
272                         if (InvokeRequired) {\r
273                                 Invoke(new EventHandler(AutoCloseTimerTickConcrete), sender, e);\r
274                         } else {\r
275                                 AutoCloseTimerTickConcrete(sender,e);\r
276                         }\r
277                 }\r
278                 \r
279                 void AutoCloseTimerTickConcrete(object sender, EventArgs e)\r
280                 {\r
281                         // autoCloseCheckBoxが有効(正常終了)かつチェックのときに限り、OKボタンを自動的にクリック\r
282                         if (autoCloseCheckBox.Enabled && autoCloseCheckBox.Checked && okButton.Enabled) {\r
283                                 OkButtonClick(sender, e);\r
284                         } else if (! autoCloseCheckBox.Enabled) {\r
285                                 // タイマーの時間がすぎて、そのとき自動で閉じないようにしていしてあるならば、手動で閉じるように\r
286                                 autoCloseCheckBox.Enabled = false;\r
287                         }\r
288                 }\r
289         }\r
290 }\r