• Aucun résultat trouvé

Load the XML file

Dans le document CHRIS ALLEN (Page 111-115)

ingredientList = new Array();

}

public function addIngredient( ingredient:String ) : void {

ingredientList.push( ingredient );

}

Now save the file and make sure there are no syntax errors in yourProblemspanel at the bottom of Eclipse that may have been caused by a typo or other error.

Load the XML file

Now that you have a class to hold the details of a recipe, you need to add functionality to load an XML file. While creating this class, let’s explore the ASDT templates feature. First, create a new class named RecipeXMLLoader. Let’s make this class a Singleton so only a single object of this type ever exists.

Instead of writing the code to do that, you will use the template functionality of ASDT. First, delete all the code in the file. Then type sing. Press Ctrl+spacebar to invoke the template functionality. You should now have code similar to the following:

class com.friendsofed.recipeviewer.RecipeXMLLoader {

private static var instance : RecipeXMLLoader;

/**

* @return singleton instance of RecipeXMLLoader

*/

public static function getInstance() : RecipeXMLLoader {

if (instance == null)

instance = new RecipeXMLLoader();

return instance;

} }

private function RecipeXMLLoader() {

} }

As you can see, code templates can make writing large amounts of boilerplate code very easy. To understand what happened when you invoked the code template, you should open theWindow Preferencesmenu. From the left panel, selectActionScript2EditorTemplates; you will see a dialog box similar to Figure 4-13.

Figure 4-13. The ASDT Templates dialog box

When you press Ctrl+spacebar in the code editor after a word or word fragment, theNamecolumn of the templates is searched for a match. In this case, there was a single match called singleton. ASDT then copied the template code into your source file and replaced template variables (the${text}

pieces) with the appropriate pieces of data. Besides the templates that come with ASDT, you can also create your own. Let’s create one for writing a public method signature.

Creating a custom template makes it available to you when pressing Ctrl+spacebar in the ActionScript editor of ASDT. You can add that custom template to ASDT with the following steps:

1. Click theNewbutton in theTemplatesdialog box.

2. You want to create a template for a public method, so let’s give it a name ofpubmeth.

3. In thedescriptionfield, type A public method signature.

4. Type the following code into thePatternfield, and then click theOKbutton:

public function ${methName} () : ${methType}

{

${cursor}

}

While you type in the pattern, you will notice a pop-up menu appears whenever you press the dollar symbol ($). In this pop-up is a list of premade variables from which you can choose. As you can see, you inserted the${cursor}variable. This will move the cursor position to that spot when the template is done executing.

You can also create your own custom variables like you did withmethNameandmethType. Custom vari-ables act as placeholders for text when you use the template. When you insert a template, Eclipse will allow you to enter text for each variable that will be replaced in the template. Every occurrence of the same custom variable will be replaced with the same text. Although the simple example contained each variable only once, you could have used them multiple times like so:

// ${methName} was an automatically generated method public function ${methName} () : ${methType}

{

${cursor}

}

This would cause a comment to appear before the method that contained the name of the method.

Since you used${methName}in multiple locations, you’d have to type it in only once when using the template.

To use this template, exit the template dialog box and return to theRecipeXMLLoader.asfile. Position your cursor in the class, but outside of any other method. Type pubmeth, and press Ctrl+spacebar. The code from your template will appear. Your first custom variable (methName) will be highlighted. You can type a method name that will overwrite the variable; let’s useloadXML. Then press the Tab key. The cursor will advance to themethTypevariable, and you can type the method type. For this example, let’s useVoid. After entering the type, you can press the Tab key one more time, and the cursor will be positioned inside the method body, ready for you to type in the body of the method.

Creating a custom template

Now that you know how to use templates, finish theRecipeXMLLoaderclass by filling in the following code:

class com.friendsofed.recipeviewer.RecipeXMLLoader {

public static var XML_LOADED_EVENT:String = "xmlLoadedEvent";

private static var instance : RecipeXMLLoader;

public function dispatchEvent() {}

public function addEventListener() {}

private var xml:XML;

public static function getInstance() : RecipeXMLLoader {

if (instance == null)

instance = new RecipeXMLLoader();

return instance;

}

public function loadXML () : Void {

If you are familiar with ActionScript, most of the preceding code should be familiar. The event dis-patching functionality that uses theGDispatcherclass may be new to you, so some explanation is in order.

These lines define two empty methods that will be filled in byGDispatcherin the constructor:

public function dispatchEvent() {}

public function addEventListener() {}

When theGDispatcher.initialize(this)call is made, those methods are dynamically replaced by code in theGDispatcherclass. The modifieddispatchEventmethod is then used by this class in the onXMLLoadedmethod:

dispatchEvent( {type:XML_LOADED_EVENT , target:this} );

This call causes an event of typeXML_LOADED_EVENTto be dispatched to any other objects listening for it. Later in this chapter you will use theaddEventListenermethod to add an event listener from the RecipeViewerclass.

Dans le document CHRIS ALLEN (Page 111-115)