Eclipse IDE Plugin: UI Actions

Rohit Satwadhar
4 min readNov 5, 2021
Eclipse Plugin development

How to write plugins on top of Eclipse platform to add customized functionality to the IDE.

Eclipse is very famous java IDE. It provides multiple features for developers. Here one specific feature that we are interested in is its extensibility. Eclipse is designed such that users can create plugins and add custom functionality to the platform. This functionality is so useful and used widely that many of the proprietary products are developed on top of eclipse.

In this article we will explore how to create a eclipse plugin project ? How to add UI element for your plugin ? and how to trigger handler on any event on UI element ?

You should have Eclipse and PDE (Plug-in development environment)installed for this. I am using java 11 and eclipse 2021–06.

First lets create a plugin project. This should be fairly straight forward.

  1. Go to file->new-> Plug-in Development -> Plugin Project.
  2. Give name to project. On next page check “ this plug-in will make contribution to UI”. Finish.
  3. New perspective with project will be opened.
New Created project will look like this.

Now we need to understand how eclipse provides us extensibility. We use something called extension points to add custom features. Read more about it here.

We will require two extension points.

  1. org.eclipse.ui.commands: we will create a command component using this. It will be more of declarative thing.
  2. org.eclipse.ui.menus: Here we will specify where we want to add our UI Component and connect it with command.

To use these extensions we will need to add org.eclipse.ui dependency. Open MANIFEST.MF file and go to Dependencies tab. Add Button -> org.eclipse.ui.

MANIFEST.MF file

Once dependency is added we will move onto using extension points to create our eclipse actions.

Creating a command :

Go to extensions tab in MANIFEST.MF. Add Button -> org.eclipse.ui.commands.
Once extension is added. Right click -> new ->command. You will see that new command is created and it asks you some info to fill. Give whatever name and id you wish to give. Important thing to observe here is defaultHandler box. This box takes in the class which will be triggered by our command.
We will add this after creating a handler.

Creating a command

Creating a handler :

This is the class which is triggered by our command. Go to src folder in out project structure. Create a new package (give whatever name you want). In that create a new class (give whatever name you want).
Now the class is created extend AbstractHandler and add unimplemented methods. AbstractHandler provides support for handler listeners. And it has single unimplemented method execute(…). This method should have things you want to do when command is triggered.
Now we will connect this to our command. Go to extensions tab in MANIFEST.MF and then in our command. Browse defaultHandler box and add our handler class.

Handler Class

Adding Command UI to main menu :

Now we will create a ui component and add it to our main menu. For this we will use org.eclipse.ui.menus extension. Add this extension similar to how you added org.eclipse.ui.commands.
Right Click -> new ->menuContribution.
This will open up box. You only need to worry about locationURI input. This input is used to determine where your command will be added. You can find these locationURI using plugin selection spy.
For adding command to main menu we will use locationURI menu:org.eclipse.ui.main.menu
Now right click on this menuContribution new -> command. This will connect this command to our command. Provide command ID you gave to your command. Provide label, icon as you wish. I labelled it testPlugin.

Adding menu and command

Now we have our UI action ready. Right click on the project. Run as -> eclipse application. A new instance of eclipse will be opened. In the menu you should be able to see label of your command in main menu. Click it.
It will trigger your handler and execute()method will be called.

Main Menu in runtime Eclipse Application

Done!!!! We just created a custom menu option on eclipse.

Note: This is meant for only touching the surface of UI action. There is a lot more to this. If you find this interesting you should go ahead and explore it further.

--

--

Rohit Satwadhar

I Write about new things that I learn. That is how I remember stuff. These things are mostly tech related.