view ServerMonitor/Controls/CheckControl.cs @ 40:c4fc74593a78 default tip

Mono fix
author Brad Greco <brad@bgreco.net>
date Sat, 13 Jun 2020 13:28:20 -0400
parents 7127d5b5ac75
children
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    /// <summary>Base class for check controls.</summary>
    /// <remarks>This control should never be used directly, but marking it abstract causes problems with the designer.</remarks>
    public partial class CheckControl : UserControl
    {
        /// <summary>Type of the check this control can edit.</summary>
        public Type CheckType => GetCheckType(GetType());

        public CheckControl()
        {
            InitializeComponent();
        }

        private void CheckControl_Load(object sender, EventArgs e)
        {
            CheckGroupBox.Text = Helpers.GetDisplayName(CheckType);

            // Bind change listeners to checkboxes to enable or disable a panel of controls.
            // The leftmost checkbox is assumed to be the "master switch" for the panel.
            IEnumerable<Panel> panels = CheckGroupBox.Controls.OfType<Panel>();
            foreach (Panel panel in panels)
            {
                CheckBox mainCheckBox = panel.Controls.OfType<CheckBox>().OrderBy(c => c.Left).FirstOrDefault();
                if (mainCheckBox != null)
                {
                    mainCheckBox.CheckedChanged += CheckControl_CheckedChanged;
                    DisablePanelByCheckBox(mainCheckBox);
                }
            }
        }

        private void CheckControl_CheckedChanged(object sender, EventArgs e)
        {
            DisablePanelByCheckBox((CheckBox)sender);
        }

        /// <summary>Enables or disables all sibling controls of a checkbox.</summary>
        /// <param name="checkBox">The checkbox to disable or enable the sibling controls of.</param>
        private void DisablePanelByCheckBox(CheckBox checkBox)
        {
            foreach (Control control in checkBox.Parent.Controls)
            {
                if (control != checkBox)
                    control.Enabled = checkBox.Checked;
            }
        }

        /// <summary>Sets the values of the controls from a check's properties.</summary>
        /// <remarks>Subclasses should implement this method. Marking as abstract causes problems with the designer.</remarks>
        public virtual void LoadCheck(Check check) { }

        /// <summary>Updates the properties of a check from user input.</summary>
        /// <remarks>Subclasses should implement this method. Marking as abstract causes problems with the designer.</remarks>
        public virtual void UpdateCheck(Check check) { }

        /// <summary>Creates an instance of a check control based on a check type.</summary>
        public static CheckControl Create(Type CheckType)
        {
            // Use reflection to find a check control that declares it handles the given type.
            Type checkControlType = typeof(CheckControl).Assembly.GetTypes().FirstOrDefault(t => GetCheckType(t) == CheckType);
            // If one was found, instantiate it.
            return checkControlType == null ? null : (CheckControl)Activator.CreateInstance(checkControlType);
        }

        /// <summary>Gets the value of the CheckTypeAttribute for a type.</summary>
        private static Type GetCheckType(Type type)
        {
            return type.GetAttribute<CheckTypeAttribute>()?.CheckType;
        }
    }
}