/Application/GUI/Windows/GeneratePlaylist.xaml.cs

http://yet-another-music-application.googlecode.com/ · C# · 195 lines · 142 code · 18 blank · 35 comment · 23 complexity · b3682afb6e5a6eb2082f653a28007dbd MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace Stoffi
  16. {
  17. /// <summary>
  18. /// Interaction logic for CreateRandomPlaylist.xaml
  19. /// </summary>
  20. public partial class GeneratePlaylist : Window
  21. {
  22. #region Constructor
  23. /// <summary>
  24. /// Creates a dialog for generating a random playlist.
  25. /// </summary>
  26. public GeneratePlaylist()
  27. {
  28. InitializeComponent();
  29. foreach (PlaylistData p in SettingsManager.Playlists)
  30. {
  31. Lists.Items.Add(new ComboBoxItem() { Content = p.Name });
  32. }
  33. if (SettingsManager.CurrentSelectedNavigation.StartsWith("Playlist:"))
  34. {
  35. string name = SettingsManager.CurrentSelectedNavigation.Split(new[] { ':' }, 2)[1];
  36. foreach (ComboBoxItem cbi in Lists.Items)
  37. if ((string)cbi.Content == name)
  38. {
  39. cbi.IsSelected = true;
  40. break;
  41. }
  42. }
  43. else
  44. {
  45. Lists.SelectedIndex = 0;
  46. Lists_SelectionChanged(null, null);
  47. }
  48. }
  49. #endregion
  50. #region Methods
  51. #region Private
  52. /// <summary>
  53. /// Gets the corresponding collection of tracks given the selected list.
  54. /// </summary>
  55. /// <returns>The collection of tracks corresponding to the selected list</returns>
  56. private ObservableCollection<TrackData> GetTracks()
  57. {
  58. if (Lists.SelectedIndex > 0)
  59. {
  60. ComboBoxItem cbi = Lists.SelectedItem as ComboBoxItem;
  61. PlaylistData p = PlaylistManager.FindPlaylist((string)cbi.Content);
  62. if (p != null)
  63. return p.Tracks;
  64. else
  65. return new ObservableCollection<TrackData>();
  66. }
  67. else
  68. return SettingsManager.FileTracks;
  69. }
  70. /// <summary>
  71. /// Tries to turn the number into an integer.
  72. /// If conversion fails or number is too large or too
  73. /// small -1 is returned and the proper error message is
  74. /// displayed.
  75. /// </summary>
  76. private int GetNumber()
  77. {
  78. string txt = Number.Text;
  79. try
  80. {
  81. int n = Convert.ToInt32(txt);
  82. int m = GetTracks().Count;
  83. if (n < 1)
  84. {
  85. ErrorMessage.Text = String.Format(U.T("DialogNumberTooSmall"), 0);
  86. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  87. return -1;
  88. }
  89. if (n > m)
  90. {
  91. ErrorMessage.Text = String.Format(U.T("DialogNumberTooLarge"), m+1);
  92. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  93. return -1;
  94. }
  95. else
  96. return n;
  97. }
  98. catch
  99. {
  100. ErrorMessage.Text = U.T("DialogNumberInvalid");
  101. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  102. return -1;
  103. }
  104. }
  105. #endregion
  106. #region Event handlers
  107. /// <summary>
  108. /// Invoked when the user clicks "Generate".
  109. /// </summary>
  110. /// <remarks>
  111. /// Will verify the name and generate a playlist.
  112. /// </remarks>
  113. /// <param name="sender">The sender of the event</param>
  114. /// <param name="e">The event data</param>
  115. private void Generate_Click(object sender, RoutedEventArgs e)
  116. {
  117. Regex alphaNumPattern = new Regex("[^a-zA-Z0-9 ]");
  118. if (alphaNumPattern.IsMatch(ListName.Text))
  119. {
  120. ErrorMessage.Text = U.T("DialogNameInvalidError");
  121. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  122. }
  123. else if (ListName.Text == "")
  124. {
  125. ErrorMessage.Text = U.T("DialogNameEmptyError");
  126. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  127. }
  128. else if (PlaylistManager.FindPlaylist(ListName.Text) != null)
  129. {
  130. ErrorMessage.Text = U.T("DialogNameExistsError");
  131. ErrorMessage.Visibility = System.Windows.Visibility.Visible;
  132. }
  133. else
  134. {
  135. // copy tracks to temporary list
  136. List<TrackData> tracks = new List<TrackData>();
  137. foreach (TrackData t in GetTracks())
  138. tracks.Add(t);
  139. int n = GetNumber();
  140. if (n < 0) return;
  141. if (tracks.Count > 0)
  142. {
  143. // create empty playlist
  144. PlaylistData p = PlaylistManager.CreatePlaylist(ListName.Text);
  145. if (p != null)
  146. {
  147. // move tracks from temporary list into playlist
  148. for (int i = 0; i < n && tracks.Count > 0; i++)
  149. {
  150. Random r = new Random();
  151. int x = r.Next(tracks.Count - 1);
  152. TrackData t = tracks[x];
  153. p.Tracks.Add(t);
  154. tracks.RemoveAt(x);
  155. }
  156. }
  157. }
  158. Close();
  159. }
  160. }
  161. /// <summary>
  162. /// Invoked when the user selects a list.
  163. /// </summary>
  164. /// <remarks>
  165. /// Will fill the Number control with 1 through "number of tracks in list"
  166. /// </remarks>
  167. /// <param name="sender">The sender of the event</param>
  168. /// <param name="e">The event data</param>
  169. private void Lists_SelectionChanged(object sender, SelectionChangedEventArgs e)
  170. {
  171. if (Number == null) return;
  172. Number.Text = GetTracks().Count.ToString();
  173. }
  174. #endregion
  175. #endregion
  176. }
  177. }