Xoetrope

 Wiki : GroovySupport

HomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
XUI 4.0

Groovy Support


XUI 4.0 adds extended support for scripting, and with it the entire page can be implemented as a script. Previously only attributes could be evaluated as scripts, but the new support simplifies this and makes it more transparent.

To use the new support the appropriate builder must first be added to the project.

NumBuilderClasses=1
BuilderClass0=net.xoetrope.optional.scripts.groovy.GroovyBuilder


Once the builder has been loaded XUI will search fro .groovy files to implement the page class, just as it would search for a .class file if the page was implemented as Java. The page builder will also check if the script has been modified since the last load, and if it has the script will be reparsed. Reloading the script means that you can easily change the behavior of a running application. Thus the page can be declared as normal

<?xml version="1.0" encoding="UTF-8"?>
<XPage class="xui.groovy.Welcome.groovy">
	<Components>
	    <Button name="nextBtn" x="100" y="100" w="100" h="20" content="Next"/>
	</Components>
	<Events>
	  <Event target="nextBtn" method="next" type="ActionHandler"/>
	</Events>
</XPage>


note that the class attribute of the page now points to a Groovy file. The Groovy is then as follows:

package xui.groovy;

import net.xoetrope.xui.XPage;
import net.xoetrope.swing.XLabel;

public class Welcome
{
  XPage page;
  XLabel counterLbl;

  int count

  public Welcome()
  {
  }
  
  public void pageCreated()
  {
	println "pageCreated invoked"
	counterLbl = page.findComponent( "counterLbl" );
	println "counterLbl set"
  }
  
  public void pageActivated()
  {
	count++
	// Try changing this at runtime
	counterLbl.setText( "This pages has been activated and displayed " + count + " times" )
  }

  public void next()
  {
	println "next invoked once again..."
	page.showPage( "MiddlePage" );
  }
}


The page field is set when the page is created. If the .groovy file is modified it is reloaded before any of its methods are invoked. As part of the procecss the properties/fields belonging to the page are rebound to the new GroovyObject.

A sample Groovy application can be found at http://xui.svn.sourceforge.net/viewvc/xui/xui/branches/xui40/samples/features/Groovy/GroovyPageTest/

There are no comments on this page. [Add comment]

Page was generated in 0.1406 seconds