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