<commit_before>using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Mirage.Urbanization.ZoneConsumption;
using Mirage.Urbanization.ZoneConsumption.Base;

namespace Mirage.Urbanization.WinForms
{
    public class ZoneSelectionPanelCreator
    {
        private readonly Panel _targetPanel;

        private readonly IDictionary<Button, Func<IAreaConsumption>> _buttonsAndFactories;
        private Func<IAreaConsumption> _currentFactory;

        private readonly Dictionary<char, Action> _handleKeyCharActions = new Dictionary<char, Action>();

        public bool HandleKeyCharAction(char @char)
        {
            if (_handleKeyCharActions.ContainsKey(@char))
            {
                _handleKeyCharActions[@char]();
                return true;
            }
            return false;
        }

        public ZoneSelectionPanelCreator(IReadOnlyArea area, Panel targetPanel)
        {
            if (targetPanel == null) throw new ArgumentNullException("targetPanel");
            if (area == null) throw new ArgumentNullException("area");

            _targetPanel = targetPanel;

            EventHandler currentClickHandler = null;

            _buttonsAndFactories = area.GetSupportedZoneConsumptionFactories().Reverse()
                .Select(factory =>
                {
                    var sample = factory();

                    var button = new Button
                    {
                        Text = string.Format("{0} ({1})", sample.Name, sample.KeyChar.ToString().ToUpperInvariant()),
                        Dock = DockStyle.Top,
                        BackColor = SystemColors.Control,
                        Parent = _targetPanel
                    };

                    _handleKeyCharActions.Add(sample.KeyChar, () => button.PerformClick());

                    currentClickHandler = (sender, e) =>
                    {
                        foreach (var btn in _buttonsAndFactories.Keys.Where(btn => btn != button))
                            btn.Enabled = true;
                        button.Enabled = false;
                        _currentFactory = factory;
                        _currentZoneConsumptionSample = _currentFactory();
                    };

                    button.Click += currentClickHandler;


                    return new KeyValuePair<Button, Func<IAreaConsumption>>(button, factory);
                })
                .ToDictionary(x => x.Key, x => x.Value);<commit_msg>Implement throwing of more informative exception when zone selection hotkey conflicts occur.<commit_after>using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using Mirage.Urbanization.ZoneConsumption;
using Mirage.Urbanization.ZoneConsumption.Base;

namespace Mirage.Urbanization.WinForms
{
    public class ZoneSelectionPanelCreator
    {
        private readonly Panel _targetPanel;

        private readonly IDictionary<Button, Func<IAreaConsumption>> _buttonsAndFactories;
        private Func<IAreaConsumption> _currentFactory;

        private readonly Dictionary<char, Action> _handleKeyCharActions = new Dictionary<char, Action>();

        public bool HandleKeyCharAction(char @char)
        {
            if (_handleKeyCharActions.ContainsKey(@char))
            {
                _handleKeyCharActions[@char]();
                return true;
            }
            return false;
        }

        public ZoneSelectionPanelCreator(IReadOnlyArea area, Panel targetPanel)
        {
            if (targetPanel == null) throw new ArgumentNullException("targetPanel");
            if (area == null) throw new ArgumentNullException("area");

            _targetPanel = targetPanel;

            EventHandler currentClickHandler = null;

            _buttonsAndFactories = area.GetSupportedZoneConsumptionFactories().Reverse()
                .Select(factory =>
                {
                    var sample = factory();

                    var button = new Button
                    {
                        Text = string.Format("{0} ({1})", sample.Name, sample.KeyChar.ToString().ToUpperInvariant()),
                        Dock = DockStyle.Top,
                        BackColor = SystemColors.Control,
                        Parent = _targetPanel
                    };

                    if (_handleKeyCharActions.ContainsKey(sample.KeyChar))
                    {
                        throw new InvalidOperationException(string.Format("Could not use key char '{0}' as unique identifier for '{1}'.",
                            sample.KeyChar, sample.Name));
                    }

                    _handleKeyCharActions.Add(sample.KeyChar, () => button.PerformClick());

                    currentClickHandler = (sender, e) =>
                    {
                        foreach (var btn in _buttonsAndFactories.Keys.Where(btn => btn != button))
                            btn.Enabled = true;
                        button.Enabled = false;
                        _currentFactory = factory;
                        _currentZoneConsumptionSample = _currentFactory();
                    };

                    button.Click += currentClickHandler;


                    return new KeyValuePair<Button, Func<IAreaConsumption>>(button, factory);
                })
                .ToDictionary(x => x.Key, x => x.Value);
{'fraction_non_alphanumeric': 0.08336366945759009, 'fraction_numerical': 0.0010921004732435385, 'mean_word_length': 1.7654755913437343, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '676750', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1220, 'n_words': 279}