PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ClassLibraryBrowser/Controls/Pools/PoolValuesControl.cs

#
C# | 341 lines | 291 code | 33 blank | 17 comment | 67 complexity | 7a8642baca77281a30858d23f106a3d4 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.Drawing;
  19. using System.Linq;
  20. using System.Windows.Forms;
  21. using IronSmalltalk.Tools.ClassLibraryBrowser.Coordinators;
  22. using IronSmalltalk.Tools.ClassLibraryBrowser.Definitions.Implementation;
  23. namespace IronSmalltalk.Tools.ClassLibraryBrowser.Controls.Pools
  24. {
  25. public partial class PoolValuesControl : UserControl
  26. {
  27. public bool Dirty { get; set; }
  28. private bool Updating = false;
  29. private ValueHolder<Pool> _pool;
  30. [Browsable(false)]
  31. [System.ComponentModel.ReadOnly(true)]
  32. public ValueHolder<Pool> PoolHolder
  33. {
  34. get
  35. {
  36. return this._pool;
  37. }
  38. set
  39. {
  40. if (value == null)
  41. throw new ArgumentNullException();
  42. if (this._pool != null)
  43. {
  44. this._pool.Changed -= new ValueChangedEventHandler<Pool>(this.PoolChanged);
  45. this._pool.Changing -= new ValueChangingEventHandler<Definitions.Implementation.Pool>(this.PoolChanging);
  46. }
  47. this._pool = value;
  48. this._pool.Changing += new ValueChangingEventHandler<Definitions.Implementation.Pool>(this.PoolChanging);
  49. this._pool.Changed += new ValueChangedEventHandler<Pool>(this.PoolChanged);
  50. }
  51. }
  52. public Pool Pool
  53. {
  54. get
  55. {
  56. if (this.PoolHolder == null)
  57. return null;
  58. return this.PoolHolder.Value;
  59. }
  60. }
  61. private ValueHolder<PoolValue> _poolValue;
  62. [Browsable(false)]
  63. [System.ComponentModel.ReadOnly(true)]
  64. public ValueHolder<PoolValue> PoolValueHolder
  65. {
  66. get
  67. {
  68. return this._poolValue;
  69. }
  70. set
  71. {
  72. if (value == null)
  73. throw new ArgumentNullException();
  74. if (this._poolValue != null)
  75. {
  76. this._poolValue.Changing -= new ValueChangingEventHandler<Definitions.Implementation.PoolValue>(this.PoolValueChanging);
  77. this._poolValue.Changed -= new ValueChangedEventHandler<PoolValue>(this.PoolValueChanged);
  78. }
  79. this._poolValue = value;
  80. this._poolValue.Changing += new ValueChangingEventHandler<Definitions.Implementation.PoolValue>(this.PoolValueChanging);
  81. this._poolValue.Changed += new ValueChangedEventHandler<PoolValue>(this.PoolValueChanged);
  82. }
  83. }
  84. public PoolValue PoolValue
  85. {
  86. get
  87. {
  88. if (this.PoolValueHolder == null)
  89. return null;
  90. return this.PoolValueHolder.Value;
  91. }
  92. }
  93. public PoolValuesControl()
  94. {
  95. InitializeComponent();
  96. }
  97. private void PoolChanged(object sender, ValueChangedEventArgs<Pool> e)
  98. {
  99. this.Enabled = (this.Pool != null);
  100. bool remember = this.Updating;
  101. this.Updating = true;
  102. try
  103. {
  104. this.FillVariablesListView();
  105. }
  106. finally
  107. {
  108. this.Updating = remember;
  109. }
  110. }
  111. private void PoolChanging(object sender, ValueChangingEventArgs<Pool> e)
  112. {
  113. if (!this.Dirty)
  114. return;
  115. if (e.Cancel)
  116. return;
  117. var dlgResult = MessageBox.Show(
  118. String.Format("Do you want to save changes to {0}?", this.txtPoolVarName.Text),
  119. "Pool Value", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  120. if (dlgResult == DialogResult.Yes)
  121. e.Cancel = !this.Save();
  122. else if (dlgResult == DialogResult.No)
  123. //this.AllProtocolsChanged(this, e);
  124. { }
  125. else
  126. e.Cancel = true;
  127. }
  128. private void PoolValueChanging(object sender, ValueChangingEventArgs<PoolValue> e)
  129. {
  130. if (!this.Dirty)
  131. return;
  132. if (e.Cancel)
  133. return;
  134. var dlgResult = MessageBox.Show(
  135. String.Format("Do you want to save changes to {0}?", this.txtPoolVarName.Text),
  136. "Pool Value", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
  137. if (dlgResult == DialogResult.Yes)
  138. e.Cancel = !this.Save();
  139. else if (dlgResult == DialogResult.No)
  140. //this.AllProtocolsChanged(this, e);
  141. { }
  142. else
  143. e.Cancel = true;
  144. }
  145. private void PoolValueChanged(object sender, ValueChangedEventArgs<PoolValue> e)
  146. {
  147. bool remember = this.Updating;
  148. this.Updating = true;
  149. try
  150. {
  151. this.FillDefinitionView();
  152. this.SetSelection();
  153. this.MarkClean();
  154. }
  155. finally
  156. {
  157. this.Updating = remember;
  158. }
  159. }
  160. private void FillVariablesListView()
  161. {
  162. this.listPoolVars.Items.Clear();
  163. if (this.Pool == null)
  164. return;
  165. foreach (PoolValue value in this.Pool.Values.OrderBy(v => v.Name))
  166. {
  167. ListViewItem lvi = this.listPoolVars.Items.Add(value.Name, "field");
  168. lvi.SubItems.Add((value.PoolValueType == PoolValueTypeEnum.Constant) ? "Constant" : "Variable");
  169. lvi.Tag = value;
  170. lvi.Selected = (value == this.PoolValue);
  171. }
  172. }
  173. private void SetSelection()
  174. {
  175. foreach (ListViewItem lvi in this.listPoolVars.Items)
  176. lvi.Selected = (lvi.Tag == this.PoolValue);
  177. }
  178. private void FillDefinitionView()
  179. {
  180. if (this.PoolValue == null)
  181. {
  182. this.txtPoolVarName.Text = null;
  183. this.txtPoolVarName.Enabled = false;
  184. this.txtSourceCode.Text = null;
  185. this.txtSourceCode.Enabled = false;
  186. this.comboType.SelectedIndex = -1;
  187. this.comboType.Enabled = false;
  188. }
  189. else
  190. {
  191. this.txtPoolVarName.Text = this.PoolValue.Name;
  192. this.txtPoolVarName.Enabled = true;
  193. this.txtSourceCode.Text = this.PoolValue.Initializer.Source;
  194. this.txtSourceCode.Enabled = true;
  195. this.comboType.SelectedIndex = (this.PoolValue.PoolValueType == PoolValueTypeEnum.Constant) ? 0 : 1;
  196. this.comboType.Enabled = true;
  197. }
  198. }
  199. public bool Save()
  200. {
  201. if (this.Pool == null)
  202. return false;
  203. string msg = this.ValidateName();
  204. if (msg != null)
  205. {
  206. MessageBox.Show(msg, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  207. return false;
  208. }
  209. this.PoolValue.Name = this.txtPoolVarName.Text.Trim();
  210. if (this.comboType.SelectedIndex == 0)
  211. this.PoolValue.PoolValueType = PoolValueTypeEnum.Constant;
  212. else
  213. this.PoolValue.PoolValueType = PoolValueTypeEnum.Variable;
  214. this.PoolValue.Initializer.Source = this.txtSourceCode.Text;
  215. bool remember = this.Updating;
  216. try
  217. {
  218. this.Updating = true;
  219. if (!this.Pool.Values.Contains(this.PoolValue))
  220. this.Pool.Values.Add(this.PoolValue);
  221. this.MarkClean();
  222. this.PoolValueHolder.TriggerChanged(this.PoolValue, this.PoolValue);
  223. this.FillVariablesListView();
  224. this.SetSelection();
  225. }
  226. finally
  227. {
  228. this.Updating = remember;
  229. }
  230. return true;
  231. }
  232. private void MarkDirty()
  233. {
  234. this.Dirty = true;
  235. }
  236. private void MarkClean()
  237. {
  238. this.Dirty = false;
  239. }
  240. private void SetNameState()
  241. {
  242. bool ok = (this.Pool == null) || (this.ValidateName() == null);
  243. this.txtPoolVarName.BackColor = ok ? SystemColors.Window : Color.LightSalmon;
  244. }
  245. private string ValidateName()
  246. {
  247. if (String.IsNullOrWhiteSpace(this.txtPoolVarName.Text))
  248. return "Pool value must have a name";
  249. if (this.Pool != null)
  250. {
  251. foreach (PoolValue v in this.Pool.Values)
  252. {
  253. if (v != this.PoolValue)
  254. {
  255. if (v.Name == this.txtPoolVarName.Text)
  256. return "Pool value name must be unuque";
  257. }
  258. }
  259. }
  260. return null;
  261. }
  262. private void txtPoolVarName_TextChanged(object sender, EventArgs e)
  263. {
  264. if (this.Updating)
  265. return;
  266. this.SetNameState();
  267. this.MarkDirty();
  268. }
  269. private void comboType_SelectedIndexChanged(object sender, EventArgs e)
  270. {
  271. if (this.Updating)
  272. return;
  273. this.MarkDirty();
  274. }
  275. private void listPoolValues_ItemChanging(object sender, ListItemChangingEventArgs e)
  276. {
  277. if (e.Item == null)
  278. return;
  279. PoolValue item = e.Item.Tag as PoolValue;
  280. this._poolValue.Value = item;
  281. e.Item = this.listPoolVars.Items.Cast<ListViewItem>()
  282. .FirstOrDefault(i => i.Tag == this._poolValue.Value);
  283. }
  284. private void txtSourceCode_TextChanged(object sender, EventArgs e)
  285. {
  286. if (this.Updating)
  287. return;
  288. this.MarkDirty();
  289. }
  290. private void txtSourceCode_KeyDown(object sender, KeyEventArgs e)
  291. {
  292. if ((e.Modifiers == Keys.Alt) && (e.KeyCode == Keys.S))
  293. {
  294. if (this.Save())
  295. e.SuppressKeyPress = true;
  296. }
  297. if ((e.Modifiers == Keys.Control) && (e.KeyCode == Keys.A))
  298. {
  299. this.txtSourceCode.SelectAll();
  300. e.SuppressKeyPress = true;
  301. }
  302. }
  303. public void SetPrimaryFocus()
  304. {
  305. this.txtPoolVarName.Focus();
  306. }
  307. }
  308. }