Eclipse 4

Managing preference pages with Eclipse 4

Preference pages and Eclipse 4

If you have made preference pages with Eclipse 3 you probably used the org.eclipse.ui.preferencePages extension point which describes your pages displayed in the preference dialog.

In a pure Eclipse 4 application, this extension point is no more available because it stands in ‘org.eclipse.ui’ and the IPreferenceStore implementation, which is usually used, is not available (the ScopedPreferenceStore is defined in the org.eclipse.ui.preferences which depends on org.eclipse.ui once again…).

Also remind that PreferenceDialog is provided by JFace and needs a PreferenceManager to be initialized.

As the Eclipse platform does not provide  a pure E4 solution to define this preference pages, I developed a dedicated plugin to manage them.

This plugin is available on github and also on the  OPCoach P2 repository

It provides the following issues :

This solution can help you to write easily your preference pages without having a dependency to org.eclipse.ui.

Of course, you can use the injection in your preference page code, if you need it.

To get the preference value you can still use the @Preference annotation.

To use this plugin in your project, you should :

  • add the e4 preferences feature in your target platform  (see the link on the GitHub home page) or this Target Platform file (look at the end where OPCoach repository is used)
  • make your plugin depend on com.opcoach.e4.preferences
  • extend the com.opcoach.e4.preferences.e4PreferencePages extension point to add your pages
  • If you are running a pure E4 application, add, in the plugin that contains your application model, a dependency to the com.opcoach.e4.preferences.mainmenu plugin that will add, for free, the command to open the preference dialog.
  • If you are running the compatibility mode (E3 + E4), add nothing else. The E3 and E4 pages will be displayed together automaically. 
  • optionally write a com.opcoach.e4.preferences.e4PreferenceStoreProvider extension to override the default PreferenceStore policy

This example projet shows how to use it. 

Feel free to comment this post if you have any remark.

User Comments


  1. Groza Danut
    10 February 2024

    Thank you for this awesome contribution! This was exactly what I was looking for, a preference dialog for pure e4 application. Very clear explanation and easy to use library and I am really glad the repository still works in 2024.

    As per this post: https://www.eclipse.org/forums/index.php/m/1863381/#msg_1863381
    I am currently working on migrating the p2 Install New Software dialog to a pure e4 application. Should you have any experience on this issue, I would appreciate a feedback on my forum message. Best regards!


  2. OPCoach
    13 March 2019

    The plugin is now available on this p2 repository : https://opcoach.com/repository/2018-12 in this feature : com.opcoach.e4.preferences.feature.feature.group.

    This new release can mix both E3 and E4 preference pages in the same preference dialog.


  3. OPCoach
    12 May 2016

    Hi,
    Today, during my Eclipse Migration training, I just asked a participant to fix the issue #3, and it has been delivered in the e4.preferences.mainmenu plugin fragment -> https://github.com/opcoach/e4Preferences/tree/master/com.opcoach.e4.preferences.mainmenu
    If you import this project in your workspace and add it to your launch configuration it should add automatically the preferences command in the window menu (and create the menu if it does not exist).
    In this case you can remove the command from your initial application model.
    I will change the delivery to have it also in the update site later.
    Could you try it and tell me how it works ?
    Thank you
    Olivier


  4. OPCoach
    12 May 2016

    Hi Varun

    Are you sure that your com.opcoach.e4.preference plugin is correctly launched in your launch configuration? You can check it by opening the bundle spy and search for it.. It is like it is not known in the launch configuration.

    It it does not work, send me the zip file of your project and I will test and check it…

    For your information I opened an issue (https://github.com/opcoach/e4Preferences/issues/3) to provide another plugin fragment containing the model fragment that will bind this command automatically in the window menu of your application. I hope to fix it in the next days.

    Olivier


  5. Varun Raval
    11 May 2016

    Hi,

    Thanks for the reply.
    handler is bound to ‘com.opcoach.e4.preferences.handlers.E4PreferencesHandler’ and this handler is also bound to Preferences Command: org.eclipse.ui.window.preferences

    I am running eclipse on Ubuntu(15.10).

    The error that is coming in console can be seen on https://pastie.org/pastes/1083222/text. I forgot to mention this in first post.

    It says
    java.lang.ClassNotFoundException: com.opcoach.e4.preferences.handlers.E4PreferencesHandler cannot be found by com.opcoach.e4.preferences_1.0.0.qualifier


  6. OPCoach
    10 May 2016

    Hi Varun,

    Thank you for using this tooling. It seems that the command appears in the window menu so you put it right in the application model with an HandledMenuItem. Two reasons to have the command disabled are :
    1. The handler is not bound to the command -> Check the handlers in your application model and ensure you are bound to the ‘com.opcoach.e4.preferences.handlers.E4PreferencesHandler’ and then check that this handler is bound to the command
    2. If the handler is correctly bound it can be disabled by a @canExecute in the handler, but for this project there is not.

    So I guess the handler should be not correctly bound. Could you check it ?

    Olivier


  7. Varun Raval
    10 May 2016

    Hi,

    I imported example project and ran it using product file. I got the new window containing every view, menu and toolbar as described in Application.e4xmi file. But, preferences option both on toolbar and within window menu is disabled.
    Should I be doing something to enable it or there is some mistake in my import. I am using Eclipse Mars(4.5.2).

    I am a beginner and saw your tutorial link on vogella Preference page tutorials. I want to learn how to add preference page to Preferences option in Window menu of an Eclipse RCP. It would be great if you can direct me on how to do it.

    Thanking you,
    Varun Raval


  8. OPCoach
    30 March 2016

    Hi Adrzej,

    I don’t remember why, but you can still open an issue on github to open the discussion and to have a fix in a few time :)

    Thanks
    Olivier


  9. Andrzej Witecki
    30 March 2016

    Excellent job. Thankss. However one thing bothers me. Why E4PreferenceRegistry has been marked as an internal class? It would be great to use it in order to provide own handler which for example uses FilteredPreferenceDialog instead PreferenceDialog.

    Regards,
    Andrzej


  10. Ralf Heydenreich
    26 August 2015

    Awesome! It works (at least for my tested cases)! I’ve created my own IPreferenceStoreProvider (according to your example, really easy!). Then I could use it in my LifecycleManager. One remark to your Bundle: I had to write the class with bundle notation, e.g. “bundleclass://com.my.bundle/com.my.package.MyCoolPreferenceStoreProvider”. Perhaps you can state this in your blog, since the extension point suggests that there has to be a normal class name.

    Greetz,
    Ralf.

Leave a Reply