/Project/Src/StyleCop.CSharp.Rules/CompanyInformation.cs

https://github.com/StyleCop/StyleCop · C# · 367 lines · 190 code · 53 blank · 124 comment · 27 complexity · 4bff4cf887063eaee7661bb652286313 MD5 · raw file

  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="CompanyInformation.cs" company="https://github.com/StyleCop">
  3. // MS-PL
  4. // </copyright>
  5. // <license>
  6. // This source code is subject to terms and conditions of the Microsoft
  7. // Public License. A copy of the license can be found in the License.html
  8. // file at the root of this distribution. If you cannot locate the
  9. // Microsoft Public License, please send an email to dlr@microsoft.com.
  10. // By using this source code in any fashion, you are agreeing to be bound
  11. // by the terms of the Microsoft Public License. You must not remove this
  12. // notice, or any other, from this software.
  13. // </license>
  14. // <summary>
  15. // Allows setting the company and copyright requirements.
  16. // </summary>
  17. // --------------------------------------------------------------------------------------------------------------------
  18. namespace StyleCop.CSharp
  19. {
  20. using System;
  21. using System.Drawing;
  22. using System.Windows.Forms;
  23. /// <summary>
  24. /// Allows setting the company and copyright requirements.
  25. /// </summary>
  26. public partial class CompanyInformation : UserControl, IPropertyControlPage
  27. {
  28. #region Fields
  29. /// <summary>
  30. /// The analyzer that this settings page is attached to.
  31. /// </summary>
  32. private readonly SourceAnalyzer analyzer;
  33. /// <summary>
  34. /// True if the page is dirty.
  35. /// </summary>
  36. private bool dirty;
  37. /// <summary>
  38. /// The tab control which hosts this page.
  39. /// </summary>
  40. private PropertyControl tabControl;
  41. #endregion
  42. #region Constructors and Destructors
  43. /// <summary>
  44. /// Initializes a new instance of the CompanyInformation class.
  45. /// </summary>
  46. public CompanyInformation()
  47. {
  48. this.InitializeComponent();
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the CompanyInformation class.
  52. /// </summary>
  53. /// <param name="analyzer">
  54. /// The analyzer that this settings page is attached to.
  55. /// </param>
  56. public CompanyInformation(DocumentationRules analyzer)
  57. : this()
  58. {
  59. Param.RequireNotNull(analyzer, "analyzer");
  60. this.analyzer = analyzer;
  61. }
  62. #endregion
  63. #region Public Properties
  64. /// <summary>
  65. /// Gets or sets a value indicating whether any data on the page is dirty.
  66. /// </summary>
  67. public bool Dirty
  68. {
  69. get
  70. {
  71. return this.dirty;
  72. }
  73. set
  74. {
  75. Param.Ignore(value);
  76. if (this.dirty != value)
  77. {
  78. this.dirty = value;
  79. this.tabControl.DirtyChanged();
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the name of the the tab.
  85. /// </summary>
  86. public string TabName
  87. {
  88. get
  89. {
  90. return Strings.CompanyInformationTab;
  91. }
  92. }
  93. #endregion
  94. #region Public Methods and Operators
  95. /// <summary>
  96. /// Called when the page is activated.
  97. /// </summary>
  98. /// <param name="activated">
  99. /// Indicates whether the page is being activated or deactivated.
  100. /// </param>
  101. public void Activate(bool activated)
  102. {
  103. Param.Ignore(activated);
  104. }
  105. /// <summary>
  106. /// Saves the data and clears the dirty flag.
  107. /// </summary>
  108. /// <returns>Returns true if the data is saved, false if not.</returns>
  109. public bool Apply()
  110. {
  111. if (this.analyzer != null)
  112. {
  113. if (!this.checkBox.Checked)
  114. {
  115. this.analyzer.ClearSetting(this.tabControl.LocalSettings, DocumentationRules.CompanyNameProperty);
  116. this.analyzer.ClearSetting(this.tabControl.LocalSettings, DocumentationRules.CopyrightProperty);
  117. }
  118. else
  119. {
  120. if (this.companyName.Text.Length == 0 || this.copyright.Text.Length == 0)
  121. {
  122. AlertDialog.Show(this.tabControl.Core, this, Strings.MissingCompanyOrCopyright, Strings.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
  123. return false;
  124. }
  125. else
  126. {
  127. this.analyzer.SetSetting(
  128. this.tabControl.LocalSettings, new StringProperty(this.analyzer, DocumentationRules.CompanyNameProperty, this.companyName.Text));
  129. this.analyzer.SetSetting(
  130. this.tabControl.LocalSettings, new StringProperty(this.analyzer, DocumentationRules.CopyrightProperty, this.copyright.Text));
  131. }
  132. }
  133. }
  134. this.dirty = false;
  135. this.tabControl.DirtyChanged();
  136. return true;
  137. }
  138. /// <summary>
  139. /// Initializes the page.
  140. /// </summary>
  141. /// <param name="propertyControl">
  142. /// The tab control object.
  143. /// </param>
  144. public void Initialize(PropertyControl propertyControl)
  145. {
  146. Param.RequireNotNull(propertyControl, "propertyControl");
  147. this.tabControl = propertyControl;
  148. this.InitializeSettings();
  149. this.DetectBoldState();
  150. this.dirty = false;
  151. this.tabControl.DirtyChanged();
  152. }
  153. /// <summary>
  154. /// Called after all pages have been applied.
  155. /// </summary>
  156. /// <param name="wasDirty">
  157. /// The dirty state of the page before it was applied.
  158. /// </param>
  159. public void PostApply(bool wasDirty)
  160. {
  161. Param.Ignore(wasDirty);
  162. }
  163. /// <summary>
  164. /// Called before all pages are applied.
  165. /// </summary>
  166. /// <returns>Returns false if no pages should be applied.</returns>
  167. public bool PreApply()
  168. {
  169. return true;
  170. }
  171. /// <summary>
  172. /// Refreshes the bold state of items on the page.
  173. /// </summary>
  174. public void RefreshSettingsOverrideState()
  175. {
  176. if (this.dirty == false)
  177. {
  178. this.InitializeSettings();
  179. }
  180. this.DetectBoldState();
  181. }
  182. #endregion
  183. #region Methods
  184. /// <summary>
  185. /// Called when the checkbox is checked or unchecked.
  186. /// </summary>
  187. /// <param name="sender">
  188. /// The event sender.
  189. /// </param>
  190. /// <param name="e">
  191. /// The event arguments.
  192. /// </param>
  193. private void CheckBoxCheckedChanged(object sender, EventArgs e)
  194. {
  195. Param.Ignore(sender, e);
  196. this.dirty = true;
  197. this.tabControl.DirtyChanged();
  198. this.companyName.Enabled = this.checkBox.Checked;
  199. this.copyright.Enabled = this.checkBox.Checked;
  200. }
  201. /// <summary>
  202. /// Called when the company name text is changed.
  203. /// </summary>
  204. /// <param name="sender">
  205. /// The event sender.
  206. /// </param>
  207. /// <param name="e">
  208. /// The event arguments.
  209. /// </param>
  210. private void CompanyNameTextChanged(object sender, EventArgs e)
  211. {
  212. Param.Ignore(sender, e);
  213. this.DetectCompanyNameBoldState();
  214. this.dirty = true;
  215. this.tabControl.DirtyChanged();
  216. }
  217. /// <summary>
  218. /// Called when the copyright text is changed.
  219. /// </summary>
  220. /// <param name="sender">
  221. /// The event sender.
  222. /// </param>
  223. /// <param name="e">
  224. /// The event arguments.
  225. /// </param>
  226. private void CopyrightTextChanged(object sender, EventArgs e)
  227. {
  228. Param.Ignore(sender, e);
  229. this.DetectCopyrightBoldState();
  230. this.dirty = true;
  231. this.tabControl.DirtyChanged();
  232. }
  233. /// <summary>
  234. /// Detects the bold state of the controls.
  235. /// </summary>
  236. private void DetectBoldState()
  237. {
  238. this.DetectCompanyNameBoldState();
  239. this.DetectCopyrightBoldState();
  240. }
  241. /// <summary>
  242. /// Detects the bold state of the company name text box.
  243. /// </summary>
  244. private void DetectCompanyNameBoldState()
  245. {
  246. if (this.analyzer != null)
  247. {
  248. StringProperty currentValue = new StringProperty(this.analyzer, DocumentationRules.CompanyNameProperty, this.companyName.Text);
  249. this.SetBoldState(
  250. this.companyName, this.tabControl.SettingsComparer.IsAddInSettingOverwritten(this.analyzer, DocumentationRules.CompanyNameProperty, currentValue));
  251. }
  252. }
  253. /// <summary>
  254. /// Detects the bold state of the copyright text box.
  255. /// </summary>
  256. private void DetectCopyrightBoldState()
  257. {
  258. StringProperty currentValue = new StringProperty(this.analyzer, DocumentationRules.CopyrightProperty, this.copyright.Text);
  259. this.SetBoldState(
  260. this.copyright, this.tabControl.SettingsComparer.IsAddInSettingOverwritten(this.analyzer, DocumentationRules.CopyrightProperty, currentValue));
  261. }
  262. /// <summary>
  263. /// Initializes the settings on the page.
  264. /// </summary>
  265. private void InitializeSettings()
  266. {
  267. if (this.analyzer != null)
  268. {
  269. // Get the properties.
  270. StringProperty companyNameProperty = this.analyzer.GetSetting(this.tabControl.MergedSettings, DocumentationRules.CompanyNameProperty) as StringProperty;
  271. if (companyNameProperty != null)
  272. {
  273. this.companyName.Text = companyNameProperty.Value;
  274. }
  275. StringProperty copyrightProperty = this.analyzer.GetSetting(this.tabControl.MergedSettings, DocumentationRules.CopyrightProperty) as StringProperty;
  276. if (copyrightProperty != null)
  277. {
  278. this.copyright.Text = copyrightProperty.Value;
  279. }
  280. this.checkBox.Checked = companyNameProperty != null || copyrightProperty != null;
  281. this.CheckBoxCheckedChanged(this.checkBox, new EventArgs());
  282. }
  283. }
  284. /// <summary>
  285. /// Sets the bold state of the item.
  286. /// </summary>
  287. /// <param name="item">
  288. /// The item to set.
  289. /// </param>
  290. /// <param name="bold">
  291. /// The bold state.
  292. /// </param>
  293. private void SetBoldState(TextBox item, bool bold)
  294. {
  295. Param.AssertNotNull(item, "item");
  296. Param.Ignore(bold);
  297. // Dispose the item's current font if necessary.
  298. if (item.Font != this.Font && item.Font != null)
  299. {
  300. item.Font.Dispose();
  301. }
  302. // Create and set the new font.
  303. if (bold)
  304. {
  305. item.Font = new Font(this.Font, FontStyle.Bold);
  306. }
  307. else
  308. {
  309. item.Font = new Font(this.Font, FontStyle.Regular);
  310. }
  311. }
  312. #endregion
  313. }
  314. }