PageRenderTime 38ms CodeModel.GetById 11ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

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