Implementing the ScriptEditor interface

From Apache OpenOffice Wiki
Jump to: navigation, search



If you want to add support for editing scripts you need to implement the ScriptEditor interface:

  package com.sun.star.script.framework.provider;
 
  import com.sun.star.script.provider.XScriptContext;
  import com.sun.star.script.framework.container.ScriptMetaData;
 
  public interface ScriptEditor
  {
      public Object execute() throws Exception;
      public void indicateErrorLine( int lineNum );
      public void edit(XScriptContext context, ScriptMetaData entry);
      public String getTemplate();
      public String getExtension();
  }

The edit() method is called when a user presses the Edit button in the Macro Organizer. The ScriptEditor implementation can use the ScriptMetaData object to obtain the source code for the macro and display it.

The getTemplate() method should return a template of a macro in your language, for example the code to write HelloWorld into a document. The getExtension() method should return the filename extension used for macros written in your language. These methods are called when the Create button is pressed in the Macro Organizer.

The execute() and indicateErrorLine() methods are not called by the Macro Organizer and so they do not have to do anything. They are used by the implementation of the ScriptProviderForBeanShell to execute the source code that is displayed in the ScriptEditorForBeanShell, and to open the ScriptEditorForBeanShell at the line for which an error has occurred. The developer may wish to do the same when writing their ScriptProviderForYourLanguage and ScriptEditorForYourLanguage.

The following code shows an example ScriptEditorForYourLanguage.java file:

  import com.sun.star.script.framework.provider.ScriptEditor;
  import com.sun.star.script.provider.XScriptContext;
  import com.sun.star.script.framework.container.ScriptMetaData;
 
  import javax.swing.*;
 
  public class ScriptEditorForYourLanguage implements ScriptEditor
  {
      public Object execute() throws Exception
      {
          return null;
      }
 
      public void indicateErrorLine( int lineNum )
      {
          return;
      }
 
      public void edit(XScriptContext context, ScriptMetaData entry)
      {
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 
          JTextArea ta = new JTextArea();
          entry.loadSource();
          ta.setText(entry.getSource());
 
          frame.getContentPane().add(ta);
          frame.setSize(400, 400);
          frame.show();
      }
 
      public String getTemplate()
      {
          return "the code for a YourLanguage script";
      }
 
      public String getExtension()
      {
          return "yl";
      }
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages