Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Using Spring with XWT

Revision as of 10:41, 4 August 2010 by Unnamed Poltroon (Talk) (Declare Spring bean CLR)

Introduction

Work is underway to integrate XWT with Spring and Spring Dynamic Module to declare the CLR classes used into XWT file with XML Spring file.

See (for the moment) Bug 320405. Attached to this bug are zips containing bundles

  • org.eclipse.e4.xwt.springframework: bundle of XWT Spring support

CLR Class

Before explaining Spring support for XWT, it's interesting to understand how CLR Java class are managed with x:Class and x:ClassFactory.

CLR with x:Class

The Event Handling sample show you how XWT file can be linked to a Java CLR (Common Language Runtime) class to manage for instance event handler of the UI with Java code by using x:Class attribute :

<Shell xmlns="http://www.eclipse.org/xwt/presentation"
    xmlns:x="http://www.eclipse.org/xwt"
    x:Class="ui.EventHandler">
    <Shell.layout>
       <GridLayout/>
    </Shell.layout>
    <Button text="Click Me!" SelectionEvent="clickButton">
    </Button>
</Shell>

The declaration x:Class="ui.EventHandler" means that the UI is linked to the Java class ui.EventHandler :

package ui;
 
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Button;
 
public class EventHandler {
 
    protected void clickButton(Event event) {
        Button button = (Button )event.widget;
        button.setText("Hello, world!");
    }
}

When the button gets selected, the method clickButton is invoked to change the Button text to "Hello, world!".

CLR with x:ClassFactory

You can do the same thing (link a CLR class with XWT) with CLR factory :

<Shell xmlns="http://www.eclipse.org/xwt/presentation"
    xmlns:x="http://www.eclipse.org/xwt"
    x:ClassFactory="ui.EventHandlerFactory">
    <Shell.layout>
       <GridLayout/>
    </Shell.layout>
    <Button text="Click Me!" SelectionEvent="clickButton">
    </Button>
</Shell>

The declaration x:ClassFactory="ui.EventHandlerFactory" means that the UI is linked to the Java class which is provided by ui.EventHandlerFactory :

package ui;
 
import org.eclipse.e4.xwt.ICLRFactory;
 
public class EventHandlerFactory implements ICLRFactory {
    public Object createCLR(String args) {
        return new ui.EventHandler();
    }
}

This CLR factory implements org.eclipse.e4.xwt.ICLRFactory which returns an instance of ui.EventHandler which is the CLR class described below.

Spring support for XWT

The bundle org.eclipse.e4.xwt.springframework is the Spring support for XWT which implements org.eclipse.e4.xwt.ICLRFactory to declare CLR Class with bean Spring. It's possible to use this Spring support :

Using Spring with XWT

To declare CLR class with bean Spring (not OSGi context) you must

Declare Spring bean CLR

Declare your CLR class with bean into XML file Spring. To do that create a XML Spring file ui-context.xml into ui package like this :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  <bean name="myUI" class="ui.EventHandler" scope="prototype">
  </bean>
 
</beans>

scope attribute must be settted to prototype in order to create an instance of EventHandler each time XWT file is opened.

Implements Spring CLRFactory

Implements ICLRFactory wich use a XML Spring file to retrieve CLR bean. To do that you can extends and implkements org.eclipse.e4.xwt.springframework.AbstractSpringCLRFactory :

package ui;
 
import org.eclipse.e4.xwt.ICLRFactory;
import org.eclipse.e4.xwt.springframework.AbstractSpringCLRFactory;
import org.eclipse.e4.xwt.springframework.CLRFactoryParameters;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class MySpringCLRFactory extends AbstractSpringCLRFactory {
 
  public static final ICLRFactory INSTANCE = new MySpringCLRFactory();
 
  private ApplicationContext applicationContext = null;
 
  @Override
  protected ApplicationContext getApplicationContext(final String properties, CLRFactoryParameters propertiesMap) {
    if (applicationContext == null) {
      applicationContext = new ClassPathXmlApplicationContext("ui/ui-context.xml");
    }
    return applicationContext;
  }
}

To avoid loading XML Spring file ui-context.xml each time that XWT use our Spring CLR Factory you must :

  • cache the ApplicationContext (applicationContext fields manage that).
  • MySpringCLRFactory define a singleton (INSTANCE) which we will use into x:ClassFactory.

Set the x:ClassFactory

Using Spring Dynamic Module (OSGi) with XWT

Back to the top