Skip to content

OSGi declarative service to be able to contribute preference pages via OSGi declarative services

License

Notifications You must be signed in to change notification settings

SilicoSciences/e4-preferences

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

e4-preferences

This project contains a plug-in with an OSGi declarative service that can be used to contribute preference pages to a JFace PreferenceDialog via OSGi services.

This plug-in can be used for migrating an Eclipse 3.x based application to using the Eclipse 4.x platform. It is intended to remove the extension point org.eclipse.ui.preferencePages and dependencies to org.eclipse.ui, which is necessary to get rid of the compatibility layer.

To use the service, existing preference pages need to be modified:

  1. Remove the class hierarchy (implements IWorkbenchPreferencePage)

  2. Remove init()

  3. Implement a constructor to set the title and the description if necessary

  4. Remove the extension point org.eclipse.ui.preferencePages

To contribute the preference page to this service, the following steps need to be performed:

  1. Add org.fipro.e4.service.preferences to the Dependencies section of the MANIFEST.MF file of the plug-in that contributes the preference page
  2. Create a PreferenceNodeContribution for the PreferencePage (e.g. MyPreferencePage)
public class MyPreferenceContribution extends PreferenceNodeContribution {

    public MyPreferenceContribution() {
        super("myId", "myLabel", null, 
            MyPreferencePage.class, null, null);
    }
}
  1. Create a Component Definition for the PreferenceNodeContribution
  2. Create folder OSGI-INF (and ensure it is added to the build.properties)
  3. Create a component definition via File -> New -> Component Definition
  4. Set an appropriate Filename and Name
  5. Select the created PreferenceNodeContribution as Class
  6. Add org.fipro.e4.service.preferences.PreferenceNodeContribution as Provided Service
  7. Add Bundle-ActivationPolicy: lazy to the MANIFEST.MF (Activate this plug-in when one of its classes is loaded on the Overview tab of the PDE editor)

To open a JFace PreferenceDialog that looks similar to the known Eclipse workbench preference dialog, you need to create a handler that looks similar to the following snippet:

public class PreferencesHandler {
	
    @Execute
    public void execute(Shell shell, @PrefMgr PreferenceManager manager) {
        PreferenceDialog dialog = new PreferenceDialog(shell, manager) {
        
        @Override
        protected TreeViewer createTreeViewer(Composite parent) {
            TreeViewer viewer = super.createTreeViewer(parent);
				
            viewer.setComparator(new ViewerComparator() {
					
                @Override
                public int category(Object element) {
                    // this ensures that the General preferences page is always on top
                    // while the other pages are ordered alphabetical
                    if (element instanceof ContributedPreferenceNode
                            && ("general".equals(((ContributedPreferenceNode) element).getId()))) {
                        return -1;
                    }
                    return 0;
                }
            });
				
            return viewer;
			}
        };
		
        dialog.open();
    }
}

About

OSGi declarative service to be able to contribute preference pages via OSGi declarative services

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%