PageRenderTime 108ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/QuickMon3/QuickMonCoreUI/Forms/AgentTypeSelect.cs

#
C# | 205 lines | 188 code | 17 blank | 0 comment | 25 complexity | e7d7ad952477a39034005db892bfe338 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace QuickMon.Forms
  11. {
  12. public partial class AgentTypeSelect : Form
  13. {
  14. public AgentTypeSelect()
  15. {
  16. InitializeComponent();
  17. }
  18. #region Properties
  19. public RegisteredAgent SelectedAgent { get; set; }
  20. public bool ImportConfigAfterSelect { get; set; }
  21. public bool UsePresetAfterSelect { get; set; }
  22. #endregion
  23. public DialogResult ShowNotifierSelection(string currentNotifierRegistrationName)
  24. {
  25. this.Text = "Select Notifier type";
  26. SetDetailColumnSizing();
  27. lvwAgentType.Items.Clear();
  28. ListViewItem lvi;
  29. foreach (RegisteredAgent ar in (from a in RegisteredAgentCache.Agents
  30. where a.IsNotifier
  31. orderby a.Name
  32. select a))
  33. {
  34. lvi = new ListViewItem(ar.DisplayName);
  35. string details = ar.ClassName;
  36. System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(ar.AssemblyPath);
  37. details += ", Version: " + a.GetName().Version.ToString();
  38. details += ", Assembly: " + System.IO.Path.GetFileName(a.Location);
  39. lvi.SubItems.Add(details);
  40. lvi.Tag = ar;
  41. lvi.ImageIndex = 0;
  42. lvwAgentType.Items.Add(lvi);
  43. if (ar.Name == currentNotifierRegistrationName)
  44. lvi.Selected = true;
  45. }
  46. return this.ShowDialog();
  47. }
  48. public DialogResult ShowCollectorSelection(string currentCollectorRegistrationName)
  49. {
  50. this.Text = "Select Collector type";
  51. SetDetailColumnSizing();
  52. lvwAgentType.Items.Clear();
  53. lvwAgentType.Groups.Clear();
  54. ListViewGroup generalGroup = new ListViewGroup("General");
  55. lvwAgentType.Groups.Add(generalGroup);
  56. foreach(string categoryName in (from a in RegisteredAgentCache.Agents
  57. where a.IsCollector && a.CategoryName != "Test" && a.CategoryName != "General"
  58. group a by a.CategoryName into g
  59. select g.Key))
  60. {
  61. lvwAgentType.Groups.Add(new ListViewGroup(categoryName));
  62. }
  63. ListViewGroup folderGroup = new ListViewGroup("Folder");
  64. lvwAgentType.Groups.Add(folderGroup);
  65. ListViewGroup testGroup = new ListViewGroup("Test");
  66. lvwAgentType.Groups.Add(testGroup);
  67. RegisteredAgent folder = new RegisteredAgent() { ClassName = "QuickMon.Collectors.Folder", Name = "Folder", IsCollector = true, DisplayName = "Folder" };
  68. ListViewItem lvi = new ListViewItem("Folder");
  69. lvi.Group = folderGroup;
  70. lvi.ImageIndex = 1;
  71. lvi.SubItems.Add("Container for child objects");
  72. lvi.Tag = folder;
  73. lvwAgentType.Items.Add(lvi);
  74. if (currentCollectorRegistrationName == "Folder")
  75. lvi.Selected = true;
  76. foreach (RegisteredAgent ar in (from a in RegisteredAgentCache.Agents
  77. where a.IsCollector
  78. orderby a.Name
  79. select a))
  80. {
  81. ListViewGroup agentGroup = (from ListViewGroup gr in lvwAgentType.Groups
  82. where gr.Header.ToLower() == ar.CategoryName.ToLower()
  83. select gr).FirstOrDefault();
  84. if (agentGroup == null)
  85. agentGroup = generalGroup;
  86. lvi = new ListViewItem(ar.DisplayName);
  87. string details = ar.ClassName;
  88. System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom(ar.AssemblyPath);
  89. details += ", Version: " + a.GetName().Version.ToString();
  90. details += ", Assembly: " + System.IO.Path.GetFileName(a.Location);
  91. if (agentGroup == testGroup)
  92. lvi.ImageIndex = 2;
  93. else
  94. lvi.ImageIndex = 0;
  95. lvi.Group = agentGroup;
  96. lvi.SubItems.Add(details);
  97. lvi.Tag = ar;
  98. lvwAgentType.Items.Add(lvi);
  99. if (ar.Name == currentCollectorRegistrationName)
  100. lvi.Selected = true;
  101. }
  102. return this.ShowDialog();
  103. }
  104. #region Form events
  105. private void AgentTypeSelect_Load(object sender, EventArgs e)
  106. {
  107. }
  108. #endregion
  109. #region Button events
  110. private void cmdOK_Click(object sender, EventArgs e)
  111. {
  112. if (lvwAgentType.SelectedItems.Count == 1)
  113. {
  114. SelectedAgent = (RegisteredAgent)lvwAgentType.SelectedItems[0].Tag;
  115. ImportConfigAfterSelect = optCustomConfig.Checked;
  116. UsePresetAfterSelect = optSelectPreset.Checked;
  117. DialogResult = System.Windows.Forms.DialogResult.OK;
  118. Close();
  119. }
  120. }
  121. #endregion
  122. #region ListView events
  123. private void lvwAgentType_SelectedIndexChanged(object sender, EventArgs e)
  124. {
  125. cmdOK.Enabled = lvwAgentType.SelectedItems.Count == 1;
  126. bool presets = false;
  127. try
  128. {
  129. if (lvwAgentType.SelectedItems.Count > 0 && lvwAgentType.SelectedItems[0].Tag is RegisteredAgent)
  130. {
  131. RegisteredAgent ra = (RegisteredAgent)lvwAgentType.SelectedItems[0].Tag;
  132. List<AgentPresetConfig> existingPresets = AgentPresetConfig.GetPresetsForClass(ra.Name);
  133. presets = (existingPresets != null && existingPresets.Count > 0);
  134. }
  135. }
  136. catch { }
  137. optSelectPreset.Enabled = presets;
  138. if (optSelectPreset.Checked && !presets)
  139. optShowConfigEditor.Checked = true;
  140. }
  141. private void lvwAgentType_EnterKeyPressed()
  142. {
  143. cmdOK_Click(null, null);
  144. }
  145. #endregion
  146. private void llblExtraAgents_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  147. {
  148. string url = "https://quickmon.codeplex.com/";
  149. try
  150. {
  151. System.Diagnostics.Process p = new System.Diagnostics.Process();
  152. p.StartInfo = new System.Diagnostics.ProcessStartInfo(url);
  153. p.Start();
  154. }
  155. catch (Exception ex)
  156. {
  157. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  158. }
  159. }
  160. private void chkShowDetails_CheckedChanged(object sender, EventArgs e)
  161. {
  162. SetDetailColumnSizing();
  163. }
  164. #region Private methods
  165. private void SetDetailColumnSizing()
  166. {
  167. try
  168. {
  169. lvwAgentType.AutoResizeColumnEnabled = false;
  170. if (chkShowDetails.Checked)
  171. {
  172. lvwAgentType.AutoResizeColumnIndex = 1;
  173. lvwAgentType.Columns[0].Width = (int)(lvwAgentType.Width / 3.0);
  174. }
  175. else
  176. {
  177. lvwAgentType.AutoResizeColumnIndex = 0;
  178. lvwAgentType.Columns[1].Width = 1;
  179. }
  180. this.Width -= 1;
  181. lvwAgentType.AutoResizeColumnEnabled = true;
  182. this.Width += 1;
  183. }
  184. catch { }
  185. }
  186. #endregion
  187. }
  188. }