Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this guide we show how to create custom ECL commands. As an example we create two ECL commands for Eclipse Views. First one will show us all registered views in our Eclipse. And the second one will open specified view by ID.
You can find source of the examples used in this guide in the ECL repository: https://github.com/xored/ecl/tree/master/examples{

Table of Contents
}
h2.

Create Plug-in Project

First of all we'll create new plugin project for our ECL commands called *org.eclipse.ecl.example.view*. You can you any existing plug-in as well.

h2.

Define EMF model for ECL command

We need to create two EMF classes to show registered plugins with ECL:* *

  • ShowViews

...

  • which will be used as an ECL command. This EClass should extend

...

  • Command

...

  • EClass from the

...

...

  • package

...

  • View

...

  • which will be used to store view details. It should contains three string properties:

...

  • id

...

  • ,

...

  • label

...

  • and

...

  • description

...

  • .

If you're not experienced EMF user you can find detailed instruction on the [ECL:Define EMF model for ShowViews command] page.h2.

Implement ShowViews command

Now we need to implement ECL command. Make sure you have all necessary dependencies:

h4.

Implement

...

ICommandService

...

interface

Add new class *ShowViewsService* which implements *org.eclipse.ecl.runtime.ICommandService* interface as shown below:{

Code Block

...

java

...

public class ShowViewsService implements ICommandService

...

 {

	@Override
	public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException

...

 {
		final IWorkbench workbench = PlatformUI.getWorkbench();

...


		final List<View> views = new ArrayList<View>();

...


		workbench.getDisplay().syncExec(new Runnable()

...

 {
			@Override
			public void run()

...

 {
				for (IViewDescriptor descriptor : workbench.getViewRegistry().getViews())

...

 {
					View view = ViewFactory.eINSTANCE.createView();

...


					view.setId(descriptor.getId());

...


					view.setLabel(descriptor.getLabel());

...


					view.setDescription(descriptor.getLabel());

...


					views.add(view);

...


				}
			}
		});
		IPipe output = context.getOutput();

...


		for (View view : views)

...

 {
			output.write(view);

...


		}
		return Status.OK_STATUS;

...


	}

}

As you can see the implementation is very simple. We use platform *IViewRegistry* to collect details of Eclise Views and store this information using EMF View objects. Then we write collected information to the output pipe. Note that view service will be executed using Eclipse Jobs, so we use Workbench Display to access view info in the UI thread.h4.

Register ECL command implementation

Finally we need to register our service using *org.eclipse.ecl.core.scriptlet* extension point:

* *

  • name

...

  • - name of the EClass which we use for ECL command

...

  • namespace

...

  • - EMF Package URI

...

  • class

...

  • - command implementation class. Should implement

...

  • ICommandService

...

  • interface

By default name of the command EClass will be transformed to the ECL command name. For example, in our case commands will be named *show-views*. However you can override this name using *friendly_name* part of the *org.eclipse.ecl.core.scriptlet* extension point.h2.

Use

...

show-views

...

command

...

  • Let's start new Eclipse instance with

...

  • org.eclipse.ecl.example.view

...

  • plugin installed

...

  • Connect to the ECL telnet server started in this Eclipse (on the 2323 port by default) using any telnet client

...

  • Enter

...

  • show-views

...

  • command

*

  • As a result of your command you will see something like this:

There is text presentation of the EMF objects collected from the Eclipse environment. However it's not readable enought for terminal. Fortunately ECL has core *print* command which can be used for better text presentation of the EMF objects. Let's use *show-views \ | print* command instead:

This presentation looks much better.h2.

Add

...

show-view

...

command

In the previous sections we discussed how to collect information from Eclipse using ECL. Now let's add another command which will open some view by id.h4.

Add

...

ShowView

...

EClass

Create one more EClass called *ShowView* with one string field *id*. This is ECL command and should extend *Command* EClass.

h4.

Implement ShowView command

Add *ShowViewService* java class:{

Code Block

...

java

...

public class ShowViewService implements ICommandService

...

 {

	@Override
	public IStatus service(Command command, IProcess context) throws InterruptedException, CoreException

...

 {
		ShowView view = (ShowView) command;

...


		final String id = view.getId();

...


		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable()

...

 {

			@Override
			public void run()

...

 {
				try {
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(id);

...


				} catch (PartInitException e) {
					throw new RuntimeException(e.getMessage(), e);

...


				}
			}
		});
		return Status.OK_STATUS;

...


	}

}

We added this service specially for ShowView command, so we can freely cast specified command to ShowView interface. This allows us to get id of the view we need to open. Next we use Eclipse Platform API to open view by id in the UI thread.h4.

Register ShowView command

Finally we need to register new command through *org.eclipse.ecl.core.scriptlet* extension point:

h4.

Use

...

show-view

...

command

Now we can run our plugin and open necessary view by id:

By the way, you can use *show-views* command to find id of a view you want to open.