Making A Plugin for LTSA

LTSA now supports an extension mechanism that allows functionality to LTSA to be extended without changing the core implementation. This is done using a plugn technique. On initialisation, LTSA searches a certain directory for jar files and looks in each jar file for a class with a certain interface. On finding such a class it instantiates it and calls methods on the object to determine properties of the plugin. Currently, plugins can:

How to Implement A Plugin

All the classes you'll need to use to create a plugin are in the extension package. The most important class is LTSAPlugin. To create a plugin simply extend this class. There are some abstract methods in LTSAPlugin which need to be overridden, for instance getName(). A very simple plugin is shown below:


package demoplugin;

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

import ic.doc.extension.*;

/**
 * @author Robert Chatley - rbc@doc.ic.ac.uk
 *
 * This is an example of a very simple plugin for LTSA
 * It adds a new tab, a button and a menu item. Clicking either the button or the menu item
 * causes the names of the currently available LTSs to be displayed in the new tab.
 */

public class DemoPlugin extends LTSAPlugin {

    private JTextArea o_textarea;

    // You need to include the following two constructors. Do all your initialisation in initialise()

    public DemoPlugin(){}

    public DemoPlugin( LTSA p_ltsa ) { super( p_ltsa ); }
    
    // Return a name for your plugin.
    public String getName() { return "Demo Plugin"; }

    
    // return true if this plugin provides a component that should be added as a tab in user interface.
    public boolean addAsTab() { return true; }

    // return a reference to a Component - in this case we'll use a JTextArea
    public Component getComponent() { return o_textarea; }
    
    // return true to add a button or buttons to the toolbar
    public boolean addToolbarButtons() { return true; }

    // return a list of LTSAButtons to add to the toolbar
    public List getToolbarButtons() {

	List x_button_list = new ArrayList();
	ImageIcon x_icon = new ImageIcon( this.getClass().getResource( "/demoplugin/icon.gif" ) );
	LTSAButton x_button = new LTSAButton( x_icon , "Demo Plugin control" , new MyActionListener() );

	x_button_list.add( x_button );

	return x_button_list;
    }

    // Let's add a menu item that does the same thing that our toolbar button does
    public boolean addMenuItems() { return true; }

    public Map     getMenuItems() {

	Map x_items = new HashMap();

	JMenuItem x_item = new JMenuItem( "Demo - get LTS names" );
	x_item.addActionListener( new MyActionListener() );

	// this specifies that the new item should be added to the Build menu 
	x_items.put( x_item , "Build" );

	return x_items;
    }

    public void initialise() {

	// initalise our text area
	o_textarea = new JTextArea();
    }

    void doSomething() {

	// write the names of the current LTSs into our new tab

	LTSA x_ltsa = getLTSA();
	List x_ltss = x_ltsa.getLTSNames();
	o_textarea.setText( "Current LTS's :\n\n" );

	for ( Iterator i = x_ltss.iterator() ; i.hasNext() ; ) {

	    o_textarea.append( (String)i.next() + "\n" );
	}

	// swap to the plugin tab 
	x_ltsa.swapto( getName() );
    }

    class MyActionListener implements ActionListener {

	public void actionPerformed( ActionEvent p_e ) {

	    doSomething();
	}
    }
}	

Compile this class and put it into a jar file (make sure that you jar the whole demoplugin directory so that the package structure inside the jar file is correct). Then put this jar file into Ltsa's plugins directory. Now start Ltsa and your plugin should automagically be included in the system.