Action tutorial (pre 0.7) - deprecated

Writing your first Action

Background

The Action Low Down

The concept of an action in the workbench very closely reflects that of an action in Eclipse. We use actions to define to the workbench a particular action that a user can take on the workbench. As such, we use the idea of actions to define toolbar items.
There will be 2 types of actions in the workbench; one for the workbench itself, and one for a particular viewpart.

The Action Abstract Class

If you look at the Action class, you'll see the following:

getId() - As with views. You MUST return a unique ID when implementing the interface
getImageDescriptor() - You may have a particular image that you want to display on your toolbar item. This can be done by returning a reference to the image through this method.
(Currently the menu system only allows a menu depth of 1.)
getText() - As with views, returns the text for this action.
getToolTipText() - Allows you to provide tooltip information
init(IWorkbenchWindow window) - Initialisation Method, called by the workbench
run(IAction action) - Runs the action

The latest version (updated every build) of the API is always available at http://www.karora.org/projects/moomba/apidocs/index.html

Lifecycle

When the Action is instantiated, the workbench will call init, and provide the object with a reference to its workbench window. If you grab a reference to this, it allows you to use the workbench window for things such as displaying new windows, displaying modal forms, etc.

Getter methods will be called as needed by the workbench, so as to display the action you have created.

The run method is called when the Workbench managed ActionListener for your Action fires.

Note: You don't need to declare your own action listeners - these are managed by the workbench.

Lets write one!

Note - In this tutorial, I am not providing any code.

For this tutorial we want to create a workbench toolbar item, which will bring up a new window for us. Before you go cheating and read the example code, I want you to stop and think about what you're about to do and the consequences....

Cheating ruins families. (If you get caught)

Now to acheive this (no not ruining families), we're going to need to know a few things:

  • How to bring up a window in Cooee
  • How to make an Cooee window Modal
  • How to specify where the toolbar item will appear
  • How to add a new window to the workbench.

First, check out the Cooee API (http://www.karora.org/projects/cooee/apidocs/index.html) and look at the WindowPane class. Find out how to set Styles, Titles and Modality.

Done that? Cool.

Create a new class that extends Action.

First, implement the Init Method, remembering what was mentioned earlier about this method and how it may be useful for this tutorial. Then fill in the getters as appropriate.

Finally implement your run method as specified by your mission statement.

You may need to have a look at this API, along with the Window Class Api in Cooee

http://www.karora.org/projects/moomba/apidocs/org/karora/moomba/ui/IWorkbenchWindow.html

Spring/OSGi configuration files

Edit the bundle-context.xml and bundle-context-osgi.xml files and declare the new service.

bundle-context.xml
<b>bundle-context.xml</b>
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  <beans>
    <b><bean name="helloworld.HelloAction" class="helloworld.HelloAction" singleton="false"/></b>
    <bean name="helloworld.HelloBigWorldPart" class="helloworld.HelloBigWorldPart" singleton="false"/>
    <bean name="helloworld.HelloWorldPerspective" class="helloworld.HelloWorldPerspective" singleton="false"/>
  </beans>
bundle-context-osgi.xml
<b>bundle-context-osgi.xml</b>
  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:osgi="http://www.springframework.org/schema/osgi"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
    <b><osgi:service id="helloworld.HelloActionOsgi" ref="helloworld.HelloAction" interface="com.gbv.platform.jface.action.Action" singleton="false"/></b>
    <osgi:service id="helloworld.HelloBigWorldPartOsgi" ref="helloworld.HelloBigWorldPart" interface="com.gbv.platform.workbench.ui.part.IViewPart" lazy-init="false"/>
    <osgi:service id="helloworld.HelloWorldPerspectiveOsgi" ref="helloworld.HelloWorldPerspective" interface="com.gbv.platform.workbench.ui.IPerspectiveFactory" lazy-init="false"/>
  </beans>

Compile, Install, and Run

Build your project.

mvn clean package

Install your built jar into your Equinox Runtime and refresh the project.

Right click on your Equinox Runtime project, and select "Run On Server". Run it on Tomcat.
Once the server is up, type ss in the console to show all installed bundles and you should, as before, see both the Workbench and the HelloWorld Perspective bundles.

Start the Spring framework OSGi bundle, the OSGi extender bundle and both the Workbench and Hello World Perspective bundles.
Then go to the following url
http://localhost:8080/EquinoxRuntime/Workbench

You should now see your new action within your workbench.

ViewPart Actions

As mentioned above, you can also have actions within a view part. The following is a code snippet which shows how this can be done.

MyPart
public class MyPart extends ViewPart {

	private Action myAction;

	... implement other ViewPart methods here

	public void createPartControl(Component parent)
	{
  		makeActions();
  		contributeToActionBars();
  	}

	protected void makeActions() {
		// My Button
		myAction = new Action() {
			public void run()
			{
  				// do stuff
  			}
		};
		myAction.setText("myActionText");
		myAction.setToolTipText("myActionToolTip");
	}

	protected void contributeToActionBars()
	{
  		IActionBars bars = getViewSite().getActionBars();
  		fillLocalToolBar(bars.getToolBarManager());
  	}

	private void fillLocalToolBar(IToolBarManager manager)
	{
  		manager.add(myAction);
  	}
}

Styling

The actions within the workbench can be styled via a stylesheet. The style names for the buttons are:

Toolbar.Button - for a workbench action
ViewPart.Toolbar.Button - for a ViewPart action

Labels

 
(None)