Here’s a little snippet for something bigger I’m working on. The goal of the snippet is to present the user with a choice of available class paths, but the point of this post it to present the underlying technique of using JSFL to gather data dynamically, then create the XUL file, and thus the interface, on the fly.

Here’s the code, which is fairly self-contained and can be run as is.

function getClassPaths() {
	var doc = fl.getDocumentDOM();
	var classPathsList = doc.sourcePath + ";" + fl.sourcePath;
	var classPaths = classPathsList.split(";");
	return classPaths;
}

var classPathXul = "";
var classPaths = getClassPaths();
var iLen = classPaths.length;
for (var i = 0; i < iLen; i++) {
	var cp = classPaths[i];
	classPathXul += '		<radio id="'+(i.toString())+'" label="'+cp+'" />\n'
}

var xul = '<dialog title="Select a class path in which to create classes" buttons="accept,cancel">\n'
+ '	<radiogroup id="selection">\n'
+ 		classPathXul
+ '	</radiogroup>\n'
+ '</dialog>';
//fl.trace(xul);

var xulFilePath = fl.configURI + "classpath.xul";
//fl.trace(xulFilePath);

FLfile.write(xulFilePath, xul);

var classPath = fl.getDocumentDOM().xmlPanel(xulFilePath);

if (classPath.dismiss == "accept") {
	fl.trace("classPath.selection: " + classPath.selection);
} else {
	// Bye.
}

FLfile.remove(xulFilePath);

Here’s the gist. First, we gather available class paths, both from the application’s preferences and from the document’s settings. Then we loop over those and build up an XML snippet into a String.

Next we use that String in a larger XML structure, the end result being XUL with a radio group, containing radio buttons for each of the class paths.

The last stage involves writing this string to a temporary file, then using that file in the xmlPanel() method. After the XUL panel is dismissed, we can remove the file.

What I like about this is that it avoids the rather complicated and (in my opinion) messy Flash-to-XUL communication that’s possible, but requires numerous files and tricky management of passing values back and forth…something I’ve never been able to feel very confident about.

Even if you don’t need dynamically created controls, this is handy for the fact that you can just contain everything in one script. No separate XUL file that needs to get installed in a predictable location. Of course, the drawback is that you’re building XML from within a scripting language, so you’ve got to escape quotes and all of that. But consider it another tool (or technique, more accurately) in the tool box.

Advertisement