<commit_before>﻿using System;
using System.Windows.Forms;
using InnovatorAdmin.Connections;
using System.Collections.Generic;
using System.Linq;
using Innovator.Client;
using System.Data.SqlClient;
using System.Drawing;

namespace InnovatorAdmin
{
  public partial class ConnectionEditor : UserControl
  {
    private BindingSource _bs = new BindingSource();
    private static int _newConnNumber = 0;

    public event EventHandler SelectionChanged;
    public event EventHandler ConnectionSelected;

    public bool MultiSelect
    {
      get { return lstConnections.Multiselect; }
      set { lstConnections.Multiselect = value; }
    }
    public IEnumerable<ConnectionData> SelectedConnections
    {
      get
      {
        return lstConnections.Selected.OfType<ConnectionData>();
      }
      set
      {
        lstConnections.SetSelection(value.ToArray());
      }
    }

    public ConnectionEditor()
    {
      InitializeComponent();

      var iconFont = new Font(FontAwesome.Family, 12.0F);
      btnNew.Font = iconFont;
      btnNew.Text = FontAwesome.Fa_plus_circle.ToString();
      btnDelete.Font = iconFont;
      btnDelete.Text = FontAwesome.Fa_minus_circle.ToString();
      btnCopy.Font = iconFont;
      btnCopy.Text = FontAwesome.Fa_copy.ToString();
      btnMoveDown.Font = iconFont;
      btnMoveDown.Text = FontAwesome.Fa_arrow_down.ToString();
      btnMoveUp.Font = iconFont;
      btnMoveUp.Text = FontAwesome.Fa_arrow_up.ToString();

      lblUrl.Font = FontAwesome.Font;
      lblUrl.Text = FontAwesome.Fa_globe.ToString();
      lblUser.Font = FontAwesome.Font;
      lblUser.Text = FontAwesome.Fa_user.ToString();
      lblPassword.Font = FontAwesome.Font;
      lblPassword.Text = FontAwesome.Fa_key.ToString();
      lblName.Font = FontAwesome.Font;
      lblName.Text = FontAwesome.Fa_bookmark.ToString();
      lblDatabase.Font = FontAwesome.Font;
      lblDatabase.Text = FontAwesome.Fa_database.ToString();
      lblType.Font = FontAwesome.Font;
      lblType.Text = FontAwesome.Fa_tag.ToString();

      this.MultiSelect = false;
      _bs.CurrentChanged += _bs_CurrentChanged;
      cmbType.DataSource = Enum.GetValues(typeof(ConnectionType));
    }

    public void InitializeFocus()
    {
      this.ActiveControl = lstConnections;
      lstConnections.InitializeFocus();
    }

    void _bs_CurrentChanged(object sender, EventArgs e)
    {
      try
      {
        var connData = _bs.Current as ConnectionData;
        if (connData != null)
        {
          if (connData.Url != _lastDatabaseUrl
          && !string.IsNullOrEmpty(connData.Database))
          {
            _lastDatabaseUrl = null;
            cmbDatabase.Items.Clear();
            cmbDatabase.Items.Add(connData.Database);
            cmbDatabase.SelectedIndex = 0;
          }
          SetAuthentication(connData.Authentication);
        }

        btnColor.BackColor = connData.Color;
      }
      catch (Exception ex)
      {
        Utils.HandleError(ex);
      }
    }
    protected virtual void OnSelectionChanged(EventArgs e)
    {
      if (SelectionChanged != null)
      {
        SelectionChanged(this, e);
      }
    }

    public void LoadConnectionLibrary(ConnectionLibrary library)
    {
      _bs.DataSource = library.Connections;
      if (!DesignMode)
      {
        lstConnections.DisplayMember = "ConnectionName";
        lstConnections.DataSource = _bs;

        txtName.DataBindings.Add("Text", _bs, "ConnectionName");
        cmbDatabase.DataBindings.Add("Text", _bs, "Database");
        txtPassword.DataBindings.Add("Text", _bs, "Password");
        txtUrl.DataBindings.Add("Text", _bs, "Url");
        txtUser.DataBindings.Add("Text", _bs, "UserName");
        cmbType.DataBindings.Add("SelectedItem", _bs, "Type");

        if (lstConnections.Items.Count > 0 && !this.MultiSelect)
          lstConnections.SetItemSelected(0, true);
      }
    }

    private void btnTest_Click(object sender, EventArgs e)
    {
      try
      {
        btnTest.Text = "Testing...";
        ((ConnectionData)_bs.Current).ArasLogin(true)
          .UiPromise(this)
          .Done(c => btnTest.Text = "Success. Test Again.")
          .Fail(ex => btnTest.Text = ex.Message);
      }
      catch (Exception ex)
      {
        Utils.HandleError(ex);
      }
    }

    private void ClearMessage()
    {
      btnTest.Text = "Test";
    }

