Set colorpicker default value in xmlui/xul

Many peo­ple who work with Direc­tor, use also Flash. Some of those who use Flash, write exten­sions for it. And def­i­nite­ly a lot of peo­ple who write exten­sions for Flash meet with speci­fic prob­lems, one of which is the mat­ter of this arti­cle. What is the prob­lem in short? If we try to cre­ate a cus­tom dialog box, using the JSFL func­tion,

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

and we want that our dia­logue has a col­or­pick­er with a defined by us default colour val­ue, we see that this turns out to be impos­si­ble.
Accord­ing to the Macromedia/Adobe doc­u­men­ta­tion the fol­low­ing def­i­n­i­tion:

<?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 fol­low­ing result, instead:

 
 
It is obvi­ous that the attrib­ute “val­ue” has no effect on the <col­orchip> ele­ment. How to solve this prob­lem? How to set the col­or­pick­er default val­ue?
The solu­tion came to me by acci­dent, after a chain of actions caused by des­per­a­tion. Ini­tial­ly I decid­ed to set the col­orchip val­ue explic­it­ly with the help of a defined in the dialog javascript. I added the <script> sec­tion (where I insert­ed the javascript code into), as well as the oncre­ate attrib­ute in the <col­orchip> ele­ment.

<?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 dis­cour­ag­ing once again.

 
 
After cer­tain med­i­ta­tion, I decid­ed that it might be nec­es­sary the javascript code to be exe­cut­ed with some delay, after the full ini­tial­iza­tion of the <col­orchip> ele­ment. That is why I moved the oncre­ate attrib­ute to the next UI ele­ment 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 expect­ed:

 
 
By good for­tune, in my speci­fic case this next UI ele­ment turned out to be a but­ton. Lat­er I found out that the oncre­ate event does not hap­pen with the ini­tial­iza­tion of all of the UI ele­ments. Up to now I have ascer­tained that it hap­pens only when <but­ton> and <menulist> ele­ments are ini­tial­ized.

 
Comments

Very inter­est­ing, this will be use­ful for some of my future JSFL scripts!

Do you know by any­chance if, with­in a tag… can you get data from oth­er JSFL scripts? Like, basi­cal­ly pop­u­late the defaults with what­ev­er was last used by the user (or load­ed from a file with FLfile even)?

Thanks for the arti­cle!

Hi, Pier­re,

Such func­tion­al­i­ty can be achieved in the fol­low­ing way:
— Cre­ate a glob­al vari­able (object) with the lat­est user input data.
— Cre­ate the dia­logue with dynam­ic XML, and insert the data you care about in the right places.
— After each change of the data, (for exam­ple, press­ing OK in the dia­logue), “seri­al­ize” the glob­al object and save it to the disk as JSON.
— On every new Flash ses­sion, on the first request for the data, “dese­ri­al­ize” the JSON and cre­ate a glob­al object again.

You can see an exam­ple in the EDAP-Tools project
http://flash-powertools.com/downloads/12
In par­tic­u­lar in EDAPT Settings.jsfl

Hi vlad,

That was prob­a­bly one of the most help­ful advice I’ve received in regards to pop­u­lat­ing XMLUI pan­els! I’ve just cre­at­ed my own util­i­ty class­es to read the orig­i­nal XML, pop­u­late it with the JSON (stored from the last time the XMLUI was dis­missed by the user), and all the XML attrib­ut­es are changed exact­ly how I expect­ed!

I had a bit of trou­ble using E4X syn­tax to find “any” ele­ment by @id attrib­ut­es. I basi­cal­ly had to check “does check­box exists by this id? or textbox­es?” which is kin­da lim­it­ed. Do you know of a workaround to include any descen­dants in E4X with­in JSFL?

Also, I’d like to give you a shout-out on Twit­ter if you don’t mind. I was curi­ous if you’re on it by any­chance?

PS: I was lucky to find your respon­se! I didn’t receive any noti­fi­ca­tion so I had to man­u­al­ly check peri­od­i­cal­ly. If you don’t mind send­ing me an email respon­se, I’d appre­ci­ate it!

Thanks!