Plugin Actions

An action is basically an AWT action listener with a "name" attached. The name is used to obtain several properties, such as the action's menu item label and such. Actions are called by jEdit in response to user feedback such as menu item selections, keystrokes, etc.

EditAction class

public abstract class EditAction implements ActionListener
{
        public EditAction(String name);
        public String getName();
        public boolean isToggle();
        public boolean isSelected(Component comp);
        public abstract void actionPerformed(ActionEvent evt);
        public static Buffer getBuffer(ActionEvent evt);
        public static View getView(Component comp);
        public static View getView(ActionEvent evt);
}

The org.gjt.sp.jedit.EditAction class is the base class for all actions. It allows actions to be named and provides methods for obtaining the view and buffer that invoked the action. It implements the java.awt.event.ActionListener interface, and hence can be used anywhere an ordinary action listener would, for example to handle menu item and button clicks.

EditAction.EditAction() constructor

public EditAction(String name);

The constructor for the EditAction class takes the action name as a parameter. Concrete implementations of this class should provide a constructor with no parameters.

jEdit's built-in actions have all-lower case names, with multiple words separated by dashes ("-"). You should follow this convention in your own plugins, too.

EditAction.isToggle() method

public void isToggle(void);

This method returns true if the action should be represented as a check box menu item, false if it should be a normal menu item. By default, it returns false; override it to return true if you want the action to be a toggle.

EditAction.isSelected() method

import java.awt.Component;

public void isToggle(Component comp);

This method is only called if isToggle() returns true. It should return if the menu item's check box should be selected. comp is the component performing the query; for example, if the menu item state is determined on a per-view basis, you can pass the component to the getView() method to obtain the current view instance.

EditAction.actionPerformed() method

import java.awt.event.ActionEvent;

public abstract void actionPerformed(ActionEvent evt);

This abstract method must be implemented to do the work of the action. To obtain the view or buffer instance that invoked this action, pass the evt event instance to the getView() or getBuffer() methods, respectively.

jEdit.addAction() method

import org.gjt.sp.jedit.EditAction;

public static void addAction(EditAction action);

This method adds the action to jEdit's action list, making it available to the GUIUtilities.loadMenuItem() method, and so on. Plugin should add their actions to jEdit in their start() methods.

Example

Here is the source file (which should be named hello_world.java) to a very simple action:

import java.awt.event.ActionEvent;
import org.gjt.sp.jedit.*;

public class hello_world extends EditAction
{
        public hello_world()
        {
                super("hello-world");
        }

        public void actionPerformed(ActionEvent evt)
        {
                System.err.println(jEdit.getProperty("hello.world"));
                View view = getView(evt);
                String[] args = { view.getBuffer().getName() };
                System.err.println(jEdit.getProperty("hello.buffer"));

                // now we get the value of a property...
                String prop = jEdit.getProperty("hello.used");
                if(prop == null)
                {
                        // user has never run this action before!
                        System.err.println(jEdit.getProperty("hello.first-time"));

                        // set property for next time
                        jEdit.setProperty("hello.used","yes");
                }
                else
                {
                        System.err.println(jEdit.getProperty("hello.already-run"));
                }
        }
}

And here are the corresponding properties needed for this action to work:

hello-world.label=Hello World!

# String printed by System.err.println()
hello.world=Hello, World!

# The view's buffer's name is substituted for {0}
hello.buffer=The view's buffer is {0}

# Shown when action run for the first time
hello.first-time=This is the first time you invoked 'Hello World'

# Shown when it is run subsequent times
hello.already-run=You have run 'Hello World' before