Monday, September 26, 2011

Hiding UI elements in RCP

External plug-ins and features give you a tremendous power to enrich your RCP applications. Somtimes you might wish to use an external feature without getting all the visual stuff like menus or toolbar items. Hiding them is quite a tricky task. There exist several ways to do this, none of them seems either well documented nor sophisticated.

You can find some documentation on the web like a post from Miles Billsman or a tutorial by Lars Vogel. I am going to sum up things here.

Source code for this tutorial is available on github as a single zip archive, as a Team Project Set or you can browse the files online.

Preparations: A default eclipse IDE RCP

Start by creating a new Plug-in project. This will be no RCP application, nor will it contribute to UI. Just a plain Plug-in. On the overview tab hit Launch an Eclipse application. This will bring up a new Eclipse instance including your Plug-in. Nothing special so far. Now lets start hiding UI elements.

Approach 1: Activities

Activities allow to hide UI elements once you know their ID - or at least parts of it. Go to the Extensions tab of your plugin.xml and add a new extension for org.eclipse.ui.activities. Within that extension create a new activity and provide some unique id.


Create a new activityPatternBinding under activities. Refer to your activity ID.
Now for the tricky part: we need to find the ID of an element we want to hide. Then we can pass this ID to pattern to hide the element.

Finding IDs is greatly described in Miles Billsman post, so I will focus on some examples.

Hiding the Run External Tools Toolbar Item

The Plug-in Menu Spy is a great tool to find out more about menu/toolbar entries. Read more about the Plug-in Spy in Lars Vogels tutorial. So hit Alt-Shift-F2 in your running application and click on the toolbar button you want to examine.


The active action definition identifier is what we are looking for. Copy that ID and paste it to your activityPatternBinding pattern. Pattern could be a regular expression and therefore capture a whole bunch of UI elements at once. We want exact matching, so set isEqualityPattern to true.


Start your application and the toolbar button is gone.

Hiding the Next Annotation Toolbar Item

Unfortunately not all UI elements can be hidden by that ID. For example this does not work for the Next Annotation toolbar item. To hide this element we need to track down its definition within the defining Plug-in.

Therefore open 2 views: Plug-in Registry and Plug-ins. By hovering over the toolbar item we can see a text containing "Next Annotation". Lets use these words in Plug-in Registry to filter for that element.


We can see that this element is provided by the org.eclipse.ui.editors Plug-in. Switch to the Plug-ins view, locate org.eclipse.ui.editors and double click to open its plugin.xml. On the Extensions tab we need to look for the contribution definition. It should be either under org.eclipse.ui.commands or under org.eclipse.ui.actionSets. Once located it is easy to find the needed contribution ID for our activityPatternBinding pattern.


As this element is provided by an action we need to prepend the pattern with the defining Plug-in ID. So the full pattern to apply is:

org.eclipse.ui.editors/org.eclipse.ui.edit.text.gotoNextAnnotation

Approach 2: Perspective Extensions

Some elements simply cannot be hidden with activities. Try the pattern ".*" which should hide all elements. Some of them still remain. With perspective extensions we will hide the Run menu and the Run Eclipse Application toolbar item.

Create a new org.eclipse.ui.perspectiveExtensions extension. Under targetID provide the perspective ID for which the element should be hidden. You can use "*" to match all perspectives. Create a new hiddenMenuItem and add use the ID of the run menu org.eclipse.ui.run. This hides the Run menu completely.

To hide the toolbar entry for Run Eclipse Application use a hiddenToolBarItem and an ID of org.eclipse.debug.internal.ui.actions.RunDropDownAction.



The IDs were found using the Plug-in Registry and by parsing plugin.xml files.

Additional Notes

Generally you should have some idea where to look for a contribution definition. This is the most tricky part and there seems to be no easy way for that.

Keep in mind that we just hid those items. They are still available by using keyboard shortcuts or might be accessed indirectly (eg. the Plug-in Manifest Editor still can Launch an Eclipse application).

11 comments:

  1. Thanks very much for this handy and easy to comprehend article!
    Using activities, it is even possible to hide perspectives.

    E.g., if you want to hide the Java Perspective, use "org.eclipse.jdt.ui/org.eclipse.jdt.ui.JavaPerspective" as a pattern.

    Here is an extension point example that hides the default perspectives of a org.eclipse.ui.ide.workbench application, just in case someone's looking for something like this:























    ReplyDelete
  2. Okay, XML code vanished, so here are the patterns:
    pattern="org.eclipse.jdt.ui/org.eclipse.jdt.ui.JavaPerspective"
    pattern="org.eclipse.debug.ui/org.eclipse.debug.ui.DebugPerspective"

    pattern="org.eclipse.jdt.ui/org.eclipse.jdt.ui.JavaHierarchyPerspective"
    pattern="org.eclipse.jdt.ui/org.eclipse.jdt.ui.JavaBrowsingPerspective"

    pattern="org.eclipse.team.ui/org.eclipse.team.ui.TeamSynchronizingPerspective"

    ReplyDelete
  3. Thank you for the useful info! I didn't know about the plugin spy.
    I came up with a simple regular expression to wipe away all perspectives provided by default:

    isEqualityPattern="false"
    pattern="org\.eclipse\..*[Pp]erspective.*"

    isEqualityPattern means it is a regular expression, not a simple string.

    ReplyDelete
  4. Thanks a lot...this worked perfectly :)

    ReplyDelete
  5. First, Please understand me That i'm not very good at English

    I can hide Eclipse Default Toolbar item.
    I remove Allmost...

    but unfortunately, I can't Undo... redo...

    undoredo.toolbar is located in org.eclipse.birt.report.designer.ui ...
    I tried 'undoredo.undo', 'org.eclipse.ui.edit.undo', 'org.eclipse.ui.file.undo'...
    but it doesn't hide....

    Do you know how to hide in eclipse main toolbar?

    ReplyDelete
    Replies
    1. sorry, cannot help you here. But for sure the relevant item is not to be found in o.e.birt.... o.e.ui.edit seems far more reasonable

      Delete
  6. Thank you so much. Very helpful.

    ReplyDelete
  7. Thank you so much. Very helpful.

    ReplyDelete
  8. Approach 2 is not working...I tried to remove the run using the mentioned approach, but still it's coming. Can you please give some update on that whether we have to do something else also.

    ReplyDelete
    Replies
    1. Sorry, no update for this article. What is there or hidden depends heavily on the plugins you are adding to your RCP application. This article should give you hints where to look for stuff and how to hide them.

      Generally in my opinion hiding is not a good thing to do. Either use the eclipse style UI and let plugins contribute whatever they like to or come up with your own application model and show whatever you think is necessary. Hiding only works as long as your users do not start adding their own plugins to your applications - and there is no way to stop them doing this!

      Delete