PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ClassLibraryBrowser/Controls/Protocols/ProtocolMessagesControl.cs

#
C# | 294 lines | 245 code | 34 blank | 15 comment | 58 complexity | 3c3ebdd54a55bd3753bd29dd64be8b18 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * **************************************************************************
  3. *
  4. * Copyright (c) The IronSmalltalk Project.
  5. *
  6. * This source code is subject to terms and conditions of the
  7. * license agreement found in the solution directory.
  8. * See: $(SolutionDir)\License.htm ... in the root of this distribution.
  9. * By using this source code in any fashion, you are agreeing
  10. * to be bound by the terms of the license agreement.
  11. *
  12. * You must not remove this notice, or any other, from this software.
  13. *
  14. * **************************************************************************
  15. */
  16. using System;
  17. using System.ComponentModel;
  18. using System.Linq;
  19. using System.Windows.Forms;
  20. using IronSmalltalk.Tools.ClassLibraryBrowser.Coordinators;
  21. using IronSmalltalk.Tools.ClassLibraryBrowser.Definitions.Description;
  22. namespace IronSmalltalk.Tools.ClassLibraryBrowser.Controls.Protocols
  23. {
  24. public partial class ProtocolMessagesControl : UserControl
  25. {
  26. private bool Updating;
  27. #region Value Holders and Accessors
  28. private ValueHolder<Protocol> _protocol;
  29. [Browsable(false)]
  30. public ValueHolder<Protocol> ProtocolHolder
  31. {
  32. get
  33. {
  34. return this._protocol;
  35. }
  36. set
  37. {
  38. if ((this._protocol == null) && (value == null))
  39. return;
  40. if (value == null)
  41. throw new ArgumentNullException();
  42. if (this._protocol == value)
  43. return;
  44. if (this._protocol != null)
  45. throw new InvalidOperationException("Value holder may be set only once.");
  46. this._protocol = value;
  47. value.Changed += new ValueChangedEventHandler<Protocol>(this.ProtocolChanged);
  48. this.protocolMessageControl.ProtocolHolder = value;
  49. }
  50. }
  51. public Protocol Protocol
  52. {
  53. get
  54. {
  55. if (this.ProtocolHolder == null)
  56. return null;
  57. return this.ProtocolHolder.Value;
  58. }
  59. }
  60. private ValueHolder<Definitions.Description.Message> _message;
  61. [Browsable(false)]
  62. public ValueHolder<Definitions.Description.Message> MessageHolder
  63. {
  64. get
  65. {
  66. return this._message;
  67. }
  68. set
  69. {
  70. if ((this._message == null) && (value == null))
  71. return;
  72. if (value == null)
  73. throw new ArgumentNullException();
  74. if (this._message == value)
  75. return;
  76. if (this._message != null)
  77. throw new InvalidOperationException("Value holder may be set only once.");
  78. this._message = value;
  79. value.Changed += new ValueChangedEventHandler<Definitions.Description.Message>(this.MessageChanged);
  80. }
  81. }
  82. public Definitions.Description.Message Message
  83. {
  84. get
  85. {
  86. if (this.MessageHolder == null)
  87. return null;
  88. return this.MessageHolder.Value;
  89. }
  90. }
  91. #endregion
  92. public ProtocolMessagesControl()
  93. {
  94. this.MessageHolder = new ValueHolder<Definitions.Description.Message>();
  95. InitializeComponent();
  96. this.protocolMessageControl.MessageHolder = this.MessageHolder;
  97. }
  98. #region View Updating
  99. private void ProtocolChanged(object sender, ValueChangedEventArgs<Protocol> e)
  100. {
  101. bool remember = this.Updating;
  102. this.Updating = true;
  103. try
  104. {
  105. this.Enabled = (this.Protocol != null);
  106. this.FillMessagesList();
  107. this.SetMenuItemState(this.Visible);
  108. }
  109. finally
  110. {
  111. this.Updating = remember;
  112. }
  113. }
  114. private void MessageChanged(object sender, ValueChangedEventArgs<Definitions.Description.Message> e)
  115. {
  116. bool remember = this.Updating;
  117. try
  118. {
  119. this.Updating = true;
  120. this.listMessages.SelectedItems.Clear();
  121. foreach (ListViewItem lvi in this.listMessages.Items)
  122. {
  123. if (lvi.Tag == e.NewValue)
  124. lvi.Selected = true;
  125. }
  126. }
  127. finally
  128. {
  129. this.Updating = false;
  130. }
  131. this.SetMenuItemState(this.Visible);
  132. }
  133. private void FillMessagesList()
  134. {
  135. this.listMessages.BeginUpdate();
  136. try
  137. {
  138. this.listMessages.Items.Clear();
  139. if (this.Protocol == null)
  140. return;
  141. foreach (Definitions.Description.Message item in this.Protocol.Messages.OrderBy(m => m.Selector))
  142. {
  143. ListViewItem lvi = this.listMessages.Items.Add(item.Selector);
  144. lvi.Name = item.Selector;
  145. lvi.Tag = item;
  146. }
  147. }
  148. finally
  149. {
  150. this.listMessages.EndUpdate();
  151. }
  152. }
  153. private void listMessages_SizeChanged(object sender, EventArgs e)
  154. {
  155. this.listMessages.Columns[0].Width = this.listMessages.Width - 5;
  156. }
  157. private void ProtocolMessagesControl_Load(object sender, EventArgs e)
  158. {
  159. this.listMessages.Columns[0].Width = this.listMessages.Width - 5;
  160. }
  161. private void listMessages_ItemChanging(object sender, ListItemChangingEventArgs e)
  162. {
  163. if (this.Updating)
  164. return;
  165. if (e.Item == null)
  166. return;
  167. Definitions.Description.Message m = e.Item.Tag as Definitions.Description.Message;
  168. this.MessageHolder.Value = m;
  169. e.Item = this.listMessages.Items.Cast<ListViewItem>()
  170. .FirstOrDefault(i => i.Tag == this.Message);
  171. }
  172. #endregion
  173. #region Menu
  174. private ToolStripMenuItem CreateNewMessageMenuItem;
  175. private ToolStripMenuItem SaveMessageMenuItem;
  176. private ToolStripMenuItem DeleteMessageMenuItem;
  177. public void AddMenuItems(ToolStripMenuItem parentMenu)
  178. {
  179. ToolStripSeparator tsp = new ToolStripSeparator();
  180. tsp.Name = "ToolStripSeparatorMessage";
  181. tsp.MergeIndex = 200;
  182. tsp.MergeAction = MergeAction.Insert;
  183. parentMenu.DropDownItems.Add(tsp);
  184. this.CreateNewMessageMenuItem = new ToolStripMenuItem();
  185. this.CreateNewMessageMenuItem.Name = "CreateNewMessageMenuItem";
  186. this.CreateNewMessageMenuItem.Text = "Create New Message";
  187. this.CreateNewMessageMenuItem.Enabled = false;
  188. this.CreateNewMessageMenuItem.MergeIndex = 210;
  189. this.CreateNewMessageMenuItem.MergeAction = MergeAction.Insert;
  190. this.CreateNewMessageMenuItem.Click += new EventHandler(this.CreateNewMessagelMenuItem_Click);
  191. parentMenu.DropDownItems.Add(this.CreateNewMessageMenuItem);
  192. this.SaveMessageMenuItem = new ToolStripMenuItem();
  193. this.SaveMessageMenuItem.Name = "SaveMessageMenuItem";
  194. this.SaveMessageMenuItem.Text = "Save Message";
  195. this.SaveMessageMenuItem.Enabled = false;
  196. this.SaveMessageMenuItem.MergeIndex = 220;
  197. this.SaveMessageMenuItem.MergeAction = MergeAction.Insert;
  198. this.SaveMessageMenuItem.Click += new EventHandler(this.SaveMessagelMenuItem_Click);
  199. parentMenu.DropDownItems.Add(this.SaveMessageMenuItem);
  200. this.DeleteMessageMenuItem = new ToolStripMenuItem();
  201. this.DeleteMessageMenuItem.Name = "DeleteMessageMenuItem";
  202. this.DeleteMessageMenuItem.Text = "Delete Message";
  203. this.DeleteMessageMenuItem.Enabled = false;
  204. this.DeleteMessageMenuItem.MergeIndex = 230;
  205. this.DeleteMessageMenuItem.MergeAction = MergeAction.Insert;
  206. this.DeleteMessageMenuItem.Click += new EventHandler(this.DeleteMessageMenuItem_Click);
  207. parentMenu.DropDownItems.Add(this.DeleteMessageMenuItem);
  208. }
  209. void DeleteMessageMenuItem_Click(object sender, EventArgs e)
  210. {
  211. if (this.Protocol == null)
  212. return;
  213. if (this.Message == null)
  214. return;
  215. var dlgres = MessageBox.Show(String.Format("Do you want to delete message #{0}?", this.Message.Selector),
  216. "Delete Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  217. if (dlgres != DialogResult.Yes)
  218. return;
  219. this.Message.Protocol.Messages.Remove(this.Message);
  220. this.MessageHolder.Value = null;
  221. this.ProtocolHolder.TriggerChanged(this.Protocol, this.Protocol);
  222. }
  223. private void SaveMessagelMenuItem_Click(object sender, EventArgs e)
  224. {
  225. this.protocolMessageControl.Save();
  226. }
  227. private void CreateNewMessagelMenuItem_Click(object sender, EventArgs e)
  228. {
  229. if (this.Protocol == null)
  230. return;
  231. Definitions.Description.Message m = new Definitions.Description.Message(this.Protocol);
  232. if (!this.MessageHolder.SetValue(m))
  233. return;
  234. }
  235. public void VisibilityChanged(bool visible)
  236. {
  237. this.SetMenuItemState(visible);
  238. }
  239. private void SetMenuItemState(bool visible)
  240. {
  241. if (!visible)
  242. {
  243. if (this.CreateNewMessageMenuItem != null)
  244. this.CreateNewMessageMenuItem.Enabled = false;
  245. if (this.SaveMessageMenuItem != null)
  246. this.SaveMessageMenuItem.Enabled = false;
  247. if (this.DeleteMessageMenuItem != null)
  248. this.DeleteMessageMenuItem.Enabled = false;
  249. return;
  250. }
  251. if (this.CreateNewMessageMenuItem != null)
  252. this.CreateNewMessageMenuItem.Enabled = (this.Protocol != null);
  253. if (this.SaveMessageMenuItem != null)
  254. this.SaveMessageMenuItem.Enabled = (this.Message != null);
  255. if (this.DeleteMessageMenuItem != null)
  256. this.DeleteMessageMenuItem.Enabled = (this.Message != null);
  257. }
  258. #endregion
  259. }
  260. }