Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

Custom context tutorial



Overview

Suppose that we want to add a new getLength(File file) method for our ScriptHelper.

Creation of the ScriptContext and ScriptHelper classes

We will override the AbstractXULScriptHelper class:

      import org.xul.script.scripts.AbstractXULScriptHelper;
      import java.io.File;      
      
      public class MyXULScriptHelper extends AbstractXULScriptHelper {
        MyXULScriptContext context;
      
        public MyXULScriptHelper(MyXULScriptContext context) {
          this.context = context;
        }
      
        public MyXULScriptContext getContext() {
          return context;
        }      
            
        public long getLength(File file) {
          return file.length();
        }
      }

This helper class will only be available if it is returned by the getHelper meyhod of the corresponding ScriptContext. Let's override the AbstractXULScriptContext class:

      import org.xul.script.scripts.AbstractXULScriptContext;
      
      public class MyXULScriptContext extends AbstractXULScriptContext {
        MyXULScriptHelper helper = null; 
        public MyXULScriptContext() {
          this.helper = new MyXULScriptHelper();
        }      
      
        public MyXULScriptHelper getHelper() {
          return file.length();
        }
      }

Set this context for the ScriptManager

The last thing we must do is to set this context to the ScriptManager by:

      manager.setScriptContext(new MyXULScriptContext());

Usage

Now we can use the getLength(File file) in our scripts. For example in Groovy:

      FilePicker filepicker = new FilePicker();
      picker.init(null, "openMode");
      int ret = picker.show();
      if (ret == FilePicker.RETURN_OK) {
        long length = helper.getLength(picker.getFile());
        context.echo(length);
      }

See also


Categories: tutorials

Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence