PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/UiException/Controls/ErrorToolbar.cs

#
C# | 232 lines | 158 code | 49 blank | 25 comment | 31 complexity | 95929eca6f8fca509673655e1fd0747f MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You may
  3. // obtain a copy of the license at http://nunit.org
  4. // ****************************************************************
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Collections;
  10. using System.Drawing;
  11. using System.Diagnostics;
  12. using NUnit.UiException.Properties;
  13. namespace NUnit.UiException.Controls
  14. {
  15. /// <summary>
  16. /// A specialization of a ToolStrip to show instances of IErrorDisplay.
  17. /// </summary>
  18. public class ErrorToolbar :
  19. ToolStrip,
  20. IEnumerable
  21. {
  22. public event EventHandler SelectedRendererChanged;
  23. private List<IErrorDisplay> _displays;
  24. private ToolStripItem _separator;
  25. private int _selection;
  26. public ErrorToolbar()
  27. {
  28. _displays = new List<IErrorDisplay>();
  29. _separator = CreateDefaultItem("-", null, null);
  30. Items.Add(_separator);
  31. _selection = -1;
  32. BackgroundImage = Resources.ImageErrorBrowserHeader;
  33. BackgroundImageLayout = ImageLayout.Tile;
  34. return;
  35. }
  36. /// <summary>
  37. /// Create and configure a ToolStripButton.
  38. /// </summary>
  39. public static ToolStripButton NewStripButton(
  40. bool canCheck, string text, Image image, EventHandler onClick)
  41. {
  42. ToolStripButton button;
  43. button = new ToolStripButton(text, image, onClick);
  44. button.CheckOnClick = canCheck;
  45. button.Image = image;
  46. button.ImageScaling = ToolStripItemImageScaling.None;
  47. button.TextImageRelation = TextImageRelation.ImageBeforeText;
  48. button.DisplayStyle = ToolStripItemDisplayStyle.Image;
  49. return (button);
  50. }
  51. /// <summary>
  52. /// Gets the count of IErrorDisplay instances.
  53. /// </summary>
  54. public int Count
  55. {
  56. get { return (_displays.Count); }
  57. }
  58. /// <summary>
  59. /// Gets the display at the given index.
  60. /// </summary>
  61. public IErrorDisplay this[int index]
  62. {
  63. get { return (_displays[index]); }
  64. }
  65. /// <summary>
  66. /// Gets or sets the IErrorDisplay to be selected.
  67. /// </summary>
  68. public IErrorDisplay SelectedDisplay
  69. {
  70. get {
  71. if (_selection == -1)
  72. return (null);
  73. return ((IErrorDisplay)Items[_selection].Tag);
  74. }
  75. set
  76. {
  77. int index = IndexOf(value);
  78. UiExceptionHelper.CheckFalse(index == -1 && value != null,
  79. "Cannot select unregistered display.", "SelectedDisplay");
  80. if (index == _selection)
  81. return;
  82. _selection = index;
  83. SetOrUnsetCheckedFlag(_selection);
  84. ShowOrHideOptionItems(_selection);
  85. if (SelectedRendererChanged != null)
  86. SelectedRendererChanged(this, new EventArgs());
  87. return;
  88. }
  89. }
  90. /// <summary>
  91. /// Register a new IErrorDisplay in the toolbar.
  92. /// </summary>
  93. public void Register(IErrorDisplay display)
  94. {
  95. ToolStripItem item;
  96. int sepIndex;
  97. UiExceptionHelper.CheckNotNull(display, "display");
  98. UiExceptionHelper.CheckNotNull(display.PluginItem, "display.PluginItem");
  99. item = display.PluginItem;
  100. item.Tag = display;
  101. item.Click += new EventHandler(item_Click);
  102. _displays.Add(display);
  103. sepIndex = Items.IndexOf(_separator);
  104. Items.Insert(sepIndex, item);
  105. if (display.OptionItems != null)
  106. {
  107. ToolStripItem[] array = display.OptionItems;
  108. foreach (ToolStripItem value in array)
  109. {
  110. value.Visible = false;
  111. Items.Add(value);
  112. }
  113. }
  114. if (_displays.Count == 1)
  115. SelectedDisplay = display;
  116. return;
  117. }
  118. /// <summary>
  119. /// Clears all IErrorDisplay in the toolbar.
  120. /// </summary>
  121. public void Clear()
  122. {
  123. _displays.Clear();
  124. Items.Clear();
  125. Items.Add(_separator);
  126. return;
  127. }
  128. private void ShowOrHideOptionItems(int selectedIndex)
  129. {
  130. int index;
  131. foreach (IErrorDisplay item in _displays)
  132. {
  133. if ((index = IndexOf(item)) == -1)
  134. continue;
  135. if (item.OptionItems == null)
  136. continue;
  137. foreach (ToolStripItem stripItem in item.OptionItems)
  138. stripItem.Visible = (index == selectedIndex);
  139. }
  140. return;
  141. }
  142. private void SetOrUnsetCheckedFlag(int selectedIndex)
  143. {
  144. int index;
  145. foreach (IErrorDisplay item in _displays)
  146. {
  147. index = IndexOf(item);
  148. if (index == -1)
  149. continue;
  150. item.PluginItem.Checked = (index == selectedIndex);
  151. }
  152. return;
  153. }
  154. private int IndexOf(IErrorDisplay renderer)
  155. {
  156. int i;
  157. if (renderer == null)
  158. return (-1);
  159. for (i = 0; i < Items.Count; ++i)
  160. if (object.ReferenceEquals(Items[i].Tag, renderer))
  161. return (i);
  162. return (-1);
  163. }
  164. private void item_Click(object sender, EventArgs e)
  165. {
  166. ToolStripItem item = sender as ToolStripItem;
  167. IErrorDisplay renderer;
  168. if (item == null || item.Tag == null)
  169. return;
  170. renderer = item.Tag as IErrorDisplay;
  171. if (renderer == null)
  172. return;
  173. SelectedDisplay = renderer;
  174. return;
  175. }
  176. #region IEnumerable Membres
  177. public IEnumerator GetEnumerator()
  178. {
  179. return (_displays.GetEnumerator());
  180. }
  181. #endregion
  182. }
  183. }