No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Bibliobrol : la vue console

Le code qui suit est le code de la console de Bibliobrol. Cette console nous permet d'afficher tout ce qui se passe dans l'application, en fonction du niveau de détails demandé par l'utilisateur. Cette classe a donc la responsabilité de s'abonner aux notifications des différentes classes qu'elle observe, afin de détecter tous les évènements de l'application, et de déterminer si elle doit les afficher. C'est à elle aussi qu'incombe la responsabilité d'afficher certains messages sous forme de popup, et d'autres dans la console.

Gràce à notre DAO, la console s'abonne à des classes par l'intermédiaire d'interfaces. Elle ne sait pas à quelles classes elle s'abonne en réalité, la responsabilité de ce choix restant au niveau du DAO. Ceci nous permet donc de récolter les évènements de notre système de persistance sans que le système de persistance ne doive avoir connaissance de l'usage qui sera fait de ces notifications (ni qui en fera usage, car il ne visualise ses abonnés qu'en tant que classes qui implémentent l'interface IObserver), et sans que la classe console ne doive avoir connaissance du système de persistance choisi par l'utilisateur (la classe console ne fait que s'abonner à des classes au travers de leurs interfaces).

Cette page contiendra les explications nécessaires par la suite.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using be.gaudry.observer;
  9. using be.gaudry.bibliobrol.config;
  10.  
  11. namespace be.gaudry.bibliobrol.view
  12. {
  13. /// <summary>
  14. /// Form used to display notifications throws by the model of the application.
  15. ///
  16. /// </summary>
  17. public partial class ConsoleForm : Form, IObserver
  18. {
  19. public ConsoleForm()
  20. {
  21. InitializeComponent();
  22. }
  23.  
  24. #region IObserver Members
  25.  
  26. public void update(Notification n)
  27. {
  28. if (n != null)
  29. {
  30. if (n.Level.Equals(Notification.VERBOSE.internalNotification) && n.Title.Equals("display console"))
  31. {
  32. this.Visible = n.Msg.Equals(true.ToString());
  33. }
  34. }
  35. }
  36. #endregion
  37.  
  38. #region public methods
  39. public void consoleClear()
  40. {
  41. innerConsole.Clear();
  42. }
  43. public int ConsoleCount
  44. {
  45. get { return innerConsole.Count; }
  46. }
  47. #endregion
  48.  
  49. #region Form events
  50. /// <summary>
  51. /// Gets some <code>Form</code> settings like Size, Location,...
  52. /// Restores the <code>Form</code> saved when the <code>be.gaudry.bibliobrol.view.Console</code> was previously closed.
  53. /// </summary>
  54. /// <param name="sender"></param>
  55. /// <param name="e">An <code>System.EventArgs</code> that contains the event data</param>
  56. private void ConsoleForm_Load(object sender, EventArgs e)
  57. {
  58. this.Size = Properties.Settings.Default.consoleFormSize;
  59. this.Location = Properties.Settings.Default.consoleFormLocation;
  60. Config.instanceAddObserver(this);
  61. /*StaticObservable.notify(
  62.   new Notification(
  63.   Notification.VERBOSE.debug,
  64.   "load settings",
  65.   "console settings loaded",
  66.   this
  67.   )
  68.   );*/
  69. }
  70. /// <summary>
  71. /// Saves some <code>Form</code> settings like Size, Location,...
  72. /// </summary>
  73. /// <param name="sender"></param>
  74. /// <param name="e">An <code>System.EventArgs</code> that contains the event data</param>
  75. private void ConsoleForm_FormClosing(object sender, FormClosingEventArgs e)
  76. {
  77. Properties.Settings.Default.consoleFormSize = this.Size;
  78. Properties.Settings.Default.consoleFormLocation = this.Location;
  79. Properties.Settings.Default.Save();
  80. Config.instanceRemoveObserver(this);
  81. }
  82.  
  83. #endregion
  84. }
  85. }


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using be.gaudry.bibliobrol.config;
  7. using be.gaudry.bibliobrol.view.dialogs;
  8. using be.gaudry.observer;
  9. using be.gaudry.view.dialogs;
  10. using be.gaudry.view;
  11.  
  12. namespace be.gaudry.bibliobrol.view.utils
  13. {
  14. internal delegate void UpdateDelegateHandler(Notification notification);
  15. public partial class Console : UserControl, be.gaudry.observer.IObserver
  16. {
  17. #region declarations
  18. private int lineNbr, maxLines;
  19. private Font fontNormal, fontAvert;
  20. private UpdateDelegateHandler UpdateDelegate;
  21.  
  22.  
  23. private bool buffered;
  24. private List<Exception> exBuffer = new List<Exception>();
  25. #endregion
  26.  
  27. #region constructors and destructors
  28. public Console()
  29. {
  30. buffered = false;
  31. InitializeComponent();
  32. UpdateDelegate = new UpdateDelegateHandler(safeWrite);
  33. lineNbr = 0;
  34. maxLines = 200;
  35. fontNormal = new System.Drawing.Font("Microsoft Sans Serif", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  36. fontAvert = new System.Drawing.Font(fontNormal, ((System.Drawing.FontStyle)(System.Drawing.FontStyle.Bold)));
  37. subscribeToObservables();
  38. }
  39. ~Console()
  40. {
  41. unsubscribeToObservables();
  42. Dispose(false);
  43. }
  44.  
  45. #endregion
  46.  
  47. #region IObserver Members
  48.  
  49. public void update(Notification notification)
  50. {
  51. try
  52. {
  53. cleanTsStatus();
  54. StringBuilder str;
  55. switch (notification.Level)
  56. {
  57. case Notification.VERBOSE.persistentOperation:
  58. if (Config.ConsolePersistentOps)
  59. {
  60. consoleRTB.SelectionColor = System.Drawing.Color.Orchid;
  61. write(notification);
  62. }
  63. break;
  64. case Notification.VERBOSE.internalNotification:
  65. if (Config.ConsoleDebug)
  66. {
  67. consoleRTB.SelectionColor = System.Drawing.Color.DarkViolet;
  68. write(notification);
  69. }
  70. break;
  71. case Notification.VERBOSE.showErrors:
  72. if (exBuffer.Count > 0)
  73. {
  74. new ExceptionDialog(exBuffer).Show();
  75. exBuffer.Clear();
  76. }
  77. buffered = true;
  78. break;
  79. case Notification.VERBOSE.showNewErrors:
  80. exBuffer.Clear();
  81. buffered = false;
  82. break;
  83. case Notification.VERBOSE.hideErrors:
  84. buffered = true;
  85. break;
  86. case Notification.VERBOSE.advancedOperation:
  87. if (Config.ConsoleAdvancedOps)
  88. {
  89. consoleRTB.SelectionColor = System.Drawing.Color.LightSeaGreen;
  90. write(notification);
  91. }
  92. break;
  93. case Notification.VERBOSE.basicOperation:
  94. if (Config.ConsoleBasicOps)
  95. {
  96. consoleRTB.SelectionColor = System.Drawing.Color.ForestGreen;
  97. write(notification);
  98. }
  99. break;
  100. case Notification.VERBOSE.criticalError:
  101. if (this.ParentForm is IMDIParent)
  102. {
  103. ((IMDIParent)this.ParentForm).setStatusMessage(
  104. global::be.gaudry.bibliobrol.Properties.Resources.brolLevel4,
  105. "Erreur critique : " + notification.Title,
  106. notification.Msg
  107. );
  108. }
  109. if (Config.ConsoleCriticalError)
  110. {
  111. consoleRTB.SelectionColor = System.Drawing.Color.Crimson;
  112. consoleRTB.SelectionFont = fontAvert;
  113. write(notification);
  114. consoleRTB.SelectionFont = fontNormal;
  115. }
  116. if (Config.PopupCriticalError)
  117. {
  118. if (buffered)
  119. {
  120. exBuffer.Add(notification.NotificationException);
  121. }
  122. else
  123. {
  124. ExceptionDialog.ShowDialog(notification.NotificationException, this.ParentForm);
  125. }
  126. /*str = new StringBuilder(notification.Msg);
  127.   appendException(str, notification.NotificationException);
  128.   MessageBox.Show(
  129.   str.ToString(),
  130.   "ERREUR CRITIQUE : " + notification.Title,
  131.   MessageBoxButtons.OK,
  132.   MessageBoxIcon.Stop,
  133.   MessageBoxDefaultButton.Button1);*/
  134. }
  135. break;
  136. case Notification.VERBOSE.debug:
  137. if (Config.ConsoleDebug)
  138. {
  139. consoleRTB.SelectionColor = System.Drawing.Color.DarkOrange;
  140. write(notification);
  141. }
  142. break;
  143. case Notification.VERBOSE.lowError:
  144. if (Config.ConsoleLowError)
  145. {
  146. consoleRTB.SelectionColor = System.Drawing.Color.Crimson;
  147. write(notification);
  148. }
  149. break;
  150. case Notification.VERBOSE.error:
  151. if (this.ParentForm is IMDIParent)
  152. {
  153. ((IMDIParent)this.ParentForm).setStatusMessage(
  154. global::be.gaudry.bibliobrol.Properties.Resources.brolLevel3,
  155. "Erreur : " + notification.Title,
  156. notification.Msg
  157. );
  158. }
  159. str = new StringBuilder(notification.Msg);
  160. if (Config.ConsoleOpsResult)
  161. {
  162. consoleRTB.SelectionColor = System.Drawing.Color.Crimson;
  163. consoleRTB.SelectionFont = fontAvert;
  164. write(notification);
  165. consoleRTB.SelectionFont = fontNormal;
  166. if (notification.NotificationException != null)
  167. {
  168. str.Append(notification.NotificationException.Message);
  169. str.Append("\n\nVous pouvez consulter la console pour un rapport détaillé de l'erreur.");
  170. }
  171. }
  172. if (Config.PopupOpsResult)
  173. {
  174. if (notification.NotificationException != null)
  175. {
  176. if (buffered)
  177. {
  178. exBuffer.Add(notification.NotificationException);
  179. }
  180. else
  181. {
  182. ExceptionDialog.ShowDialog(notification.NotificationException, this.ParentForm);
  183. }
  184. }
  185. else MessageBox.Show(
  186. this.ParentForm,
  187. str.ToString(),
  188. "ERREUR : " + notification.Title,
  189. MessageBoxButtons.OK,
  190. MessageBoxIcon.Error,
  191. MessageBoxDefaultButton.Button1
  192. );
  193. }
  194. break;
  195. case Notification.VERBOSE.opsResult:
  196. if (!notification.Title.Equals(String.Empty) && this.ParentForm is IMDIParent)
  197. {
  198. ((IMDIParent)this.ParentForm).setStatusMessage(
  199. global::be.gaudry.bibliobrol.Properties.Resources.brolLevel1,
  200. notification.Title,
  201. notification.Msg
  202. );
  203. }
  204. if (Config.ConsoleOpsResult)
  205. {
  206. consoleRTB.SelectionColor = System.Drawing.Color.SteelBlue;
  207. write(notification);
  208. }
  209. if (Config.PopupOpsResult)
  210. {
  211. MessageBox.Show(
  212. notification.Msg,
  213. String.Empty.Equals(notification.Title) ? "Information" : notification.Title,
  214. MessageBoxButtons.OK,
  215. MessageBoxIcon.Information,
  216. MessageBoxDefaultButton.Button1
  217. );
  218. }
  219. break;
  220. default:
  221. consoleRTB.SelectionColor = System.Drawing.Color.Black;
  222. write(notification);
  223. break;
  224. }
  225. if (lineNbr != 0 && (lineNbr % maxLines == 0))
  226. {
  227. DialogResult r = MessageBox.Show(
  228. this,
  229. String.Format("La console contient actuellement {0} notifications.Toutes les {1} notifications il est préférable de supprimer les anciens message afin de maintenir un niveau de performances acceptable.\nDésirez-vous vider la console ?", lineNbr, maxLines),
  230. "Console",
  231. MessageBoxButtons.YesNo,
  232. MessageBoxIcon.Exclamation,
  233. MessageBoxDefaultButton.Button1);
  234. if (r == DialogResult.Yes)
  235. {
  236. Clear();
  237. }
  238. }
  239. }
  240. catch (Exception ex)
  241. {
  242. StringBuilder str = new StringBuilder("Tentative d'écriture dans la console depuis un autre thread : ");
  243. appendException(str, ex);
  244. System.Console.WriteLine(str.ToString());
  245. }
  246. }
  247.  
  248. #endregion
  249.  
  250. #region private methods
  251. private void cleanTsStatus()
  252. {
  253. if (this.ParentForm is IMDIParent)
  254. {
  255. ((IMDIParent)this.ParentForm).setStatusMessage(
  256. global::be.gaudry.bibliobrol.Properties.Resources.brolConsole,
  257. String.Format("{0} message(s) dans la console", Count),
  258. ""
  259. );
  260. }
  261. }
  262. private void subscribeToObservables()
  263. {
  264. Config.instanceAddObserver(this);
  265. //model
  266. model.dao.DAOFactory.Instance.getConfigDao().addObserver(this);
  267. model.dao.DAOFactory.Instance.getPersonDao().addObserver(this);
  268. model.dao.DAOFactory.Instance.getBrolDao().addObserver(this);
  269. model.dao.DAOFactory.Instance.getMediaBrolDao().addObserver(this);
  270. model.dao.DAOFactory.Instance.getTaskDao().addObserver(this);
  271. model.dao.DAOFactory.Instance.getExporterDao().addObserver(this);
  272. model.dao.DAOFactory.Instance.getImporterDao().addObserver(this);
  273. model.dao.DAOFactory.Instance.getStatsDao().addObserver(this);
  274. model.dao.DAOFactory.Instance.getSerieDao().addObserver(this);
  275. //other views
  276. StaticObservable.addObserver(this);
  277. }
  278. private void unsubscribeToObservables()
  279. {
  280. Config.instanceRemoveObserver(this);
  281. //model
  282. model.dao.DAOFactory.Instance.getConfigDao().removeObserver(this);
  283. model.dao.DAOFactory.Instance.getPersonDao().removeObserver(this);
  284. model.dao.DAOFactory.Instance.getBrolDao().removeObserver(this);
  285. model.dao.DAOFactory.Instance.getMediaBrolDao().removeObserver(this);
  286. model.dao.DAOFactory.Instance.getTaskDao().removeObserver(this);
  287. model.dao.DAOFactory.Instance.getExporterDao().removeObserver(this);
  288. model.dao.DAOFactory.Instance.getImporterDao().removeObserver(this);
  289. model.dao.DAOFactory.Instance.getStatsDao().removeObserver(this);
  290. model.dao.DAOFactory.Instance.getSerieDao().removeObserver(this);
  291. //other views
  292. StaticObservable.removeObserver(this);
  293. }
  294.  
  295. private void appendException(StringBuilder str, Exception e)
  296. {
  297. if (e != null)
  298. {
  299. str.Append("\nDétails de l'erreur : \nSource : ");
  300. str.Append(e.Source);
  301. str.Append("\nException : ");
  302. str.Append(e.GetType().ToString());
  303. str.Append("\nTargetSite : ");
  304. str.Append(e.TargetSite);
  305. str.Append("\nInnerException : ");
  306. str.Append(e.InnerException);
  307. str.Append("\nStackTrace :\n");
  308. str.Append(e.StackTrace);
  309. str.Append("\n\nAide : http://www.gaudry.be/infobrol.html");
  310. }
  311. }
  312. private String getInfo(Notification n)
  313. {
  314. StringBuilder str = new StringBuilder("\n\n");
  315. str.Append(++lineNbr);
  316. str.Append("\t");
  317. str.Append(String.Empty.Equals(n.Title) ? "Sans titre" : n.Title);
  318. if (!String.Empty.Equals(n.Msg))
  319. {
  320. str.Append("\nMessage : ");
  321. str.Append(n.Msg);
  322. }
  323. str.Append("\nNiveau de notification : ");
  324. str.Append(Enum.GetName(typeof(Notification.VERBOSE), n.Level));
  325. if (n.Sender != null)
  326. {
  327. str.Append("\nEnvoyé par ");
  328. str.Append(n.Sender);
  329. }
  330. if (n.NotificationException != null)
  331. {
  332. appendException(str, n.NotificationException);
  333. }
  334. return str.ToString();
  335. }
  336. private void safeWrite(Notification n)
  337. {
  338. consoleRTB.AppendText(getInfo(n));
  339. consoleRTB.ScrollToCaret();
  340. }
  341. private void write(Notification n)
  342. {
  343. if (consoleRTB.InvokeRequired)
  344. {
  345. consoleRTB.Invoke(UpdateDelegate, n);
  346. }
  347. else
  348. {
  349. safeWrite((n));
  350. }
  351. }
  352. #endregion
  353.  
  354. #region public methods
  355. /// <summary>
  356. /// Cleans all displayed text on the console and resets notifications counter.
  357. /// </summary>
  358. public void Clear()
  359. {
  360. consoleRTB.Clear();
  361. lineNbr = 0;
  362. }
  363. #endregion
  364.  
  365. #region properties
  366. /// <summary>
  367. /// Number of notifications in the console
  368. /// </summary>
  369. public int Count
  370. {
  371. get { return this.lineNbr; }
  372. }
  373. #endregion
  374.  
  375. /// <summary>
  376. /// Cleans all displayed text on the console and resets notifications counter.
  377. /// </summary>
  378. /// <param name="sender"></param>
  379. /// <param name="e"></param>
  380. private void cleanConsole_Click(object sender, EventArgs e)
  381. {
  382. Clear();
  383. }
  384. /// <summary>
  385. /// Displays an option dialog to select verbose level of the console
  386. /// (allows to select the kinds of notification to display).
  387. /// </summary>
  388. /// <param name="sender"></param>
  389. /// <param name="e"></param>
  390. public void selectVerbose_Click(object sender, EventArgs e)
  391. {
  392. SelectConsoleVerboseDialog dialog = new SelectConsoleVerboseDialog();
  393. dialog.ShowDialog(ParentForm);
  394. }
  395. }
  396. }

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 17/12/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/bibliobrol-view-console.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.