Search code examples
eclipseeclipse-pluginpreferences

Eclipse Plugin Open Preferences Page From Menu Item


I have a

public class PrefMenu extends FieldEditorPreferencePage implements IWorkbenchPreferencePage

implementing the init() and createFieldMethods(). And I do see it in Window>Preferences>PREFMENU_NAME after adding the class to the preferencePage extension.

But how do I open the preference page from a menu-item? I created a command and a handler and the execute()-Method (which works with other commands) does...

    //other commands
    } else if (commandID.equals(PREFERENCES_COMMAND_ID)){
        final PrefMenu prefMenu = new PrefMenu();
        prefMenu.init(PlatformUI.getWorkbench());
    }

Yet nothing happens when I click on the menu-Item. In the debug mode I see it simply executes the init()-Method and returns. But I want it to open up the Preferences-Window and only close it when I click on OK or Cancel.


Solution

  • Use org.eclipse.ui.dialogs.PreferencesUtil to do this:

    String id = ... your preference page id
    
    Shell shell = ... parent shell to use ...
    
    PreferencesUtil.createPreferenceDialogOn(shell, id, new String[] {id}, null).open()
    

    This will open just your preference page in the preferences dialog. You can adjust the string array to include other pages or specify null for the array to show all preference pages.