1
import flash.events.MouseEvent;
2
import com.example.programmingas3.playlist.PlayList;
3
import com.example.programmingas3.playlist.Song;
4
import com.example.programmingas3.playlist.SortProperty;
6
// constants for the different "states" of the song form
7
private static const ADD_SONG:uint = 1;
8
private static const SONG_DETAIL:uint = 2;
10
private var playList:PlayList = new PlayList();
12
private function initApp():void
14
// set the initial state of the song form, for adding a new song
15
setFormState(ADD_SONG);
17
// prepopulate the list with a few songs
18
playList.addSong(new Song("Nessun Dorma", "Luciano Pavarotti", 1990, "nessundorma.mp3", ["90's", "Opera"]));
19
playList.addSong(new Song("Come Undone", "Duran Duran", 1993, "comeundone.mp3", ["90's", "Pop"]));
20
playList.addSong(new Song("Think of Me", "Sarah Brightman", 1987, "thinkofme.mp3", ["Showtunes"]));
21
playList.addSong(new Song("Unbelievable", "EMF", 1991, "unbelievable.mp3", ["90's", "Pop"]));
23
songList.dataProvider = playList.songList;
27
private function sortList(sortField:SortProperty):void
29
// Make all the sort type buttons enabled.
30
// The active one will be grayed-out below
31
sortByTitle.selected = false;
32
sortByArtist.selected = false;
33
sortByYear.selected = false;
37
case SortProperty.TITLE:
38
sortByTitle.selected = true;
40
case SortProperty.ARTIST:
41
sortByArtist.selected = true;
43
case SortProperty.YEAR:
44
sortByYear.selected = true;
48
playList.sortList(sortField);
54
private function refreshList():void
56
// remember which song was selected
57
var selectedSong:Song = Song(songList.selectedItem);
59
// re-assign the song list as the dataprovider to get the newly sorted list
60
// and force the List control to refresh itself
61
songList.dataProvider = playList.songList;
63
// reset the song selection
64
if (selectedSong != null)
66
songList.selectedItem = selectedSong;
71
private function songSelectionChange():void
73
if (songList.selectedIndex != -1)
75
setFormState(SONG_DETAIL);
79
setFormState(ADD_SONG);
84
private function addNewSong():void
86
// gather the values from the form and add the new song
87
var title:String = newSongTitle.text;
88
var artist:String = newSongArtist.text;
89
var year:uint = newSongYear.value;
90
var filename:String = newSongFilename.text;
91
var genres:Array = newSongGenres.selectedItems;
93
playList.addSong(new Song(title, artist, year, filename, genres));
97
// clear out the "add song" form fields
98
setFormState(ADD_SONG);
102
private function songListLabel(item:Object):String
104
return item.toString();
108
private function setFormState(state:uint):void
110
// set the form title and control state
114
formTitle.text = "Add New Song";
115
// show the submit button
116
submitSongData.visible = true;
117
showAddControlsBtn.visible = false;
118
// clear the form fields
119
newSongTitle.text = "";
120
newSongArtist.text = "";
121
newSongYear.value = (new Date()).fullYear;
122
newSongFilename.text = "";
123
newSongGenres.selectedIndex = -1;
124
// deselect the currently selected song (if any)
125
songList.selectedIndex = -1;
129
formTitle.text = "Song Details";
130
// populate the form with the selected item's data
131
var selectedSong:Song = Song(songList.selectedItem);
132
newSongTitle.text = selectedSong.title;
133
newSongArtist.text = selectedSong.artist;
134
newSongYear.value = selectedSong.year;
135
newSongFilename.text = selectedSong.filename;
136
newSongGenres.selectedItems = selectedSong.genres;
137
// hide the submit button
138
submitSongData.visible = false;
139
showAddControlsBtn.visible = true;