Apollo Alpha 1 Documentation - collected by Jimbob | Back to MY RESOURCES


Building a directory search application

Back to: Apollo Documentation home page

The File Search sample application shows a number of features of working with files in Apollo, including the following:

Note: This is a sample application provided, as is, for instructional purposes.

Contents
  1. Installing and testing the application
  2. Files used to build the application
  3. Understanding the code
    1. Setting the root directory to search
    2. Searching the directory for matching files
    3. Displaying search results

Installing and testing the application

The File Search application lets you search a directory — and its subdirectories — for files that have names matching the search pattern you specify:

Files used to build the application

The application includes the following source files:

FileDescription
FileSearch.mxmlThe main application file in MXML for Flex. Details of the code are discussed in the Understanding the code section.
FileSearch-app.xmlThe Apollo application descriptor file.
icons/ApolloApp_16.png icons/ApolloApp_32.png icons/ApolloApp_48.png icons/ApolloApp_128.pngSample Apollo icon files.

To download the source files for this example, click here (358K).

For help on building this quick start sample application, see How to build the quick start sample applications.

Understanding the code

The following sections discuss how the Apollo-related code works in the file.

This article does not describe all of the Flex components used in the MXML code for the file. For information on these, see the Flex documentation.

Setting the root directory to search

The init() method sets the text value of the folderPath TextInput component to a pre-defined path (the user's documents directory):

 folderPath.text=File.documentsDirectory.nativePath;

The File.documentsDirectory is a File object pointing the the user's documents directory (such as "My Documents" on Windows), and the nativePath property is a string value of the platform-specific path to the directory. For example, on Windows, this path could be:

 C:\Documents and Settings\userName\My Documents

Whereas on Mac, it could be:

 /Users/userName/Documents

The user can edit this value (in the TextArea component), and when the user clicks the Search button, the search() method checks to see if the path is valid, and displays an error message if it is not:

 var dir:File = new File(folderPath.text);
 if (!dir.isDirectory)
 {
     Alert.show("Invalid directory path.", "Error");
 }

Note the difference between the nativePath property and the url property of a File, listed here for the same directory File object:

Searching the directory for matching files

The main search process lists directory contents asynchronously, one at a time. By doing this, other ActionScript-based processes (such as the progress bar animation and result listing) can take place as Apollo gets directory listing information (asynchronously) from the file system.

The search() method sets up event listeners for the dirListing event, which is dispatched when the listDirectoryAsync() process has completed:

 dir.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListed);
 dir.listDirectoryAsync();

The dirListed() method processes the list of files in the current directory. This list is represented as the files property of the dirListing event. The method sets a currentNodes variable to this array:

 currentNodes = event.files;

The dirListed() method iterates through each member of this array, to see if it is a directory. If it is a directory, the object is added to a list of current subdirectories (of the current directory being examined).

 node = currentNodes[i];
 if (node.isDirectory) 
 {
      currentSubdirectories.push(currentNodes[i]);
 }

After the iteration is completed, the members of this array are added to a master array of directories to be searched, named directoryStack:

 for (i = currentSubdirectories.length - 1; i > -1; i--) 
 {
     directoryStack.push(currentSubdirectories[i]);
 } 

At this point, now that processing of the current directory is complete, the application can call another asynchronous listing of the next directory in the stack (if there is one):

 var dir:File = directoryStack.pop();
 if (dir == null) {
     progress.visible = false; // There are no further directories to search. The search is completed.
 } else {
     dir.addEventListener(FileListEvent.DIRECTORY_LISTING, dirListed);
     dir.listDirectoryAsync();
 }

Displaying search results

As the dirListed() method iterates through each member of this array of contents of the current directory, it checks if the its name matches the Search pattern, and if so, it creates an object that is added to the ArrayCollection that is a data provider for the resultsGrid DataGrid component:

 if (node.name.search(pattern) > -1) 
 {
     if (node.isDirectory) 
     {
         fileExtension= "";
     }
     else
     {
         fileExtension = node.name.substr(node.name.lastIndexOf(".") + 1);
     }
     var newData:Object = {name:node.name,
                           path:node.nativePath,
                           type:fileExtension}
     resultData.addItem(newData);
 } 

Note that the file extension, represented by the fileExtension variable, is extracted from the filename by using the substr() and lastIndexOf() methods of the name property of the File object. (The file extension is the string portion of the filename that follows the last occurence of the "." character in the filename.)


Back to: Apollo Documentation home page


collected by Jimbob 2007.05