Embedding Flash into ZK applications

Bernd Will, Project Leader for OpenPavilion
June 5, 2008

Version

Applicable to ZK 3.0.5 and later

installation

ZK is an excellent server-centric framework for createing AJAX applications. To incorporate film snippets or animated movies into ZK application, Adobe Flash can be used to enhance ZK’s power. This article will guide you through how to integrate ZK with Flash. First of all, you need to install ZK and the zk flash components. And then, unzip the attached “instruments.zip” file into the {TOMCAT}\ZKFLASH\instruments folder. You can see the folder structure as following:


The example of Flash instrument in pure HTML

Now, we simply open the HTML page http://localhost:8080/zkflash/instruments/Glas.html to understand how HTML and Flash collaborate:

Look into the HTML file source; the section “” is responsible for loading the flash movie.

	
	...
	<body bgcolor="#ffffff">
	<!--url's used in the movie-->
	<!--text used in the movie-->
	<!-- saved from url=(0013)about:internet -->
		<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com
		/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100" height="135" id="Glas" 
		align="middle">
			<param name="allowScriptAccess" value="sameDomain" />
			<param name="movie" value="Glas.swf?currentValue=33&min=0&lo=10&li=20&hi=50&ho=90&
			max=100" />
			<param name="quality" value="high" />
			<param name="bgcolor" value="#ffffff" />
			<embed src="Glas.swf?currentValue=33&min=0&lo=10&li=20&hi=80&ho=90&max=100" quality="
			high" bgcolor="#ffffff" width="100" height="135" name="Glas" align="middle"
			allowScriptAccess="sameDomain" type="application/x-shockwave-flash" 
			pluginspage="http://www.macromedia.com/go/getflashplayer" />
		</object>
	</body>
	...

	

When opening the HTML file, the flash file “Glas.swf” is loaded and its parameters are passed to the flash movie (currentValue, min, lo, li, hi, ho, max). This is how ZK communicates with Flash – by passing parameters using the URL syntax.

The Flash instrument in ZK source

Now we open the same application, but written in ZK http://localhost:8080/zkflash/instruments/ZKGlas.zul : The most obvious difference is the complicity of the source code. By ZK, you achieve the same functionality as Html tags with much less lines of code. Since ZK generates and encodes HTML tags automcatically, use the encoded “&amp;” instead of “&”.

	
	<?xml version="1.0" encoding="utf-8"?> 
		<hbox>
		<flash id="myInstrument" src="Glas.swf?currentValue=33&amp;min=0&
		amp;lo=10&amp;li=20&amp;hi=80&amp;ho=90&amp;max=100" 
		width="128" height="128" /> 
	</hbox>
	

Interactive Flash in ZK pages

Now we come to the final question: How can we change the values of the flash movie programmatically? The solution is simple: Create some event handlers in ZK and change the URL of the Flash movie component ! Open the page http://localhost:8080/zkflash/instruments/ZKGlasInteractive.zul and move the slider to change the charge of the glass:

	
<?xml version="1.0" encoding="utf-8"?>
<window id="window" title="ZK Flash Instrument" style="width:100%"
	border="normal">
	<zscript>
		int instrumentValue=0; int instrumentMax = 100; String
		showGlasBorder="false";

		public void changeValue(aValue) { instrumentValue = aValue;
		slider.setCurpos( instrumentValue ); drawGlas(); } public void
		setInstrumentBorder() { showGlasBorder=
		(instrumentCheckbox.isChecked()).toString(); drawGlas(); }
		public drawGlas() { String url =
		"Glas.swf?currentValue="+instrumentValue+"&amp;border="+showGlasBorder+"&amp;
		min=0&amp;lo=10&amp;li=20&amp;hi=80&amp;ho=90&amp;max=100";
		myInstrument.src = url; }

		void zoom(Slider slider) { changeValue( event.getPos() ); }
	</zscript>

	<hbox>
		<flash id="myInstrument"
			src="Glas.swf?currentValue=0&amp;min=0&amp;lo=10&amp;li=20&amp;hi=80&amp;ho=90&
			amp;max=100"
			width="128" height="128" />
	</hbox>
	<vbox>
		<label value="Fill glas:" />
		<slider id="slider" onScroll="zoom(slider)" />
		<checkbox id="instrumentCheckbox" label="Show Borders on Glas"
			onCheck="setInstrumentBorder()" />
	</vbox>
</window>

	

