Using JavaAction

1.JavaAction

    The X-Meta engine is written in Java, and the execution model ultimately executes Java programs. Java programs can be invoked through Java Action. Java Ation is the bridge between the model and Java.

    In addition, JavaAction is recommended for action models with performance requirements.

2.Where to write Java code?

    It is recommended to write code in your own Java project. In addition, Java code can also be written directly in the Java Action model.

3.Java Code Specification

3.1.pubic static Object xxx(ActionContext actionContext)

    Only a Java static method with a fixed ActionContext parameter can be invoked. Calling this method does not instantiate an instance of the class to which the method belongs.

3.2.Use annotations

    Objects are instantiated when non-static Java methods are executed, and annotations can be used at this time. The following example is given.

package xworker.ide;

import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.annotation.ActionClass;
import org.xmeta.annotation.ActionField;
import org.xmeta.annotation.ActionParams;

@ActionClass(creator="newInstance")
public class TestJavaActionAnnotation {
	@ActionField(name="shell")
	Shell shellAbc;
	
	@ActionField
	Browser browser;
		
	@ActionParams(names="urlText")
	public void urlTextSelection(Text urlText, ActionContext actionContext) {
		browser.setUrl(urlText.getText());
	}
	
	public void okSelection(ActionContext actionContext) {
		System.out.println("okButton session ");
	}
	
	public void cancelSelection() {
		System.out.println("cancelButton session ");
		shellAbc.dispose();
	}
	
	public static Object newInstance(ActionContext actionContext) {
		return new TestJavaActionAnnotation();
	}
	
	public static void main(String[] args) {
		try {
			
			World world = World.getInstance();			
			world.init("./xworker/");
			
			Thing thing = World.getInstance().getThing("_local.test.core.actions.javaaction.TestAnnotation");
			thing.doAction("run");
			
		}catch(Exception e) {
			e.printStackTrace();
		}
	}	
}
  • @ActionClass
    The creator attribute is used to specify the static method of instantiating the object, and the static method parameter is fixed to ActionContext. If there is no ActionClass annotation, class. newInstance () is called to instantiate the object.
     
  • @ActionField
    For attributes, when an object is instantiated, variables are obtained from the context of the variable to assign values to the attribute. Where the name attribute is the name of the variable in the context of the variable, if it is the same name as the field, you can ignore the need for no settings.
       
  • @ActionParams
    The name attribute is the variable name of the parameter in the variable context. If there are many commas separated in English, pay attention to the order of the parameters. If the last parameter is ActionContext, you can leave it unchanged.      
           

    The test code of the model.

<?xml version="1.0" encoding="utf-8"?>

<Shell name="shell" descriptors="xworker.swt.widgets.Shell" text="测试Annotation" width="800"
     height="600" label="TestAnnotation">
    <GridLayout name="shellGridLayout"></GridLayout>
    <Text name="urlText" BORDER="true">
        <GridData name="GridData" style="FILL_HORIZONTAL"></GridData>
        <Listeners>
            <Listener type="DefaultSelection">
                <JavaAction name="okSelection" outerClassName="xworker.ide.TestJavaActionAnnotation"
                     methodName="urlTextSelection"></JavaAction>
            </Listener>
        </Listeners>
    </Text>
    <Browser name="browser" _xmeta_id_="browsre">
        <GridData name="browserGridData" style="FILL_BOTH"></GridData>
    </Browser>
    <Composite name="buttonComposite" _xmeta_id_="1264">
        <GridData name="buttonCompositeGridData" _xmeta_id_="1265" style="FILL_HORIZONTAL"
             horizontalAlignment="END"></GridData>
        <RowLayout name="buttonCompositeRowLayout" _xmeta_id_="1266"></RowLayout>
        <Button name="okButton" _xmeta_id_="1267" text="Ok">
            <RowData name="RowData" _xmeta_id_="1268" width="80"></RowData>
            <Listeners _xmeta_id_="1269">
                <Listener name="okButtonSelection" _xmeta_id_="1270">
                    <JavaAction name="okSelection" outerClassName="xworker.ide.TestJavaActionAnnotation"
                         methodName="okSelection"></JavaAction>
                </Listener>
            </Listeners>
        </Button>
        <Button name="cancelButton" _xmeta_id_="1271" text="Cancel">
            <RowData name="RowData" _xmeta_id_="1272" width="80"></RowData>
            <Listeners _xmeta_id_="1273">
                <Listener name="cancelButtonSelection" _xmeta_id_="1274">
                    <JavaAction name="okSelection" outerClassName="xworker.ide.TestJavaActionAnnotation"
                         methodName="cancelSelection"></JavaAction>
                </Listener>
            </Listeners>
        </Button>
    </Composite>
</Shell>

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号