Dynamic Menu Item to select Theme in Eclipse IDE

In Bug 514458, I added the “Theme” dynamic menu to the Eclipse IDE. This post explains how I did this.

Menu Eclipse 3.x style

First edit the plugin.xml, and add a menu contribution. With the locationURI menu:org.eclipse.ui.appearance?after=org.eclipse.ui.window.appearance.separator1, you’ll contribute a submenu to the Window > Appearance menu, just after the separator.

appearance-menu-structure

Add a Theme menu under the menuContribution, and then add a child dynamic element.

Next, create the java class implementing the dynamic menu, and add mock code to verify the menu works.

 

Verify the menu is displayed where you expect, and the submenu dynamic entries are working.

new-theme-dynamic-menu

Now remove the line which adds the “Theme 1” item, and rewrite the body of the widgetSelected.

The menu should have one menu item for each available theme, and each widgetSelected(){…} should activate the corresponding theme.

The theme selection code is inspired tothe ViewsPreferencePage one.

Finally, launch the Eclipse IDE to check the menu works as expected.

theming-menu

7 comments

    1. The user should not select a theme that is already active. So, the setEnabled(!active) disables the menuItem, for the next time the menu is shown, the user can not select it.
      The setSelected, on the other hand, is responsible for showing the “v” mark next to the menuItem.

  1. Thank you for the interesting blog post, Patrik. One thing I am curious about, however, is why you chose this curious mixture of Eclipse 3.x and 4.x APIs. While you use an 3.x-style menu contribution, you later on use the 4.x IEclipseContext and IThemeEngine. Why not Eclipse 4.x all the way through?

    (One more thing: The link to Bug 514458 above actually takes you to a different, unrelated bug.)

Leave a Reply

Your email address will not be published. Required fields are marked *