<commit_before>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Hudson.TrayTracker.BusinessComponents;
using Hudson.TrayTracker.Entities;
using DevExpress.XtraBars;
using Hudson.TrayTracker.Utils.BackgroundProcessing;
using Spring.Context.Support;

namespace Hudson.TrayTracker.UI.Controls
{
    public partial class ServersSettingsControl : DevExpress.XtraEditors.XtraUserControl
    {
        ConfigurationService configurationService;
        HudsonService hudsonService;
        BindingList<Server> serversDataSource;
        List<Project> projectsDataSource;

        public ServersSettingsControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // designer mode
            if (DesignMode)
                return;

            configurationService = (ConfigurationService)ContextRegistry.GetContext().GetObject("ConfigurationService");
            hudsonService = (HudsonService)ContextRegistry.GetContext().GetObject("HudsonService");

            serversDataSource = new BindingList<Server>();
            foreach (Server server in configurationService.Servers)
                serversDataSource.Add(server);
            serversGridControl.DataSource = serversDataSource;

            serversGridView_FocusedRowChanged(null, null);
        }

        private void addServerButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditServerForm namingForm = new EditServerForm();
            if (namingForm.ShowDialog() != DialogResult.OK)
                return;

            Server server = configurationService.AddServer(namingForm.ServerAddress,
                namingForm.Username, namingForm.Password);
            if (server == null)
                return;

            serversDataSource.Add(server);
        }

        private void editServerButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Server server = GetSelectedServer();
            if (server == null)
                return;

            EditServerForm namingForm = new EditServerForm(server);
            if (namingForm.ShowDialog() != DialogResult.OK)
                return;

            configurationService.UpdateServer(server,
                   namingForm.ServerAddress, namingForm.Username, namingForm.Password);

            UpdateProjectList(server);
        }

        private void removeServerButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Server server = GetSelectedServer();
            serversDataSource.Remove(server);
            configurationService.RemoveServer(server);
        }

        private void serversGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            Server server = GetSelectedServer();

            // update the toolbar
            editServerButtonItem.Enabled
                = removeServerButtonItem.Enabled
                = server != null;

            // update the project list
            UpdateProjectList(server);
        }

        private void UpdateProjectList(Server server)
        {
#if SYNCRHONOUS
            List<Project> dataSource = new List<Project>();

            if (server != null)
            {
                IList<Project> projects = hudsonService.LoadProjects(server);
                foreach (Project project in projects)
                    dataSource.Add(project);<commit_msg>- fixed issue #33: The settings form sometimes does not show the correct list of projects<commit_after>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Hudson.TrayTracker.BusinessComponents;
using Hudson.TrayTracker.Entities;
using DevExpress.XtraBars;
using Hudson.TrayTracker.Utils.BackgroundProcessing;
using Spring.Context.Support;

namespace Hudson.TrayTracker.UI.Controls
{
    public partial class ServersSettingsControl : DevExpress.XtraEditors.XtraUserControl
    {
        ConfigurationService configurationService;
        HudsonService hudsonService;
        BindingList<Server> serversDataSource;
        List<Project> projectsDataSource;

        bool initialized;

        public ServersSettingsControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // designer mode
            if (DesignMode)
                return;

            if (configurationService == null)
                configurationService = (ConfigurationService)ContextRegistry.GetContext().GetObject("ConfigurationService");
            if (hudsonService == null)
                hudsonService = (HudsonService)ContextRegistry.GetContext().GetObject("HudsonService");

            initialized = false;

            serversDataSource = new BindingList<Server>();
            foreach (Server server in configurationService.Servers)
                serversDataSource.Add(server);
            serversGridControl.DataSource = serversDataSource;

            initialized = true;

            serversGridView_FocusedRowChanged(null, null);
        }

        private void addServerButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditServerForm namingForm = new EditServerForm();
            if (namingForm.ShowDialog() != DialogResult.OK)
                return;

            Server server = configurationService.AddServer(namingForm.ServerAddress,
                namingForm.Username, namingForm.Password);
            if (server == null)
                return;

            serversDataSource.Add(server);
        }

        private void editServerButtonItem_ItemClick(object sender, ItemClickEventArgs e)
        {
            Server server = GetSelectedServer();
            if (server == null)
                return;

            EditServerForm namingForm = new EditServerForm(server);
            if (namingForm.ShowDialog() != DialogResult.OK)
                return;

            configurationService.UpdateServer(server,
                   namingForm.ServerAddress, namingForm.Username, namingForm.Password);

            UpdateProjectList(server);
        }

        private void removeServerButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Server server = GetSelectedServer();
            serversDataSource.Remove(server);
            configurationService.RemoveServer(server);
        }

        private void serversGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (initialized == false)
                return;

            Server server = GetSelectedServer();

            // update the toolbar
            editServerButtonItem.Enabled
                = removeServerButtonItem.Enabled
                = server != null;

            // update the project list
            UpdateProjectList(server);
        }

        private void UpdateProjectList(Server server)
        {
#if SYNCRHONOUS
            List<Project> dataSource = new List<Project>();

            if (server != null)
            {
                IList<Project> projects = hudsonService.LoadProjects(server);
                foreach (Project project in projects)
                    dataSource.Add(project);
{'fraction_non_alphanumeric': 0.07293291731669267, 'fraction_numerical': 0.00026001040041601667, 'mean_word_length': 2.2988850771869638, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6956784', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1747, 'n_words': 382}