PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/Source Code/PowerSong/Categories/frmManageCategories.vb

#
Visual Basic | 145 lines | 91 code | 39 blank | 15 comment | 0 complexity | 236b00f58f8d79ce6a718ee4836b4858 MD5 | raw file
  1. Imports System.Windows.Forms
  2. Imports PowerSong.SongDatabase
  3. Imports PowerSong.SongDatabase.Items
  4. Public Class frmManageCategories
  5. Private FDatabase As Database = Nothing
  6. Public Sub New(ByVal database As Database)
  7. InitializeComponent()
  8. FDatabase = database
  9. UpdateCategoriesList()
  10. End Sub
  11. Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
  12. Me.DialogResult = System.Windows.Forms.DialogResult.OK
  13. Me.Close()
  14. End Sub
  15. Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
  16. ' Ensure a category has been selected
  17. If lstCategories.SelectedIndex = -1 Then
  18. MsgBox("Please select a category first.", MsgBoxStyle.Exclamation)
  19. Exit Sub
  20. End If
  21. ' Confirm the delete
  22. Dim SelectedItem As ListItem(Of Guid) = lstCategories.SelectedItem
  23. If MsgBox("Are you sure you wish to remove category '" + SelectedItem.ToString + "'?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  24. ' Check there are no problems deleting the category
  25. Dim ProblemSongs As New List(Of Guid)
  26. For Each SongID As Guid In FDatabase.Songs.GetCategorySongIDs(SelectedItem.Key)
  27. ' Determine if there is only one category (which is obviously the one we are removing)
  28. If FDatabase.Songs.GetSongCategories(SongID).Count = 1 Then
  29. ProblemSongs.Add(SongID)
  30. End If
  31. Next
  32. ' Resolve problem songs
  33. Dim Proceed As Boolean = True
  34. If ProblemSongs.Count > 0 Then
  35. ' Find out what the user wants to do
  36. Dim ResolutionForm As New frmProblemSongs(FDatabase, SelectedItem.Key, ProblemSongs)
  37. If ResolutionForm.ShowDialog <> Windows.Forms.DialogResult.OK Then Proceed = False
  38. ' Do what the user asked
  39. Select Case ResolutionForm.SelectedChoice
  40. Case frmProblemSongs.ChoiceResult.DoNothing
  41. Proceed = False
  42. Case frmProblemSongs.ChoiceResult.Reassign
  43. Dim NewCategoryID As Guid = ResolutionForm.SelectedReassignmentCategory
  44. For Each SongID As Guid In ProblemSongs
  45. Dim Song As SongItem = FDatabase.Songs.GetSong(SongID)
  46. Song.Categories.Remove(SelectedItem.Key)
  47. Song.Categories.Add(NewCategoryID)
  48. FDatabase.Songs.EditSong(SongID, Song)
  49. Next
  50. MsgBox(ProblemSongs.Count.ToString + " songs have been reassigned to the '" + FDatabase.Categories.GetCategoryName(NewCategoryID) + "' category.", MsgBoxStyle.Information)
  51. Case frmProblemSongs.ChoiceResult.Delete
  52. For Each SongID As Guid In ProblemSongs
  53. FDatabase.Songs.DeleteSong(SongID)
  54. Next
  55. MsgBox(ProblemSongs.Count.ToString + " songs have been deleted.", MsgBoxStyle.Information)
  56. Case Else
  57. Throw New Exception("Internal error.")
  58. End Select
  59. End If
  60. ' Delete the category
  61. If Proceed Then
  62. FDatabase.Categories.DeleteCategory(SelectedItem.Key)
  63. UpdateCategoriesList()
  64. End If
  65. End If
  66. End Sub
  67. Private Sub UpdateCategoriesList()
  68. ' Clear the list first
  69. lstCategories.Items.Clear()
  70. ' Add each of the categories to the list
  71. For Each Category As KeyValuePair(Of Guid, String) In FDatabase.Categories.GetAllCategories
  72. lstCategories.Items.Add(New ListItem(Of Guid)(Category.Value, Category.Key))
  73. Next
  74. End Sub
  75. Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
  76. If New frmCategory(FDatabase).ShowDialog = Windows.Forms.DialogResult.OK Then
  77. UpdateCategoriesList()
  78. End If
  79. End Sub
  80. Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
  81. ' Ensure a category has been selected
  82. If lstCategories.SelectedIndex = -1 Then
  83. MsgBox("Please select a category first.", MsgBoxStyle.Exclamation)
  84. Exit Sub
  85. End If
  86. ' Edit the category
  87. Dim SelectedItem As ListItem(Of Guid) = lstCategories.SelectedItem
  88. If New frmCategory(FDatabase, SelectedItem.Key).ShowDialog = Windows.Forms.DialogResult.OK Then
  89. UpdateCategoriesList()
  90. End If
  91. End Sub
  92. Private Sub btnShowSongs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowSongs.Click
  93. ' Check that a category has been selected
  94. If lstCategories.SelectedIndex = -1 Then
  95. MsgBox("Please select a category before attempting to view the songs assigned to it.", MsgBoxStyle.Information)
  96. Exit Sub
  97. End If
  98. ' Determine which songs are in the selected category
  99. Dim Item As ListItem(Of Guid) = lstCategories.SelectedItem
  100. Dim SongsInCategory As List(Of Guid) = FDatabase.Songs.GetCategorySongIDs(Item.Key)
  101. ' Show the songs in the selected category
  102. Call New frmSongList(FDatabase, _
  103. "Songs in category '" + lstCategories.SelectedItem.ToString + "':", _
  104. SongsInCategory).ShowDialog()
  105. End Sub
  106. End Class