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:
-
Remove the class hierarchy (
implements IWorkbenchPreferencePage
) -
Remove
init()
-
Implement a constructor to set the title and the description if necessary
-
Remove the extension point org.eclipse.ui.preferencePages
To contribute the preference page to this service, the following steps need to be performed:
- Add org.fipro.e4.service.preferences to the Dependencies section of the MANIFEST.MF file of the plug-in that contributes the preference page
- Create a
PreferenceNodeContribution
for thePreferencePage
(e.g.MyPreferencePage
)
public class MyPreferenceContribution extends PreferenceNodeContribution {
public MyPreferenceContribution() {
super("myId", "myLabel", null,
MyPreferencePage.class, null, null);
}
}
- Create a Component Definition for the
PreferenceNodeContribution
- Create folder OSGI-INF (and ensure it is added to the build.properties)
- Create a component definition via File -> New -> Component Definition
- Set an appropriate Filename and Name
- Select the created
PreferenceNodeContribution
as Class - Add
org.fipro.e4.service.preferences.PreferenceNodeContribution
as Provided Service - 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();
}
}