When you build an Adobe AIR application that includes dynamically created windows, it quickly becomes clear that good management must be integral to the design. As I mentioned in the previous section, users have grown accustomed to the way instant messaging applications work and they will expect the same experience when using your application. As you examine the code used to create this functionality, follow along using the sample files available for download. Your first task will be to generate the new chat window dynamically.
In this example, the IMWinManager class is used whenever a window is created, deleted, referenced, or accessed in any way. References to each new window, which are all instances of Window, are kept in a WinManager object.
The code below generates a new chat window:
public function getWin(queue:String,owner:MainWindow):WinIM{
//does the current window exist?
if (this.aWins[queue]==null){
//doesn't exist so create window
var win:WinIM = new WinIM();
win.init(queue,owner);
aWins[queue]=win;
win.open();
win.addEventListener(Event.CLOSING,closeWinCall);//
return win;
}
return this.aWins[queue];
}
In the
code example above, getWin is a simple method that looks for
a window reference, as described here:
this.aWins[queue]
If the
window reference cannot be found (==null), then Adobe AIR creates a new window—accomplished with
this line of code:
var win:WinIM = new WinIM()
When a user clicks the Close button, the chat window is closed. As the Close button receives the click event, the window manager (ImWinManager) needs to be notified so that the reference can be removed.
In the
previous code example, every time a new window is created in the getWin method, a listener event is also
added. The listener event is set to listen to Event.CLOSING. When this event is fired by clicking the Close
button, the listener is alerted and that causes the chat window's reference to
be cleared out of the WinManager object using this.aWins[title]=null, and by applying the removeWin method.
When the user quits the chat application and closes their buddy window, all other chat windows should close simultaneously. You need to capture the event that indicates that the main window (the buddy list) has been closed and then ensure that any other chat windows close along with it.
When a
user first logs on to the system, an event is created exactly like the one
found above using Event.CLOSING. When the user logs off and the CLOSING event gets fired, it triggers a clearWins method within the ImWinManager class.
The clearWins method contains a for loop that basically iterates
through each item within the aWins object, closing each chat window that is referenced:
for(var i:String in this.aWins) removeWin(i,this.aWins[i]._buddy,false)
Now that you've dynamically created your chat windows, set up management for their references, and added functionality that closes all windows when a user logs off, you're ready for the next step. The next section of this article describes how to set up WebORB to handle user connections and messages.