Investigating the source, there are some important aspects to note:

  1. All event handlers have been written in the ZSCRIPT section
  2. The function “drawGlas” creates the new URL by joining the URL string and setting the SRC attribute of the flash movie
  3. The SRC-attribute in the ZK flash component section is relevant to create the initial view with reasonable data (such as startValue, maxValue, minValue etc.)

The Flash Movie component

Now finally we are drilling down the Flash movie to understand how the variables are being passed and consumed. First of all, I have used Flash Macromedia 8 (Actionscript2, Flash Player 8 export) to create that example. It also would work with AS3 and Flash Player 9 or 10. Inside the Flash movie, you need to create an Actionscript routine on stage level to consume the variables being passed over by the HTML page.

  1. Move to the root movieclip level in your Flash Movie
  2. Make sure that you enter the Actionscript code on root level
  3. The variable names in your Flash Movie must be syntactically identical to the variable names being passed in the URL (e.g. min = min, max=max, currentValue = currentValue etc.) Don’t forget to cast the variables in your Actionscript source to INTEGER or BOOLEAN, because all variables being passed using the URL syntax are string variables !

More examples

To demonstrate some more options of flash integration, you can check http://localhost:8080/zkflash/instruments/ZKFlash.zul.

The “Widget” components presented here are flash movie components written in Actionscript classes. Its details aren’t important here. In this tutorial, we concentrate on the transfer of data from ZK to the Flash Movie itself. ( If you want to research the components in detail feel free to check the OpenSource project http://www.openpavilion.org )

Download

  1. ZK Flash 0.81
  2. instruments.zip

About Author

Bernd Will is the project leader of OpenPavilion project. OpenPaviliion is is a free surveillance suite made for observing, monitoring and benchmarking your applications and networks. He lives in Germany, Bavaria. He is a senior project manager at "Siemens Industry" sector. Also, he is a father of two daughters. Triathlon races are what he likes to do at the weekends

Following is the short interview with him:

How did u start openPavilion project?

The idea about OpenPavilion was born, since no user friendly business process and application dashboard was available on the market; the idea of OpenPavilion is to create a framework for modeling easy dashboards for technical and business applications

Where did you first hear ZK?

I first heard of ZK by surfing through Wikipedia; ZK was presented as an "alternative" to Struts, JSF, OpenLaszlo, Flex and all the other AJAX and RIA tools out there, so I was curious

Comments
 
Mike
2008-06-05

Impressive! What is the license of instruments? Is it open-source?

Jeff liu
2008-06-05

Hey Mike

Check http://www.openpavilion.org/
pretty interesting

Bernd
2008-06-05

Hi,

it is OpenSource .... but not finished yet.
I stumbled upon ZK a few days ago and it might be possible that we switch the editor and viewer part towards ZK. Today, everything is programmed in pure Flash, but that is very "expensive". ZK is much easier, so I tried to embed my instruments into ZK, which workes like a charm.

Simply click onto the "connect" button at:
http://www.openpavilion.org/wiki/Documents/index.html

Then choose some of the dashboards on the left side - they are autogenerated.
And don't forget to switch the "live"button to see the flash written editor, which I would like to change with ZK.

The instruments ( widgets, emoticons, gagdets etc.) will stay OpenSource anyway.

Best wishes

Serge
2008-06-05

Danke schon Bernard

Great work

jeff liu
2008-06-05

Dear all

If you are interested in contributing articles, contact info@zkoss.org

Simon
2008-06-07

It is not apparent from the article if changing the URL of the zscript on the server cause the flash movie to be reloaded passinng in the new URL of if changing the URL is actually passing parameters into the running flash app to cause it to update its state. Could you please elaborate?

bernd
2008-06-08

Hello Simon,

as soon as the JavaScript function "drawGlas()" is being called ...
1.) the new URL-String is being concatenated:
[...]
String url = "Glas.swf?currentValue="+instrumentValue+"amp;border="+showGlasBorder+"&min=0&lo=10&li=20&hi=80&ho=90&max=100";
[...]

2.) the URL of the movie is changed by:
[...]
myInstrument.src = url;
[...]

Changing the URL of the ZK flash component immediately reloads the movie.
The parameters of the URL are then being passed to the flash movie.
That's a standard ZKFlash feature.

Greetings
Bernd

bernd
2008-06-08

The ZKflash component today does not offer the option to update the Flash movie or parameters.
Instead, the complete Movie is being reloaded and restarted.

Greetings
Bernd

 
 
Leave a Reply
 
Name (required)
Mail (will not be published) (required)
Website
(Case Insensitive)
Bold textItalic textUnderLine textSource CodeHorizontal rulerExternal Link