25.4 Putting Together the Application

Once you have created all the components, you can breathe a sigh of relief knowing that you have completed all the difficult work. All that remains is to put the components together so they work in conjunction with one another to produce a complete, working application.

The main routine of the application is responsible for the following:

  • Adding all the necessary components to the movie at runtime

  • Loading the XML data

  • Listening for keypresses

To complete the main routine of the image viewer application, add the following code to the first frame of the default layer of the main timeline:

// Include the necessary libraries in the document. MovieClip.as is from Chapter 7,
// and DrawingMethods.as is from Chapter 4
#include "MovieClip.as"
#include "DrawingMethods.as"
// Table.as and Forms.as are from Chapter 11.
#include "Table.as"
#include "Forms.as"
// TextField.as is from Chapter 8.
#include "TextField.as"

function init (  ) {
  draw(  );

  // Create the XML object and load the images.xml document. Be sure to set
  // ignoreWhite to true so that the extra whitespace nodes are eliminated.
  imagesXML = new XML(  );
  imagesXML.ignoreWhite = true;
  imagesXML.onLoad = XMLOnLoad;

function draw (  ) {

  // Create the Menu component and the Preview Pane component.
  _root.attachMovie("MenuSymbol", "imagesMenu", _root.getNewDepth(  ));
  _root.attachMovie("PreviewPaneSymbol", "previewPn", _root.getNewDepth(  ));

  // Add the menu and preview pane to a table row.
  tr0 = new TableRow(3, new TableColumn(3, imagesMenu), 
                        new TableColumn(3, previewPn));

  // Create the Sequencer component and add it to a table row.
  _root.attachMovie("SequencerSymbol", "seqncr", _root.getNewDepth(  ));
  tr1 = new TableRow(3, new TableColumn(3, seqncr));

  // Create the table to position the elements on the Stage.
  appTable = new Table(3, 0, 0, tr0, tr1);

  // Create the sequence viewer and set the proper references in the Sequencer and
  // Menu components.
  _root.attachMovie("SequenceViewerSymbol", "seqViewer", _root.getNewDepth(  ));
  imagesMenu.setSequencer(seqncr, seqViewer);

// This is the onLoad method for the XML object.
function XMLOnLoad (  ) {

  // Call the setValues(  ) method of the Menu component and 
  // pass it a reference to the XML object.

  // Calculate the dimensions of the preview pane according to the new width of the
  // menu, then set the size of the preview pane and render the table.
  var menuW = _root.imagesMenu._width;
  var pvW = Stage.width - menuW - 12;
  var pvH = Stage.height - _root.seqncr._height - 12;
  _root.previewPn.setSize(pvW, pvH);

// Create a listener object to detect keypresses. If the delete key is pressed, call
// the removeSelected(  ) method of the sequencer to remove the selected sequencer
// item. Otherwise, call the stop(  ) method of the sequence viewer to stop the
// playback (if it is playing).
keyListener = new Object(  );
keyListener.onKeyDown = function (  ) {
  if (Key.getCode(  ) == Key.DELETEKEY) {
    _root.seqncr.removeSelected(  );
  } else {
    _root.seqViewer.stop(  );

init(  );

    Part I: Local Recipes
    Part II: Remote Recipes