PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/source/library/Interlace.UserInterface/ReactorUtilities/DeferredProgressBar.cs

https://bitbucket.org/VahidN/interlace
C# | 262 lines | 198 code | 39 blank | 25 comment | 14 complexity | 1877a0f556fca7bb7d694157a45a490c MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.ComponentModel;
  30. using System.Data;
  31. using System.Drawing;
  32. using System.Text;
  33. using System.Windows.Forms;
  34. using DevExpress.XtraEditors;
  35. #endregion
  36. namespace Interlace.ReactorUtilities
  37. {
  38. public enum OperationType
  39. {
  40. StandardProgressBar,
  41. MarqueeProgressBar
  42. }
  43. public partial class DeferredProgressBar : DevExpress.XtraEditors.XtraForm, IProgressIndicator
  44. {
  45. OperationType _operationType;
  46. DeferredObject _deferred;
  47. DeferredFailure _failureObject;
  48. object _successObject;
  49. IMonitoredOperation _monitoredOperation;
  50. public DeferredProgressBar()
  51. {
  52. InitializeComponent();
  53. }
  54. public static Deferred<object> ChainIndefinite(DeferredObject deferred, string statusMessage)
  55. {
  56. using (DeferredProgressBar form = new DeferredProgressBar())
  57. {
  58. form.OperationType = OperationType.MarqueeProgressBar;
  59. form.StatusMessage = statusMessage;
  60. form.DeferredObject = deferred;
  61. if (form.ShowDialog() == DialogResult.OK)
  62. {
  63. return Deferred.Success(form.SuccessObject);
  64. }
  65. else
  66. {
  67. return Deferred.Failure<object>(form.FailureObject);
  68. }
  69. }
  70. }
  71. public static object ShowIndefinite(DeferredObject deferred, string statusMessage)
  72. {
  73. return ShowIndefinite(deferred, statusMessage, null);
  74. }
  75. public static object ShowIndefinite(DeferredObject deferred, string statusMessage, string successMessage)
  76. {
  77. return ShowIndefinite(deferred, statusMessage, successMessage, RemoteExceptionForm.ShowFailureModal);
  78. }
  79. public static object ShowIndefinite(DeferredObject deferred, string statusMessage, string successMessage, Interlace.ReactorUtilities.VoidDeferred.Failback failback)
  80. {
  81. using (DeferredProgressBar form = new DeferredProgressBar())
  82. {
  83. form.OperationType = OperationType.MarqueeProgressBar;
  84. form.StatusMessage = statusMessage;
  85. form.DeferredObject = deferred;
  86. if (form.ShowDialog() == DialogResult.OK)
  87. {
  88. if (!string.IsNullOrEmpty(successMessage)) MessageBox.Show(successMessage, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  89. return form.SuccessObject;
  90. }
  91. else
  92. {
  93. failback(form.FailureObject);
  94. return form.FailureObject;
  95. }
  96. }
  97. }
  98. public static object ShowFinite(IMonitoredOperation monitoredOperation)
  99. {
  100. return ShowFinite(monitoredOperation, "");
  101. }
  102. public static object ShowFinite(IMonitoredOperation monitoredOperation, string successMessage)
  103. {
  104. using (DeferredProgressBar form = new DeferredProgressBar())
  105. {
  106. form.OperationType = OperationType.StandardProgressBar;
  107. form.MonitoredOperation = monitoredOperation;
  108. if (form.ShowDialog() == DialogResult.OK)
  109. {
  110. if (!string.IsNullOrEmpty(successMessage)) MessageBox.Show(successMessage, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
  111. return form.SuccessObject;
  112. }
  113. else
  114. {
  115. RemoteExceptionForm.ShowFailureModal(form.FailureObject);
  116. return form.FailureObject;
  117. }
  118. }
  119. }
  120. public IMonitoredOperation MonitoredOperation
  121. {
  122. get { return _monitoredOperation; }
  123. set
  124. {
  125. _monitoredOperation = value;
  126. _monitoredOperation.ProgressIndicator = this;
  127. }
  128. }
  129. public DeferredObject DeferredObject
  130. {
  131. get { return _deferred; }
  132. set
  133. {
  134. _deferred = value;
  135. _deferred.ObjectCompletion(delegate(object successObject)
  136. {
  137. _successObject = successObject;
  138. _failureObject = null;
  139. DialogResult = DialogResult.OK;
  140. return null;
  141. },
  142. delegate(DeferredFailure failure)
  143. {
  144. _successObject = null;
  145. _failureObject = failure;
  146. DialogResult = DialogResult.Cancel;
  147. return null;
  148. }, null);
  149. }
  150. }
  151. public object SuccessObject
  152. {
  153. get { return _successObject; }
  154. }
  155. public DeferredFailure FailureObject
  156. {
  157. get { return _failureObject; }
  158. }
  159. public OperationType OperationType
  160. {
  161. get { return _operationType; }
  162. set
  163. {
  164. _operationType = value;
  165. _marqueeProgressBar.Visible = _operationType == OperationType.MarqueeProgressBar;
  166. _progressBar.Visible = _operationType == OperationType.StandardProgressBar;
  167. }
  168. }
  169. public int Maximum
  170. {
  171. get { return _progressBar.Properties.Maximum; }
  172. set { _progressBar.Properties.Maximum = value; }
  173. }
  174. public int Current
  175. {
  176. get { return (int)_progressBar.EditValue; }
  177. set { _progressBar.EditValue = value; }
  178. }
  179. public string StatusMessage
  180. {
  181. get { return _statusLabel.Text; }
  182. set { _statusLabel.Text = value; }
  183. }
  184. private void DeferredProgressBar_FormClosing(object sender, FormClosingEventArgs e)
  185. {
  186. if (e.CloseReason == CloseReason.UserClosing)
  187. {
  188. e.Cancel = true;
  189. }
  190. }
  191. private void DeferredProgressBar_Load(object sender, EventArgs e)
  192. {
  193. if (_operationType == OperationType.StandardProgressBar)
  194. {
  195. _monitoredOperation.Start();
  196. }
  197. }
  198. #region IProgressIndicator Members
  199. public void Success()
  200. {
  201. Success(null);
  202. }
  203. public void Success(object successObject)
  204. {
  205. _successObject = successObject;
  206. _failureObject = null;
  207. DialogResult = DialogResult.OK;
  208. }
  209. public void Failure(DeferredFailure failure)
  210. {
  211. _failureObject = failure;
  212. _successObject = null;
  213. DialogResult = DialogResult.Cancel;
  214. }
  215. public void Cancel()
  216. {
  217. _failureObject = null;
  218. _successObject = null;
  219. DialogResult = DialogResult.Cancel;
  220. }
  221. #endregion
  222. }
  223. }