/Source Code/PowerSong/Categories/frmManageCategories.vb
Visual Basic | 145 lines | 91 code | 39 blank | 15 comment | 0 complexity | 236b00f58f8d79ce6a718ee4836b4858 MD5 | raw file
- Imports System.Windows.Forms
- Imports PowerSong.SongDatabase
- Imports PowerSong.SongDatabase.Items
-
- Public Class frmManageCategories
-
- Private FDatabase As Database = Nothing
-
- Public Sub New(ByVal database As Database)
- InitializeComponent()
- FDatabase = database
- UpdateCategoriesList()
- End Sub
-
- Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
- Me.DialogResult = System.Windows.Forms.DialogResult.OK
- Me.Close()
- End Sub
-
- Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
-
- ' Ensure a category has been selected
- If lstCategories.SelectedIndex = -1 Then
- MsgBox("Please select a category first.", MsgBoxStyle.Exclamation)
- Exit Sub
- End If
-
- ' Confirm the delete
- Dim SelectedItem As ListItem(Of Guid) = lstCategories.SelectedItem
- If MsgBox("Are you sure you wish to remove category '" + SelectedItem.ToString + "'?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
-
- ' Check there are no problems deleting the category
- Dim ProblemSongs As New List(Of Guid)
- For Each SongID As Guid In FDatabase.Songs.GetCategorySongIDs(SelectedItem.Key)
-
- ' Determine if there is only one category (which is obviously the one we are removing)
- If FDatabase.Songs.GetSongCategories(SongID).Count = 1 Then
- ProblemSongs.Add(SongID)
- End If
-
- Next
-
- ' Resolve problem songs
- Dim Proceed As Boolean = True
- If ProblemSongs.Count > 0 Then
-
- ' Find out what the user wants to do
- Dim ResolutionForm As New frmProblemSongs(FDatabase, SelectedItem.Key, ProblemSongs)
- If ResolutionForm.ShowDialog <> Windows.Forms.DialogResult.OK Then Proceed = False
-
- ' Do what the user asked
- Select Case ResolutionForm.SelectedChoice
-
- Case frmProblemSongs.ChoiceResult.DoNothing
- Proceed = False
-
- Case frmProblemSongs.ChoiceResult.Reassign
- Dim NewCategoryID As Guid = ResolutionForm.SelectedReassignmentCategory
- For Each SongID As Guid In ProblemSongs
- Dim Song As SongItem = FDatabase.Songs.GetSong(SongID)
- Song.Categories.Remove(SelectedItem.Key)
- Song.Categories.Add(NewCategoryID)
- FDatabase.Songs.EditSong(SongID, Song)
- Next
- MsgBox(ProblemSongs.Count.ToString + " songs have been reassigned to the '" + FDatabase.Categories.GetCategoryName(NewCategoryID) + "' category.", MsgBoxStyle.Information)
-
- Case frmProblemSongs.ChoiceResult.Delete
- For Each SongID As Guid In ProblemSongs
- FDatabase.Songs.DeleteSong(SongID)
- Next
- MsgBox(ProblemSongs.Count.ToString + " songs have been deleted.", MsgBoxStyle.Information)
-
- Case Else
- Throw New Exception("Internal error.")
-
- End Select
-
- End If
-
- ' Delete the category
- If Proceed Then
- FDatabase.Categories.DeleteCategory(SelectedItem.Key)
- UpdateCategoriesList()
- End If
-
- End If
-
- End Sub
-
- Private Sub UpdateCategoriesList()
-
- ' Clear the list first
- lstCategories.Items.Clear()
-
- ' Add each of the categories to the list
- For Each Category As KeyValuePair(Of Guid, String) In FDatabase.Categories.GetAllCategories
- lstCategories.Items.Add(New ListItem(Of Guid)(Category.Value, Category.Key))
- Next
-
- End Sub
-
- Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
-
- If New frmCategory(FDatabase).ShowDialog = Windows.Forms.DialogResult.OK Then
- UpdateCategoriesList()
- End If
-
- End Sub
-
- Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
-
- ' Ensure a category has been selected
- If lstCategories.SelectedIndex = -1 Then
- MsgBox("Please select a category first.", MsgBoxStyle.Exclamation)
- Exit Sub
- End If
-
- ' Edit the category
- Dim SelectedItem As ListItem(Of Guid) = lstCategories.SelectedItem
- If New frmCategory(FDatabase, SelectedItem.Key).ShowDialog = Windows.Forms.DialogResult.OK Then
- UpdateCategoriesList()
- End If
-
- End Sub
-
- Private Sub btnShowSongs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowSongs.Click
-
- ' Check that a category has been selected
- If lstCategories.SelectedIndex = -1 Then
- MsgBox("Please select a category before attempting to view the songs assigned to it.", MsgBoxStyle.Information)
- Exit Sub
- End If
-
- ' Determine which songs are in the selected category
- Dim Item As ListItem(Of Guid) = lstCategories.SelectedItem
- Dim SongsInCategory As List(Of Guid) = FDatabase.Songs.GetCategorySongIDs(Item.Key)
-
- ' Show the songs in the selected category
- Call New frmSongList(FDatabase, _
- "Songs in category '" + lstCategories.SelectedItem.ToString + "':", _
- SongsInCategory).ShowDialog()
-
- End Sub
-
- End Class