topical media & game development

talk show tell print

actionscript-book-PlayList-PlayListApp.mx

actionscript-book-PlayList-PlayListApp.mx [swf] flex


  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          xmlns="*"
          paddingTop="0"
          layout="vertical"
          creationComplete="initApp()">
          
          <mx:Script>
                  <![CDATA[
                          import flash.events.MouseEvent;
                          import com.example.programmingas3.playlist.PlayList;
                          import com.example.programmingas3.playlist.Song;
                          import com.example.programmingas3.playlist.SortProperty;
  
                          // constants for the different "states" of the song form
                          private static const ADD_SONG:uint = 1;
                          private static const SONG_DETAIL:uint = 2;
                          
                          private var playList:PlayList = new PlayList();
  
                          private function initApp():void
                          {
                                  // set the initial state of the song form, for adding a new song
                                  setFormState(ADD_SONG);
                                  
                                  // prepopulate the list with a few songs
                                  playList.addSong(new Song("Nessun Dorma", "Luciano Pavarotti", 1990, "nessundorma.mp3", ["90's", "Opera"]));
                                  playList.addSong(new Song("Come Undone", "Duran Duran", 1993, "comeundone.mp3", ["90's", "Pop"]));
                                  playList.addSong(new Song("Think of Me", "Sarah Brightman", 1987, "thinkofme.mp3", ["Showtunes"]));
                                  playList.addSong(new Song("Unbelievable", "EMF", 1991, "unbelievable.mp3", ["90's", "Pop"]));
  
                                  songList.dataProvider = playList.songList;
                          }
  
                          private function sortList(sortField:SortProperty):void
                          {
                                  // Make all the sort type buttons enabled.
                                  // The active one will be grayed-out below
                                  sortByTitle.selected = false;
                                  sortByArtist.selected = false;
                                  sortByYear.selected = false;
  
                                  switch (sortField)
                                  {
                                          case SortProperty.TITLE:
                                                  sortByTitle.selected = true;
                                                  break;
                                          case SortProperty.ARTIST:
                                                  sortByArtist.selected = true;
                                                  break;
                                          case SortProperty.YEAR:
                                                  sortByYear.selected = true;
                                                  break;
                                  }
  
                                  playList.sortList(sortField);
                                  
                                  refreshList();
                          }
  
                          private function refreshList():void
                          {
                                  // remember which song was selected
                                  var selectedSong:Song = Song(songList.selectedItem);
                                  
                                  // re-assign the song list as the dataprovider to get the newly sorted list
                                  // and force the List control to refresh itself
                                  songList.dataProvider = playList.songList;
                                  
                                  // reset the song selection
                                  if (selectedSong != null)
                                  {
                                          songList.selectedItem = selectedSong;
                                  }
                          }
  
                          private function songSelectionChange():void
                          {
                                  if (songList.selectedIndex != -1)
                                  {
                                          setFormState(SONG_DETAIL);
                                  }
                                  else
                                  {
                                          setFormState(ADD_SONG);
                                  }
                          }
  
                          private function addNewSong():void
                          {
                                  // gather the values from the form and add the new song
                                  var title:String = newSongTitle.text;
                                  var artist:String = newSongArtist.text;
                                  var year:uint = newSongYear.value;
                                  var filename:String = newSongFilename.text;
                                  var genres:Array = newSongGenres.selectedItems;
  
                                  playList.addSong(new Song(title, artist, year, filename, genres));
  
                                  refreshList();
          
                                  // clear out the "add song" form fields
                                  setFormState(ADD_SONG);
                          }
  
                          private function songListLabel(item:Object):String
                          {
                                  return item.toString();
                          }
  
                          private function setFormState(state:uint):void
                          {
                                  // set the form title and control state
                                  switch (state)
                                  {
                                          case ADD_SONG:
                                                  formTitle.text = "Add New Song";
                                                  // show the submit button
                                                  submitSongData.visible = true;
                                                  showAddControlsBtn.visible = false;
                                                  // clear the form fields
                                                  newSongTitle.text = "";
                                                  newSongArtist.text = "";
                                                  newSongYear.value = (new Date()).fullYear;
                                                  newSongFilename.text = "";
                                                  newSongGenres.selectedIndex = -1;
                                                  // deselect the currently selected song (if any)
                                                  songList.selectedIndex = -1;
                                                  break;
                                                  
                                          case SONG_DETAIL:
                                                  formTitle.text = "Song Details";
                                                  // populate the form with the selected item's data
                                                  var selectedSong:Song = Song(songList.selectedItem);
                                                  newSongTitle.text = selectedSong.title;
                                                  newSongArtist.text = selectedSong.artist;
                                                  newSongYear.value = selectedSong.year;
                                                  newSongFilename.text = selectedSong.filename;
                                                  newSongGenres.selectedItems = selectedSong.genres;
                                                  // hide the submit button
                                                  submitSongData.visible = false;
                                                  showAddControlsBtn.visible = true;
                                                  break;
                                  }
                          }
                  ]]>
          </mx:Script>
          
          <mx:Label id="title" text="PlayList Example" fontSize="24" fontStyle="bold" />
          <mx:Label id="subtitle" text="From Programming ActionScript 3.0, Chapter 7: Working With arrays" fontSize="12" />
          
                  <mx:VBox width="500">
                          <mx:HBox width="100%">
                                  <mx:Label text="Sort by:"/>
                                  <mx:Button id="sortByTitle" label="Title" toggle="true" click="sortList(SortProperty.TITLE)" selected="true"/>
                                  <mx:Button id="sortByArtist" label="Artist" toggle="true" click="sortList(SortProperty.ARTIST)"/>
                                  <mx:Button id="sortByYear" label="Year" toggle="true" click="sortList(SortProperty.YEAR)"/>
                                  <mx:Spacer width="100%" />
                                  <mx:Button id="showAddControlsBtn" label="New Song" click="setFormState(ADD_SONG);" />
                          </mx:HBox>
                          <mx:List id="songList" width="100%" change="songSelectionChange()" labelFunction="songListLabel"></mx:List>
                  </mx:VBox>
                  
                  <mx:HBox width="500" borderStyle="solid" paddingBottom="10">
                          <mx:VBox>
                                  <mx:Text id="formTitle" text="Add New Song" fontSize="14" fontWeight="bold" paddingLeft="10" width="400" height="23"/>
                                  <mx:Form paddingBottom="0" paddingTop="0" paddingRight="0">
                                          <mx:FormItem label="Title:">
                                                  <mx:TextInput width="275" id="newSongTitle"/>
                                          </mx:FormItem>
                                          <mx:FormItem label="Artist:">
                                                  <mx:TextInput width="275" id="newSongArtist"/>
                                          </mx:FormItem>
                                          <mx:FormItem label="Year:">
                                                  <mx:NumericStepper maximum="2006" minimum="1400" value="2006" id="newSongYear"/>
                                          </mx:FormItem>
                                          <mx:FormItem label="File name:">
                                                  <mx:TextInput id="newSongFilename" width="275"/>
                                          </mx:FormItem>
                                          <mx:FormItem>
                                                  <mx:Button id="submitSongData" label="Add Song" click="addNewSong()" width="150"/>
                                          </mx:FormItem>                        
          
                                  </mx:Form>
  
                          </mx:VBox>
                          
                          <mx:VBox width="25%" paddingRight="10" paddingTop="4">
                                  <mx:Label text="Genre(s):"/>
                                  <mx:List id="newSongGenres" allowMultipleSelection="true" width="100%" height="130">
                                          <mx:dataProvider>
                                                  <mx:Array>
                                                          <mx:String>90's</mx:String>
                                                          <mx:String>Classical</mx:String>
                                                          <mx:String>Country</mx:String>
                                                          <mx:String>Hip-hop</mx:String>
                                                          <mx:String>Opera</mx:String>
                                                          <mx:String>Pop</mx:String>
                                                          <mx:String>Rock</mx:String>
                                                          <mx:String>Showtunes</mx:String>
                                                  </mx:Array>
                                          </mx:dataProvider>
                                  </mx:List>
                          </mx:VBox>
                  </mx:HBox>
  
  </mx:Application>


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.