Пример #1
0
        /// <summary>
        /// Event delegate method fired when the <see cref="MySQLInstanceToolStripMenuItem"/> context menu item is clicked.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Event arguments.</param>
        private void MySQLInstanceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            MonitoredServicesListView.BeginUpdate();
            MySqlWorkbenchConnection selectedConnection = null;

            using (var monitorInstancesDialog = new MonitorMySqlServerInstancesDialog(MachinesList, InstancesList))
            {
                if (monitorInstancesDialog.ShowDialog() == DialogResult.OK)
                {
                    selectedConnection = monitorInstancesDialog.SelectedWorkbenchConnection;
                    if (selectedConnection != null)
                    {
                        bool connectionAlreadyInInstance = false;

                        // If the selected connection exists for an already monitored instance but it is not its main connection, replace the main connection with this one.
                        foreach (var instance in InstancesList.Where(inst => inst.RelatedConnections.Exists(conn => conn.Id == selectedConnection.Id)))
                        {
                            if (selectedConnection.ConnectionStatus == MySqlWorkbenchConnection.ConnectionStatusType.Unknown)
                            {
                                Exception ex;
                                selectedConnection.TestConnectionSilently(out ex);
                            }

                            instance.WorkbenchConnection = selectedConnection;
                            connectionAlreadyInInstance  = true;
                            foreach (ListViewItem lvi in MonitoredInstancesListView.Items)
                            {
                                MySqlInstance existingInstance = lvi.Tag as MySqlInstance;
                                if (existingInstance != instance)
                                {
                                    continue;
                                }

                                lvi.Text             = instance.HostIdentifier;
                                lvi.SubItems[1].Text = instance.WorkbenchConnection.ConnectionMethod.GetDescription();
                                lvi.SubItems[2].Text = instance.ConnectionStatusText;
                                break;
                            }

                            break;
                        }

                        if (!connectionAlreadyInInstance)
                        {
                            MySqlInstance newInstance = new MySqlInstance(selectedConnection);
                            InstancesList.Add(newInstance);
                            AddInstance(newInstance, true);
                            InstancesList.SaveToFile();
                        }
                    }
                }

                // Workbench connections may have been edited so we may need to refresh the items in the list.
                foreach (ListViewItem lvi in MonitoredInstancesListView.Items)
                {
                    var existingInstance = lvi.Tag as MySqlInstance;
                    if (existingInstance == null || (selectedConnection != null && existingInstance.WorkbenchConnection.Id == selectedConnection.Id))
                    {
                        continue;
                    }

                    var connectionInDisk = MySqlWorkbench.Connections.GetConnectionForId(existingInstance.WorkbenchConnection.Id);
                    if (connectionInDisk == null || connectionInDisk.Equals(existingInstance.WorkbenchConnection))
                    {
                        continue;
                    }

                    lvi.Text             = connectionInDisk.HostIdentifier;
                    lvi.SubItems[1].Text = connectionInDisk.ConnectionMethod.GetDescription();
                    lvi.SubItems[2].Text = connectionInDisk.ConnectionStatusText;
                }

                InstancesListChanged = monitorInstancesDialog.InstancesListChanged;
            }

            MonitoredServicesListView.EndUpdate();
            Cursor.Current = Cursors.Default;
        }
Пример #2
0
        /// <summary>
        /// Performs logic when clicking the Add button for instances.
        /// </summary>
        private void AddInstanceClick()
        {
            Cursor.Current = Cursors.WaitCursor;
            MonitoredServicesListView.BeginUpdate();
            MySqlWorkbenchConnection selectedConnection = null;

            using (var monitorInstancesDialog = new MonitorMySqlServerInstancesDialog(MachinesList, InstancesList))
            {
                if (monitorInstancesDialog.ShowDialog() == DialogResult.OK)
                {
                    selectedConnection = monitorInstancesDialog.SelectedWorkbenchConnection;
                    var mySqlInstanceAndExistingFlag = InstancesList.AddConnectionToMonitor(selectedConnection);
                    if (mySqlInstanceAndExistingFlag != null)
                    {
                        var instance = mySqlInstanceAndExistingFlag.Item1;
                        var instanceAlreadyExists = mySqlInstanceAndExistingFlag.Item2;
                        if (instanceAlreadyExists)
                        {
                            var correspondingListViewItem = MonitoredInstancesListView.Items.OfType <ListViewItem>().FirstOrDefault(lvi => lvi.Tag is MySqlInstance existingInstance &&
                                                                                                                                    existingInstance == instance);
                            if (correspondingListViewItem != null)
                            {
                                correspondingListViewItem.Text             = instance.DisplayConnectionSummaryText;
                                correspondingListViewItem.SubItems[1].Text = instance.WorkbenchConnection.ConnectionMethod.GetDescription();
                                correspondingListViewItem.SubItems[2].Text = instance.ConnectionStatusText;
                            }
                        }
                        else
                        {
                            AddInstance(instance, true);
                        }
                    }
                }

                // Workbench connections may have been edited so we may need to refresh the items in the list.
                foreach (ListViewItem lvi in MonitoredInstancesListView.Items)
                {
                    if (!(lvi.Tag is MySqlInstance existingInstance) ||
                        selectedConnection != null &&
                        existingInstance.WorkbenchConnection.Id == selectedConnection.Id)
                    {
                        continue;
                    }

                    var connectionInDisk = MySqlWorkbench.Connections.GetConnectionForId(existingInstance.WorkbenchConnection.Id);
                    if (connectionInDisk == null ||
                        connectionInDisk.Equals(existingInstance.WorkbenchConnection))
                    {
                        continue;
                    }

                    lvi.Text             = connectionInDisk.HostIdentifier;
                    lvi.SubItems[1].Text = connectionInDisk.ConnectionMethod.GetDescription();
                    lvi.SubItems[2].Text = connectionInDisk.ConnectionStatusText;
                }

                InstancesListChanged = monitorInstancesDialog.InstancesListChanged;
            }

            MonitoredServicesListView.EndUpdate();
            Cursor.Current = Cursors.Default;
        }