Set colorpicker default value in xmlui/xul

Many people who work with Director, use also Flash. Some of those who use Flash, write extensions for it. And definitely a lot of people who write extensions for Flash meet with specific problems, one of which is the matter of this article. What is the problem in short? If we try to create a custom dialog box, using the JSFL function,

fl.getDocumentDOM().xmlPanel( "the path to your dialogue definition file" );

and we want that our dialogue has a colorpicker with a defined by us default colour value, we see that this turns out to be impossible.
According to the Macromedia/Adobe documentation the following definition:

<?xml version="1.0"?>
<dialog id="demo" title="ColorPicker Test" buttons="accept, cancel">
	<colorchip id="color1" value="#FF6600"/>
	<button id="dummy" label="Dummy"/>
</dialog>

must return this result:

 
 
We receive the following result, instead:

 
 
It is obvious that the attribute “value” has no effect on the <colorchip> element. How to solve this problem? How to set the colorpicker default value?
The solution came to me by accident, after a chain of actions caused by desperation. Initially I decided to set the colorchip value explicitly with the help of a defined in the dialog javascript. I added the <script> section (where I inserted the javascript code into), as well as the oncreate attribute in the <colorchip> element.

<?xml version="1.0"?>
<dialog id="demo" title="ColorPicker Test" buttons="accept, cancel">
	<colorchip id="color1" value="#FF6600" oncreate ="init()"/>
	<button id="dummy" label="Dummy"/>
	<script>	
	function init(){	
		fl.xmlui.set( "color1", "#FF6600" );	
	}
	</script>	
</dialog>

The result was discouraging once again.

 
 
After certain meditation, I decided that it might be necessary the javascript code to be executed with some delay, after the full initialization of the <colorchip> element. That is why I moved the oncreate attribute to the next UI element in the dialog:

<?xml version="1.0"?>
<dialog id="demo" title="ColorPicker Test" buttons="accept, cancel">
	<colorchip id="color1" value="#FF6600"/>
	<button id="dummy" label="Dummy" oncreate ="init()"/>
	<script>	
	function init(){	
		fl.xmlui.set( "color1", "#FF6600" );	
	}
	</script>	
</dialog>

Now the result is as expected:

 
 
By good fortune, in my specific case this next UI element turned out to be a button. Later I found out that the oncreate event does not happen with the initialization of all of the UI elements. Up to now I have ascertained that it happens only when <button> and <menulist> elements are initialized.

 
Comments

Very interesting, this will be useful for some of my future JSFL scripts!

Do you know by anychance if, within a tag… can you get data from other JSFL scripts? Like, basically populate the defaults with whatever was last used by the user (or loaded from a file with FLfile even)?

Thanks for the article!

Hi, Pierre,

Such functionality can be achieved in the following way:
– Create a global variable (object) with the latest user input data.
– Create the dialogue with dynamic XML, and insert the data you care about in the right places.
– After each change of the data, (for example, pressing OK in the dialogue), “serialize” the global object and save it to the disk as JSON.
– On every new Flash session, on the first request for the data, “deserialize” the JSON and create a global object again.

You can see an example in the EDAP-Tools project
http://flash-powertools.com/downloads/12
In particular in EDAPT Settings.jsfl

Hi vlad,

That was probably one of the most helpful advice I’ve received in regards to populating XMLUI panels! I’ve just created my own utility classes to read the original XML, populate it with the JSON (stored from the last time the XMLUI was dismissed by the user), and all the XML attributes are changed exactly how I expected!

I had a bit of trouble using E4X syntax to find “any” element by @id attributes. I basically had to check “does checkbox exists by this id? or textboxes?” which is kinda limited. Do you know of a workaround to include any descendants in E4X within JSFL?

Also, I’d like to give you a shout-out on Twitter if you don’t mind. I was curious if you’re on it by anychance?

PS: I was lucky to find your response! I didn’t receive any notification so I had to manually check periodically. If you don’t mind sending me an email response, I’d appreciate it!

Thanks!

Leave a Reply