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; }
public void ShowError(string errorText) { ShowError(HResult.FromErrorText(errorText)); }