/Application/GUI/Windows/NameDialog.xaml.cs
C# | 144 lines | 84 code | 20 blank | 40 comment | 14 complexity | 99e757f3bc68d7fb6d1cfae0c1998862 MD5 | raw file
1/** 2 * NameDialog.xaml.cs 3 * 4 * The small dialog shown when creating or renaming a keyboard 5 * shortcut profile. 6 * 7 * * * * * * * * * 8 * 9 * This code is part of the Stoffi Music Player Project. 10 * Visit our website at: stoffiplayer.com 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 15 * 3 of the License, or (at your option) any later version. 16 * 17 * See stoffiplayer.com/license for more information. 18 **/ 19 20using System; 21using System.Collections.Generic; 22using System.Linq; 23using System.Text; 24using System.Text.RegularExpressions; 25using System.Windows; 26using System.Windows.Controls; 27using System.Windows.Data; 28using System.Windows.Documents; 29using System.Windows.Input; 30using System.Windows.Media; 31using System.Windows.Media.Imaging; 32using System.Windows.Shapes; 33 34namespace Stoffi 35{ 36 /// <summary> 37 /// Interaction logic for NameDialog.xaml 38 /// </summary> 39 public partial class NameDialog : Window 40 { 41 #region Members 42 43 private List<string> occupiedNames; 44 45 #endregion 46 47 #region Properties 48 49 /// <summary> 50 /// Gets or sets the name we are renaming from. 51 /// If set to "" the dialog will act as if we are creating a name. 52 /// </summary> 53 public String RenameFrom { get; set; } 54 55 #endregion 56 57 #region Constructor 58 59 /// <summary> 60 /// Creates an instance of the name dialog 61 /// </summary> 62 /// <param name="occupied">A list of all occupied names</param> 63 /// <param name="renameFrom">The original name. If set to "" dialog will act as creating a name</param> 64 public NameDialog(List<string> occupied, String renameFrom = "") 65 { 66 U.L(LogLevel.Debug, "NAME DIALOG", "Initialize"); 67 InitializeComponent(); 68 U.L(LogLevel.Debug, "NAME DIALOG", "Initialized"); 69 occupiedNames = occupied; 70 RenameFrom = renameFrom; 71 if (RenameFrom != "") Title = U.T("DialogNameRenameTitle"); 72 else Title = U.T("DialogNameCreateTitle"); 73 ProfileName.Text = RenameFrom; 74 ProfileName.Focus(); 75 ProfileName.SelectAll(); 76 } 77 78 #endregion 79 80 #region Methods 81 82 #region Event handlers 83 84 /// <summary> 85 /// 86 /// </summary> 87 /// <param name="sender"></param> 88 /// <param name="e"></param> 89 private void OK_Click(object sender, RoutedEventArgs e) 90 { 91 // allow rename to same name 92 if (ProfileName.Text == RenameFrom && RenameFrom != "") 93 { 94 DialogResult = false; 95 Close(); 96 } 97 98 // check for duplicate names 99 foreach (string name in occupiedNames) 100 { 101 if (name == ProfileName.Text) 102 { 103 Error.Text = U.T("DialogNameExistsError"); 104 Error.Visibility = System.Windows.Visibility.Visible; 105 return; 106 } 107 } 108 109 // check for invalid name 110 // TODO: Pattern is too restrictive! 111 Regex alphaNumPattern = new Regex("[^a-zA-Z0-9 ]"); 112 if (alphaNumPattern.IsMatch(ProfileName.Text)) 113 { 114 Error.Text = U.T("DialogNameInvalidError"); 115 Error.Visibility = System.Windows.Visibility.Visible; 116 return; 117 } 118 119 // check for empty name 120 if (ProfileName.Text.Replace(" ", "").Replace("\t", "") == "") 121 { 122 Error.Text = U.T("DialogNameEmptyError"); 123 Error.Visibility = System.Windows.Visibility.Visible; 124 return; 125 } 126 127 DialogResult = true; 128 Close(); 129 } 130 131 #endregion 132 133 private void ShortcutProfileDialog_KeyDown(object sender, KeyEventArgs e) 134 { 135 if (e.Key == Key.Escape) 136 { 137 DialogResult = false; 138 Close(); 139 } 140 } 141 142 #endregion 143 } 144}