static void AssignAlias(ConsoleIdentifier identifier, List <XtfConsole> consoles)
        {
            var console = consoles.FirstOrDefault(c => c.Address == identifier.Address);

            if (console == null)
            {
                identifier.Alias = null;
            }
            else
            {
                identifier.Alias = console.Alias;
            }
        }
        static void AssignAlias(ConsoleIdentifier identifier, List<XtfConsole> consoles)
        {
            var console = consoles.FirstOrDefault(c => c.Address == identifier.Address);

            if (console == null)
            {
                identifier.Alias = null;
            }
            else
            {
                identifier.Alias = console.Alias;
            }
        }
        void InitConsolesList()
        {
            if (changesMade)
            {
                // (any non-null value will work -- the display is controlled by FootnoteTemplate in the xaml...)
                this.Footnote = true;
                return;
            }

            var            defaultConsole       = this.consoleManager.GetDefaultConsole();
            string         defaultConsoleAlias  = defaultConsole == null ? null : defaultConsole.Alias;
            string         selectedConsoleAlias = null;
            ConsoleWrapper selectedConsole      = null;

            if (this.listview != null)
            {
                var selected = this.listview.SelectedItem as ConsoleWrapper;

                if (selected != null)
                {
                    selectedConsoleAlias = selected.Alias;
                }
            }

            this.Consoles.Clear();
            this.consoleInEditMode = null;

            var consoles = this.consoleManager.GetConsoles();

            ConsoleIdentifier.UpdateOutstandingIdentifiers(defaultConsole, consoles);

            foreach (var c in consoles)
            {
                var newWrapper = new ConsoleWrapper(c, c.Alias == defaultConsoleAlias);
                this.Consoles.Add(newWrapper);
                if (c.Alias == selectedConsoleAlias)
                {
                    selectedConsole = newWrapper;
                }
            }

            if (selectedConsole != null)
            {
                this.listview.SelectedItem = selectedConsole;
            }
        }
        void OnOkButtonClicked(object sender, RoutedEventArgs e)
        {
            if (this.consoleInEditMode != null)
            {
                SetConsoleInEditMode(null, false);
                return;
            }

            var selectedConsole = this.listview.SelectedItem as ConsoleWrapper;

            if (selectedConsole == null)
            {
                return;
            }

            if (this.changesMade)
            {
                var dupeTable = new Dictionary <string, ConsoleWrapper>();

                // Check the console aliases for duplicates
                foreach (var c in this.Consoles)
                {
                    ConsoleWrapper dupe;

                    if (dupeTable.TryGetValue(c.Alias, out dupe))
                    {
                        this.notificationService.ShowError(string.Format(StringResources.ConsoleAliasAlreadyExistsFmt, c.Alias), HResult.FromErrorText(StringResources.ConsoleAliasesMustBeUnique));
                        this.listview.SelectedItem = c;
                        return;
                    }
                    dupeTable[c.Alias] = c;
                }

                // No duplicates... let's try to make the console manager look like what we've got.
                try
                {
                    // First, nuke the pure removals
                    foreach (var c in this.removals)
                    {
                        // NOTE:  Remove by the real console alias.  The user might have changed the local one...
                        this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                    }

                    // Now remove anything that has changed.  The replacements list ends up being all consoles that
                    // need to be written, either because they're new, or changed (removed + added).
                    var replacements = new List <ConsoleWrapper>();

                    foreach (var c in this.Consoles)
                    {
                        if (c.RealConsole != null && (c.RealConsole.Alias != c.Alias || c.RealConsole.Address != c.Address))
                        {
                            this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                            replacements.Add(c);
                        }
                        else if (c.RealConsole == null)
                        {
                            replacements.Add(c);
                        }
                    }

                    // Re-add everything in replacements list
                    foreach (var c in replacements)
                    {
                        this.consoleManager.AddConsole(c.Alias, c.Address);
                    }

                    // Set the default kit
                    var defaultConsole = this.Consoles.FirstOrDefault(c => c.IsDefault);

                    if (defaultConsole != null)
                    {
                        // NOTE:  If the user deletes the default console and doesn't set a new one, the console
                        // manager automatically clears the (now bogus) default console specification.
                        this.consoleManager.SetDefaultConsole(defaultConsole.Alias);
                    }
                }
                catch (Exception ex)
                {
                    this.notificationService.ShowError(HResult.FromException(ex));

                    // This is somewhat bad... we possibly made *some* of our changes, so chances are that
                    // trying again will end up failing again, even if the user fixed the original problem.
                    // Best hope is to reset the dialog to whatever state the actual list is in.
                    InitConsolesList();
                    return;
                }
            }

            if (selectedConsole.IsDefault)
            {
                this.SpecificKit = null;
            }
            else
            {
                this.SpecificKit = selectedConsole.Address;
            }

            this.SelectedConsole = new ConsoleIdentifier(selectedConsole.Alias, selectedConsole.Address, selectedConsole.IsDefault);

            // Re-update the outstanding console identifiers
            ConsoleIdentifier.UpdateOutstandingIdentifiers(this.consoleManager.GetDefaultConsole(), this.consoleManager.GetConsoles());
            DialogResult = true;
        }
        void OnOkButtonClicked(object sender, RoutedEventArgs e)
        {
            if (this.consoleInEditMode != null)
            {
                SetConsoleInEditMode(null, false);
                return;
            }

            var selectedConsole = this.listview.SelectedItem as ConsoleWrapper;

            if (selectedConsole == null)
            {
                return;
            }

            if (this.changesMade)
            {
                var dupeTable = new Dictionary<string, ConsoleWrapper>();

                // Check the console aliases for duplicates
                foreach (var c in this.Consoles)
                {
                    ConsoleWrapper dupe;

                    if (dupeTable.TryGetValue(c.Alias, out dupe))
                    {
                        this.notificationService.ShowError(string.Format(StringResources.ConsoleAliasAlreadyExistsFmt, c.Alias), HResult.FromErrorText(StringResources.ConsoleAliasesMustBeUnique));
                        this.listview.SelectedItem = c;
                        return;
                    }
                    dupeTable[c.Alias] = c;
                }

                // No duplicates... let's try to make the console manager look like what we've got.
                try
                {
                    // First, nuke the pure removals
                    foreach (var c in this.removals)
                    {
                        // NOTE:  Remove by the real console alias.  The user might have changed the local one...
                        this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                    }

                    // Now remove anything that has changed.  The replacements list ends up being all consoles that
                    // need to be written, either because they're new, or changed (removed + added).
                    var replacements = new List<ConsoleWrapper>();

                    foreach (var c in this.Consoles)
                    {
                        if (c.RealConsole != null && (c.RealConsole.Alias != c.Alias || c.RealConsole.Address != c.Address))
                        {
                            this.consoleManager.RemoveConsole(c.RealConsole.Alias);
                            replacements.Add(c);
                        }
                        else if (c.RealConsole == null)
                        {
                            replacements.Add(c);
                        }
                    }

                    // Re-add everything in replacements list
                    foreach (var c in replacements)
                    {
                        this.consoleManager.AddConsole(c.Alias, c.Address);
                    }

                    // Set the default kit
                    var defaultConsole = this.Consoles.FirstOrDefault(c => c.IsDefault);

                    if (defaultConsole != null)
                    {
                        // NOTE:  If the user deletes the default console and doesn't set a new one, the console
                        // manager automatically clears the (now bogus) default console specification.
                        this.consoleManager.SetDefaultConsole(defaultConsole.Alias);
                    }
                }
                catch (Exception ex)
                {
                    this.notificationService.ShowError(HResult.FromException(ex));

                    // This is somewhat bad... we possibly made *some* of our changes, so chances are that 
                    // trying again will end up failing again, even if the user fixed the original problem.  
                    // Best hope is to reset the dialog to whatever state the actual list is in.
                    InitConsolesList();
                    return;
                }
            }

            if (selectedConsole.IsDefault)
            {
                this.SpecificKit = null;
            }
            else
            {
                this.SpecificKit = selectedConsole.Address;
            }

            this.SelectedConsole = new ConsoleIdentifier(selectedConsole.Alias, selectedConsole.Address, selectedConsole.IsDefault);

            // Re-update the outstanding console identifiers
            ConsoleIdentifier.UpdateOutstandingIdentifiers(this.consoleManager.GetDefaultConsole(), this.consoleManager.GetConsoles());
            DialogResult = true;
        }