/Application/GUI/Windows/NameDialog.xaml.cs

http://yet-another-music-application.googlecode.com/ · 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. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Text.RegularExpressions;
  24. using System.Windows;
  25. using System.Windows.Controls;
  26. using System.Windows.Data;
  27. using System.Windows.Documents;
  28. using System.Windows.Input;
  29. using System.Windows.Media;
  30. using System.Windows.Media.Imaging;
  31. using System.Windows.Shapes;
  32. namespace Stoffi
  33. {
  34. /// <summary>
  35. /// Interaction logic for NameDialog.xaml
  36. /// </summary>
  37. public partial class NameDialog : Window
  38. {
  39. #region Members
  40. private List<string> occupiedNames;
  41. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// Gets or sets the name we are renaming from.
  45. /// If set to "" the dialog will act as if we are creating a name.
  46. /// </summary>
  47. public String RenameFrom { get; set; }
  48. #endregion
  49. #region Constructor
  50. /// <summary>
  51. /// Creates an instance of the name dialog
  52. /// </summary>
  53. /// <param name="occupied">A list of all occupied names</param>
  54. /// <param name="renameFrom">The original name. If set to "" dialog will act as creating a name</param>
  55. public NameDialog(List<string> occupied, String renameFrom = "")
  56. {
  57. U.L(LogLevel.Debug, "NAME DIALOG", "Initialize");
  58. InitializeComponent();
  59. U.L(LogLevel.Debug, "NAME DIALOG", "Initialized");
  60. occupiedNames = occupied;
  61. RenameFrom = renameFrom;
  62. if (RenameFrom != "") Title = U.T("DialogNameRenameTitle");
  63. else Title = U.T("DialogNameCreateTitle");
  64. ProfileName.Text = RenameFrom;
  65. ProfileName.Focus();
  66. ProfileName.SelectAll();
  67. }
  68. #endregion
  69. #region Methods
  70. #region Event handlers
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void OK_Click(object sender, RoutedEventArgs e)
  77. {
  78. // allow rename to same name
  79. if (ProfileName.Text == RenameFrom && RenameFrom != "")
  80. {
  81. DialogResult = false;
  82. Close();
  83. }
  84. // check for duplicate names
  85. foreach (string name in occupiedNames)
  86. {
  87. if (name == ProfileName.Text)
  88. {
  89. Error.Text = U.T("DialogNameExistsError");
  90. Error.Visibility = System.Windows.Visibility.Visible;
  91. return;
  92. }
  93. }
  94. // check for invalid name
  95. // TODO: Pattern is too restrictive!
  96. Regex alphaNumPattern = new Regex("[^a-zA-Z0-9 ]");
  97. if (alphaNumPattern.IsMatch(ProfileName.Text))
  98. {
  99. Error.Text = U.T("DialogNameInvalidError");
  100. Error.Visibility = System.Windows.Visibility.Visible;
  101. return;
  102. }
  103. // check for empty name
  104. if (ProfileName.Text.Replace(" ", "").Replace("\t", "") == "")
  105. {
  106. Error.Text = U.T("DialogNameEmptyError");
  107. Error.Visibility = System.Windows.Visibility.Visible;
  108. return;
  109. }
  110. DialogResult = true;
  111. Close();
  112. }
  113. #endregion
  114. private void ShortcutProfileDialog_KeyDown(object sender, KeyEventArgs e)
  115. {
  116. if (e.Key == Key.Escape)
  117. {
  118. DialogResult = false;
  119. Close();
  120. }
  121. }
  122. #endregion
  123. }
  124. }