    private void btnNew_Click(object sender, EventArgs e)
    {
      try
      {
        ClearMessage();
        _bs.Add(new ConnectionData()
        {
          ConnectionName = "New Connection " + _newConnNumber++
        });
      }
      catch (Exception ex)
      {<commit_msg>Fix Connection Test Behavior<commit_after>﻿using System;
using System.Windows.Forms;
using InnovatorAdmin.Connections;
using System.Collections.Generic;
using System.Linq;
using Innovator.Client;
using System.Data.SqlClient;
using System.Drawing;

namespace InnovatorAdmin
{
  public partial class ConnectionEditor : UserControl
  {
    private BindingSource _bs = new BindingSource();
    private static int _newConnNumber = 0;

    public event EventHandler SelectionChanged;
    public event EventHandler ConnectionSelected;

    public bool MultiSelect
    {
      get { return lstConnections.Multiselect; }
      set { lstConnections.Multiselect = value; }
    }
    public IEnumerable<ConnectionData> SelectedConnections
    {
      get
      {
        return lstConnections.Selected.OfType<ConnectionData>();
      }
      set
      {
        lstConnections.SetSelection(value.ToArray());
      }
    }

    public ConnectionEditor()
    {
      InitializeComponent();

      var iconFont = new Font(FontAwesome.Family, 12.0F);
      btnNew.Font = iconFont;
      btnNew.Text = FontAwesome.Fa_plus_circle.ToString();
      btnDelete.Font = iconFont;
      btnDelete.Text = FontAwesome.Fa_minus_circle.ToString();
      btnCopy.Font = iconFont;
      btnCopy.Text = FontAwesome.Fa_copy.ToString();
      btnMoveDown.Font = iconFont;
      btnMoveDown.Text = FontAwesome.Fa_arrow_down.ToString();
      btnMoveUp.Font = iconFont;
      btnMoveUp.Text = FontAwesome.Fa_arrow_up.ToString();

      lblUrl.Font = FontAwesome.Font;
      lblUrl.Text = FontAwesome.Fa_globe.ToString();
      lblUser.Font = FontAwesome.Font;
      lblUser.Text = FontAwesome.Fa_user.ToString();
      lblPassword.Font = FontAwesome.Font;
      lblPassword.Text = FontAwesome.Fa_key.ToString();
      lblName.Font = FontAwesome.Font;
      lblName.Text = FontAwesome.Fa_bookmark.ToString();
      lblDatabase.Font = FontAwesome.Font;
      lblDatabase.Text = FontAwesome.Fa_database.ToString();
      lblType.Font = FontAwesome.Font;
      lblType.Text = FontAwesome.Fa_tag.ToString();

      this.MultiSelect = false;
      _bs.CurrentChanged += _bs_CurrentChanged;
      cmbType.DataSource = Enum.GetValues(typeof(ConnectionType));
    }

    public void InitializeFocus()
    {
      this.ActiveControl = lstConnections;
      lstConnections.InitializeFocus();
    }

    void _bs_CurrentChanged(object sender, EventArgs e)
    {
      try
      {
        var connData = _bs.Current as ConnectionData;
        if (connData != null)
        {
          if (connData.Url != _lastDatabaseUrl
          && !string.IsNullOrEmpty(connData.Database))
          {
            _lastDatabaseUrl = null;
            cmbDatabase.Items.Clear();
            cmbDatabase.Items.Add(connData.Database);
            cmbDatabase.SelectedIndex = 0;
          }
          SetAuthentication(connData.Authentication);
        }

        btnColor.BackColor = connData.Color;
      }
      catch (Exception ex)
      {
        Utils.HandleError(ex);
      }
    }
    protected virtual void OnSelectionChanged(EventArgs e)
    {
      if (SelectionChanged != null)
      {
        SelectionChanged(this, e);
      }
    }

    public void LoadConnectionLibrary(ConnectionLibrary library)
    {
      _bs.DataSource = library.Connections;
      if (!DesignMode)
      {
        lstConnections.DisplayMember = "ConnectionName";
        lstConnections.DataSource = _bs;

        txtName.DataBindings.Add("Text", _bs, "ConnectionName");
        cmbDatabase.DataBindings.Add("Text", _bs, "Database");
        txtPassword.DataBindings.Add("Text", _bs, "Password");
        txtUrl.DataBindings.Add("Text", _bs, "Url");
        txtUser.DataBindings.Add("Text", _bs, "UserName");
        cmbType.DataBindings.Add("SelectedItem", _bs, "Type");

        if (lstConnections.Items.Count > 0 && !this.MultiSelect)
          lstConnections.SetItemSelected(0, true);
      }
    }

    private void btnTest_Click(object sender, EventArgs e)
    {
      try
      {
        btnTest.Text = "Testing...";


        var connData = (ConnectionData)_bs.Current;
        switch (connData.Type)
        {
          case ConnectionType.Innovator:
            ((ConnectionData)_bs.Current).ArasLogin(true)
              .UiPromise(this)
              .Done(c => btnTest.Text = "Success. Test Again.")
              .Fail(ex => btnTest.Text = ex.Message);
            break;
          case ConnectionType.SqlServer:
            try
            {
              using (var conn = Editor.SqlEditorProxy.GetConnection(connData, "master"))
              {
                conn.Open();
              }
              btnTest.Text = "Success. Test Again.";
            }
            catch (Exception ex)
            {
              btnTest.Text = ex.Message;
            }
            break;
          default:
            ProxyFactory.FromConn(connData)
              .UiPromise(this)
              .Done(c => btnTest.Text = "Success. Test Again.")
              .Fail(ex => btnTest.Text = ex.Message);
            break;
        }
      }
      catch (Exception ex)
      {
        Utils.HandleError(ex);
      }
    }

    private void ClearMessage()
    {
      btnTest.Text = "Test";
    }

    private void btnNew_Click(object sender, EventArgs e)
    {
      try
      {
        ClearMessage();
        _bs.Add(new ConnectionData()
        {
          ConnectionName = "New Connection " + _newConnNumber++
        });
      }
      catch (Exception ex)
      {
{'fraction_non_alphanumeric': 0.10376428852975955, 'fraction_numerical': 0.0013795821836815136, 'mean_word_length': 2.28021978021978, 'pattern_counts': {'":': 0, '<': 7, '<?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': '800136', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2920, 'n_words': 530}