public interface MyScript { public int compute(); }We will need to create a custom ScriptFactory, which will use one ScriptLanguageFactory to create script for this interface.
ScriptLanguageFactory
because we want to implement the same interface for all scripts, so we don't need to take care of any of the arguments of the method. We will have this very simple code: public class CustomScriptFactory extends AbstractScriptFactory { private final CustomScriptLanguageFactory langFactory = new CustomScriptLanguageFactory(); public CustomScriptFactory() { super(); langFactories.put(CustomScript.class, langFactory); } public ScriptLanguageFactory<?> createScriptLanguageFactoryImpl(short scriptType, int countScripts, ScriptSource source) { return langFactory; } }As you see, we keep the unique
ScriptLanguageFactory
as a field of our ScriptFactory
nand we will return it for all scripts.
MyScript
interface: public class DefaultScriptLanguageFactory extends AbstractScriptLanguageFactory<MyScript> { protected Class<ScriptBuilder> createBuilderClass(short scriptType) { create a builder which will create scripts of the specified script type } }
public class MyGroovyScriptBuilder extends AbstractScriptBuilder { public ScriptWrapper<MyScript> createScriptWrapper() { // create the GroovyScriptWrapper, see the documentation for the scriptHelper scripting library // (https://sourceforge.net/projects/scripthelper/) ScriptWrapper<MyScript> wrapper = new GroovyScriptWrapper<MyScript> () { }; // add the default imports for the javaXUL library addDefaultImports(wrapper); // set the exception listener and the script context wrapper.addExceptionListener(exceptionListener); wrapper.setScriptContext(context); return wrapper; } }We can do the same for all the languages we want to implement. We will then have the following code in our
ScriptLanguageFactory
: public class MyScriptLanguageFactory extends AbstractScriptLanguageFactory<MyScript> { protected Class<ScriptBuilder> createBuilderClass(short scriptType) { Class<ScriptBuilder> clazz = null; if (scriptType == ScriptTypes.GROOVY) { clazz = createBuilderClassImpl("my.package.MyGroovyScriptBuilder"); } else if (scriptType == ScriptTypes.JAVASCRIPT) { clazz = createBuilderClassImpl("org.xul.script.custom.factory.MyJSScriptBuilder"); } // etc... } return clazz; } }
ScriptFactory
, we just have to call the ScriptManager.setScriptFactory(ScriptFactory) method in our ScriptManager. For example: ScriptManager manager = new DefaultScriptManager(); manager.setScriptFactory(new CustomScriptFactory());
Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence