Dimitri Missoh
enthusiastic technologist and problem solver

November 2nd, 2008

Disable or enable actions sets on perspective change in an Eclipse RCP application

Here is the challenge: I would like to enable or disable some action-sets according to the perspective the user activates. This should be done through a customization plug-in, because I’m not allowed to directly modify the code of the RCP application. I can only contribute my modifications through a new plug-in. Let’s walk through the necessary implementation steps. First we need to register our plug-in as a perspective change listener.  The second step will be to find a way to programmatically enable or disable a given action-set according to the new perspective.

Since I cannot modify the code of the existent application, I will create a new plug-in and extend the early start-up extension point org.eclipse.ui.startup to be able to run some code when the workbench starts. This extension point requires a class that implements the org.eclipse.ui.IStartup interface. The following snippet shows how the plug-in descriptor file plugin.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
   <extension
         point="org.eclipse.ui.startup">
      <startup
            class="perspectiveactionsets.startup.EarlyStartup"></startup>
   </extension>
</plugin>

To be notified on a perspective change event, a org.eclipse.ui.IPerspectiveListener will be registered to the active window of the workbench IWorkbenchWindow in the form of an anonymous class. I use the org.eclipse.ui.PerspectiveAdapter which provides default implementations for the methods described by the IPerspectiveListener interface. There is a very important point we should not forget when trying to retrieve the active workbench window (IWorkbenchWindow) using the static call PlatformUI.getWorkbench().getActiveWorkbenchWindow(). Let’s seek the javadoc for the getActiveWorkbenchwindow() method of the WorkbenchWindow class:

/**
* Returns the currently active window for this workbench (if any). Returns
* <code>null</code> if there is no active workbench window. Returns
* <code>null</code> if called from a non-UI thread.
*
* @return the active workbench window, or <code>null</code> if there is
*         no active workbench window or if called from a non-UI thread
*/
public IWorkbenchWindow getActiveWorkbenchWindow();

That is, we have to retrieve the active workbench window within the UI thread. Here is approximately how the earlyStartup() looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* (non-Javadoc)
* @see org.eclipse.ui.IStartup#earlyStartup()
*/
public void earlyStartup() {
  /*
  * The registration of the listener should have been done in the UI thread
  * since  PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null
  * if it is called outside of the UI thread.
  * */
  Display.getDefault().asyncExec(new Runnable() {
    /* (non-Javadoc)
    * @see java.lang.Runnable#run()
    */
    public void run() {
      final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (workbenchWindow != null) {
          workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
            /* (non-Javadoc)
            * @see org.eclipse.ui.PerspectiveAdapter#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
            */
            @Override
            public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspectiveDescriptor) {
              super.perspectiveActivated(page, perspectiveDescriptor);
              // TODO implement the task to execute when the perspective change
            }
          });
        }
    }
  });
}

Now let us face the second problem. I would like to disable or enable some action-sets on perspective change.
In the eclipse IDE, you can customize a given perspective using the right mouse click on the corresponding perspective icons (see the screenshot below).

Customize a perspective using the context menu on the correpsonding icon

Customize a perspective using the context menu on the correpsonding icon

This opens a dialog where you can select the command groups (e.g. action-sets) that should be disabled in the workbench as shown in the following screenshot.

Disable command groups with the Customize Perspective Dialog

Disable command groups with the Customize Perspective Dialog

If we can manually customize it, it should also be possible to achieve this task with some lines of code. Let us consult the best eclipse book ever, i.e. the eclipse code itself. A quick search reveals that the problem has been solved in the class org.eclipse.ui.internal.dialogs.CustomizePerspectiveDialog. Having a look at the okPressed() in this class gives us the solution: a method turnOnActionSets() can be used on the perspective object to enable or disable an action-set.

After some modifications, the earlyStartup() method has now the form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* (non-Javadoc)
* @see org.eclipse.ui.IStartup#earlyStartup()
*/
public void earlyStartup() {
  /*
  * The registration of the listener should have been done in the UI thread
  * since  PlatformUI.getWorkbench().getActiveWorkbenchWindow() returns null
  * if it is called outside of the UI thread.
  * */
  Display.getDefault().asyncExec(new Runnable() {
    /* (non-Javadoc)
    * @see java.lang.Runnable#run()
    */
    public void run() {
      final IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if (workbenchWindow != null) {
          workbenchWindow.addPerspectiveListener(new PerspectiveAdapter() {
            /* (non-Javadoc)
            * @see org.eclipse.ui.PerspectiveAdapter#perspectiveActivated(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.IPerspectiveDescriptor)
            */
            @Override
            public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspectiveDescriptor) {
              super.perspectiveActivated(page, perspectiveDescriptor);
                if (perspectiveDescriptor.getId().indexOf("org.eclipse.debug.ui.DebugPerspective") &gt; -1) {
                  if (workbenchWindow.getActivePage() instanceof WorkbenchPage) {
                    WorkbenchPage worbenchPage = (WorkbenchPage) workbenchWindow.getActivePage();
                    // Get the perspective
                    Perspective perspective = worbenchPage.findPerspective(perspectiveDescriptor);
                    ArrayList toRemove = new ArrayList();
                      if (perspective != null) {
                        for (IActionSetDescriptor actionSetDescriptor : perspective.getAlwaysOnActionSets()) {
                          if (actionSetDescriptor.getId().indexOf("org.eclipse.search.searchActionSet") &gt; -1) {
                            // Add the action set descriptor to the list of the action sets to remove
                            toRemove.add(actionSetDescriptor);
                          }
                        }
                      perspective.turnOffActionSets((IActionSetDescriptor[]) toRemove.toArray(new IActionSetDescriptor[toRemove.size()]));
                    }
                  }
                }
            }
          });
        }
    }
  });
}

P.S.: In the example above, all search related actions (denoted by the ID ‘org.eclipse.search.searchActionSet‘) will disappear if you set the debug perspective (denoted by the ID ‘org.eclipse.debug.ui.DebugPerspective‘) as the active one.

For more details check out the source code of the project PerspectiveActionSets from the google code repository http://homeworks.googlecode.com/svn/trunk/ or download the plug-in project under the link below.

Download Projects Files.

October 20th, 2008

Use conditional breakpoints to simulate a bug or system out in third party code

Suppose you want to system out the value of a variable your are interested in or even change it value to simulate a bug during a debug session in a third party code, e.g. in a packaged library. We assume that you download the source code and attach it to your library. Since you are compiling your code again the library, you can neither change any part of the code located in the library nor compile it to see the effect of your change. You can easily achieve this task in eclipse using conditional breakpoints. Just double click to add a breakpoint as usual and use the context menu on this breakpoint to open the “Breakpoint Properties” dialog. The default option stops the debugger at a conditional break point only if it returns true. So terminate you code block with a return false, to no interrupt the code execution. Screenshots show two use cases. In the first one we use this feature to system out the value of a given label, and in the second example we use conditional break point to set the value of the variable window to null to simulate a bug and see how our code react on a null pointer exception.

System out with conditional breakpoints

Set a variable to null with conditional breakpoint

August 28th, 2008

Programmatically split an editor area to show two editors side by side.

Last time a workmate of mine asked me if it is possible, to programmatically split the editor area of an eclipse RCP application.

We all know that if you have two editors opened in the workbench, you can drag one of the editors and drop it in one of the regions of the editor area (bottom, top, left or right) so that both editors are side by side.

I started to analyze the eclipse code and look for a public API that I can use to achieve this task. I didn’t find anything. My first idea was to simulate the Drag&Drop behavior, but it came out to be a very challenging task.

So I’ve decided to use some of the API calls, which the eclipse team discourages to use. And it works. I bundled my code into a plug-in. Maybe someone will find this useful.

Download Projects Files.

Here is the most important part of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Split the editor area if there is at least two editors in it.
 */
private void splitEditorArea() {
	IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
	IWorkbenchPart part = workbenchPage.getActivePart();
	PartPane partPane = ((PartSite) part.getSite()).getPane();
	LayoutPart layoutPart = partPane.getPart();
 
	IEditorReference[] editorReferences = workbenchPage.getEditorReferences();
	// Do it only if we have more that one editor
	if (editorReferences.length > 1) {
		// Get PartPane that correspond to the active editor
		PartPane currentEditorPartPane = ((PartSite) workbenchPage.getActiveEditor().getSite()).getPane();
		EditorSashContainer editorSashContainer = null;
		ILayoutContainer rootLayoutContainer = layoutPart.getContainer();
		if (rootLayoutContainer instanceof LayoutPart) {
			ILayoutContainer editorSashLayoutContainer = ((LayoutPart) rootLayoutContainer).getContainer();
			if (editorSashLayoutContainer instanceof EditorSashContainer) {
				editorSashContainer = ((EditorSashContainer) editorSashLayoutContainer);
			}
		}
		/*
		 * Create a new part stack (i.e. a workbook) to home the
		 * currentEditorPartPane which hold the active editor
		 */
		PartStack newPart = createStack(editorSashContainer);
		editorSashContainer.stack(currentEditorPartPane, newPart);
		if (rootLayoutContainer instanceof LayoutPart) {
			ILayoutContainer cont = ((LayoutPart) rootLayoutContainer).getContainer();
			if (cont instanceof PartSashContainer) {
				// "Split" the editor area by adding the new part
				((PartSashContainer) cont).add(newPart);
			}
		}
	}
}
 
/**
 * A method to create a part stack container (a new workbook)
 * 
 * @param editorSashContainer the <code>EditorSashContainer</code> to set for the returned <code>PartStack</code>
 * @return a new part stack container
 */
private PartStack createStack(EditorSashContainer editorSashContainer) {
	WorkbenchPage workbenchPage = (WorkbenchPage) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
	EditorStack newWorkbook = EditorStack.newEditorWorkbook(editorSashContainer, workbenchPage);
	return newWorkbook;
}

April 29th, 2008

Embed a Hessian based Client and Server in a Eclipse Plug-in

I’am currently working on a small project. The goal is to embed a Hessian based client and server in a eclipse plug-in. This can for example be used to let either two RCP Applications to communicate with each other or an RCP Client and a arbitrary external Application to communicate over this binary protocol. In the first step Client and Server have been both implemented in java. I get it work so far. Within the next “few” days I will try to implement a combination where the server is written in java and the client in C# (using HessianC#).

This diagram shows the structure of the whole project.

The overall structure of the project

I add a zip that contains the “dirty and uncommented” source code of the projects. The java and plug-in projects can be imported into eclipse. To compile the C#-Part of the project you will need either Microsoft Visual Studio Express (available for free) or the open source SharpDevelop. I hope this can be useful for someone.

Download Projects Files.

April 20th, 2008

A not so evil shell visiting my IDE

Everyday I have to execute some batch files and start some processes (e.g. start the http server or tomcat) using the command prompt. I found a very usefull Eclipse plug-in that integrate all this nice stuffs directly in my IDE. Check out the free available plug-in at http://www.wickedshell.net/.